Skip to content

Commit

Permalink
1068: [Backport] Bot finds original commit in wrong repository
Browse files Browse the repository at this point in the history
Reviewed-by: kcr
  • Loading branch information
erikj79 committed Jul 16, 2021
1 parent fd42d90 commit e98965d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
15 changes: 10 additions & 5 deletions forge/src/main/java/org/openjdk/skara/forge/github/GitHubHost.java
Expand Up @@ -351,17 +351,22 @@ public boolean isMemberOf(String groupId, HostUser user) {

@Override
public Optional<HostedCommit> search(Hash hash) {
var orgsToSearch = orgs.stream().map(o -> "org:" + o).collect(Collectors.joining("+"));
var orgsToSearch = orgs.stream().map(o -> "org:" + o).collect(Collectors.joining(" "));
if (!orgsToSearch.isEmpty()) {
orgsToSearch = "+" + orgsToSearch;
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 repo = repository(first.get("repository").get("full_name").asString());
return repo.get().commit(hash);
// When searching for a commit, there may be hits in multiple repositories.
// There is no good way of knowing for sure which repository we would rather
// get the commit from, but a reasonable default is to go by the shortest
// name as that is most likely the main repository of the project.
var shortestName = items.stream()
.map(o -> o.get("repository").get("full_name").asString())
.min(Comparator.comparing(String::length));
return shortestName.flatMap(this::repository).flatMap(r -> r.commit(hash));
}
}
Expand Up @@ -232,6 +232,11 @@ public Optional<HostedCommit> search(Hash hash) {
var ids = request.get("groups/" + group + "/projects")
.execute()
.stream()
// When searching for a commit, there may be hits in multiple repositories.
// There is no good way of knowing for sure which repository we would rather
// get the commit from, but a reasonable default is to go by the shortest
// path name as that is most likely the main repository of the project.
.sorted(Comparator.comparing(o -> o.get("path").asString().length()))
.map(o -> o.get("id").asInt())
.collect(Collectors.toList());
for (var id : ids) {
Expand Down

1 comment on commit e98965d

@openjdk-notifier
Copy link

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.