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

Make it possible to only allow PRs against a select set of target branches #377

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -25,13 +25,13 @@
import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.HostUser;
import org.openjdk.skara.issuetracker.*;
import org.openjdk.skara.vcs.Commit;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.vcs.openjdk.Issue;

import java.io.*;
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.regex.*;
import java.util.stream.*;

class CheckRun {
@@ -75,6 +75,16 @@ private boolean checkTargetBranch() {
return matcher.matches();
}

private List<String> allowedTargetBranches() {
var remoteBranches = prInstance.remoteBranches().stream()
.map(Reference::name)
.map(name -> workItem.bot.allowedTargetBranches().matcher(name))
.filter(Matcher::matches)
.map(Matcher::group)
.collect(Collectors.toList());
return remoteBranches;
}

// For unknown contributors, check that all commits have the same name and email
private boolean checkCommitAuthor(List<Commit> commits) throws IOException {
var author = censusInstance.namespace().get(pr.author().id());
@@ -115,9 +125,10 @@ private List<String> botSpecificChecks() throws IOException {
var ret = new ArrayList<String>();

if (!checkTargetBranch()) {
rwestberg marked this conversation as resolved.
Show resolved Hide resolved
var error = "The target branch of this PR does not match the allowed set of branches that can be targeted. " +
"The following restriction is currently in place: `" + workItem.bot.allowedTargetBranches().pattern() +
"`. Please select a different target branch for this PR.";
var error = "The branch `" + pr.targetRef() + "` is not allowed as target branch. The allowed target branches are:\n" +
allowedTargetBranches().stream()
.map(name -> " - " + name)
.collect(Collectors.joining("\n"));
ret.add(error);
}

@@ -226,4 +226,12 @@ void executeChecks(Hash localHash, CensusInstance censusInstance, PullRequestChe
}
}
}

List<Reference> remoteBranches() {
try {
return localRepo.remoteBranches(pr.repository().url().toString());
} catch (IOException e) {
return List.of();
}
}
}
@@ -1219,7 +1219,8 @@ void targetBranchPattern(TestInfo testInfo) throws IOException {
assertEquals(1, checks.size());
var check = checks.get("jcheck");
assertEquals(CheckStatus.FAILURE, check.status());
assertTrue(check.summary().orElseThrow().contains("The target branch of this PR does not match the allowed set of branches"));
assertTrue(check.summary().orElseThrow().contains("The branch `master` is not allowed as target branch"));
assertTrue(check.summary().orElseThrow().contains("notmaster"));

var anotherPr = credentials.createPullRequest(author, "notmaster", "edit",
"This is a pull request", true);