Skip to content
Permalink
Browse files
pr: fetch commit comments in work item
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Feb 23, 2021
1 parent 7e9643c commit 874a89ceb0d5fd742db75b6f9744b56e200cfe32
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.bots.pr;

import org.openjdk.skara.bot.WorkItem;
import org.openjdk.skara.forge.HostedRepository;

import java.nio.file.Path;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import java.util.stream.Collectors;

class CommitCommentsWorkItem implements WorkItem {
private final PullRequestBot bot;
private final HostedRepository repo;

private static final ConcurrentHashMap<String, Boolean> processed = new ConcurrentHashMap<>();
private static final Logger log = Logger.getLogger("org.openjdk.skara.bots.pr");

CommitCommentsWorkItem(PullRequestBot bot, HostedRepository repo) {
this.bot = bot;
this.repo = repo;
}

@Override
public boolean concurrentWith(WorkItem other) {
return true;
}

@Override
public Collection<WorkItem> run(Path scratchPath) {
log.info("Looking for recent commit comments for repository " + repo.name());

return repo.recentCommitComments()
.stream()
.filter(cc -> !processed.containsKey(cc.id()))
.map(cc -> {
processed.put(cc.id(), true);
return new CommitCommandWorkItem(bot, cc, e -> processed.remove(cc.id()));
})
.collect(Collectors.toList());

}
}
@@ -63,7 +63,6 @@ class PullRequestBot implements Bot {
private final Logger log = Logger.getLogger("org.openjdk.skara.bots.pr");

private Instant lastFullUpdate;
private final ConcurrentHashMap<String, Boolean> processedCommitComments;

PullRequestBot(HostedRepository repo, HostedRepository censusRepo, String censusRef,
LabelConfiguration labelConfiguration, Map<String, String> externalCommands,
@@ -100,7 +99,6 @@ class PullRequestBot implements Bot {

// Only check recently updated when starting up to avoid congestion
lastFullUpdate = Instant.now();
processedCommitComments = new ConcurrentHashMap<>();
}

static PullRequestBotBuilder newBuilder() {
@@ -152,8 +150,9 @@ void scheduleRecheckAt(PullRequest pr, Instant expiresAt) {
scheduledRechecks.put(pr.webUrl().toString(), expiresAt);
}

private List<WorkItem> getWorkItems(List<PullRequest> pullRequests, List<CommitComment> commitComments) {
private List<WorkItem> getWorkItems(List<PullRequest> pullRequests) {
var ret = new LinkedList<WorkItem>();
ret.add(new CommitCommentsWorkItem(this, remoteRepo));

for (var pr : pullRequests) {
if (updateCache.needsUpdate(pr) || checkHasExpired(pr)) {
@@ -169,12 +168,6 @@ private List<WorkItem> getWorkItems(List<PullRequest> pullRequests, List<CommitC
}
}

for (var commitComment : commitComments) {
processedCommitComments.put(commitComment.id(), true);
ret.add(new CommitCommandWorkItem(this, commitComment,
e -> processedCommitComments.remove(commitComment.id())));
}

return ret;
}

@@ -190,12 +183,7 @@ public List<WorkItem> getPeriodicItems() {
prs = remoteRepo.pullRequests(ZonedDateTime.now().minus(Duration.ofDays(1)));
}

var commitComments = remoteRepo.recentCommitComments()
.stream()
.filter(cc -> !processedCommitComments.containsKey(cc.id()))
.collect(Collectors.toList());

return getWorkItems(prs, commitComments);
return getWorkItems(prs);
}

@Override
@@ -205,7 +193,7 @@ public List<WorkItem> processWebHook(JSONValue body) {
return new ArrayList<>();
}

return getWorkItems(webHook.get().updatedPullRequests(), List.of());
return getWorkItems(webHook.get().updatedPullRequests());
}

HostedRepository repo() {
@@ -41,7 +41,6 @@ public class TestHostedRepository extends TestIssueProject implements HostedRepo
private final Repository localRepository;
private final Pattern pullRequestPattern;
private final Map<Hash, List<CommitComment>> commitComments;
private int nextCommitCommentId;

public TestHostedRepository(TestHost host, String projectName, Repository localRepository) {
super(host, projectName);
@@ -50,7 +49,6 @@ public TestHostedRepository(TestHost host, String projectName, Repository localR
this.localRepository = localRepository;
pullRequestPattern = Pattern.compile(url().toString() + "/pr/" + "(\\d+)");
commitComments = new HashMap<>();
nextCommitCommentId = 0;
}

@Override
@@ -222,15 +220,14 @@ public List<CommitComment> recentCommitComments() {

@Override
public CommitComment addCommitComment(Hash hash, String body) {
var id = nextCommitCommentId;
nextCommitCommentId += 1;
var createdAt = ZonedDateTime.now();
var id = createdAt.toInstant().toString();

if (!commitComments.containsKey(hash)) {
commitComments.put(hash, new ArrayList<>());
}
var comments = commitComments.get(hash);
var comment = new CommitComment(hash, null, -1, Integer.toString(id), body, host.currentUser(), createdAt, createdAt);
var comment = new CommitComment(hash, null, -1, id, body, host.currentUser(), createdAt, createdAt);
comments.add(comment);
return comment;
}

1 comment on commit 874a89c

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 874a89c Feb 23, 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.