Skip to content
Permalink
Browse files
pr-bot: add contributors to summary
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Feb 13, 2020
1 parent f8a0b66 commit 3857844ace5980a47128e310f03f24d4b0c881f0
@@ -27,6 +27,7 @@
import org.openjdk.skara.issuetracker.*;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.vcs.openjdk.Issue;
import org.openjdk.skara.email.EmailAddress;

import java.io.*;
import java.util.*;
@@ -275,6 +276,23 @@ private Optional<String> getReviewersList(List<Review> reviews) {
}
}

private String formatContributor(EmailAddress contributor) {
var name = contributor.fullName().orElseThrow();
return name + " `<" + contributor.address() + ">`";
}

private Optional<String> getContributorsList(List<Comment> comments) {
var contributors = Contributors.contributors(pr.repository().forge().currentUser(), comments)
.stream()
.map(c -> " * " + formatContributor(c))
.collect(Collectors.joining("\n"));
if (contributors.length() > 0) {
return Optional.of(contributors);
} else {
return Optional.empty();
}
}

private String getStatusMessage(List<Comment> comments, List<Review> reviews, PullRequestCheckIssueVisitor visitor) {
var progressBody = new StringBuilder();
progressBody.append("## Progress\n");
@@ -314,6 +332,11 @@ private String getStatusMessage(List<Comment> comments, List<Review> reviews, Pu
progressBody.append(reviewers);
});

getContributorsList(comments).ifPresent(contributors -> {
progressBody.append("\n\n## Contributors\n");
progressBody.append(contributors);
});

return progressBody.toString();
}

@@ -118,6 +118,7 @@ private List<WorkItem> getWorkItems(List<PullRequest> pullRequests) {

ret.add(new CheckWorkItem(this, pr, e -> updateCache.invalidate(pr)));
ret.add(new CommandWorkItem(this, pr, e -> updateCache.invalidate(pr)));
ret.add(new CheckWorkItem(this, pr, e -> updateCache.invalidate(pr)));
ret.add(new LabelerWorkItem(this, pr, e -> updateCache.invalidate(pr)));
}
}
@@ -29,6 +29,7 @@
import org.junit.jupiter.api.*;

import java.io.IOException;
import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;
import static org.openjdk.skara.bots.pr.PullRequestAsserts.assertLastCommentContains;
@@ -296,4 +297,63 @@ void removeContributor(TestInfo testInfo) throws IOException {
assertLastCommentContains(pr, "successfully removed.");
}
}

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

var censusBuilder = credentials.getCensusBuilder()
.addReviewer(integrator.forge().currentUser().id())
.addCommitter(author.forge().currentUser().id());
var prBot = PullRequestBot.newBuilder().repo(integrator).censusRepo(censusBuilder.build()).build();

// Populate the projects repository
var localRepoFolder = tempFolder.path().resolve("localrepo");
var localRepo = CheckableRepository.init(localRepoFolder, author.repositoryType());
var masterHash = localRepo.resolve("master").orElseThrow();
assertFalse(CheckableRepository.hasBeenEdited(localRepo));
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", "This is a pull request");

// Add a contributor
pr.addComment("/contributor add Foo Bar <foo.bar@host.com>");
TestBotRunner.runPeriodicItems(prBot);
assertLastCommentContains(pr, "successfully added.");

// Verify that body is updated
var body = pr.body().split("\n");
var contributorsHeaderIndex = -1;
for (var i = 0; i < body.length; i++) {
var line = body[i];
if (line.equals("## Contributors")) {
contributorsHeaderIndex = i;
break;
}
}
assertNotEquals(contributorsHeaderIndex, -1);
var contributors = new ArrayList<String>();
for (var i = contributorsHeaderIndex + 1; i < body.length && body[i].startsWith(" * "); i++) {
contributors.add(body[i].substring(3));
}
assertEquals(1, contributors.size());
assertEquals("Foo Bar `<foo.bar@host.com>`", contributors.get(0));

// Remove contributor
pr.addComment("/contributor remove Foo Bar <foo.bar@host.com>");
TestBotRunner.runPeriodicItems(prBot);
assertLastCommentContains(pr, "successfully removed.");

// Verify that body does not contain "Contributors" section
for (var line : pr.body().split("\n")) {
assertNotEquals("## Contributors", line);
}
}
}
}

0 comments on commit 3857844

Please sign in to comment.