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

Add a way to filter PR based on source branch #127

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Select *Stash Pull Request Builder* then configure:
**Advanced options**
- Ignore ssl certificates:
- Build PR targetting only these branches: common separated list of branch names (or regexes). Blank for all.
- Build PR from these branches only: common separated list of branch names (or regexes). Blank for all.
- Rebuild if destination branch changes:
- Build only if Stash reports no conflicts: this should be set if using the merge branch to avoid issues with out of data merge branch in stash
- Build only if PR is mergeable (note this will stop the PR being built if you have required approvers limit set >0 and the PR hasn't been approved)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class StashBuildTrigger extends Trigger<Job<?, ?>> {
private final String ciSkipPhrases;
private final String ciBuildPhrases;
private final String targetBranchesToBuild;
private final String sourceBranchesToBuild;
private final boolean ignoreSsl;
private final boolean checkDestinationCommit;
private final boolean checkMergeable;
Expand Down Expand Up @@ -93,6 +94,7 @@ public StashBuildTrigger(
String ciBuildPhrases,
boolean deletePreviousBuildFinishComments,
String targetBranchesToBuild,
String sourceBranchesToBuild,
boolean cancelOutdatedJobsEnabled
) throws ANTLRException {
super(cron);
Expand All @@ -113,6 +115,7 @@ public StashBuildTrigger(
this.onlyBuildOnComment = onlyBuildOnComment;
this.deletePreviousBuildFinishComments = deletePreviousBuildFinishComments;
this.targetBranchesToBuild = targetBranchesToBuild;
this.sourceBranchesToBuild = sourceBranchesToBuild;
}

public String getStashHost() {
Expand Down Expand Up @@ -187,6 +190,10 @@ public String getTargetBranchesToBuild() {
return targetBranchesToBuild;
}

public String getSourceBranchesToBuild() {
return sourceBranchesToBuild;
}

public boolean getMergeOnSuccess() {
return mergeOnSuccess;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ private boolean isBuildTarget(StashPullRequestResponseValue pullRequest) {
return false;
}

if (!isFromSourceBranch(pullRequest)) {
logger.info("Skipping PR: " + pullRequest.getId() + " as sourcing from branch: " + pullRequest.getFromRef().getBranch().getName());
return false;
}

if(!isPullRequestMergable(pullRequest)) {
logger.info("Skipping PR: " + pullRequest.getId() + " as cannot be merged");
return false;
Expand Down Expand Up @@ -359,6 +364,20 @@ private boolean isForTargetBranch(StashPullRequestResponseValue pullRequest) {
return true;
}

private boolean isFromSourceBranch(StashPullRequestResponseValue pullRequest) {
String sourceBranchesToBuild = this.trigger.getSourceBranchesToBuild();
if (sourceBranchesToBuild !=null && !"".equals(sourceBranchesToBuild)) {
String[] branches = sourceBranchesToBuild.split(",");
for(String branch : branches) {
if (pullRequest.getFromRef().getBranch().getName().matches(branch.trim())) {
return true;
}
}
return false;
}
return true;
}

private boolean isSkipBuild(String pullRequestContentString) {
String skipPhrases = this.trigger.getCiSkipPhrases();
if (skipPhrases != null && !"".equals(skipPhrases)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<f:entry title="Build PR targetting only these branches" field="targetBranchesToBuild">
<f:textbox default="" />
</f:entry>
<f:entry title="Build PR from these branches only" field="sourceBranchesToBuild">
<f:textbox default="" />
</f:entry>
<f:entry title="Rebuild if destination branch changes" field="checkDestinationCommit">
<f:checkbox />
</f:entry>
Expand Down