Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-34727] Delaying branch indexing (#54)
* [JENKINS-34727] Delaying branch indexing
* [JENKINS-34727] Disabling GH API client cache
* [JENKINS-34727] Reduce indexing delay to 5 seconds
  • Loading branch information
amuniz authored and recena committed May 13, 2016
1 parent 38a28e6 commit 096eb38
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
Expand Up @@ -102,11 +102,6 @@ public class Connector {

gb.withRateLimitHandler(CUSTOMIZED);
OkHttpClient client = new OkHttpClient().setProxy(getProxy(defaultIfBlank(apiUrl, GITHUB_URL)));
client.setCache(GitHubClientCacheOps.toCacheDir().apply(config));
if (config.getClientCacheSize() > 0) {
Cache cache = toCacheDir().apply(config);
client.setCache(cache);
}

gb.withConnector(new OkHttpConnector(new OkUrlFactory(client)));

Expand Down
Expand Up @@ -24,6 +24,8 @@

package org.jenkinsci.plugins.github_branch_source;

import java.util.concurrent.TimeUnit;

import com.cloudbees.jenkins.GitHubRepositoryName;
import com.cloudbees.jenkins.GitHubWebHook;
import hudson.Extension;
Expand All @@ -33,6 +35,7 @@
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.api.SCMSourceOwners;
import jenkins.util.Timer;

/**
* This listener is registered only when {@link org.kohsuke.github.GHEvent} PUSH is received.
Expand All @@ -44,26 +47,31 @@ public class GitHubWebhookListenerImpl extends GitHubWebHook.Listener {

@Override
public void onPushRepositoryChanged(final String pusherName, final GitHubRepositoryName changedRepository) {
ACL.impersonate(ACL.SYSTEM, new Runnable() {
// Delaying the indexing for some seconds to avoid GitHub cache
Timer.get().schedule(new Runnable() {
@Override public void run() {
boolean found = false;
for (final SCMSourceOwner owner : SCMSourceOwners.all()) {
for (SCMSource source : owner.getSCMSources()) {
if (source instanceof GitHubSCMSource) {
GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
if (gitHubSCMSource.getRepoOwner().equals(changedRepository.getUserName()) &&
gitHubSCMSource.getRepository().equals(changedRepository.getRepositoryName())) {
found = true;
LOGGER.log(Level.FINE, "push event from {0} on {1}:{2}/{3} forwarded to {4}", new Object[] {pusherName, changedRepository.getHost(), changedRepository.getUserName(), changedRepository.getRepositoryName(), owner.getFullName()});
owner.onSCMSourceUpdated(gitHubSCMSource);
ACL.impersonate(ACL.SYSTEM, new Runnable() {
@Override public void run() {
boolean found = false;
for (final SCMSourceOwner owner : SCMSourceOwners.all()) {
for (SCMSource source : owner.getSCMSources()) {
if (source instanceof GitHubSCMSource) {
GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
if (gitHubSCMSource.getRepoOwner().equals(changedRepository.getUserName()) &&
gitHubSCMSource.getRepository().equals(changedRepository.getRepositoryName())) {
found = true;
LOGGER.log(Level.FINE, "push event from {0} on {1}:{2}/{3} forwarded to {4}", new Object[] {pusherName, changedRepository.getHost(), changedRepository.getUserName(), changedRepository.getRepositoryName(), owner.getFullName()});
owner.onSCMSourceUpdated(gitHubSCMSource);
}
}
}
}
if (!found) {
LOGGER.log(Level.FINE, "push event from {0} on {1}:{2}/{3} did not match any project", new Object[] {pusherName, changedRepository.getHost(), changedRepository.getUserName(), changedRepository.getRepositoryName()});
}
}
}
if (!found) {
LOGGER.log(Level.FINE, "push event from {0} on {1}:{2}/{3} did not match any project", new Object[] {pusherName, changedRepository.getHost(), changedRepository.getUserName(), changedRepository.getRepositoryName()});
}
});
}
});
}, 5, TimeUnit.SECONDS);
}
}
Expand Up @@ -27,19 +27,18 @@
import com.cloudbees.jenkins.GitHubRepositoryName;
import hudson.Extension;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import hudson.security.ACL;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.api.SCMSourceOwners;
import jenkins.util.Timer;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.github.extension.GHEventsSubscriber;
import org.kohsuke.github.GHEvent;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -97,21 +96,26 @@ protected void onEvent(GHEvent event, String payload) {
LOGGER.log(Level.WARNING, "Malformed repository URL {0}", repoUrl);
return;
}
ACL.impersonate(ACL.SYSTEM, new Runnable() {
// Delaying the indexing for some seconds to avoid GitHub cache
Timer.get().schedule(new Runnable() {
@Override public void run() {
for (final SCMSourceOwner owner : SCMSourceOwners.all()) {
for (SCMSource source : owner.getSCMSources()) {
if (source instanceof GitHubSCMSource) {
GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
if (gitHubSCMSource.getRepoOwner().equals(changedRepository.getUserName()) &&
gitHubSCMSource.getRepository().equals(changedRepository.getRepositoryName())) {
owner.onSCMSourceUpdated(gitHubSCMSource);
ACL.impersonate(ACL.SYSTEM, new Runnable() {
@Override public void run() {
for (final SCMSourceOwner owner : SCMSourceOwners.all()) {
for (SCMSource source : owner.getSCMSources()) {
if (source instanceof GitHubSCMSource) {
GitHubSCMSource gitHubSCMSource = (GitHubSCMSource) source;
if (gitHubSCMSource.getRepoOwner().equals(changedRepository.getUserName()) &&
gitHubSCMSource.getRepository().equals(changedRepository.getRepositoryName())) {
owner.onSCMSourceUpdated(gitHubSCMSource);
}
}
}
}
}
}
});
}
});
}, 5, TimeUnit.SECONDS);
} else {
LOGGER.log(Level.WARNING, "Malformed repository URL {0}", repoUrl);
}
Expand Down

0 comments on commit 096eb38

Please sign in to comment.