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

1092: /backport commit command does not validate non existing repos #1192

Closed
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
@@ -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) {