Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Option to report success to PR when build is not triggered #739

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ public List<Pattern> getExcludedRegionPatterns() {
return patterns;
}

public boolean getReportSuccessIfNotRegion() {
return trigger.getReportSuccessIfNotRegion();
}

public static String replaceMacros(Run<?, ?> build, TaskListener listener, String inputString) {
String returnString = inputString;
if (build != null && inputString != null) {
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbBuilds.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.ghprb.extensions.GhprbBuildStep;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommentAppender;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommitStatus;
import org.jenkinsci.plugins.ghprb.extensions.status.GhprbSimpleStatus;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommitStatusException;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension;
import org.kohsuke.github.GHCommitState;
Expand Down Expand Up @@ -76,10 +76,10 @@ public void build(GhprbPullRequest pr, GHUser triggerSender, String commentBody)
repo.getName(),
trigger.getGitHubApiAuth().getCredentialsId());

for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbCommitStatus.class)) {
if (ext instanceof GhprbCommitStatus) {
for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbSimpleStatus.class)) {
if (ext instanceof GhprbSimpleStatus) {
try {
((GhprbCommitStatus) ext).onBuildTriggered(
((GhprbSimpleStatus) ext).onBuildTriggered(
trigger.getActualProject(),
pr.getHead(),
pr.isMergeable(),
Expand Down Expand Up @@ -138,10 +138,10 @@ public void onStarted(Run<?, ?> build, TaskListener listener) {
e.printStackTrace(logger);
}

for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbCommitStatus.class)) {
if (ext instanceof GhprbCommitStatus) {
for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbSimpleStatus.class)) {
if (ext instanceof GhprbSimpleStatus) {
try {
((GhprbCommitStatus) ext).onBuildStart(build, listener, repo.getGitHubRepo());
((GhprbSimpleStatus) ext).onBuildStart(build, listener, repo.getGitHubRepo());
} catch (GhprbCommitStatusException e) {
repo.commentOnFailure(build, listener, e);
}
Expand Down Expand Up @@ -199,10 +199,10 @@ public void onCompleted(Run<?, ?> build, TaskListener listener) {
}
}

for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbCommitStatus.class)) {
if (ext instanceof GhprbCommitStatus) {
for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbSimpleStatus.class)) {
if (ext instanceof GhprbSimpleStatus) {
try {
((GhprbCommitStatus) ext).onBuildComplete(build, listener, repo.getGitHubRepo());
((GhprbSimpleStatus) ext).onBuildComplete(build, listener, repo.getGitHubRepo());
} catch (GhprbCommitStatusException e) {
repo.commentOnFailure(build, listener, e);
}
Expand Down Expand Up @@ -260,10 +260,10 @@ public void onEnvironmentSetup(@SuppressWarnings("rawtypes") Run build, Launcher

LOGGER.log(Level.FINE, "Job: " + build.getFullDisplayName() + " Attempting to send GitHub commit status");

for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbCommitStatus.class)) {
if (ext instanceof GhprbCommitStatus) {
for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbSimpleStatus.class)) {
if (ext instanceof GhprbSimpleStatus) {
try {
((GhprbCommitStatus) ext).onEnvironmentSetup(build, listener, repo.getGitHubRepo());
((GhprbSimpleStatus) ext).onEnvironmentSetup(build, listener, repo.getGitHubRepo());
} catch (GhprbCommitStatusException e) {
repo.commentOnFailure(build, listener, e);
}
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbPullRequest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package org.jenkinsci.plugins.ghprb;

import com.google.common.base.Joiner;
import hudson.model.Job;
import hudson.model.Run;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.ghprb.extensions.status.GhprbSimpleStatus;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommitStatusException;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension;
import org.kohsuke.github.GHCommitPointer;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueComment;
import org.kohsuke.github.GHLabel;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHPullRequestCommitDetail;
import org.kohsuke.github.GHPullRequestFileDetail;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitUser;

Expand Down Expand Up @@ -511,7 +517,7 @@ boolean containsWatchedPaths(GHPullRequest pr) {
}
}

private void tryBuild() {
void tryBuild() {
synchronized (this) {
if (helper.isProjectDisabled()) {
LOGGER.log(Level.FINEST, "Project is disabled, not trying to build");
Expand All @@ -530,7 +536,7 @@ private void tryBuild() {
}

if (shouldRun && !containsWatchedPaths(pr)) {
LOGGER.log(Level.FINEST, "Pull request contains no watched paths, skipping the build");
skipBuildForWatchedPaths();
shouldRun = false;
}

Expand Down Expand Up @@ -690,6 +696,31 @@ public boolean checkMergeable() {
return mergeable;
}

void skipBuildForWatchedPaths() {
if (helper.getReportSuccessIfNotRegion()) {
LOGGER.log(Level.FINEST, "Pull request contains no watched paths, skipping the build and reporting success.");
createCommitStatus(GHCommitState.SUCCESS, "Skipped, no pertinent files changed.");
} else {
LOGGER.log(Level.FINEST,
"Pull request contains no watched paths, skipping the build");
}
}

public void createCommitStatus(GHCommitState state, String message) {
GHRepository ghRepository = repo.getGitHubRepo();
GhprbTrigger trigger = helper.getTrigger();
Job<?, ?> actualProject = trigger.getActualProject();
for (GhprbExtension ext : Ghprb.getJobExtensions(trigger, GhprbSimpleStatus.class)) {
if (ext instanceof GhprbSimpleStatus) {
try {
((GhprbSimpleStatus) ext).createCommitStatus(actualProject, id, head, state, ghRepository, message);
} catch (GhprbCommitStatusException e) {
repo.commentOnFailure(null, null, e);
}
}
}
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof GhprbPullRequest)) {
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.ghprb.extensions.GhprbBuildStep;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommitStatus;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtensionDescriptor;
import org.jenkinsci.plugins.ghprb.extensions.GhprbGlobalDefault;
Expand Down Expand Up @@ -135,6 +134,8 @@ public class GhprbTrigger extends GhprbTriggerBackwardsCompatible {

private String excludedRegions;

private Boolean reportSuccessIfNotRegion;


private transient Ghprb helper;

Expand All @@ -161,7 +162,7 @@ private void setExtensions(List<GhprbExtension> extensions) {

// Filter out items that we only want one of, like the status updater.
this.extensions = Ghprb.onlyOneEntry(rawList,
GhprbCommitStatus.class
GhprbSimpleStatus.class
);

// Make sure we have at least one of the types we need one of.
Expand Down Expand Up @@ -198,7 +199,8 @@ public GhprbTrigger(String adminlist,
String whiteListLabels,
List<GhprbExtension> extensions,
String includedRegions,
String excludedRegions
String excludedRegions,
Boolean reportSuccessIfNotRegion
) throws ANTLRException {
super(cron);
this.adminlist = adminlist;
Expand All @@ -222,6 +224,7 @@ public GhprbTrigger(String adminlist,
this.whiteListLabels = whiteListLabels;
this.includedRegions = includedRegions;
this.excludedRegions = excludedRegions;
this.reportSuccessIfNotRegion = reportSuccessIfNotRegion;
setExtensions(extensions);
configVersion = LATEST_VERSION;
}
Expand Down Expand Up @@ -661,6 +664,13 @@ public String getExcludedRegions() {
return excludedRegions;
}

public Boolean getReportSuccessIfNotRegion() {
if (reportSuccessIfNotRegion == null) {
return false;
}
return reportSuccessIfNotRegion;
}

@Override
public DescriptorImpl getDescriptor() {
return DESCRIPTOR;
Expand Down Expand Up @@ -920,8 +930,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc

try {
extensions.rebuildHetero(req, formData, getGlobalExtensionDescriptors(), "extensions");
// Now make sure we have at least one of the types we need one of.
Ghprb.addIfMissing(this.extensions, new GhprbSimpleStatus(), GhprbCommitStatus.class);
Ghprb.addIfMissing(this.extensions, new GhprbSimpleStatus(), GhprbSimpleStatus.class);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.jenkinsci.plugins.ghprb.Ghprb;
import org.jenkinsci.plugins.ghprb.GhprbCause;
import org.jenkinsci.plugins.ghprb.GhprbTrigger;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommitStatus;
import org.jenkinsci.plugins.ghprb.extensions.GhprbCommitStatusException;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtensionDescriptor;
Expand All @@ -32,7 +31,7 @@
import java.util.Map;

public class GhprbSimpleStatus extends GhprbExtension implements
GhprbCommitStatus, GhprbGlobalExtension, GhprbProjectExtension, GhprbGlobalDefault {
GhprbGlobalExtension, GhprbProjectExtension, GhprbGlobalDefault {

@Extension
public static final DescriptorImpl /*GhprbSimpleStatusDescriptor*/ DESCRIPTOR = new DescriptorImpl();
Expand Down Expand Up @@ -108,7 +107,6 @@ public boolean addIfMissing() {
return true;
}

@Override
public void onBuildTriggered(Job<?, ?> project,
String commitSha,
boolean isMergeable,
Expand All @@ -123,14 +121,6 @@ public void onBuildTriggered(Job<?, ?> project,
return;
}

String statusUrl = getDescriptor().getStatusUrlDefault(this);
if (commitStatusContext == "") {
commitStatusContext = getDescriptor().getCommitStatusContextDefault(this);
}

String context = Util.fixEmpty(commitStatusContext);
context = Ghprb.replaceMacros(project, context);

if (!StringUtils.isEmpty(triggeredStatus)) {
sb.append(Ghprb.replaceMacros(project, triggeredStatus));
} else {
Expand All @@ -142,28 +132,17 @@ public void onBuildTriggered(Job<?, ?> project,
}
}

String url = Ghprb.replaceMacros(project, statusUrl);
if (StringUtils.equals(statusUrl, "--none--")) {
url = "";
}

String message = sb.toString();
try {
ghRepository.createCommitStatus(commitSha, state, url, message, context);
} catch (IOException e) {
throw new GhprbCommitStatusException(e, state, message, prId);
}
createCommitStatus(project, prId, commitSha, state, ghRepository, message);
}

@Override
public void onEnvironmentSetup(Run<?, ?> build,
TaskListener listener,
GHRepository repo) throws GhprbCommitStatusException {
// no need to create a commit here -- the onBuildStart() event will fire
// soon and will respect's the user's settings for startedStatus.
}

@Override
public void onBuildStart(Run<?, ?> build,
TaskListener listener,
GHRepository repo) throws GhprbCommitStatusException {
Expand Down Expand Up @@ -286,6 +265,33 @@ private void createCommitStatus(Run<?, ?> build,
}
}

public void createCommitStatus(Job<?, ?> project,
int prId,
String commitSha,
GHCommitState state,
GHRepository ghRepository,
String message) throws GhprbCommitStatusException {
String statusUrl = getDescriptor().getStatusUrlDefault(this);
if (commitStatusContext == "") {
commitStatusContext = getDescriptor().getCommitStatusContextDefault(this);
}

String context = Util.fixEmpty(commitStatusContext);
context = Ghprb.replaceMacros(project, context);

String url = Ghprb.replaceMacros(project, statusUrl);
// "--none--" means the user does not want a message, "Jenkins" is the default when we don't have a URL.
if (StringUtils.equals(statusUrl, "--none--") || StringUtils.equals(statusUrl, "Jenkins")) {
url = "";
}

try {
ghRepository.createCommitStatus(commitSha, state, url, message, context);
} catch (IOException e) {
throw new GhprbCommitStatusException(e, state, message, prId);
}
}

@Override
public DescriptorImpl /*GhprbSimpleStatusDescriptor*/ getDescriptor() {
return DESCRIPTOR;
Expand Down
Loading