Permalink
3 comments
on commit
sign in to comment.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
with
426 additions
and 1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
0b326a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
0b326a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/help
0b326a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Available commands: