Skip to content
Permalink
Browse files
574: git-info should use info from commit notifications
Reviewed-by: ehelin
  • Loading branch information
magicus authored and edvbld committed Feb 17, 2021
1 parent e267010 commit a8fe187eed6c1dd683cec6799dde7ddd21281fcc
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.skara.cli;

import org.openjdk.skara.args.Arguments;
import org.openjdk.skara.forge.Forge;
import org.openjdk.skara.forge.HostedRepository;
import org.openjdk.skara.host.Credential;
import org.openjdk.skara.vcs.ReadOnlyRepository;
import org.openjdk.skara.vcs.Repository;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;

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

private static String gitConfig(String key) {
try {
var pb = new ProcessBuilder("git", "config", key);
pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
pb.redirectError(ProcessBuilder.Redirect.DISCARD);
var p = pb.start();

var output = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
var res = p.waitFor();
if (res != 0) {
return null;
}

return output == null ? null : output.replace("\n", "");
} catch (InterruptedException e) {
return null;
} catch (IOException e) {
return null;
}
}

public static String getOption(String name, String command, String subsection, Arguments arguments) {
if (arguments.contains(name)) {
return arguments.get(name).asString();
}

if (subsection != null && !subsection.isEmpty()) {
var subsectionSpecific = gitConfig(command + "." + subsection + "." + name);
if (subsectionSpecific != null) {
return subsectionSpecific;
}
}

return gitConfig(command + "." + name);
}

public static boolean getSwitch(String name, String command, String subsection, Arguments arguments) {
if (arguments.contains(name)) {
return true;
}

if (subsection != null && !subsection.isEmpty()) {
var subsectionSpecific = gitConfig(command + "." + subsection + "." + name);
if (subsectionSpecific != null) {
return subsectionSpecific.toLowerCase().equals("true");
}
}

var sectionSpecific = gitConfig(command + "." + name);
return sectionSpecific != null && sectionSpecific.toLowerCase().equals("true");
}

static Repository getRepo() throws IOException {
var cwd = Path.of("").toAbsolutePath();
return Repository.get(cwd).orElseThrow(() -> new IOException("no git repository found at " + cwd.toString()));
}

public static String getRemote(ReadOnlyRepository repo, String command, Arguments arguments) throws IOException {
var remote = getOption("remote", command, null, arguments);
return remote == null ? "origin" : remote;
}

public static URI getURI(ReadOnlyRepository repo, String command, Arguments arguments) throws IOException {
var remotePullPath = repo.pullPath(getRemote(repo, command, arguments));
return Remote.toWebURI(remotePullPath);
}

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");
var shouldUseToken = !getSwitch("no-token", command, null, arguments);
var credentials = !shouldUseToken ?
null :
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()));
if (forge.isEmpty()) {
if (!shouldUseToken) {
if (arguments.contains("verbose")) {
System.err.println("");
}
System.err.println("warning: using this command with --no-token may result in rate limiting from " + forgeURI);
if (!arguments.contains("verbose")) {
System.err.println(" Re-run with --verbose to see if you are being rate limited");
System.err.println("");
}
}
exit("error: failed to connect to host: " + forgeURI);
}
if (credentials != null) {
GitCredentials.approve(credentials);
}
return forge.get();
}

public static String projectName(URI uri) {
var name = uri.getPath().toString().substring(1);
if (name.endsWith(".git")) {
name = name.substring(0, name.length() - ".git".length());
}
return name;
}

public static HostedRepository getHostedRepositoryFor(URI uri, ReadOnlyRepository repo, Forge host) throws IOException {
HostedRepository targetRepo = null;

try {
var upstream = Remote.toWebURI(repo.pullPath("upstream"));
targetRepo = host.repository(projectName(upstream)).orElse(null);
} catch (IOException e) {
// do nothing
}

if (targetRepo == null) {
var remoteRepo = host.repository(projectName(uri)).orElseThrow(() ->
new IOException("Could not find repository at: " + uri.toString())
);
var parentRepo = remoteRepo.parent();
targetRepo = parentRepo.isPresent() ? parentRepo.get() : remoteRepo;
}

return targetRepo;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -36,6 +36,7 @@
import java.util.*;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class GitInfo {
@@ -64,7 +65,20 @@ private static boolean getSwitch(String name, Arguments arguments, ReadOnlyRepos

private static String jbsProject(ReadOnlyRepository repo, Hash hash) throws IOException {
var conf = JCheckConfiguration.from(repo, hash).orElseThrow();
return conf.general().jbs().toUpperCase();
var jbsProject = conf.general().jbs();
if (jbsProject != null) {
return jbsProject.toUpperCase();
} else {
return null;
}
}

private static URI getReviewUrl(ReadOnlyRepository repo, Arguments arguments, Hash hash, CommitMessage message) throws IOException {
var repoUrl = ForgeUtils.getURI(repo, "info", arguments);
var forge = ForgeUtils.getForge(repoUrl, repo, "info", arguments);
var remoteRepo = ForgeUtils.getHostedRepositoryFor(repoUrl, repo, forge);

return remoteRepo.reviewUrl(hash);
}

public static void main(String[] args) throws IOException {
@@ -238,38 +252,29 @@ public static void main(String[] args) throws IOException {
}

if (showReview) {
var decoration = useDecoration? "Review: " : "";
var project = jbsProject(repo, hash);
if (message.issues().size() == 1) {
var issueId = message.issues().get(0).shortId();
var issueTracker = IssueTracker.from("jira", JBS);
var issue = issueTracker.project(project).issue(issueId);
if (issue.isPresent()) {
for (var link : issue.get().links()) {
if (link.title().isPresent() && link.uri().isPresent()) {
if (link.title().get().equals("Review")) {
System.out.println(decoration + link.uri().get());
}
}
}
}
var reviewUrl = getReviewUrl(repo, arguments, hash, message);
if (reviewUrl != null) {
var decoration = useDecoration? "Review: " : "";
System.out.println(decoration + reviewUrl);
}
}
if (showIssues) {
var project = jbsProject(repo, hash);
var uri = JBS + "/browse/" + project + "-";
var issues = message.issues();
if (issues.size() > 1) {
if (useDecoration) {
System.out.println("Issues:");
}
var decoration = useDecoration ? "- " : "";
for (var issue : issues) {
System.out.println(decoration + uri + issue.shortId());
if (project != null) {
var uri = JBS + "/browse/" + project + "-";
var issues = message.issues();
if (issues.size() > 1) {
if (useDecoration) {
System.out.println("Issues:");
}
var decoration = useDecoration ? "- " : "";
for (var issue : issues) {
System.out.println(decoration + uri + issue.shortId());
}
} else if (issues.size() == 1) {
var decoration = useDecoration ? "Issue: " : "";
System.out.println(decoration + uri + issues.get(0).shortId());
}
} else if (issues.size() == 1) {
var decoration = useDecoration ? "Issue: " : "";
System.out.println(decoration + uri + issues.get(0).shortId());
}
}
}
@@ -33,7 +33,6 @@
import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.regex.Pattern;

import static org.openjdk.skara.cli.pr.Utils.*;

@@ -145,7 +144,7 @@ public static void main(String[] args) throws IOException, InterruptedException
return null;
});

var remoteRepo = host.repository(projectName(uri)).orElseThrow(() ->
var remoteRepo = host.repository(ForgeUtils.projectName(uri)).orElseThrow(() ->
new IOException("Could not find repository at " + uri.toString())
);
var parentRepo = remoteRepo.parent().orElseThrow(() ->
@@ -305,7 +304,7 @@ public static void main(String[] args) throws IOException, InterruptedException
}

var mailingLists = new ArrayList<String>();
var parentProject = projectName(parentRepo.url());
var parentProject = ForgeUtils.projectName(parentRepo.url());
var isTargetingJDKRepo = parentProject.matches(".*\\/jdk[0-9]*");
var cc = getOption("cc", "create", arguments);
var isCCManual = cc != null && !cc.equals("auto");
@@ -23,6 +23,7 @@
package org.openjdk.skara.cli.pr;

import org.openjdk.skara.args.*;
import org.openjdk.skara.cli.ForgeUtils;
import org.openjdk.skara.host.HostUser;

import static org.openjdk.skara.cli.pr.Utils.*;
@@ -115,7 +116,7 @@ public static void main(String[] args) throws IOException {
var repo = getRepo();
var uri = getURI(repo, arguments);
var host = getForge(uri, repo, arguments);
var remoteRepo = getHostedRepositoryFor(uri, repo, host);
var remoteRepo = ForgeUtils.getHostedRepositoryFor(uri, repo, host);

var prs = remoteRepo.pullRequests();
var ids = new ArrayList<String>();

1 comment on commit a8fe187

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on a8fe187 Feb 17, 2021

Please sign in to comment.