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

273: PR bot should check for empty pull request body #445

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 14 additions & 3 deletions bots/pr/src/main/java/org/openjdk/skara/bots/pr/CheckRun.java
Expand Up @@ -135,6 +135,11 @@ private Optional<MergeSource> mergeSource() {
private List<String> botSpecificChecks() throws IOException {
var ret = new ArrayList<String>();

if (bodyWithoutStatus().isBlank()) {
var error = "The pull request body must not be empty.";
ret.add(error);
}

if (!isTargetBranchAllowed()) {
var error = "The branch `" + pr.targetRef() + "` is not allowed as target branch. The allowed target branches are:\n" +
allowedTargetBranches().stream()
Expand Down Expand Up @@ -365,6 +370,14 @@ private String checkoutCommands() {
"`$ git checkout pull/" + pr.id() + "`\n";
}

private String bodyWithoutStatus() {
var description = pr.body();
var markerIndex = description.lastIndexOf(progressMarker);
return (markerIndex < 0 ?
description :
description.substring(0, markerIndex)).trim();
}

private String updateStatusMessage(String message) {
var description = pr.body();
var markerIndex = description.lastIndexOf(progressMarker);
Expand All @@ -373,9 +386,7 @@ private String updateStatusMessage(String message) {
log.info("Progress already up to date");
return description;
}
var newBody = (markerIndex < 0 ?
description :
description.substring(0, markerIndex)).trim() + "\n" + progressMarker + "\n" + message;
var newBody = bodyWithoutStatus() + "\n" + progressMarker + "\n" + message;

// TODO? Retrieve the body again here to lower the chance of concurrent updates
pr.setBody(newBody);
Expand Down
50 changes: 50 additions & 0 deletions bots/pr/src/test/java/org/openjdk/skara/bots/pr/CheckTests.java
Expand Up @@ -654,6 +654,56 @@ void blockingLabel(TestInfo testInfo) throws IOException {
}
}

@Test
void emptyPRBody(TestInfo testInfo) throws IOException {
try (var credentials = new HostCredentials(testInfo);
var tempFolder = new TemporaryDirectory()) {
var author = credentials.getHostedRepository();
var reviewer = credentials.getHostedRepository();

var censusBuilder = credentials.getCensusBuilder()
.addAuthor(author.forge().currentUser().id())
.addReviewer(reviewer.forge().currentUser().id());
var checkBot = PullRequestBot.newBuilder()
.repo(author)
.censusRepo(censusBuilder.build())
.build();

// Populate the projects repository
var localRepo = CheckableRepository.init(tempFolder.path(), author.repositoryType());
var masterHash = localRepo.resolve("master").orElseThrow();
localRepo.push(masterHash, author.url(), "master", true);

// Make a change with a corresponding PR
var editHash = CheckableRepository.appendAndCommit(localRepo);
localRepo.push(editHash, author.url(), "edit", true);
var pr = credentials.createPullRequest(author, "master", "edit", "Another PR");
pr.setBody(" ");

// Check the status
TestBotRunner.runPeriodicItems(checkBot);

// Verify that the check failed
var checks = pr.checks(editHash);
assertEquals(1, checks.size());
var check = checks.get("jcheck");
assertEquals(CheckStatus.FAILURE, check.status());
assertTrue(check.summary().orElseThrow().contains("The pull request body must not be empty."));

// The PR should not yet be ready for review
assertFalse(pr.labels().contains("rfr"));
assertFalse(pr.labels().contains("ready"));

// Check the status again
pr.setBody("Here's that body");
TestBotRunner.runPeriodicItems(checkBot);

// The PR should now be ready for review
assertTrue(pr.labels().contains("rfr"));
assertFalse(pr.labels().contains("ready"));
}
}

@Test
void missingReadyLabel(TestInfo testInfo) throws IOException {
try (var credentials = new HostCredentials(testInfo);
Expand Down
Expand Up @@ -355,7 +355,7 @@ public IssueProject getIssueProject() {
}

public PullRequest createPullRequest(HostedRepository hostedRepository, String targetRef, String sourceRef, String title, boolean draft) {
var pr = hostedRepository.createPullRequest(hostedRepository, targetRef, sourceRef, title, List.of(), draft);
var pr = hostedRepository.createPullRequest(hostedRepository, targetRef, sourceRef, title, List.of("PR body"), draft);
pullRequestsToBeClosed.add(pr);
return pr;
}
Expand Down