Skip to content
Permalink
Browse files
pr: add /tag commit command
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Mar 15, 2021
1 parent d93932e commit 0b326a68c4db0a42ea659330189edbb9bd68de70
@@ -46,7 +46,8 @@ public class CommitCommandWorkItem implements WorkItem {

private static final Map<String, CommandHandler> commandHandlers = Map.ofEntries(
Map.entry("help", new HelpCommand()),
Map.entry("backport", new BackportCommand())
Map.entry("backport", new BackportCommand()),
Map.entry("tag", new TagCommand())
);

static class HelpCommand implements CommandHandler {
@@ -0,0 +1,120 @@
/*
* 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.bots.pr;

import org.openjdk.skara.forge.HostedCommit;
import org.openjdk.skara.forge.PullRequest;
import org.openjdk.skara.issuetracker.Comment;
import org.openjdk.skara.vcs.*;
import org.openjdk.skara.vcs.openjdk.CommitMessageParsers;
import org.openjdk.skara.jcheck.JCheckConfiguration;

import java.io.PrintWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.time.format.DateTimeFormatter;

public class TagCommand implements CommandHandler {
private void showHelp(PrintWriter reply) {
reply.println("Usage: `/tag <name>`");
}

@Override
public String description() {
return "Create a tag for the given commit";
}

@Override
public boolean allowedInCommit() {
return true;
}

@Override
public boolean allowedInPullRequest() {
return false;
}

@Override
public void handle(PullRequestBot bot, HostedCommit commit, CensusInstance censusInstance, Path scratchPath, CommandInvocation command, List<Comment> allComments, PrintWriter reply) {
try {
var username = command.user().username();
if (censusInstance.contributor(command.user()).isEmpty()) {
reply.println("@" + username + " only OpenJDK [contributors](https://openjdk.java.net/bylaws#contributor) can use the `/tag` command.");
return;
}
if (!bot.integrators().contains(username)) {
reply.println("@" + username + " only integrators for this repository are allowed to use the `/tag` command.");
return;
}

var args = command.args();
if (args.isBlank()) {
showHelp(reply);
return;
}

var parts = args.split(" ");
if (parts.length > 1) {
showHelp(reply);
return;
}
var tagName = parts[0];

var localRepoDir = scratchPath.resolve("tag-command")
.resolve(bot.repo().name());
var localRepo = bot.hostedRepositoryPool()
.orElseThrow(() -> new IllegalStateException("Missing repository pool for PR bot"))
.materialize(bot.repo(), localRepoDir);

var existingTagNames = localRepo.tags().stream().map(Tag::name).collect(Collectors.toSet());
if (existingTagNames.contains(tagName)) {
var hash = localRepo.resolve(tagName).orElseThrow(() ->
new IllegalStateException("Cannot resolve tag with name " + tagName + " in repo " + bot.repo().name()));
var hashUrl = bot.repo().webUrl(hash);
reply.println("@" + username + " a tag with name `" + tagName + "` already exists that refers to commit [" + hash.abbreviate() + "](" + hashUrl + "].");
return;
}

var jcheckConf = JCheckConfiguration.from(localRepo, commit.hash());
var tagPattern = jcheckConf.isPresent() ? jcheckConf.get().repository().tags() : null;
if (tagPattern != null && !tagName.matches(tagPattern)) {
reply.println("@" + username + " the given tag name `" + tagName + "` is not of the form `" + tagPattern + "`.");
return;
}

var domain = censusInstance.configuration().census().domain();
var contributor = censusInstance.contributor(command.user()).orElseThrow();
var email = contributor.username() + "@" + domain;
var message = "Added tag " + tagName + " for changeset " + commit.hash().abbreviate();
var tag = localRepo.tag(commit.hash(), tagName, message, contributor.username(), email);
localRepo.push(tag, bot.repo().url(), false);
reply.println("@" + username + " the tag [" + tag.name() + "](" + bot.repo().webUrl(tag) + ") was successfully created.");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

3 comments on commit 0b326a6

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 0b326a6 Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edvbld
Copy link
Member Author

@edvbld edvbld commented on 0b326a6 Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/help

@openjdk
Copy link

@openjdk openjdk bot commented on 0b326a6 Mar 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Available commands:

  • backport - Create a backport
  • help - shows this text
  • tag - Create a tag for the given commit

Please sign in to comment.