diff --git a/src/main/java/com/cloudbees/jenkins/GitHubPushCause.java b/src/main/java/com/cloudbees/jenkins/GitHubPushCause.java index 75b7d3b4d..1c9d22654 100644 --- a/src/main/java/com/cloudbees/jenkins/GitHubPushCause.java +++ b/src/main/java/com/cloudbees/jenkins/GitHubPushCause.java @@ -8,8 +8,23 @@ * @author Kohsuke Kawaguchi */ public class GitHubPushCause extends SCMTriggerCause { + /** + * The name of the user who pushed to GitHub. + */ + private String pushedBy; + + public GitHubPushCause(String pusher) { + this("", pusher); + } + + public GitHubPushCause(String pollingLog, String pusher) { + super(pollingLog); + pushedBy = pusher; + } + @Override public String getShortDescription() { - return "Started by GitHub push by "; + String pusher = pushedBy != null ? pushedBy : ""; + return "Started by GitHub push by " + pusher; } } diff --git a/src/main/java/com/cloudbees/jenkins/GitHubPushTrigger.java b/src/main/java/com/cloudbees/jenkins/GitHubPushTrigger.java index 813c8e117..ea4a0a5a7 100644 --- a/src/main/java/com/cloudbees/jenkins/GitHubPushTrigger.java +++ b/src/main/java/com/cloudbees/jenkins/GitHubPushTrigger.java @@ -40,7 +40,6 @@ import org.eclipse.jgit.transport.URIish; import org.jenkinsci.plugins.multiplescms.MultiSCM; import org.kohsuke.github.GHException; -import org.kohsuke.github.GHHook; import org.kohsuke.github.GHRepository; import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.StaplerRequest; @@ -50,7 +49,7 @@ * * @author Kohsuke Kawaguchi */ -public class GitHubPushTrigger extends Trigger implements GitHubTrigger, Runnable { +public class GitHubPushTrigger extends Trigger> implements GitHubTrigger { @DataBoundConstructor public GitHubPushTrigger() { } @@ -58,8 +57,41 @@ public GitHubPushTrigger() { /** * Called when a POST is made. */ + @Deprecated public void onPost() { - getDescriptor().queue.execute(this); + onPost(""); + } + + /** + * Called when a POST is made. + */ + public void onPost(String triggeredByUser) { + final String pushBy = triggeredByUser; + getDescriptor().queue.execute(new Runnable() { + public void run() { + try { + StreamTaskListener listener = new StreamTaskListener(getLogFile()); + + try { + PrintStream logger = listener.getLogger(); + long start = System.currentTimeMillis(); + logger.println("Started on "+ DateFormat.getDateTimeInstance().format(new Date())); + boolean result = job.poll(listener).hasChanges(); + logger.println("Done. Took "+ Util.getTimeSpanString(System.currentTimeMillis()-start)); + if(result) { + logger.println("Changes found"); + job.scheduleBuild(new GitHubPushCause(pushBy)); + } else { + logger.println("No changes"); + } + } finally { + listener.close(); + } + } catch (IOException e) { + LOGGER.log(Level.SEVERE,"Failed to record SCM polling",e); + } + } + }); } /** @@ -69,30 +101,6 @@ public File getLogFile() { return new File(job.getRootDir(),"github-polling.log"); } - public void run() { - try { - StreamTaskListener listener = new StreamTaskListener(getLogFile()); - - try { - PrintStream logger = listener.getLogger(); - long start = System.currentTimeMillis(); - logger.println("Started on "+ DateFormat.getDateTimeInstance().format(new Date())); - boolean result = job.poll(listener).hasChanges(); - logger.println("Done. Took "+ Util.getTimeSpanString(System.currentTimeMillis()-start)); - if(result) { - logger.println("Changes found"); - job.scheduleBuild(new GitHubPushCause()); - } else { - logger.println("No changes"); - } - } finally { - listener.close(); - } - } catch (IOException e) { - LOGGER.log(Level.SEVERE,"Failed to record SCM polling",e); - } - } - /** * Does this project read from a repository of the given user name and the * given repository name? @@ -131,7 +139,7 @@ protected void addRepositories(Set r, SCM scm) { } @Override - public void start(AbstractProject project, boolean newInstance) { + public void start(AbstractProject project, boolean newInstance) { super.start(project, newInstance); if (newInstance && getDescriptor().isManageHook()) { // make sure we have hooks installed. do this lazily to avoid blocking the UI thread. diff --git a/src/main/java/com/cloudbees/jenkins/GitHubTrigger.java b/src/main/java/com/cloudbees/jenkins/GitHubTrigger.java index 75d037085..42d0a6802 100644 --- a/src/main/java/com/cloudbees/jenkins/GitHubTrigger.java +++ b/src/main/java/com/cloudbees/jenkins/GitHubTrigger.java @@ -1,8 +1,5 @@ package com.cloudbees.jenkins; -import hudson.model.AbstractProject; -import hudson.triggers.Trigger; - import java.util.Set; /** @@ -12,6 +9,8 @@ */ public interface GitHubTrigger { + @Deprecated public void onPost(); + public void onPost(String triggeredByUser); public Set getGitHubRepositories(); } diff --git a/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java b/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java index b5a340fa6..56bd04276 100644 --- a/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java +++ b/src/main/java/com/cloudbees/jenkins/GitHubWebHook.java @@ -149,12 +149,13 @@ public void doIndex(StaplerRequest req) { processGitHubPayload(req.getParameter("payload"),GitHubPushTrigger.class); } - public void processGitHubPayload(String payload, Class triggerClass) { + public void processGitHubPayload(String payload, Class> triggerClass) { JSONObject o = JSONObject.fromObject(payload); JSONObject repository = o.getJSONObject("repository"); String repoUrl = repository.getString("url"); // something like 'https://github.com/kohsuke/foo' String repoName = repository.getString("name"); // 'foo' portion of the above URL String ownerName = repository.getJSONObject("owner").getString("name"); // 'kohsuke' portion of the above URL + String pusherName = o.getJSONObject("pusher").getString("name"); LOGGER.info("Received POST for "+repoUrl); LOGGER.fine("Full details of the POST was "+o.toString()); @@ -173,7 +174,7 @@ public void processGitHubPayload(String payload, Class trigge LOGGER.fine("Considering to poke "+job.getFullDisplayName()); if (trigger.getGitHubRepositories().contains(changedRepository)) { LOGGER.info("Poked "+job.getFullDisplayName()); - trigger.onPost(); + trigger.onPost(pusherName); } else LOGGER.fine("Skipped "+job.getFullDisplayName()+" because it doesn't have a matching repository."); }