Skip to content

Commit

Permalink
Other plugins may want to listen for the push notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Aug 27, 2013
1 parent e8d0b0d commit 25ce5eb
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/main/java/com/cloudbees/jenkins/GitHubWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cloudbees.jenkins.GitHubPushTrigger.DescriptorImpl;

import hudson.Extension;
import hudson.ExtensionPoint;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.RootAction;
Expand All @@ -11,6 +12,7 @@
import hudson.triggers.Trigger;
import hudson.util.AdaptedIterator;
import hudson.util.Iterators.FilterIterator;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;
Expand Down Expand Up @@ -163,13 +165,18 @@ public void processGitHubPayload(String payload, Class<? extends Trigger<?>> tri
LOGGER.fine("Full details of the POST was "+o.toString());
Matcher matcher = REPOSITORY_NAME_PATTERN.matcher(repoUrl);
if (matcher.matches()) {
GitHubRepositoryName changedRepository = GitHubRepositoryName.create(repoUrl);
if (changedRepository == null) {
LOGGER.warning("Malformed repo url "+repoUrl);
return;
}

// run in high privilege to see all the projects anonymous users don't see.
// this is safe because when we actually schedule a build, it's a build that can
// happen at some random time anyway.
Authentication old = SecurityContextHolder.getContext().getAuthentication();
SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
try {
GitHubRepositoryName changedRepository = GitHubRepositoryName.create(repoUrl);
for (AbstractProject<?,?> job : Hudson.getInstance().getAllItems(AbstractProject.class)) {
GitHubTrigger trigger = (GitHubTrigger) job.getTrigger(triggerClass);
if (trigger!=null) {
Expand All @@ -184,6 +191,9 @@ public void processGitHubPayload(String payload, Class<? extends Trigger<?>> tri
} finally {
SecurityContextHolder.getContext().setAuthentication(old);
}
for (Listener listener: Jenkins.getInstance().getExtensionList(Listener.class)) {
listener.onPushRepositoryChanged(pusherName, changedRepository);
}
} else {
LOGGER.warning("Malformed repo url "+repoUrl);
}
Expand All @@ -194,4 +204,22 @@ public void processGitHubPayload(String payload, Class<? extends Trigger<?>> tri
public static GitHubWebHook get() {
return Hudson.getInstance().getExtensionList(RootAction.class).get(GitHubWebHook.class);
}

/**
* Other plugins may be interested in listening for these updates.
*
* @since 1.8
*/
public static abstract class Listener implements ExtensionPoint {

/**
* Called when there is a change notification on a specific repository.
*
* @param pusherName the pusher name.
* @param changedRepository the changed repository.
* @since 1.8
*/
public abstract void onPushRepositoryChanged(String pusherName, GitHubRepositoryName changedRepository);
}

}

0 comments on commit 25ce5eb

Please sign in to comment.