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

Allow reading label configuration from a hosted repository file #767

Closed
wants to merge 2 commits 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
@@ -33,7 +33,7 @@ public class PullRequestBotBuilder {
private HostedRepository repo;
private HostedRepository censusRepo;
private String censusRef = "master";
private LabelConfiguration labelConfiguration = LabelConfiguration.builder().build();
private LabelConfiguration labelConfiguration = LabelConfigurationJson.builder().build();
private Map<String, String> externalCommands = Map.of();
private Map<String, String> blockingCheckLabels = Map.of();
private Set<String> readyLabels = Set.of();
@@ -23,7 +23,7 @@
package org.openjdk.skara.bots.pr;

import org.openjdk.skara.bot.*;
import org.openjdk.skara.forge.LabelConfiguration;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.json.*;

import java.util.*;
@@ -65,8 +65,16 @@ public List<Bot> create(BotConfiguration configuration) {

var labelConfigurations = new HashMap<String, LabelConfiguration>();
for (var labelGroup : specific.get("labels").fields()) {
labelConfigurations.put(labelGroup.name(),
LabelConfiguration.fromJSON(labelGroup.value()));
if (labelGroup.value().contains("repository")) {
var repository = configuration.repository(labelGroup.value().get("repository").asString());
var ref = configuration.repositoryRef(labelGroup.value().get("repository").asString());
var filename = labelGroup.value().get("filename").asString();
labelConfigurations.put(labelGroup.name(),
LabelConfigurationHostedRepository.from(repository, ref, filename));
} else {
labelConfigurations.put(labelGroup.name(),
LabelConfigurationJson.from(labelGroup.value()));
}
}

for (var repo : specific.get("repositories").fields()) {
@@ -22,7 +22,7 @@
*/
package org.openjdk.skara.bots.pr;

import org.openjdk.skara.forge.LabelConfiguration;
import org.openjdk.skara.forge.*;
import org.junit.jupiter.api.*;
import org.openjdk.skara.test.*;

@@ -45,12 +45,12 @@ void simple(TestInfo testInfo) throws IOException {
var censusBuilder = credentials.getCensusBuilder()
.addReviewer(integrator.forge().currentUser().id())
.addCommitter(author.forge().currentUser().id());
var labelConfiguration = LabelConfiguration.builder()
.addMatchers("1", List.of(Pattern.compile("cpp$")))
.addMatchers("2", List.of(Pattern.compile("hpp$")))
.addGroup("group", List.of("1", "2"))
.addExtra("extra")
.build();
var labelConfiguration = LabelConfigurationJson.builder()
.addMatchers("1", List.of(Pattern.compile("cpp$")))
.addMatchers("2", List.of(Pattern.compile("hpp$")))
.addGroup("group", List.of("1", "2"))
.addExtra("extra")
.build();
var prBot = PullRequestBot.newBuilder()
.repo(integrator)
.censusRepo(censusBuilder.build())
@@ -135,12 +135,12 @@ void adjustAutoApplied(TestInfo testInfo) throws IOException {
var censusBuilder = credentials.getCensusBuilder()
.addReviewer(integrator.forge().currentUser().id())
.addCommitter(author.forge().currentUser().id());
var labelConfiguration = LabelConfiguration.builder()
.addMatchers("1", List.of(Pattern.compile("cpp$")))
.addMatchers("2", List.of(Pattern.compile("hpp$")))
.addGroup("group", List.of("1", "2"))
.addExtra("extra")
.build();
var labelConfiguration = LabelConfigurationJson.builder()
.addMatchers("1", List.of(Pattern.compile("cpp$")))
.addMatchers("2", List.of(Pattern.compile("hpp$")))
.addGroup("group", List.of("1", "2"))
.addExtra("extra")
.build();
var prBot = PullRequestBot.newBuilder()
.repo(integrator)
.censusRepo(censusBuilder.build())
@@ -203,12 +203,12 @@ void overrideAutoApplied(TestInfo testInfo) throws IOException {
var censusBuilder = credentials.getCensusBuilder()
.addReviewer(integrator.forge().currentUser().id())
.addCommitter(author.forge().currentUser().id());
var labelConfiguration = LabelConfiguration.builder()
.addMatchers("1", List.of(Pattern.compile("cpp$")))
.addMatchers("2", List.of(Pattern.compile("hpp$")))
.addGroup("group", List.of("1", "2"))
.addExtra("extra")
.build();
var labelConfiguration = LabelConfigurationJson.builder()
.addMatchers("1", List.of(Pattern.compile("cpp$")))
.addMatchers("2", List.of(Pattern.compile("hpp$")))
.addGroup("group", List.of("1", "2"))
.addExtra("extra")
.build();
var prBot = PullRequestBot.newBuilder()
.repo(integrator)
.censusRepo(censusBuilder.build())
@@ -265,12 +265,12 @@ void commandAuthor(TestInfo testInfo) throws IOException {
.addCommitter(committer.forge().currentUser().id())
.addAuthor(author.forge().currentUser().id())
.addAuthor(other.forge().currentUser().id());
var labelConfiguration = LabelConfiguration.builder()
.addMatchers("1", List.of(Pattern.compile("cpp$")))
.addMatchers("2", List.of(Pattern.compile("hpp$")))
.addGroup("group", List.of("1", "2"))
.addExtra("extra")
.build();
var labelConfiguration = LabelConfigurationJson.builder()
.addMatchers("1", List.of(Pattern.compile("cpp$")))
.addMatchers("2", List.of(Pattern.compile("hpp$")))
.addGroup("group", List.of("1", "2"))
.addExtra("extra")
.build();
var prBot = PullRequestBot.newBuilder()
.repo(integrator)
.censusRepo(censusBuilder.build())
@@ -22,8 +22,8 @@
*/
package org.openjdk.skara.bots.pr;

import org.openjdk.skara.forge.*;
import org.openjdk.skara.test.*;
import org.openjdk.skara.forge.LabelConfiguration;

import org.junit.jupiter.api.*;

@@ -42,10 +42,10 @@ void simple(TestInfo testInfo) throws IOException {
var author = credentials.getHostedRepository();
var reviewer = credentials.getHostedRepository();

var labelConfiguration = LabelConfiguration.builder()
.addMatchers("test1", List.of(Pattern.compile("a.txt")))
.addMatchers("test2", List.of(Pattern.compile("b.txt")))
.build();
var labelConfiguration = LabelConfigurationJson.builder()
.addMatchers("test1", List.of(Pattern.compile("a.txt")))
.addMatchers("test2", List.of(Pattern.compile("b.txt")))
.build();
var censusBuilder = credentials.getCensusBuilder()
.addAuthor(author.forge().currentUser().id())
.addReviewer(reviewer.forge().currentUser().id());
@@ -23,23 +23,19 @@
package org.openjdk.skara.cli.pr;

import org.openjdk.skara.args.*;
import org.openjdk.skara.cli.GitPublish;
import org.openjdk.skara.cli.GitJCheck;
import org.openjdk.skara.vcs.Branch;
import org.openjdk.skara.vcs.ReadOnlyRepository;
import org.openjdk.skara.vcs.openjdk.CommitMessageParsers;
import org.openjdk.skara.forge.Forge;
import org.openjdk.skara.forge.LabelConfiguration;
import org.openjdk.skara.cli.*;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.json.JSON;

import static org.openjdk.skara.cli.pr.Utils.*;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.vcs.openjdk.CommitMessageParsers;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;

import static org.openjdk.skara.cli.pr.Utils.*;

public class GitPrCreate {
static final List<Flag> flags = List.of(
Option.shortcut("u")
@@ -111,7 +107,7 @@ private static LabelConfiguration labelConfiguration(Forge forge, String project
);
var rules = skaraRemoteRepo.fileContents("config/mailinglist/rules/jdk.json", "master");
var json = JSON.parse(rules);
return LabelConfiguration.fromJSON(json);
return LabelConfigurationJson.from(json);
}

private static Set<String> suggestedLabels(ReadOnlyRepository repo, Forge forge, String project, String targetRef, String headRef) throws IOException {
@@ -22,130 +22,11 @@
*/
package org.openjdk.skara.forge;

import org.openjdk.skara.json.JSONValue;
import org.openjdk.skara.json.JSONObject;

import java.nio.file.Path;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class LabelConfiguration {
private final Map<String, List<Pattern>> matchers;
private final Map<String, List<String>> groups;
private final Set<String> extra;
private final Set<String> allowed;

private LabelConfiguration(Map<String, List<Pattern>> matchers, Map<String, List<String>> groups, Set<String> extra) {
this.matchers = Collections.unmodifiableMap(matchers);
this.groups = Collections.unmodifiableMap(groups);
this.extra = Collections.unmodifiableSet(extra);

var allowed = new HashSet<String>();
allowed.addAll(matchers.keySet());
allowed.addAll(groups.keySet());
allowed.addAll(extra);
this.allowed = Collections.unmodifiableSet(allowed);
}

public static class Builder {
private final Map<String, List<Pattern>> matchers = new HashMap<>();
private final Map<String, List<String>> groups = new HashMap<>();
private final Set<String> extra = new HashSet<>();

public Builder addMatchers(String label, List<Pattern> matchers) {
this.matchers.put(label, matchers);
return this;
}

public Builder addGroup(String label, List<String> members) {
groups.put(label, members);
return this;
}

public Builder addExtra(String label) {
extra.add(label);
return this;
}

public LabelConfiguration build() {
return new LabelConfiguration(matchers, groups, extra);
}
}

public static Builder builder() {
return new Builder();
}

public static LabelConfiguration fromJSON(JSONValue json) {
var builder = builder();
if (json.contains("matchers")) {
var fields = json.get("matchers").fields();
var matchers = fields.stream()
.collect(Collectors.toMap(JSONObject.Field::name,
field -> field.value()
.stream()
.map(JSONValue::asString)
.map(Pattern::compile)
.collect(Collectors.toList())));
matchers.forEach(builder::addMatchers);
}
if (json.contains("groups")) {
var fields = json.get("groups").fields();
var groups = fields.stream()
.collect(Collectors.toMap(JSONObject.Field::name,
field -> field.value()
.stream()
.map(JSONValue::asString)
.collect(Collectors.toList())));
groups.forEach(builder::addGroup);
}
if (json.contains("extra")) {
var extra = json.get("extra").stream()
.map(JSONValue::asString)
.collect(Collectors.toList());
extra.forEach(builder::addExtra);
}
return builder.build();
}

public Set<String> label(Set<Path> changes) {
var labels = new HashSet<String>();
for (var file : changes) {
for (var label : matchers.entrySet()) {
for (var pattern : label.getValue()) {
var matcher = pattern.matcher(file.toString());
if (matcher.find()) {
labels.add(label.getKey());
break;
}
}
}
}

var ret = new HashSet<>(labels);
// If the current labels matches at least two members of a group, use the group instead
for (var group : groups.entrySet()) {
var count = 0;
for (var groupEntry : group.getValue()) {
if (ret.contains(groupEntry)) {
count++;
if (count == 2) {
ret.add(group.getKey());
ret.removeAll(group.getValue());
break;
}
}
}
}
return ret;
}

public Set<String> allowed() {
return allowed;
}
import java.util.Set;

public boolean isAllowed(String s) {
return allowed.contains(s);
}
public interface LabelConfiguration {
Set<String> label(Set<Path> changes);
Set<String> allowed();
boolean isAllowed(String s);
}
@@ -0,0 +1,73 @@
/*
* 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.forge;

import org.openjdk.skara.json.JSON;

import java.nio.file.Path;
import java.util.Set;

public class LabelConfigurationHostedRepository implements LabelConfiguration {
private final HostedRepository repository;
private final String ref;
private final String filename;

private String latestFileContents = "";
private LabelConfiguration latestParsedConfiguration;

private LabelConfigurationHostedRepository(HostedRepository repository, String ref, String filename) {
this.repository = repository;
this.ref = ref;
this.filename = filename;
}

public static LabelConfiguration from(HostedRepository repository, String ref, String filename) {
return new LabelConfigurationHostedRepository(repository, ref, filename);
}

private LabelConfiguration labelConfiguration() {
var contents = repository.fileContents(filename, ref);
if (!contents.equals(latestFileContents)) {
latestFileContents = contents;
var json = JSON.parse(contents);
latestParsedConfiguration = LabelConfigurationJson.from(json);
}
return latestParsedConfiguration;
}

@Override
public Set<String> label(Set<Path> changes) {
return labelConfiguration().label(changes);
}

@Override
public Set<String> allowed() {
return labelConfiguration().allowed();
}

@Override
public boolean isAllowed(String s) {
return labelConfiguration().isAllowed(s);
}
}