Skip to content
Permalink
Browse files
git-sync should use APIs to find upstream
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Nov 22, 2019
1 parent 6bea8f3 commit 2f8cfc903554fc29d762f9c3555073c3b59c3472
@@ -154,7 +154,9 @@ private Map<String, HostedRepository> parseRepositories(JSONObject config) throw
throw new ConfigurationError("Repository " + entry.name() + " uses undefined host '" + hostName + "'");
}
var host = repositoryHosts.get(hostName);
var repo = host.repository(entry.value().get("repository").asString());
var repo = host.repository(entry.value().get("repository").asString()).orElseThrow(() ->
new ConfigurationError("Repository " + entry.value().get("repository").asString() + " is not available at " + hostName)
);
ret.put(entry.name(), repo);
}

@@ -181,7 +183,9 @@ private RepositoryEntry parseRepositoryEntry(String entry) throws ConfigurationE
throw new ConfigurationError("Repository entry " + entry + " uses undefined host '" + hostName + "'");
}
var repositoryName = entry.substring(hostSeparatorIndex + 1);
ret.repository = host.repository(repositoryName);
ret.repository = host.repository(repositoryName).orElseThrow(() ->
new ConfigurationError("Repository " + repositoryName + " is not available at " + hostName)
);
} else {
if (!repositories.containsKey(entry)) {
throw new ConfigurationError("Repository " + entry + " is not defined!");
@@ -137,7 +137,9 @@ private List<String> botSpecificChecks() throws IOException {
var sourceBranch = mergeSourceBranch();
if (sourceBranch.isPresent() && sourceRepo.isPresent()) {
try {
var mergeSourceRepo = pr.repository().forge().repository(sourceRepo.get());
var mergeSourceRepo = pr.repository().forge().repository(sourceRepo.get()).orElseThrow(() ->
new RuntimeException("Could not find repository " + sourceRepo.get())
);
try {
var sourceHash = prInstance.localRepo().fetch(mergeSourceRepo.url(), sourceBranch.get());
if (!prInstance.localRepo().isAncestor(commits.get(1).hash(), sourceHash)) {
@@ -37,8 +37,8 @@ public boolean isValid() {
}

@Override
public HostedRepository repository(String name) {
return null;
public Optional<HostedRepository> repository(String name) {
return Optional.empty();
}

@Override
@@ -157,7 +157,10 @@ public static void main(String[] args) throws IOException {
path = path.substring(1);
}

var fork = host.get().repository(path).fork();
var hostedRepo = host.get().repository(path).orElseThrow(() ->
new IOException("Could not find repository at " + uri.toString())
);
var fork = hostedRepo.fork();

if (token == null) {
GitCredentials.approve(credentials);
@@ -108,7 +108,9 @@ private static HostedRepository getHostedRepositoryFor(URI uri, GitCredentials c
if (host.isEmpty() || !host.get().isValid()) {
exit("error: failed to connect to host " + uri);
}
var remoteRepo = host.get().repository(projectName(uri));
var remoteRepo = host.get().repository(projectName(uri)).orElseThrow(() ->
new IOException("Could not find repository at: " + uri.toString())
);
var parentRepo = remoteRepo.parent();
var targetRepo = parentRepo.isPresent() ? parentRepo.get() : remoteRepo;
return targetRepo;
@@ -418,7 +420,9 @@ public static void main(String[] args) throws IOException, InterruptedException
System.exit(1);
}

var remoteRepo = host.get().repository(projectName(uri));
var remoteRepo = host.get().repository(projectName(uri)).orElseThrow(() ->
new IOException("Could not find repository at " + uri.toString())
);
if (token == null) {
GitCredentials.approve(credentials);
}
@@ -575,7 +579,9 @@ public static void main(String[] args) throws IOException, InterruptedException
System.exit(1);
}

var remoteRepo = host.get().repository(projectName(uri));
var remoteRepo = host.get().repository(projectName(uri)).orElseThrow(() ->
new IOException("Could not find repository at " + uri.toString())
);
if (token == null) {
GitCredentials.approve(credentials);
}
@@ -24,9 +24,11 @@

import org.openjdk.skara.args.*;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.forge.*;
import org.openjdk.skara.proxy.HttpProxy;

import java.io.*;
import java.net.URI;
import java.net.*;
import java.nio.file.*;
import java.util.*;
import java.util.logging.*;
@@ -101,6 +103,8 @@ public static void main(String[] args) throws IOException, InterruptedException
die("error: no repository found at " + cwd.toString())
);

HttpProxy.setup();

var remotes = repo.remotes();

String upstream = null;
@@ -111,9 +115,25 @@ public static void main(String[] args) throws IOException, InterruptedException
if (lines.size() == 1 && remotes.contains(lines.get(0))) {
upstream = lines.get(0);
} else {
die("No remote provided to fetch from, please set the --from flag");
if (remotes.contains("origin")) {
var originPullPath = repo.pullPath("origin");
try {
var uri = Remote.toWebURI(originPullPath);
upstream = Forge.from(URI.create(uri.getScheme() + "://" + uri.getHost()))
.flatMap(f -> f.repository(uri.getPath().substring(1)))
.flatMap(r -> r.parent())
.map(p -> p.webUrl().toString())
.orElse(null);
} catch (IllegalArgumentException e) {
upstream = null;
}
}
}
}

if (upstream == null) {
die("Could not find upstream repository, please specify one with --from");
}
var upstreamPullPath = remotes.contains(upstream) ?
Remote.toURI(repo.pullPath(upstream)) : URI.create(upstream);

@@ -34,6 +34,9 @@ public static URI toWebURI(String remotePath) throws IOException {
if (remotePath.startsWith("git+")) {
remotePath = remotePath.substring("git+".length());
}
if (remotePath.endsWith(".git")) {
remotePath = remotePath.substring(0, remotePath.length() - ".git".length());
}
if (remotePath.startsWith("http")) {
return URI.create(remotePath);
} else {
@@ -30,7 +30,7 @@
import java.util.stream.Collectors;

public interface Forge extends Host {
HostedRepository repository(String name);
Optional<HostedRepository> repository(String name);
boolean supportsReviewBody();

static Forge from(String name, URI uri, Credential credential, JSONObject configuration) {
@@ -30,7 +30,7 @@
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.*;
import java.util.logging.Logger;
import java.util.regex.Pattern;

@@ -169,8 +169,12 @@ JSONObject runSearch(String query) {
}

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

@Override
@@ -190,7 +190,7 @@ public Optional<WebHook> parseWebHook(JSONValue body) {
@Override
public HostedRepository fork() {
var response = request.post("forks").execute();
return gitHubHost.repository(response.get("full_name").asString());
return gitHubHost.repository(response.get("full_name").asString()).orElseThrow(RuntimeException::new);
}

@Override
@@ -102,8 +102,12 @@ JSONObject getProjectInfo(String name) {
}

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

private HostUser parseUserDetails(JSONObject details) {
@@ -235,7 +235,7 @@ public HostedRepository fork() {
e.printStackTrace();
}
}
return gitLabHost.repository(forkedRepoName);
return gitLabHost.repository(forkedRepoName).orElseThrow(RuntimeException::new);
}

@Override
@@ -208,7 +208,7 @@ private void generateVersion(Path folder) throws IOException {
public HostedRepository build() {
try {
var host = TestHost.createNew(List.of(new HostUser(1, "cu", "Census User")));
var repository = host.repository("census");
var repository = host.repository("census").orElseThrow();
var folder = Files.createTempDirectory("censusbuilder");
var localRepository = Repository.init(folder, VCS.GIT);

@@ -91,7 +91,7 @@ public IssueTracker createIssueHost(int userIndex) {

@Override
public HostedRepository getHostedRepository(Forge host) {
return host.repository(config.get("project").asString());
return host.repository(config.get("project").asString()).orElseThrow();
}

@Override
@@ -128,7 +128,7 @@ public IssueTracker createIssueHost(int userIndex) {

@Override
public HostedRepository getHostedRepository(Forge host) {
return host.repository(config.get("project").asString());
return host.repository(config.get("project").asString()).orElseThrow();
}

@Override
@@ -165,7 +165,7 @@ public IssueTracker createIssueHost(int userIndex) {

@Override
public HostedRepository getHostedRepository(Forge host) {
return host.repository(config.get("project").asString());
return host.repository(config.get("project").asString()).orElseThrow();
}

@Override
@@ -209,7 +209,7 @@ public IssueTracker createIssueHost(int userIndex) {

@Override
public HostedRepository getHostedRepository(Forge host) {
return host.repository("test");
return host.repository("test").orElseThrow();
}

@Override
@@ -85,7 +85,7 @@ public boolean isValid() {
}

@Override
public HostedRepository repository(String name) {
public Optional<HostedRepository> repository(String name) {
Repository localRepository;
if (data.repositories.containsKey(name)) {
localRepository = data.repositories.get(name);
@@ -96,7 +96,7 @@ public HostedRepository repository(String name) {
localRepository = createLocalRepository();
data.repositories.put(name, localRepository);
}
return new TestHostedRepository(this, name, localRepository);
return Optional.of(new TestHostedRepository(this, name, localRepository));
}

@Override

0 comments on commit 2f8cfc9

Please sign in to comment.