Skip to content
Permalink
Browse files
279: Optionally only update project-specific issues
Reviewed-by: ehelin
  • Loading branch information
rwestberg committed Feb 17, 2020
1 parent 7f3f4b2 commit a0b9185ba5432025f7599946ea4aa39866c8c660
@@ -47,16 +47,19 @@ public class IssueUpdater implements RepositoryUpdateConsumer, PullRequestUpdate
private final URI commitIcon;
private final boolean setFixVersion;
private final Map<String, String> fixVersions;
private final boolean prOnly;
private final Logger log = Logger.getLogger("org.openjdk.skara.bots.notify");

IssueUpdater(IssueProject issueProject, boolean reviewLink, URI reviewIcon, boolean commitLink, URI commitIcon,boolean setFixVersion, Map<String, String> fixVersions) {
IssueUpdater(IssueProject issueProject, boolean reviewLink, URI reviewIcon, boolean commitLink, URI commitIcon,
boolean setFixVersion, Map<String, String> fixVersions, boolean prOnly) {
this.issueProject = issueProject;
this.reviewLink = reviewLink;
this.reviewIcon = reviewIcon;
this.commitLink = commitLink;
this.commitIcon = commitIcon;
this.setFixVersion = setFixVersion;
this.fixVersions = fixVersions;
this.prOnly = prOnly;
}

private final static Set<String> primaryTypes = Set.of("Bug", "New Feature", "Enhancement", "Task", "Sub-task");
@@ -252,25 +255,40 @@ public void handleCommits(HostedRepository repository, Repository localRepositor
continue;
}

// The actual issue to be updated can change depending on the fix version
String requestedVersion = null;
if (setFixVersion) {
requestedVersion = fixVersions != null ? fixVersions.getOrDefault(branch.name(), null) : null;
if (requestedVersion == null) {
try {
var conf = localRepository.lines(Path.of(".jcheck/conf"), commit.hash());
if (conf.isPresent()) {
var parsed = JCheckConfiguration.parse(conf.get());
var version = parsed.general().version();
requestedVersion = version.orElse(null);
if (prOnly) {
var pullRequestCount = issue.links().stream()
.filter(link -> link.title().orElse("notitle").equals("Review"))
.filter(link -> link.summary().orElse("nosummary").startsWith(repository.name() + "/"))
.map(link -> link.summary().orElseThrow().substring(repository.name().length() + 1))
.map(repository::pullRequest)
.filter(pr -> pr.targetRef().equals(branch.name()))
.count();
if (pullRequestCount == 0) {
log.info("Skipping commit " + commit.hash().abbreviate() + " for repository " + repository.name() +
" on branch " + branch.name() + " - no matching PR found");
continue;
}
} else {
// The actual issue to be updated can change depending on the fix version
if (setFixVersion) {
requestedVersion = fixVersions != null ? fixVersions.getOrDefault(branch.name(), null) : null;
if (requestedVersion == null) {
try {
var conf = localRepository.lines(Path.of(".jcheck/conf"), commit.hash());
if (conf.isPresent()) {
var parsed = JCheckConfiguration.parse(conf.get());
var version = parsed.general().version();
requestedVersion = version.orElse(null);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

if (requestedVersion != null) {
issue = findIssue(issue, requestedVersion);
if (requestedVersion != null) {
issue = findIssue(issue, requestedVersion);
}
}
}

@@ -164,7 +164,15 @@ public List<Bot> create(BotConfiguration configuration) {
fixVersions = issuesConf.get("fixversions").fields().stream()
.collect(Collectors.toMap(JSONObject.Field::name, f -> f.value().asString()));
}
var updater = new IssueUpdater(issueProject, reviewLink, reviewIcon, commitLink, commitIcon, setFixVersion, fixVersions);
var prOnly = false;
if (issuesConf.contains("pronly")) {
prOnly = issuesConf.get("pronly").asBoolean();
if (setFixVersion) {
throw new RuntimeException("cannot combine pronly with fixversions");
}
}
var updater = new IssueUpdater(issueProject, reviewLink, reviewIcon, commitLink, commitIcon,
setFixVersion, fixVersions, prOnly);
updaters.add(updater);
prUpdaters.add(updater);
}
@@ -1008,7 +1008,7 @@ void testIssue(TestInfo testInfo) throws IOException {

var issueProject = credentials.getIssueProject();
var commitIcon = URI.create("http://www.example.com/commit.png");
var updater = new IssueUpdater(issueProject, false, null, true, commitIcon, true, null);
var updater = new IssueUpdater(issueProject, false, null, true, commitIcon, true, null, false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(updater), List.of(), Set.of(), Map.of());

@@ -1064,7 +1064,7 @@ void testIssueNoVersion(TestInfo testInfo) throws IOException {

var issueProject = credentials.getIssueProject();
var commitIcon = URI.create("http://www.example.com/commit.png");
var updater = new IssueUpdater(issueProject, false, null, true, commitIcon, true, null);
var updater = new IssueUpdater(issueProject, false, null, true, commitIcon, true, null, false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(updater), List.of(), Set.of(), Map.of());

@@ -1105,7 +1105,7 @@ void testIssueConfiguredVersionNoCommit(TestInfo testInfo) throws IOException {

var issueProject = credentials.getIssueProject();
var commitIcon = URI.create("http://www.example.com/commit.png");
var updater = new IssueUpdater(issueProject, false, null, false, commitIcon, true, Map.of("master", "2.0"));
var updater = new IssueUpdater(issueProject, false, null, false, commitIcon, true, Map.of("master", "2.0"), false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(updater), List.of(), Set.of(), Map.of());

@@ -1150,7 +1150,7 @@ void testIssueIdempotence(TestInfo testInfo) throws IOException {

var issueProject = credentials.getIssueProject();
var commitIcon = URI.create("http://www.example.com/commit.png");
var updater = new IssueUpdater(issueProject, false, null, true, commitIcon, true, null);
var updater = new IssueUpdater(issueProject, false, null, true, commitIcon, true, null, false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(updater), List.of(), Set.of(), Map.of());

@@ -1213,7 +1213,7 @@ void testIssuePoolVersion(TestInfo testInfo) throws IOException {
var storageFolder = tempFolder.path().resolve("storage");

var issueProject = credentials.getIssueProject();
var updater = new IssueUpdater(issueProject, false, null, false, null, true, Map.of("master", "12u14"));
var updater = new IssueUpdater(issueProject, false, null, false, null, true, Map.of("master", "12u14"), false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(updater), List.of(), Set.of(), Map.of());

@@ -1249,7 +1249,7 @@ void testIssuePoolOpenVersion(TestInfo testInfo) throws IOException {
var storageFolder = tempFolder.path().resolve("storage");

var issueProject = credentials.getIssueProject();
var updater = new IssueUpdater(issueProject, false, null, false, null, true, Map.of("master", "12u14"));
var updater = new IssueUpdater(issueProject, false, null, false, null, true, Map.of("master", "12u14"), false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(updater), List.of(), Set.of(), Map.of());

@@ -1285,7 +1285,7 @@ void testIssueBackport(TestInfo testInfo) throws IOException {
var storageFolder = tempFolder.path().resolve("storage");

var issueProject = credentials.getIssueProject();
var updater = new IssueUpdater(issueProject, false, null, false, null, true, Map.of("master", "12.0.2"));
var updater = new IssueUpdater(issueProject, false, null, false, null, true, Map.of("master", "12.0.2"), false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(updater), List.of(), Set.of(), Map.of());

@@ -1357,7 +1357,7 @@ void testPullRequest(TestInfo testInfo) throws IOException {

var issueProject = credentials.getIssueProject();
var reviewIcon = URI.create("http://www.example.com/review.png");
var updater = new IssueUpdater(issueProject, true, reviewIcon, false, null, false, null);
var updater = new IssueUpdater(issueProject, true, reviewIcon, false, null, false, null, false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(), List.of(updater), Set.of("rfr"),
Map.of(reviewer.forge().currentUser().userName(), Pattern.compile("This is now ready")));
@@ -1446,7 +1446,7 @@ void testPullRequestNoReview(TestInfo testInfo) throws IOException {

var issueProject = credentials.getIssueProject();
var reviewIcon = URI.create("http://www.example.com/review.png");
var updater = new IssueUpdater(issueProject, false, reviewIcon, false, null, false,null);
var updater = new IssueUpdater(issueProject, false, reviewIcon, false, null, false,null, false);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile("master"), tagStorage, branchStorage,
prIssuesStorage, List.of(), List.of(updater), Set.of("rfr"),
Map.of(reviewer.forge().currentUser().userName(), Pattern.compile("This is now ready")));
@@ -1475,4 +1475,66 @@ void testPullRequestNoReview(TestInfo testInfo) throws IOException {
assertEquals(0, links.size());
}
}

@Test
void testPullRequestPROnly(TestInfo testInfo) throws IOException {
try (var credentials = new HostCredentials(testInfo);
var tempFolder = new TemporaryDirectory()) {
var repo = credentials.getHostedRepository();
var repoFolder = tempFolder.path().resolve("repo");
var localRepo = CheckableRepository.init(repoFolder, repo.repositoryType());
credentials.commitLock(localRepo);
localRepo.pushAll(repo.url());

var tagStorage = createTagStorage(repo);
var branchStorage = createBranchStorage(repo);
var prIssuesStorage = createPullRequestIssuesStorage(repo);
var storageFolder = tempFolder.path().resolve("storage");

var issueProject = credentials.getIssueProject();
var reviewIcon = URI.create("http://www.example.com/review.png");
var updater = new IssueUpdater(issueProject, true, reviewIcon, false, null, false, null, true);
var notifyBot = new NotifyBot(repo, storageFolder, Pattern.compile(".*"), tagStorage, branchStorage,
prIssuesStorage, List.of(updater), List.of(updater), Set.of(), Map.of());

// Initialize history
localRepo.push(localRepo.resolve("master").orElseThrow(), repo.url(), "other");
TestBotRunner.runPeriodicItems(notifyBot);

// Create an issue and a pull request to fix it
var issue = issueProject.createIssue("This is an issue", List.of("Indeed"), Map.of("issuetype", JSON.of("Enhancement")));
var editHash = CheckableRepository.appendAndCommit(localRepo, "Another line", issue.id() + ": Fix that issue");
localRepo.push(editHash, repo.url(), "edit", true);
var pr = credentials.createPullRequest(repo, "other", "edit", issue.id() + ": Fix that issue");
pr.setBody("\n\n## Issue\n[" + issue.id() + "](http://www.test.test/): The issue");
TestBotRunner.runPeriodicItems(notifyBot);

// The issue should now contain a link to the PR
var links = issue.links();
assertEquals(1, links.size());
assertEquals(pr.webUrl(), links.get(0).uri().orElseThrow());
assertEquals(reviewIcon, links.get(0).iconUrl().orElseThrow());

// Simulate integration
localRepo.push(editHash, repo.url(), "other");
TestBotRunner.runPeriodicItems(notifyBot);

// The changeset should be reflected in a comment
var updatedIssue = issueProject.issue(issue.id()).orElseThrow();

var comments = updatedIssue.comments();
assertEquals(1, comments.size());
var comment = comments.get(0);
assertTrue(comment.body().contains(editHash.abbreviate()));

// Now simulate a merge to another branch
localRepo.push(editHash, repo.url(), "master");
TestBotRunner.runPeriodicItems(notifyBot);

// No additional comment should have been made
updatedIssue = issueProject.issue(issue.id()).orElseThrow();
comments = updatedIssue.comments();
assertEquals(1, comments.size());
}
}
}

0 comments on commit a0b9185

Please sign in to comment.