Skip to content
Permalink
Browse files
1170: Notify bot reposts commit comments if another notifier fails
Reviewed-by: kcr
  • Loading branch information
erikj79 committed Sep 13, 2021
1 parent 57944f6 commit a6c854a5af65c4dc9488829ca24843dc1cff7783
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
@@ -30,9 +30,12 @@

import java.nio.file.Path;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

class CommitCommentNotifier implements Notifier, PullRequestListener {
private final Logger log = Logger.getLogger("org.openjdk.skara.bots.notify");

private final IssueProject issueProject;

CommitCommentNotifier(IssueProject issueProject) {
@@ -77,7 +80,13 @@ public void onIntegratedPullRequest(PullRequest pr, Path scratchPath, Hash hash)
comment.add("- [" + issue.id() + "](" + issue.webUrl() + ")");
}
}
repository.addCommitComment(hash, String.join("\n", comment));
var existingComments = repository.commitComments(hash);
var commentBody = String.join("\n", comment);
if (existingComments.stream().anyMatch(c -> c.body().equals(commentBody))) {
log.warning("Commit comment for " + hash + " already posted");
} else {
repository.addCommitComment(hash, commentBody);
}
}

@Override
@@ -165,4 +165,61 @@ void testCommitCommentWithIssues(TestInfo testInfo) throws IOException {
assertEquals("- [" + issue.id() + "](" + issue.webUrl() + ")", lines[7]);
}
}

/**
* Test that the CommitCommentNotifier never repeates the exact same comment
*/
@Test
void testNoRepeatedCommitComment(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 prStateStorage = createPullRequestStateStorage(repo);
var storageFolder = tempFolder.path().resolve("storage");

var issueProject = credentials.getIssueProject();
var notifyBot = NotifyBot.newBuilder()
.repository(repo)
.storagePath(storageFolder)
.branches(Pattern.compile("master"))
.tagStorageBuilder(tagStorage)
.branchStorageBuilder(branchStorage)
.prStateStorageBuilder(prStateStorage)
.integratorId(repo.forge().currentUser().id())
.build();
var notifier = new CommitCommentNotifier(issueProject);
notifier.attachTo(notifyBot);

// Initialize history
TestBotRunner.runPeriodicItems(notifyBot);

// Save the state
var historyState = localRepo.fetch(repo.url(), "history");

// Commit a fix
var editHash = CheckableRepository.appendAndCommit(localRepo, "Another line", "Fix an issue");
localRepo.push(editHash, repo.url(), "master");
var pr = credentials.createPullRequest(repo, "master", "master", "Fix an issue");
pr.setBody("I made a fix");
pr.addLabel("integrated");
pr.addComment("@user Pushed as commit " + editHash.hex() + ".");

// Run the notifier manually to add a comment
notifier.onIntegratedPullRequest(pr, storageFolder, editHash);

// Run the bot officially
TestBotRunner.runPeriodicItems(notifyBot);

// Check that we only have 1 commit comment
var comments = repo.commitComments(editHash);
assertEquals(1, comments.size());
}
}
}

1 comment on commit a6c854a

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on a6c854a Sep 13, 2021

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.