Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notify: use "listener" instead of "consumer"/"updater" in names #651

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2020, 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.notify;

public interface Emitter {
void registerPullRequestListener(PullRequestListener listener);
void registerRepositoryListener(RepositoryListener listener);
}
@@ -26,8 +26,6 @@
import org.openjdk.skara.json.JSONObject;

public interface Notifier {
String name();

static Notifier create(String name, BotConfiguration botConfiguration, JSONObject notifierConfiguration) {
var factory = NotifierFactory.getNotifierFactories().stream()
.filter(f -> f.name().equals(name))
@@ -37,4 +35,6 @@ static Notifier create(String name, BotConfiguration botConfiguration, JSONObjec
}
return factory.get().create(botConfiguration, notifierConfiguration);
}

void attachTo(Emitter e);
}
@@ -31,33 +31,30 @@
import java.util.logging.Logger;
import java.util.regex.Pattern;

public class NotifyBot implements Bot {
public class NotifyBot implements Bot, Emitter {
private final Logger log = Logger.getLogger("org.openjdk.skara.bots");;
private final HostedRepository repository;
private final Path storagePath;
private final Pattern branches;
private final StorageBuilder<UpdatedTag> tagStorageBuilder;
private final StorageBuilder<UpdatedBranch> branchStorageBuilder;
private final StorageBuilder<PullRequestState> prStateStorageBuilder;
private final List<RepositoryUpdateConsumer> updaters;
private final List<PullRequestUpdateConsumer> prUpdaters;
private final List<RepositoryListener> repoListeners = new ArrayList<>();
private final List<PullRequestListener> prListeners = new ArrayList<>();
private final PullRequestUpdateCache updateCache;
private final Set<String> readyLabels;
private final Map<String, Pattern> readyComments;
private final String integratorId;

NotifyBot(HostedRepository repository, Path storagePath, Pattern branches, StorageBuilder<UpdatedTag> tagStorageBuilder,
StorageBuilder<UpdatedBranch> branchStorageBuilder, StorageBuilder<PullRequestState> prStateStorageBuilder,
List<RepositoryUpdateConsumer> updaters, List<PullRequestUpdateConsumer> prUpdaters,
Set<String> readyLabels, Map<String, Pattern> readyComments, String integratorId) {
this.repository = repository;
this.storagePath = storagePath;
this.branches = branches;
this.tagStorageBuilder = tagStorageBuilder;
this.branchStorageBuilder = branchStorageBuilder;
this.prStateStorageBuilder = prStateStorageBuilder;
this.updaters = updaters;
this.prUpdaters = prUpdaters;
this.updateCache = new PullRequestUpdateCache();
this.readyLabels = readyLabels;
this.readyComments = readyComments;
@@ -98,6 +95,16 @@ private boolean isReady(PullRequest pr) {
return true;
}

@Override
public void registerPullRequestListener(PullRequestListener listener) {
prListeners.add(listener);
}

@Override
public void registerRepositoryListener(RepositoryListener listener) {
repoListeners.add(listener);
}

@Override
public String toString() {
return "JNotifyBot@" + repository.name();
@@ -116,14 +123,14 @@ public List<WorkItem> getPeriodicItems() {
}
ret.add(new PullRequestWorkItem(pr,
prStateStorageBuilder,
prUpdaters,
prListeners,
e -> updateCache.invalidate(pr),
integratorId));
}
}

// Repository events
ret.add(new RepositoryWorkItem(repository, storagePath, branches, tagStorageBuilder, branchStorageBuilder, updaters));
ret.add(new RepositoryWorkItem(repository, storagePath, branches, tagStorageBuilder, branchStorageBuilder, repoListeners));

return ret;
}
@@ -36,8 +36,6 @@ public class NotifyBotBuilder {
private StorageBuilder<UpdatedTag> tagStorageBuilder;
private StorageBuilder<UpdatedBranch> branchStorageBuilder;
private StorageBuilder<PullRequestState> prStateStorageBuilder;
private List<RepositoryUpdateConsumer> updaters = List.of();
private List<PullRequestUpdateConsumer> prUpdaters = List.of();
private Set<String> readyLabels = Set.of();
private Map<String, Pattern> readyComments = Map.of();
private String integratorId;
@@ -72,16 +70,6 @@ public NotifyBotBuilder prStateStorageBuilder(StorageBuilder<PullRequestState> p
return this;
}

public NotifyBotBuilder updaters(List<RepositoryUpdateConsumer> updaters) {
this.updaters = updaters;
return this;
}

public NotifyBotBuilder prUpdaters(List<PullRequestUpdateConsumer> prUpdaters) {
this.prUpdaters = prUpdaters;
return this;
}

public NotifyBotBuilder readyLabels(Set<String> readyLabels) {
this.readyLabels = readyLabels;
return this;
@@ -98,6 +86,6 @@ public NotifyBotBuilder integratorId(String integratorId) {
}

public NotifyBot build() {
return new NotifyBot(repository, storagePath, branches, tagStorageBuilder, branchStorageBuilder, prStateStorageBuilder, updaters, prUpdaters, readyLabels, readyComments, integratorId);
return new NotifyBot(repository, storagePath, branches, tagStorageBuilder, branchStorageBuilder, prStateStorageBuilder, readyLabels, readyComments, integratorId);
}
}
@@ -89,34 +89,6 @@ public List<Bot> create(BotConfiguration configuration) {
branchPattern = Pattern.compile(repo.value().get("branches").asString());
}

var updaters = new ArrayList<RepositoryUpdateConsumer>();
var prUpdaters = new ArrayList<PullRequestUpdateConsumer>();

for (var notifierFactory : notifierFactories) {
if (repo.value().contains(notifierFactory.name())) {
var confArray = repo.value().get(notifierFactory.name());
if (!confArray.isArray()) {
confArray = JSON.array().add(confArray);
}
for (var conf : confArray.asArray()) {
var finalConfiguration = combineConfiguration(notifierConfiguration.get(notifierFactory.name()), conf.asObject());
var notifier = Notifier.create(notifierFactory.name(), configuration, finalConfiguration);
log.info("Configuring notifier " + notifierFactory.name() + " for repository " + repoName);
if (notifier instanceof PullRequestUpdateConsumer) {
prUpdaters.add((PullRequestUpdateConsumer)notifier);
}
if (notifier instanceof RepositoryUpdateConsumer) {
updaters.add((RepositoryUpdateConsumer)notifier);
}
}
}
}

if (updaters.isEmpty() && prUpdaters.isEmpty()) {
log.warning("No notifiers configured for notify bot repository: " + repoName);
continue;
}

var baseName = repo.value().contains("basename") ? repo.value().get("basename").asString() : configuration.repositoryName(repoName);

var tagStorageBuilder = new StorageBuilder<UpdatedTag>(baseName + ".tags.txt")
@@ -132,12 +104,33 @@ public List<Bot> create(BotConfiguration configuration) {
.tagStorageBuilder(tagStorageBuilder)
.branchStorageBuilder(branchStorageBuilder)
.prStateStorageBuilder(prStateStorageBuilder)
.updaters(updaters)
.prUpdaters(prUpdaters)
.readyLabels(readyLabels)
.readyComments(readyComments)
.integratorId(integratorId)
.build();

var hasAttachedNotifier = false;
for (var notifierFactory : notifierFactories) {
if (repo.value().contains(notifierFactory.name())) {
var confArray = repo.value().get(notifierFactory.name());
if (!confArray.isArray()) {
confArray = JSON.array().add(confArray);
}
for (var conf : confArray.asArray()) {
var finalConfiguration = combineConfiguration(notifierConfiguration.get(notifierFactory.name()), conf.asObject());
var notifier = Notifier.create(notifierFactory.name(), configuration, finalConfiguration);
log.info("Configuring notifier " + notifierFactory.name() + " for repository " + repoName);
notifier.attachTo(bot);
hasAttachedNotifier = true;
}
}
}

if (!hasAttachedNotifier) {
log.warning("No notifiers configured for notify bot repository: " + repoName);
continue;
}

ret.add(bot);
}

@@ -26,7 +26,7 @@
import org.openjdk.skara.vcs.Hash;
import org.openjdk.skara.vcs.openjdk.Issue;

public interface PullRequestUpdateConsumer extends Notifier {
public interface PullRequestListener {
default void handleNewIssue(PullRequest pr, Issue issue) {
}
default void handleRemovedIssue(PullRequest pr, Issue issue) {
@@ -38,14 +38,14 @@
public class PullRequestWorkItem implements WorkItem {
private final PullRequest pr;
private final StorageBuilder<PullRequestState> prStateStorageBuilder;
private final List<PullRequestUpdateConsumer> pullRequestUpdateConsumers;
private final List<PullRequestListener> listeners;
private final Consumer<RuntimeException> errorHandler;
private final String integratorId;

PullRequestWorkItem(PullRequest pr, StorageBuilder<PullRequestState> prStateStorageBuilder, List<PullRequestUpdateConsumer> pullRequestUpdateConsumers, Consumer<RuntimeException> errorHandler, String integratorId) {
PullRequestWorkItem(PullRequest pr, StorageBuilder<PullRequestState> prStateStorageBuilder, List<PullRequestListener> listeners, Consumer<RuntimeException> errorHandler, String integratorId) {
this.pr = pr;
this.prStateStorageBuilder = prStateStorageBuilder;
this.pullRequestUpdateConsumers = pullRequestUpdateConsumers;
this.listeners = listeners;
this.errorHandler = errorHandler;
this.integratorId = integratorId;
}
@@ -156,20 +156,20 @@ public boolean concurrentWith(WorkItem other) {
return false;
}

private void notifyListenersAdded(String issueId) {
pullRequestUpdateConsumers.forEach(c -> c.handleNewIssue(pr, new Issue(issueId, "")));
private void notifyNewIssue(String issueId) {
listeners.forEach(c -> c.handleNewIssue(pr, new Issue(issueId, "")));
}

private void notifyListenersRemoved(String issueId) {
pullRequestUpdateConsumers.forEach(c -> c.handleRemovedIssue(pr, new Issue(issueId, "")));
private void notifyRemovedIssue(String issueId) {
listeners.forEach(c -> c.handleRemovedIssue(pr, new Issue(issueId, "")));
}

private void notifyNewPr(PullRequest pr) {
pullRequestUpdateConsumers.forEach(c -> c.handleNewPullRequest(pr));
listeners.forEach(c -> c.handleNewPullRequest(pr));
}

private void notifyIntegratedPr(PullRequest pr, Hash hash) {
pullRequestUpdateConsumers.forEach(c -> c.handleIntegratedPullRequest(pr, hash));
listeners.forEach(c -> c.handleIntegratedPullRequest(pr, hash));
}

@Override
@@ -197,18 +197,18 @@ public Collection<WorkItem> run(Path scratchPath) {
var storedIssues = storedState.get().issueIds();
storedIssues.stream()
.filter(issue -> !issues.contains(issue))
.forEach(this::notifyListenersRemoved);
.forEach(this::notifyRemovedIssue);
issues.stream()
.filter(issue -> !storedIssues.contains(issue))
.forEach(this::notifyListenersAdded);
.forEach(this::notifyNewIssue);

var storedCommit = storedState.get().commitId();
if (!storedCommit.isPresent() && state.commitId().isPresent()) {
notifyIntegratedPr(pr, state.commitId().get());
}
} else {
notifyNewPr(pr);
issues.forEach(this::notifyListenersAdded);
issues.forEach(this::notifyNewIssue);
if (state.commitId().isPresent()) {
notifyIntegratedPr(pr, state.commitId().get());
}
@@ -28,7 +28,7 @@

import java.util.List;

public interface RepositoryUpdateConsumer extends Notifier {
public interface RepositoryListener {
default void handleCommits(HostedRepository repository, Repository localRepository, List<Commit> commits, Branch branch) throws NonRetriableException {
}
default void handleOpenJDKTagCommits(HostedRepository repository, Repository localRepository, List<Commit> commits, OpenJDKTag tag, Tag.Annotated annotated) throws NonRetriableException {