Skip to content
Permalink
Browse files
1092: /backport commit command does not validate non existing repos
Reviewed-by: kcr
  • Loading branch information
erikj79 committed Jun 24, 2021
1 parent c11e604 commit 8801187c088ed261de4db4084631bd672b0f6857
@@ -32,6 +32,14 @@

public interface Forge extends Host {
String name();

/**
* Gets a HostedRepository on this Forge. This method should verify that the
* repository exists.
* @param name Name of repository to get
* @return Optional containing the repository, or empty if the repository
* does not exist on the Forge.
*/
Optional<HostedRepository> repository(String name);
boolean supportsReviewBody();
Optional<HostedCommit> search(Hash hash);
@@ -31,7 +31,6 @@
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.*;
import java.util.*;
import java.util.stream.Collectors;
@@ -239,10 +238,14 @@ public boolean isValid() {
}
}

JSONObject getProjectInfo(String name) {
Optional<JSONObject> getProjectInfo(String name) {
var project = request.get("repos/" + name)
.execute();
return project.asObject();
.onError(r -> r.statusCode() == 404 ? Optional.of(JSON.object().put("NOT_FOUND", true)) : Optional.empty())
.execute();
if (project.contains("NOT_FOUND")) {
return Optional.empty();
}
return Optional.of(project.asObject());
}

JSONObject runSearch(String category, String query) {
@@ -268,11 +271,8 @@ JSONObject runSearch(String category, String query) {

@Override
public Optional<HostedRepository> repository(String name) {
try {
return Optional.of(new GitHubRepository(this, name));
} catch (Throwable t) {
return Optional.empty();
}
return getProjectInfo(name)
.map(jsonObject -> new GitHubRepository(this, name, jsonObject));
}

@Override
@@ -46,6 +46,11 @@ public class GitHubRepository implements HostedRepository {
private JSONValue cachedJSON;
private List<HostedBranch> branches;

GitHubRepository(GitHubHost gitHubHost, String repository, JSONValue json) {
this(gitHubHost, repository);
cachedJSON = json;
}

GitHubRepository(GitHubHost gitHubHost, String repository) {
this.gitHubHost = gitHubHost;
this.repository = repository;
@@ -69,14 +74,14 @@ public class GitHubRepository implements HostedRepository {
}
return headers;
});
this.cachedJSON = null;
var urlPattern = gitHubHost.getWebURI("/" + repository + "/pull/").toString();
pullRequestPattern = Pattern.compile(urlPattern + "(\\d+)");
}

private JSONValue json() {
if (cachedJSON == null) {
cachedJSON = gitHubHost.getProjectInfo(repository);
cachedJSON = gitHubHost.getProjectInfo(repository)
.orElseThrow(() -> new RuntimeException("Repository not found: " + repository));
}
return cachedJSON;
}
@@ -138,11 +138,8 @@ Optional<JSONObject> getProjectInfo(int id) {

@Override
public Optional<HostedRepository> repository(String name) {
try {
return Optional.of(new GitLabRepository(this, name));
} catch (Throwable t) {
return Optional.empty();
}
return getProjectInfo(name)
.map(jsonObject -> new GitLabRepository(this, jsonObject));
}

HostUser parseAuthorField(JSONValue json) {

1 comment on commit 8801187

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 8801187 Jun 24, 2021

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.