Skip to content

Commit

Permalink
Merge pull request jenkinsci#260 from jenkinsci/master
Browse files Browse the repository at this point in the history
Merging down from jenkinsci
  • Loading branch information
DavidTanner committed May 7, 2015
2 parents dcb18eb + e9e6e15 commit f64f385
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 100 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ If you want to manually build the job, in the job setting check ``This build is

### Updates

#### -> 1.20
* PullRequestMerger now notifies the taskListener of failures.
* AutoCloseFailedPullRequest has been extracted from the published URL check.

#### -> 1.19
* More work for disabled builds.
* Unified tabs to spaces.
* Updates to the tests, and added some tests.

#### -> 1.18
* Add support for folder projects.
* Correcting issue with default credentials.
Expand Down
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<artifactId>ghprb</artifactId>
<name>GitHub Pull Request Builder</name>
<version>1.18-SNAPSHOT</version>
<version>1.21-SNAPSHOT</version>
<packaging>hpi</packaging>

<url>https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin</url>
Expand Down Expand Up @@ -94,12 +94,6 @@
<version>0.12</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>
<version>3.9.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void addWhitelist(String author) {
trigger.addWhitelist(author);
}

public boolean isDisabled() {
public boolean isProjectDisabled() {
return project.isDisabled();
}

Expand Down
154 changes: 80 additions & 74 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbBuilds.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,92 +132,98 @@ public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
}
repo.createCommitStatus(build, state, "Build finished.", c.getPullID(), trigger.getCommitStatusContext(), listener.getLogger());

StringBuilder msg = new StringBuilder();

String publishedURL = GhprbTrigger.getDscp().getPublishedURL();
if (publishedURL != null && !publishedURL.isEmpty()) {
String commentFilePath = trigger.getCommentFilePath();

if (commentFilePath != null && !commentFilePath.isEmpty()) {
try {
String scriptFilePathResolved = Ghprb.replaceMacros(build, commentFilePath);

String content = FileUtils.readFileToString(new File(scriptFilePathResolved));
msg.append("Build comment file: \n--------------\n");
msg.append(content);
msg.append("\n--------------\n");
} catch (IOException e) {
msg.append("\n!!! Couldn't read commit file !!!\n");
listener.getLogger().println("Couldn't read comment file");
e.printStackTrace(listener.getLogger());
}
buildResultMessage(build, listener, state, c);
}
// close failed pull request automatically
if (state == GHCommitState.FAILURE && trigger.isAutoCloseFailedPullRequests()) {
closeFailedRequest(listener, c);
}
}

private void closeFailedRequest(TaskListener listener, GhprbCause c) {
try {
GHPullRequest pr = repo.getPullRequest(c.getPullID());

if (pr.getState().equals(GHIssueState.OPEN)) {
repo.closePullRequest(c.getPullID());
}
} catch (IOException ex) {
listener.getLogger().println("Can't close pull request");
ex.printStackTrace(listener.getLogger());
}
}

private void buildResultMessage(AbstractBuild<?, ?> build, TaskListener listener, GHCommitState state, GhprbCause c) {
StringBuilder msg = new StringBuilder();
String commentFilePath = trigger.getCommentFilePath();

if (commentFilePath != null && !commentFilePath.isEmpty()) {
try {
String scriptFilePathResolved = Ghprb.replaceMacros(build, commentFilePath);

String content = FileUtils.readFileToString(new File(scriptFilePathResolved));
msg.append("Build comment file: \n--------------\n");
msg.append(content);
msg.append("\n--------------\n");
} catch (IOException e) {
msg.append("\n!!! Couldn't read commit file !!!\n");
listener.getLogger().println("Couldn't read comment file");
e.printStackTrace(listener.getLogger());
}
}

msg.append("\nRefer to this link for build results (access rights to CI server needed): \n");
msg.append(generateCustomizedMessage(build));

int numLines = GhprbTrigger.getDscp().getlogExcerptLines();
if (state != GHCommitState.SUCCESS && numLines > 0) {
// on failure, append an excerpt of the build log
try {
// wrap log in "code" markdown
msg.append("\n\n**Build Log**\n*last ").append(numLines).append(" lines*\n");
msg.append("\n ```\n");
List<String> log = build.getLog(numLines);
for (String line : log) {
msg.append(line).append('\n');
}
msg.append("```\n");
} catch (IOException ex) {
listener.getLogger().println("Can't add log excerpt to commit comments");
ex.printStackTrace(listener.getLogger());
msg.append("\nRefer to this link for build results (access rights to CI server needed): \n");
msg.append(generateCustomizedMessage(build));

int numLines = GhprbTrigger.getDscp().getlogExcerptLines();
if (state != GHCommitState.SUCCESS && numLines > 0) {
// on failure, append an excerpt of the build log
try {
// wrap log in "code" markdown
msg.append("\n\n**Build Log**\n*last ").append(numLines).append(" lines*\n");
msg.append("\n ```\n");
List<String> log = build.getLog(numLines);
for (String line : log) {
msg.append(line).append('\n');
}
msg.append("```\n");
} catch (IOException ex) {
listener.getLogger().println("Can't add log excerpt to commit comments");
ex.printStackTrace(listener.getLogger());
}
}

String buildMessage = null;
if (state == GHCommitState.SUCCESS) {
if (trigger.getMsgSuccess() != null && !trigger.getMsgSuccess().isEmpty()) {
buildMessage = trigger.getMsgSuccess();
} else if (GhprbTrigger.getDscp().getMsgSuccess(build) != null
&& !GhprbTrigger.getDscp().getMsgSuccess(build).isEmpty()) {
buildMessage = GhprbTrigger.getDscp().getMsgSuccess(build);
}
} else if (state == GHCommitState.FAILURE) {
if (trigger.getMsgFailure() != null && !trigger.getMsgFailure().isEmpty()) {
buildMessage = trigger.getMsgFailure();
} else if (GhprbTrigger.getDscp().getMsgFailure(build) != null
&& !GhprbTrigger.getDscp().getMsgFailure(build).isEmpty()) {
buildMessage = GhprbTrigger.getDscp().getMsgFailure(build);
}
String buildMessage = null;
if (state == GHCommitState.SUCCESS) {
if (trigger.getMsgSuccess() != null && !trigger.getMsgSuccess().isEmpty()) {
buildMessage = trigger.getMsgSuccess();
} else if (GhprbTrigger.getDscp().getMsgSuccess(build) != null
&& !GhprbTrigger.getDscp().getMsgSuccess(build).isEmpty()) {
buildMessage = GhprbTrigger.getDscp().getMsgSuccess(build);
}
// Only Append the build's custom message if it has been set.
if (buildMessage != null && !buildMessage.isEmpty()) {
// When the msg is not empty, append a newline first, to seperate it from the rest of the String
if (!"".equals(msg.toString())) {
msg.append("\n");
}
msg.append(buildMessage);
} else if (state == GHCommitState.FAILURE) {
if (trigger.getMsgFailure() != null && !trigger.getMsgFailure().isEmpty()) {
buildMessage = trigger.getMsgFailure();
} else if (GhprbTrigger.getDscp().getMsgFailure(build) != null
&& !GhprbTrigger.getDscp().getMsgFailure(build).isEmpty()) {
buildMessage = GhprbTrigger.getDscp().getMsgFailure(build);
}

if (msg.length() > 0) {
listener.getLogger().println(msg);
repo.addComment(c.getPullID(), msg.toString(), build, listener);
}
// Only Append the build's custom message if it has been set.
if (buildMessage != null && !buildMessage.isEmpty()) {
// When the msg is not empty, append a newline first, to seperate it from the rest of the String
if (!"".equals(msg.toString())) {
msg.append("\n");
}
msg.append(buildMessage);
}

// close failed pull request automatically
if (state == GHCommitState.FAILURE && trigger.isAutoCloseFailedPullRequests()) {

try {
GHPullRequest pr = repo.getPullRequest(c.getPullID());

if (pr.getState().equals(GHIssueState.OPEN)) {
repo.closePullRequest(c.getPullID());
}
} catch (IOException ex) {
listener.getLogger().println("Can't close pull request");
ex.printStackTrace(listener.getLogger());
}
}
if (msg.length() > 0) {
listener.getLogger().println(msg);
repo.addComment(c.getPullID(), msg.toString(), build, listener);
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/org/jenkinsci/plugins/ghprb/GhprbPullRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ private void checkSkipBuild(GHIssue issue) {
* @param pr
*/
public void check(GHPullRequest pr) {
if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Project is disabled, ignoring pull request");
return;
}
if (target == null) {
target = pr.getBase().getRef(); // If this instance was created before target was introduced (before v1.8), it can be null.
}
Expand Down Expand Up @@ -171,6 +175,10 @@ public void check(GHPullRequest pr) {
}

public void check(GHIssueComment comment) {
if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Project is disabled, ignoring comment");
return;
}
try {
checkComment(comment);
updated = comment.getUpdatedAt();
Expand Down Expand Up @@ -220,7 +228,7 @@ private boolean isUpdated(GHPullRequest pr) {
}

private void tryBuild(GHPullRequest pr) {
if (helper.isDisabled()) {
if (helper.isProjectDisabled()) {
logger.log(Level.FINEST, "Project is disabled, not trying to build");
shouldRun = false;
triggered = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, final Build
if (isMergeable == null || !isMergeable) {
logger.println("Pull request cannot be automerged.");
commentOnRequest("Pull request is not mergeable.");
listener.finished(Result.FAILURE);
return false;
}

Expand Down Expand Up @@ -157,6 +158,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, final Build
// deleteBranch(); //TODO: Update so it also deletes the branch being pulled from. probably make it an option.
}

if (merge) {
listener.finished(Result.SUCCESS);
} else {
listener.finished(Result.FAILURE);
}
return merge;
}

Expand Down
17 changes: 14 additions & 3 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public void check() {
if (!initGhRepository()) {
return;
}

if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Project is disabled, not checking github state");
return;
}

List<GHPullRequest> openPulls;
try {
Expand Down Expand Up @@ -249,6 +254,10 @@ public GHPullRequest getPullRequest(int id) throws IOException {
}

void onIssueCommentHook(IssueComment issueComment) throws IOException {
if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Not checking comments since build is disabled");
return;
}
int id = issueComment.getIssue().getNumber();
logger.log(Level.FINER, "Comment on issue #{0} from {1}: {2}", new Object[] { id, issueComment.getComment().getUser(), issueComment.getComment().getBody() });
if (!"created".equals(issueComment.getAction())) {
Expand All @@ -269,7 +278,11 @@ void onIssueCommentHook(IssueComment issueComment) throws IOException {
}

void onPullRequestHook(PullRequest pr) {
if ("opened".equals(pr.getAction()) || "reopened".equals(pr.getAction())) {
if ("closed".equals(pr.getAction())) {
pulls.remove(pr.getNumber());
} else if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Not processing Pull request since the build is disabled");
} else if ("opened".equals(pr.getAction()) || "reopened".equals(pr.getAction())) {
GhprbPullRequest pull = pulls.get(pr.getNumber());
if (pull == null) {
pulls.putIfAbsent(pr.getNumber(), new GhprbPullRequest(pr.getPullRequest(), helper, this));
Expand All @@ -287,8 +300,6 @@ void onPullRequestHook(PullRequest pr) {
return;
}
pull.check(pr.getPullRequest());
} else if ("closed".equals(pr.getAction())) {
pulls.remove(pr.getNumber());
} else {
logger.log(Level.WARNING, "Unknown Pull Request hook action: {0}", pr.getAction());
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbRootAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hudson.model.AbstractProject;
import hudson.model.UnprotectedRootAction;
import hudson.security.ACL;
import hudson.security.csrf.CrumbExclusion;
import jenkins.model.Jenkins;

import org.acegisecurity.Authentication;
Expand All @@ -23,6 +24,11 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
* @author Honza Brázdil <[email protected]>
*/
Expand Down Expand Up @@ -140,4 +146,22 @@ private Set<GhprbRepository> getRepos(String repo) {

return ret;
}

@Extension
public static class GhprbRootActionCrumbExclusion extends CrumbExclusion {

@Override
public boolean process(HttpServletRequest req, HttpServletResponse resp, FilterChain chain) throws IOException, ServletException {
String pathInfo = req.getPathInfo();
if (pathInfo != null && pathInfo.equals(getExclusionPath())) {
chain.doFilter(req, resp);
return true;
}
return false;
}

public String getExclusionPath() {
return "/" + URL + "/";
}
}
}
16 changes: 14 additions & 2 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import java.util.logging.Logger;
import java.util.regex.Pattern;

import jenkins.model.Jenkins;

/**
* @author Honza Brázdil <[email protected]>
*/
Expand Down Expand Up @@ -111,6 +109,10 @@ public static DescriptorImpl getDscp() {

@Override
public void start(AbstractProject<?, ?> project, boolean newInstance) {
if (project.isDisabled()) {
logger.log(Level.FINE, "Project is disabled, not starting trigger");
return;
}
this.project = project.getFullName();
if (project.getProperty(GithubProjectProperty.class) == null) {
logger.log(Level.INFO, "GitHub project not set up, cannot start ghprb trigger for job " + this.project);
Expand Down Expand Up @@ -149,6 +151,16 @@ public void run() {
if (getUseGitHubHooks()) {
return;
}

if (helper.isProjectDisabled()) {
logger.log(Level.FINE, "Project is disabled, ignoring trigger run call");
return;
}

if (helper == null) {
logger.log(Level.SEVERE, "Helper is null, unable to run trigger");
return;
}
helper.run();
getDescriptor().save();
}
Expand Down
Loading

0 comments on commit f64f385

Please sign in to comment.