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

cli: make it possible to configure forge name #1076

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
@@ -33,13 +33,27 @@
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Optional;

public class ForgeUtils {
private static void exit(String fmt, Object... args) {
System.err.println(String.format(fmt, args));
System.exit(1);
}

private static void gitConfig(String key, String value) {
try {
var pb = new ProcessBuilder("git", "config", key, value);
pb.redirectOutput(ProcessBuilder.Redirect.DISCARD);
pb.redirectError(ProcessBuilder.Redirect.DISCARD);
pb.start().waitFor();
} catch (InterruptedException e) {
// do nothing
} catch (IOException e) {
// do nothing
}
}

private static String gitConfig(String key) {
try {
var pb = new ProcessBuilder("git", "config", key);
@@ -107,6 +121,23 @@ public static URI getURI(ReadOnlyRepository repo, String command, Arguments argu
return Remote.toWebURI(remotePullPath);
}

public static Optional<Forge> from(URI uri) {
return from(uri, null);
}

public static Optional<Forge> from(URI uri, Credential credentials) {
var name = gitConfig("forge.name");
if (name != null) {
var forge = credentials == null ? Forge.from(name, uri) : Forge.from(name, uri, credentials);
return Optional.of(forge);
}
var forge = credentials == null ? Forge.from(uri) : Forge.from(uri, credentials);
if (forge.isPresent()) {
gitConfig("forge.name", forge.get().name().toLowerCase());
}
return forge;
}

public static Forge getForge(URI uri, ReadOnlyRepository repo, String command, Arguments arguments) throws IOException {
var username = getOption("username", null, null, arguments);
var token = System.getenv("GIT_TOKEN");
@@ -116,8 +147,8 @@ public static Forge getForge(URI uri, ReadOnlyRepository repo, String command, A
GitCredentials.fill(uri.getHost(), uri.getPath(), username, token, uri.getScheme());
var forgeURI = URI.create(uri.getScheme() + "://" + uri.getHost());
var forge = credentials == null ?
Forge.from(forgeURI) :
Forge.from(forgeURI, new Credential(credentials.username(), credentials.password()));
from(forgeURI) :
from(forgeURI, new Credential(credentials.username(), credentials.password()));
if (forge.isEmpty()) {
if (!shouldUseToken) {
if (arguments.contains("verbose")) {
@@ -151,7 +151,7 @@ public static void main(String[] args) throws IOException, InterruptedException
var token = System.getenv("GIT_TOKEN");
var credentials = GitCredentials.fill(canonical.getHost(), canonical.getPath(), username, token, canonical.getScheme());
var forgeURI = URI.create(canonical.getScheme() + "://" + canonical.getHost());
var forge = Forge.from(forgeURI, new Credential(credentials.username(), credentials.password()));
var forge = ForgeUtils.from(forgeURI, new Credential(credentials.username(), credentials.password()));
if (forge.isEmpty()) {
System.err.println("error: could not find forge at " + forgeURI.getHost());
System.exit(1);
@@ -174,7 +174,7 @@ public static void main(String[] args) throws IOException, InterruptedException
exit("error: no username for " + webURI.getHost() + " found, use git-credentials or the flag --username");
}

var host = Forge.from(webURI, new Credential(credentials.username(), credentials.password()));
var host = ForgeUtils.from(webURI, new Credential(credentials.username(), credentials.password()));
if (host.isEmpty()) {
exit("error: could not connect to host " + webURI.getHost());
}
@@ -289,11 +289,11 @@ public static void main(String[] args) throws IOException {
var originPullPath = repo.pullPath("origin");
var uri = Remote.toWebURI(originPullPath);
try {
upstreamURI = Forge.from(uri)
.flatMap(f -> f.repository(uri.getPath().substring(1)))
.flatMap(r -> r.parent())
.map(p -> p.webUrl())
.orElse(null);
upstreamURI = ForgeUtils.from(uri)
.flatMap(f -> f.repository(uri.getPath().substring(1)))
.flatMap(r -> r.parent())
.map(p -> p.webUrl())
.orElse(null);
} catch (Throwable t) {
System.err.println("error: could not find upstream repository");
System.exit(1);
@@ -324,11 +324,11 @@ public static void main(String[] args) throws IOException {
System.exit(1);
}
try {
forkURI = Forge.from(uri, new Credential(credentials.username(), credentials.password()))
.flatMap(f -> f.repository(uri.getPath().substring(1)))
.map(r -> r.fork())
.map(fork -> fork.webUrl())
.orElse(null);
forkURI = ForgeUtils.from(uri, new Credential(credentials.username(), credentials.password()))
.flatMap(f -> f.repository(uri.getPath().substring(1)))
.map(r -> r.fork())
.map(fork -> fork.webUrl())
.orElse(null);
} catch (Throwable t) {
System.err.println("error: could not find fork for upstream repository");
System.exit(1);
@@ -265,7 +265,7 @@ public static void main(String[] args) throws IOException, InterruptedException
exit("error: no username for " + webURI.getHost() + " found, use git-credentials or the flag --username");
}

var host = Forge.from(webURI, new Credential(credentials.username(), credentials.password()));
var host = ForgeUtils.from(webURI, new Credential(credentials.username(), credentials.password()));
if (host.isEmpty()) {
exit("error: could not connect to host " + webURI.getHost());
}
@@ -156,11 +156,11 @@ static void sync(Repository repo, String[] args) throws IOException, Interrupted
var originPullPath = repo.pullPath("origin");
try {
var uri = Remote.toWebURI(originPullPath);
from = Forge.from(uri)
.flatMap(f -> f.repository(uri.getPath().substring(1)))
.flatMap(r -> r.parent())
.map(p -> p.webUrl().toString())
.orElse(null);
from = ForgeUtils.from(uri)
.flatMap(f -> f.repository(uri.getPath().substring(1)))
.flatMap(r -> r.parent())
.map(p -> p.webUrl().toString())
.orElse(null);
} catch (Throwable e) {
from = null;
}
@@ -46,6 +46,14 @@ static Forge from(String name, URI uri, Credential credential, JSONObject config
return factory.get().create(uri, credential, configuration);
}

static Forge from(String name, URI uri, Credential credential) {
return from(name, uri, credential, null);
}

static Forge from(String name, URI uri) {
return from(name, uri, null);
}

static Optional<Forge> from(URI uri, Credential credential, JSONObject configuration) {
var factories = ForgeFactory.getForgeFactories();