Skip to content
Permalink
Browse files

forge: add search method

Reviewed-by: rwestberg
  • Loading branch information
Erik Helin
Erik Helin committed Sep 30, 2020
1 parent f023a34 commit 10f3a1f07e0ee2ced9aaa8be8d944078fd629e64
@@ -24,6 +24,7 @@

import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.*;
import org.openjdk.skara.vcs.Hash;

import java.util.*;

@@ -65,4 +66,9 @@ public boolean supportsReviewBody() {
public boolean isMemberOf(String groupId, HostUser user) {
return groups.get(groupId).contains(user);
}

@Override
public Optional<HostedCommitMetadata> search(Hash hash) {
return Optional.empty();
}
}
@@ -24,6 +24,7 @@

import org.openjdk.skara.host.*;
import org.openjdk.skara.json.JSONObject;
import org.openjdk.skara.vcs.Hash;

import java.net.URI;
import java.util.*;
@@ -33,6 +34,7 @@
String name();
Optional<HostedRepository> repository(String name);
boolean supportsReviewBody();
Optional<HostedCommitMetadata> search(Hash hash);

static Forge from(String name, URI uri, Credential credential, JSONObject configuration) {
var factory = ForgeFactory.getForgeFactories().stream()
@@ -0,0 +1,105 @@
/*
* 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.vcs.*;

import java.net.URI;
import java.util.*;
import java.time.*;
import java.time.format.*;

public class HostedCommitMetadata {
private final CommitMetadata metadata;
private final URI url;

public HostedCommitMetadata(CommitMetadata metadata, URI url) {
this.metadata = metadata;
this.url = url;
}

public Hash hash() {
return metadata.hash();
}

public Author author() {
return metadata.author();
}

public Author committer() {
return metadata.committer();
}

public List<String> message() {
return metadata.message();
}

public List<Hash> parents() {
return metadata.parents();
}

public ZonedDateTime authored() {
return metadata.authored();
}

public ZonedDateTime committed() {
return metadata.committed();
}

public boolean isInitialCommit() {
return metadata.isInitialCommit();
}

public boolean isMerge() {
return metadata.isMerge();
}

public int numParents() {
return metadata.numParents();
}

public URI url() {
return url;
}

@Override
public String toString() {
return url.toString();
}

@Override
public int hashCode() {
return Objects.hash(metadata, url);
}

@Override
public boolean equals(Object o) {
if (!(o instanceof HostedCommitMetadata)) {
return false;
}

var other = (HostedCommitMetadata) o;
return Objects.equals(metadata, other.metadata) &&
Objects.equals(url, other.url);
}
}
@@ -3,10 +3,13 @@
import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.Credential;
import org.openjdk.skara.json.JSONObject;
import org.openjdk.skara.json.JSONValue;

import java.net.URI;
import java.util.Set;
import java.util.HashSet;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class GitHubForgeFactory implements ForgeFactory {
@Override
@@ -28,18 +31,26 @@ public Forge create(URI uri, Credential credential, JSONObject configuration) {
webUriReplacement = configuration.get("weburl").get("replacement").asString();
}

Set<String> orgs = new HashSet<String>();
if (configuration != null && configuration.contains("orgs")) {
orgs = configuration.get("orgs")
.stream()
.map(JSONValue::asString)
.collect(Collectors.toSet());
}

if (credential != null) {
if (credential.username().contains(";")) {
var separator = credential.username().indexOf(";");
var id = credential.username().substring(0, separator);
var installation = credential.username().substring(separator + 1);
var app = new GitHubApplication(credential.password(), id, installation);
return new GitHubHost(uri, app, webUriPattern, webUriReplacement);
return new GitHubHost(uri, app, webUriPattern, webUriReplacement, orgs);
} else {
return new GitHubHost(uri, credential, webUriPattern, webUriReplacement);
return new GitHubHost(uri, credential, webUriPattern, webUriReplacement, orgs);
}
} else {
return new GitHubHost(uri, webUriPattern, webUriReplacement);
return new GitHubHost(uri, webUriPattern, webUriReplacement, orgs);
}
}
}
@@ -26,12 +26,14 @@
import org.openjdk.skara.host.*;
import org.openjdk.skara.json.*;
import org.openjdk.skara.network.*;
import org.openjdk.skara.vcs.*;

import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.time.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.logging.Logger;
import java.util.regex.Pattern;

@@ -47,13 +49,15 @@
private HostUser currentUser;
private volatile Instant lastSearch = Instant.now();
private final Logger log = Logger.getLogger("org.openjdk.skara.forge.github");
private final Set<String> orgs;

public GitHubHost(URI uri, GitHubApplication application, Pattern webUriPattern, String webUriReplacement) {
public GitHubHost(URI uri, GitHubApplication application, Pattern webUriPattern, String webUriReplacement, Set<String> orgs) {
this.uri = uri;
this.webUriPattern = webUriPattern;
this.webUriReplacement = webUriReplacement;
this.application = application;
this.pat = null;
this.orgs = orgs;
searchInterval = Duration.ofSeconds(3);

var baseApi = URIBuilder.base(uri)
@@ -86,12 +90,13 @@ RestRequest graphQL() {
return graphQL;
}

public GitHubHost(URI uri, Credential pat, Pattern webUriPattern, String webUriReplacement) {
public GitHubHost(URI uri, Credential pat, Pattern webUriPattern, String webUriReplacement, Set<String> orgs) {
this.uri = uri;
this.webUriPattern = webUriPattern;
this.webUriReplacement = webUriReplacement;
this.pat = pat;
this.application = null;
this.orgs = orgs;
searchInterval = Duration.ofSeconds(3);

var baseApi = URIBuilder.base(uri)
@@ -115,12 +120,13 @@ public GitHubHost(URI uri, Credential pat, Pattern webUriPattern, String webUriR
));
}

GitHubHost(URI uri, Pattern webUriPattern, String webUriReplacement) {
GitHubHost(URI uri, Pattern webUriPattern, String webUriReplacement, Set<String> orgs) {
this.uri = uri;
this.webUriPattern = webUriPattern;
this.webUriReplacement = webUriReplacement;
this.pat = null;
this.application = null;
this.orgs = orgs;
searchInterval = Duration.ofSeconds(10);

var baseApi = URIBuilder.base(uri)
@@ -225,7 +231,7 @@ JSONObject getProjectInfo(String name) {
return project.asObject();
}

JSONObject runSearch(String query) {
JSONObject runSearch(String category, String query) {
// Searches on GitHub uses a special rate limit, so make sure to wait between consecutive searches
while (true) {
synchronized (this) {
@@ -240,7 +246,7 @@ JSONObject runSearch(String query) {
} catch (InterruptedException ignored) {
}
}
var result = request.get("search/issues")
var result = request.get("search/" + category)
.param("q", query)
.execute();
return result.asObject();
@@ -323,4 +329,37 @@ public boolean isMemberOf(String groupId, HostUser user) {

return false;
}

CommitMetadata toCommitMetadata(JSONValue o) {
var hash = new Hash(o.get("sha").asString());
var parents = o.get("parents").stream()
.map(p -> new Hash(p.get("sha").asString()))
.collect(Collectors.toList());
var commit = o.get("commit").asObject();
var author = new Author(commit.get("author").get("name").asString(),
commit.get("author").get("email").asString());
var authored = ZonedDateTime.parse(commit.get("author").get("date").asString());
var committer = new Author(commit.get("committer").get("name").asString(),
commit.get("committer").get("email").asString());
var committed = ZonedDateTime.parse(commit.get("committer").get("date").asString());
var message = Arrays.asList(commit.get("message").asString().split("\n"));
return new CommitMetadata(hash, parents, author, authored, committer, committed, message);
}

@Override
public Optional<HostedCommitMetadata> search(Hash hash) {
var orgsToSearch = orgs.stream().map(o -> "org:" + o).collect(Collectors.joining("+"));
if (!orgsToSearch.isEmpty()) {
orgsToSearch = "+" + orgsToSearch;
}
var result = runSearch("commits", "hash:" + hash.hex() + orgsToSearch);
var items = result.get("items").asArray();
if (items.isEmpty()) {
return Optional.empty();
}
var first = items.get(0);
var metadata = toCommitMetadata(first);
var url = URI.create(first.get("url").asString());
return Optional.of(new HostedCommitMetadata(metadata, url));
}
}
@@ -151,7 +151,7 @@ public PullRequest pullRequest(String id) {
if (author != null) {
query += " commenter:" + author;
}
var result = gitHubHost.runSearch(query);
var result = gitHubHost.runSearch("issues", query);
return result.get("items").stream()
.map(jsonValue -> jsonValue.get("number").asInt())
.map(id -> pullRequest(id.toString()))
@@ -299,17 +299,6 @@ public void addCommitComment(Hash hash, String body) {
if (o.isNull()) {
return Optional.empty();
}
var parents = o.get("parents").stream()
.map(p -> new Hash(p.get("sha").asString()))
.collect(Collectors.toList());
var commit = o.get("commit").asObject();
var author = new Author(commit.get("author").get("name").asString(),
commit.get("author").get("email").asString());
var authored = ZonedDateTime.parse(commit.get("author").get("date").asString());
var committer = new Author(commit.get("committer").get("name").asString(),
commit.get("committer").get("email").asString());
var committed = ZonedDateTime.parse(commit.get("committer").get("date").asString());
var message = Arrays.asList(commit.get("message").asString().split("\n"));
return Optional.of(new CommitMetadata(hash, parents, author, authored, committer, committed, message));
return Optional.of(gitHubHost.toCommitMetadata(o));
}
}
@@ -3,9 +3,12 @@
import org.openjdk.skara.forge.*;
import org.openjdk.skara.host.Credential;
import org.openjdk.skara.json.JSONObject;
import org.openjdk.skara.json.JSONValue;

import java.net.URI;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class GitLabForgeFactory implements ForgeFactory {
@Override
@@ -24,10 +27,17 @@ public Forge create(URI uri, Credential credential, JSONObject configuration) {
if (configuration != null && configuration.contains("name")) {
name = configuration.get("name").asString();
}
Set<String> groups = new HashSet<String>();
if (configuration != null && configuration.contains("groups")) {
groups = configuration.get("groups")
.stream()
.map(JSONValue::asString)
.collect(Collectors.toSet());
}
if (credential != null) {
return new GitLabHost(name, uri, credential);
return new GitLabHost(name, uri, credential, groups);
} else {
return new GitLabHost(name, uri);
return new GitLabHost(name, uri, groups);
}
}
}

1 comment on commit 10f3a1f

@bridgekeeper

This comment has been minimized.

Copy link

@bridgekeeper bridgekeeper bot commented on 10f3a1f Sep 30, 2020

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