Skip to content
Permalink
Browse files

39: Use the lastUpdated field of pull requests to avoid unnecessary p…

…olls

Reviewed-by: ehelin
  • Loading branch information
Robin Westberg
Robin Westberg committed Jul 3, 2019
1 parent fcbae43 commit 8a62fc939b78c434353ea3633d615ea8ad0c6722
@@ -24,11 +24,12 @@

import org.openjdk.skara.bot.*;
import org.openjdk.skara.email.EmailAddress;
import org.openjdk.skara.host.HostedRepository;
import org.openjdk.skara.host.*;
import org.openjdk.skara.jcheck.JCheckConfiguration;

import java.net.URI;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -44,6 +45,7 @@
private final WebrevStorage webrevStorage;
private final Set<String> readyLabels;
private final Map<String, Pattern> readyComments;
private final PullRequestUpdateCache updateCache;

MailingListBridgeBot(EmailAddress from, HostedRepository repo, HostedRepository archive, EmailAddress list,
Set<String> ignoredUsers, URI listArchive, String smtpServer,
@@ -62,6 +64,7 @@

this.webrevStorage = new WebrevStorage(webrevStorageRepository, webrevStorageRef, webrevStorageBase,
webrevStorageBaseUri, from);
this.updateCache = new PullRequestUpdateCache();
}

HostedRepository codeRepo() {
@@ -109,7 +112,9 @@ WebrevStorage webrevStorage() {
List<WorkItem> ret = new LinkedList<>();

for (var pr : codeRepo.getPullRequests()) {
ret.add(new ArchiveWorkItem(pr, this));
if (updateCache.needsUpdate(pr)) {
ret.add(new ArchiveWorkItem(pr, this));
}
}

return ret;
@@ -39,6 +39,7 @@
private final Map<String, String> externalCommands;
private final Map<String, String> blockingLabels;
private final ConcurrentMap<Hash, Boolean> currentLabels = new ConcurrentHashMap<>();
private final PullRequestUpdateCache updateCache;

PullRequestBot(HostedRepository repo, HostedRepository censusRepo, String censusRef, Map<String,
List<Pattern>> labelPatterns, Map<String, String> externalCommands, Map<String, String> blockingLabels) {
@@ -48,6 +49,7 @@
this.labelPatterns = labelPatterns;
this.externalCommands = externalCommands;
this.blockingLabels = blockingLabels;
this.updateCache = new PullRequestUpdateCache();
}

PullRequestBot(HostedRepository repo, HostedRepository censusRepo, String censusRef) {
@@ -58,9 +60,11 @@
var ret = new LinkedList<WorkItem>();

for (var pr : pullRequests) {
ret.add(new CheckWorkItem(pr, censusRepo, censusRef, blockingLabels));
ret.add(new CommandWorkItem(pr, censusRepo, censusRef, externalCommands));
ret.add(new LabelerWorkItem(pr, labelPatterns, currentLabels));
if (updateCache.needsUpdate(pr)) {
ret.add(new CheckWorkItem(pr, censusRepo, censusRef, blockingLabels));
ret.add(new CommandWorkItem(pr, censusRepo, censusRef, externalCommands));
ret.add(new LabelerWorkItem(pr, labelPatterns, currentLabels));
}
}

return ret;
@@ -23,23 +23,26 @@
package org.openjdk.skara.bots.submit;

import org.openjdk.skara.bot.*;
import org.openjdk.skara.host.HostedRepository;
import org.openjdk.skara.host.*;

import java.util.List;
import java.util.stream.Collectors;

public class SubmitBot implements Bot {
private final HostedRepository repository;
private final List<SubmitExecutor> executors;
private final PullRequestUpdateCache updateCache;

SubmitBot(HostedRepository repository, List<SubmitExecutor> executors) {
this.repository = repository;
this.executors = executors;
this.updateCache = new PullRequestUpdateCache();
}

@Override
public List<WorkItem> getPeriodicItems() {
return repository.getPullRequests().stream()
.filter(updateCache::needsUpdate)
.flatMap(pr -> executors.stream()
.map(executor -> new SubmitBotWorkItem(this, executor, pr)))
.collect(Collectors.toList());
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2019, 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.host;

import org.openjdk.skara.host.gitlab.GitLabMergeRequest;

import java.time.ZonedDateTime;
import java.util.*;
import java.util.logging.Logger;

public class PullRequestUpdateCache {
private final Map<HostedRepository, String> repositoryIds = new HashMap<>();
private final Map<String, ZonedDateTime> lastUpdates = new HashMap<>();

private final Logger log = Logger.getLogger("org.openjdk.skara.host");

public boolean needsUpdate(PullRequest pr) {
// GitLab CE does not update this field on events such as adding an award
if (pr instanceof GitLabMergeRequest) {
return true;
}

var repo = pr.repository();
if (!repositoryIds.containsKey(repo)) {
repositoryIds.put(repo, Integer.toString(repositoryIds.size()));
}
var uniqueId = repositoryIds.get(repo) + ";" + pr.getId();
var update = pr.getUpdated();

if (!lastUpdates.containsKey(uniqueId)) {
lastUpdates.put(uniqueId, update);
return true;
}
var lastUpdate = lastUpdates.get(uniqueId);
if (lastUpdate.isBefore(update)) {
lastUpdates.put(uniqueId, update);
return true;
}
log.info("Skipping update for " + repo.getName() + "#" + pr.getId());
return false;
}
}
@@ -41,10 +41,10 @@
private final String sourceRef;
private final String title;
private final List<String> body;
private final Hash headHash;
private final PullRequestData data;

private static class PullRequestData {
private Hash headHash;
String body = "";
final List<Comment> comments = new ArrayList<>();
final List<ReviewComment> reviewComments = new ArrayList<>();
@@ -67,7 +67,11 @@ private TestPullRequest(TestHostedRepository repository, String id, HostUserDeta
this.data = data;

try {
headHash = repository.localRepository().resolve(sourceRef).orElseThrow();
var headHash = repository.localRepository().resolve(sourceRef).orElseThrow();
if (!headHash.equals(data.headHash)) {
data.headHash = headHash;
data.lastUpdate = ZonedDateTime.now();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
@@ -143,7 +147,7 @@ public ReviewComment addReviewCommentReply(ReviewComment parent, String body) {

@Override
public Hash getHeadHash() {
return headHash;
return data.headHash;
}

@Override

0 comments on commit 8a62fc9

Please sign in to comment.
You can’t perform that action at this time.