Skip to content

Commit

Permalink
422: Add CLI tool expand issues in commit message
Browse files Browse the repository at this point in the history
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Jun 16, 2020
1 parent ddb6dc7 commit 9af4807
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cli/build.gradle
Expand Up @@ -61,7 +61,8 @@ images {
'git-sync': 'org.openjdk.skara.cli/org.openjdk.skara.cli.GitSync',
'git-publish': 'org.openjdk.skara.cli/org.openjdk.skara.cli.GitPublish',
'git-proxy': 'org.openjdk.skara.cli/org.openjdk.skara.cli.GitProxy',
'git-trees': 'org.openjdk.skara.cli/org.openjdk.skara.cli.GitTrees'
'git-trees': 'org.openjdk.skara.cli/org.openjdk.skara.cli.GitTrees',
'git-expand': 'org.openjdk.skara.cli/org.openjdk.skara.cli.GitExpand'
]

ext.modules = ['jdk.crypto.ec']
Expand Down
159 changes: 159 additions & 0 deletions cli/src/main/java/org/openjdk/skara/cli/GitExpand.java
@@ -0,0 +1,159 @@
/*
* Copyright (c) 2020, 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.*;
import org.openjdk.skara.vcs.Commit;
import org.openjdk.skara.vcs.ReadOnlyRepository;
import org.openjdk.skara.vcs.Repository;
import org.openjdk.skara.jcheck.JCheckConfiguration;
import org.openjdk.skara.issuetracker.IssueTracker;
import org.openjdk.skara.version.Version;

import java.io.IOException;
import java.nio.file.Path;
import java.net.URI;
import java.util.regex.Pattern;
import java.util.*;
import java.util.logging.Level;

public class GitExpand {
static final Pattern ISSUE_ID_PATTERN = Pattern.compile("([A-Za-z][A-Za-z0-9]+)?-([0-9]+)");

private static String getOption(String name, Arguments arguments, ReadOnlyRepository repo) throws IOException {
if (arguments.contains(name)) {
return arguments.get(name).asString();
}

var lines = repo.config("publish." + name);
return lines.size() == 1 ? lines.get(0) : null;
}

private static boolean getSwitch(String name, Arguments arguments, ReadOnlyRepository repo) throws IOException {
if (arguments.contains(name)) {
return true;
}

var lines = repo.config("publish." + name);
return lines.size() == 1 && lines.get(0).toLowerCase().equals("true");
}

private static Repository repo(Path p) throws IOException {
var repo = Repository.get(p);
if (repo.isEmpty()) {
System.err.println("error: no repository found at " + p.toString());
System.exit(1);
}
return repo.get();
}

private static Commit lookup(ReadOnlyRepository repo, String rev) throws IOException {
var hash = repo.resolve(rev);
if (hash.isEmpty()) {
System.err.println("error: could not resolve " + rev);
System.exit(1);
}
var commit = repo.lookup(hash.get());
if (commit.isEmpty()) {
System.err.println("error: could not find commit for hash " + hash.get());
System.exit(1);
}

return commit.get();
}

public static void main(String[] args) throws IOException, InterruptedException {
var flags = List.of(
Switch.shortcut("")
.fullname("issues")
.helptext("Expand issues in the commit message")
.optional(),
Switch.shortcut("")
.fullname("verbose")
.helptext("Turn on verbose output")
.optional(),
Switch.shortcut("")
.fullname("debug")
.helptext("Turn on debugging output")
.optional(),
Switch.shortcut("")
.fullname("version")
.helptext("Print the version of this tool")
.optional());

var inputs = List.of(
Input.position(0)
.describe("REV")
.singular()
.optional()
);

var parser = new ArgumentParser("git-publish", flags, inputs);
var arguments = parser.parse(args);

if (arguments.contains("version")) {
System.out.println("git-expand version: " + Version.fromManifest().orElse("unknown"));
System.exit(0);
}

if (arguments.contains("verbose") || arguments.contains("debug")) {
var level = arguments.contains("debug") ? Level.FINER : Level.FINE;
Logging.setup(level);
}

var cwd = Path.of("").toAbsolutePath();
var repo = repo(cwd);
var rev = arguments.at(0).orString("HEAD");
var commit = lookup(repo, rev);
var message = commit.message();

var shouldExpandIssues = getSwitch("issues", arguments, repo);
if (shouldExpandIssues) {
var conf = JCheckConfiguration.from(repo, commit.hash());
if (conf.isPresent()) {
var project = conf.get().general().jbs();
var tracker = IssueTracker.from("jira", URI.create("https://bugs.openjdk.java.net"));

var amended = new ArrayList<String>();
for (var line : message) {
var m = ISSUE_ID_PATTERN.matcher(line);
if (m.matches()) {
var id = m.group(2);
var issue = tracker.project(project).issue(id);
if (issue.isPresent()) {
amended.add(id + ": " + issue.get().title());
}
} else {
amended.add(line);
}
}

repo.amend(String.join("\n", amended));
} else {
System.err.println("warning: could not expand issues commit message,\n" +
" no JBS project configured in .jcheck/conf");
}
}
}
}

1 change: 1 addition & 0 deletions vcs/src/main/java/org/openjdk/skara/vcs/Repository.java
Expand Up @@ -102,6 +102,7 @@ Hash commit(String message,
ZonedDateTime committerDate,
List<Hash> parents,
Tree tree) throws IOException;
Hash amend(String message) throws IOException;
Hash amend(String message,
String authorName,
String authorEmail) throws IOException;
Expand Down
14 changes: 14 additions & 0 deletions vcs/src/main/java/org/openjdk/skara/vcs/git/GitRepository.java
Expand Up @@ -710,13 +710,27 @@ public Hash commit(String message, String authorName, String authorEmail, ZonedD
}
}

@Override
public Hash amend(String message) throws IOException {
return amend(message, null, null, null, null);
}

@Override
public Hash amend(String message, String authorName, String authorEmail) throws IOException {
return amend(message, authorName, authorEmail, null, null);
}

@Override
public Hash amend(String message, String authorName, String authorEmail, String committerName, String committerEmail) throws IOException {
if (authorName == null || authorEmail == null) {
var head = lookup(head()).orElseThrow();
if (authorName == null) {
authorName = head.author().name();
}
if (authorEmail == null) {
authorEmail = head.author().email();
}
}
if (committerName == null) {
committerName = authorName;
committerEmail = authorEmail;
Expand Down
8 changes: 8 additions & 0 deletions vcs/src/main/java/org/openjdk/skara/vcs/hg/HgRepository.java
Expand Up @@ -621,6 +621,14 @@ public Hash commit(String message, String authorName, String authorEmail, ZonedD
throw new RuntimeException("not implemented yet");
}

@Override
public Hash amend(String message) throws IOException {
try (var p = capture("hg", "commit", "--amend", "--message=" + message)) {
await(p);
}
return resolve("tip").orElseThrow(() -> new IOException("Could not resolve 'tip'"));
}

@Override
public Hash amend(String message, String authorName, String authorEmail) throws IOException {
var user = authorEmail == null ? authorName : authorName + " <" + authorEmail + ">";
Expand Down

1 comment on commit 9af4807

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 9af4807 Jun 16, 2020

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.