Skip to content
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
17 changes: 15 additions & 2 deletions src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
* @author janinko
*/
public class Ghprb {
private static final String DEPENDABOT_USERNAME = "dependabot[bot]";

private static final Logger LOGGER = Logger.getLogger(Ghprb.class.getName());

static final Pattern GITHUB_USER_REPO_PATTERN = Pattern.compile("^(http[s]?://[^/]*)/([^/]*/[^/]*).*");
Expand Down Expand Up @@ -267,9 +269,12 @@ public boolean ifOnlyTriggerPhrase() {
}

public boolean isWhitelisted(GHUser user) {
String username = user.getLogin().toLowerCase();

return trigger.getPermitAll()
|| whitelisted().contains(user.getLogin().toLowerCase())
|| admins().contains(user.getLogin().toLowerCase())
|| isDependabotUser(user)
|| whitelisted().contains(username)
|| admins().contains(username)
|| isInWhitelistedOrganisation(user);
}

Expand All @@ -279,6 +284,10 @@ public boolean isAdmin(GHUser user) {
&& isInWhitelistedOrganisation(user));
}

public boolean isDependabotUser(GHUser user) {
return user != null && user.getLogin().equals(DEPENDABOT_USERNAME);
}

public boolean isBotUser(GHUser user) {
return user != null && user.getLogin().equals(getGitHub().getBotUserLogin());
}
Expand Down Expand Up @@ -326,6 +335,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
40 changes: 36 additions & 4 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 @@ -334,11 +340,12 @@ private void updatePR(GHPullRequest ghpr, GHIssueComment comment, boolean isWebh
Date updatedDate = comment != null ? comment.getUpdatedAt() : ghpr.getUpdatedAt();
// Don't log unless it was actually updated
if (updated == null || updated.compareTo(updatedDate) < 0) {
String user = comment != null ? comment.getUser().getName() : ghpr.getUser().getName();
GHUser user = comment != null ? comment.getUser() : ghpr.getUser();
String name = StringUtils.defaultIfBlank(user.getName(), user.getLogin());
LOGGER.log(
Level.INFO,
"Pull request #{0} was updated/initialized on {1} at {2} by {3} ({4})",
new Object[] {this.id, this.repo.getName(), updatedDate, user,
new Object[] {this.id, this.repo.getName(), updatedDate, name,
comment != null ? "comment" : "PR update"}
);
}
Expand Down Expand Up @@ -511,7 +518,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 +537,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 +697,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) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, shouldnt that method already exist somewhere else?

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.

Loading