Skip to content

Commit

Permalink
Add the name of the user who pushed to Github into the log.
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoote committed Aug 14, 2012
1 parent d44819d commit ccbfd70
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
17 changes: 16 additions & 1 deletion src/main/java/com/cloudbees/jenkins/GitHubPushCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
64 changes: 36 additions & 28 deletions src/main/java/com/cloudbees/jenkins/GitHubPushTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,16 +49,49 @@
*
* @author Kohsuke Kawaguchi
*/
public class GitHubPushTrigger extends Trigger<AbstractProject> implements GitHubTrigger, Runnable {
public class GitHubPushTrigger extends Trigger<AbstractProject<?,?>> implements GitHubTrigger {
@DataBoundConstructor
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);
}
}
});
}

/**
Expand All @@ -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?
Expand Down Expand Up @@ -131,7 +139,7 @@ protected void addRepositories(Set<GitHubRepositoryName> 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.
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/cloudbees/jenkins/GitHubTrigger.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.cloudbees.jenkins;

import hudson.model.AbstractProject;
import hudson.triggers.Trigger;

import java.util.Set;

/**
Expand All @@ -12,6 +9,8 @@
*/
public interface GitHubTrigger {

@Deprecated
public void onPost();
public void onPost(String triggeredByUser);
public Set<GitHubRepositoryName> getGitHubRepositories();
}
5 changes: 3 additions & 2 deletions src/main/java/com/cloudbees/jenkins/GitHubWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,13 @@ public void doIndex(StaplerRequest req) {
processGitHubPayload(req.getParameter("payload"),GitHubPushTrigger.class);
}

public void processGitHubPayload(String payload, Class<? extends Trigger> triggerClass) {
public void processGitHubPayload(String payload, Class<? extends Trigger<?>> 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());
Expand All @@ -173,7 +174,7 @@ public void processGitHubPayload(String payload, Class<? extends Trigger> 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.");
}
Expand Down

0 comments on commit ccbfd70

Please sign in to comment.