Skip to content
Permalink
Browse files
420: Expand JBS issues in PR titles
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jun 17, 2020
1 parent 2f4d5a8 commit 2c0df39f8f5871de8338c5a61da9ee600e565cfd
Showing with 105 additions and 1 deletion.
  1. +25 −1 bots/pr/src/main/java/org/openjdk/skara/bots/pr/CheckRun.java
  2. +80 −0 bots/pr/src/test/java/org/openjdk/skara/bots/pr/CheckTests.java
@@ -35,6 +35,7 @@
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.*;

class CheckRun {
@@ -59,6 +60,7 @@ class CheckRun {
private static final String emptyPrBodyMarker = "<!--\nReplace this text with a description of your pull request (also remove the surrounding HTML comment markers).\n" +
"If in doubt, feel free to delete everything in this edit box first, the bot will restore the progress section as needed.\n-->";
private final Set<String> newLabels;
static final Pattern ISSUE_ID_PATTERN = Pattern.compile("^(?:[A-Za-z][A-Za-z0-9]+-)?([0-9]+)$");

private CheckRun(CheckWorkItem workItem, PullRequest pr, Repository localRepo, List<Comment> comments,
List<Review> allReviews, List<Review> activeReviews, Set<String> labels,
@@ -457,6 +459,27 @@ private String updateStatusMessage(String message) {
return newBody;
}

private String updateTitle() {
var title = pr.title();
var m = ISSUE_ID_PATTERN.matcher(title);
var project = issueProject();

var newTitle = title;
if (m.matches() && project != null) {
var id = m.group(1);
var issue = project.issue(id);
if (issue.isPresent()) {
newTitle = id + ": " + issue.get().title();
}
}

if (!title.equals(newTitle)) {
pr.setTitle(newTitle);
}

return newTitle;
}

private String verdictToString(Review.Verdict verdict) {
switch (verdict) {
case APPROVED:
@@ -716,6 +739,7 @@ private void checkStatus() {
// Calculate and update the status message if needed
var statusMessage = getStatusMessage(comments, activeReviews, visitor, additionalErrors);
var updatedBody = updateStatusMessage(statusMessage);
var title = updateTitle();

// Post / update approval messages (only needed if the review itself can't contain a body)
if (!pr.repository().forge().supportsReviewBody()) {
@@ -756,7 +780,7 @@ private void checkStatus() {
}

// Calculate current metadata to avoid unnecessary future checks
var metadata = workItem.getMetadata(pr.title(), updatedBody, pr.comments(), activeReviews, newLabels,
var metadata = workItem.getMetadata(title, updatedBody, pr.comments(), activeReviews, newLabels,
censusInstance, pr.targetHash(), pr.isDraft());
checkBuilder.metadata(metadata);
} catch (Exception e) {
@@ -1448,4 +1448,84 @@ void allowedIssueTypes(TestInfo testInfo) throws IOException {
" - Bug\n"));
}
}

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

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

var bug = issues.createIssue("My first bug", List.of("A bug"), Map.of());
var numericId = bug.id().split("-")[1];

// 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 bugHash = CheckableRepository.appendAndCommit(localRepo);
localRepo.push(bugHash, author.url(), "bug", true);
var bugPR = credentials.createPullRequest(author, "master", "bug", numericId, true);
assertEquals(numericId, bugPR.title());

// Check the status (should expand title)
TestBotRunner.runPeriodicItems(checkBot);

// Verify that the title is expanded
bugPR = author.pullRequest(bugPR.id());
assertEquals(numericId + ": " + bug.title(), bugPR.title());
}
}

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

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

var bug = issues.createIssue("My first bug", List.of("A bug"), Map.of());

// 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 bugHash = CheckableRepository.appendAndCommit(localRepo);
localRepo.push(bugHash, author.url(), "bug", true);
var bugPR = credentials.createPullRequest(author, "master", "bug", bug.id(), true);
assertEquals(bug.id(), bugPR.title());

// Check the status (should expand title)
TestBotRunner.runPeriodicItems(checkBot);

// Verify that the title is expanded
bugPR = author.pullRequest(bugPR.id());
var numericId = bug.id().split("-")[1];
assertEquals(numericId + ": " + bug.title(), bugPR.title());
}
}
}

1 comment on commit 2c0df39

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 2c0df39 Jun 17, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.