-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for test/restest command in pull request comment
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/io/quarkus/bot/PullRequestCommandHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.quarkus.bot; | ||
|
||
import io.quarkiverse.githubapp.event.IssueComment; | ||
import io.quarkus.bot.command.Command; | ||
import io.quarkus.bot.config.QuarkusBotConfig; | ||
import org.jboss.logging.Logger; | ||
import org.kohsuke.github.GHEventPayload; | ||
import org.kohsuke.github.GHIssue; | ||
import org.kohsuke.github.GHOrganization; | ||
import org.kohsuke.github.GHTeam; | ||
import org.kohsuke.github.GHUser; | ||
|
||
import javax.enterprise.inject.Instance; | ||
import javax.inject.Inject; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class PullRequestCommandHandler { | ||
|
||
private static final Logger LOG = Logger.getLogger(PullRequestCommandHandler.class); | ||
|
||
private static final String QUARKUS_BOT_NAME = "quarkus-bot"; | ||
private static final String QUARKUS_TEAM_MEMBERS = "quarkus-push"; | ||
private static final Pattern QUARKUS_BOT_MENTION = Pattern.compile("^@(?:quarkus-?)?bot\\s+([a-z _\\-]+)"); | ||
|
||
@Inject | ||
QuarkusBotConfig quarkusBotConfig; | ||
|
||
@Inject | ||
Instance<Command> commands; | ||
|
||
public void onComment(@IssueComment.Created @IssueComment.Edited GHEventPayload.IssueComment commentPayload) | ||
throws IOException { | ||
GHUser user = commentPayload.getComment().getUser(); | ||
GHIssue issue = commentPayload.getIssue(); | ||
|
||
if (issue.isPullRequest() | ||
&& !QUARKUS_BOT_NAME.equals(user.getName())) { | ||
Optional<Command> ghIssueConsumer = extractCommand(commentPayload.getComment().getBody()); | ||
if (ghIssueConsumer.isPresent() && isQuarkusTeamMember(user, commentPayload.getOrganization())) { | ||
if (!quarkusBotConfig.isDryRun()) { | ||
ghIssueConsumer.get().run(issue); | ||
} else { | ||
LOG.info("Pull request #" + issue.getNumber() + " - Restart workflow."); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private Optional<Command> extractCommand(String comment) { | ||
Matcher matcher = QUARKUS_BOT_MENTION.matcher(comment); | ||
if (matcher.matches()) { | ||
String commandLabel = matcher.group(1); | ||
return commands.stream().filter(command -> command.labels().contains(commandLabel)).findFirst(); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
private boolean isQuarkusTeamMember(GHUser user, GHOrganization organization) throws IOException { | ||
GHTeam quarkusPushTeam = organization.getTeams().get(QUARKUS_TEAM_MEMBERS); | ||
if (quarkusPushTeam == null) { | ||
LOG.warn("Unable to find " + QUARKUS_TEAM_MEMBERS + " team in the organization."); | ||
return false; | ||
} | ||
return quarkusPushTeam.hasMember(user); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.quarkus.bot.command; | ||
|
||
import org.kohsuke.github.GHIssue; | ||
|
||
import java.util.List; | ||
|
||
public interface Command { | ||
|
||
List<String> labels(); | ||
|
||
void run(GHIssue issue); | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/io/quarkus/bot/command/RerunWorkflowCommand.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.quarkus.bot.command; | ||
|
||
import io.quarkus.bot.workflow.WorkflowConstants; | ||
import org.jboss.logging.Logger; | ||
import org.kohsuke.github.GHIssue; | ||
import org.kohsuke.github.GHPullRequest; | ||
import org.kohsuke.github.GHRepository; | ||
import org.kohsuke.github.GHWorkflowRun; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@ApplicationScoped | ||
public class RerunWorkflowCommand implements Command { | ||
|
||
private static final Logger LOG = Logger.getLogger(RerunWorkflowCommand.class); | ||
|
||
@Override | ||
public List<String> labels() { | ||
return Arrays.asList("test", "retest"); | ||
} | ||
|
||
@Override | ||
public void run(GHIssue issue) { | ||
try { | ||
GHRepository repository = issue.getRepository(); | ||
GHPullRequest pullRequest = repository.getPullRequest(issue.getNumber()); | ||
|
||
List<GHWorkflowRun> ghWorkflowRuns = repository | ||
.queryWorkflowRuns() | ||
.branch(pullRequest.getHead().getRef()) | ||
.status(GHWorkflowRun.Status.COMPLETED) | ||
.list().toList(); | ||
|
||
Optional<GHWorkflowRun> ghWorkflowRun = ghWorkflowRuns.stream() | ||
.filter(workflowRun -> WorkflowConstants.QUARKUS_CI_WORKFLOW_NAME.equals(workflowRun.getName())) | ||
.filter(workflowRun -> workflowRun.getHeadRepository().getOwnerName() | ||
.equals(pullRequest.getHead().getRepository().getOwnerName())) | ||
.sorted(Comparator.comparingLong(GHWorkflowRun::getId)) | ||
.reduce((run, last) -> last); | ||
|
||
if (ghWorkflowRun.isPresent()) { | ||
LOG.info("Rerunning workflow: " + ghWorkflowRun.get().getHtmlUrl()); | ||
ghWorkflowRun.get().rerun(); | ||
} else { | ||
LOG.warn("Unable to find a workflow run to rerun"); | ||
} | ||
} catch (IOException e) { | ||
LOG.error("Unable to run workflow", e); | ||
} | ||
} | ||
} |