|
| 1 | +package org.keycloak.gh.bot; |
| 2 | + |
| 3 | +import io.quarkiverse.githubapp.event.PullRequest; |
| 4 | +import org.jboss.logging.Logger; |
| 5 | +import org.keycloak.gh.bot.utils.CommitUtils; |
| 6 | +import org.kohsuke.github.*; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +public class SyncPRTeamLabels { |
| 16 | + |
| 17 | + private static final Logger logger = Logger.getLogger(SyncPRTeamLabels.class); |
| 18 | + private static final org.slf4j.Logger log = LoggerFactory.getLogger(SyncPRTeamLabels.class); |
| 19 | + |
| 20 | + void onOpen(@PullRequest.Opened GHEventPayload.PullRequest payload) throws IOException { |
| 21 | + GHPullRequest pullRequest = payload.getPullRequest(); |
| 22 | + List<String> commitMessages = pullRequest.listCommits().toList().stream().map(i -> i.getCommit().getMessage()).toList(); |
| 23 | + |
| 24 | + Set<String> teamLabelsToAdd = new HashSet<>(); |
| 25 | + |
| 26 | + for (String commitMessage : commitMessages) { |
| 27 | + Integer issuerNumber = CommitUtils.getIssuerNumber(commitMessage); |
| 28 | + if (issuerNumber != null) { |
| 29 | + GHRepository repository = payload.getRepository(); |
| 30 | + GHIssue issue = repository.getIssue(issuerNumber); |
| 31 | + logger.infov("PR {0} {1} linked to issue {2} {3}", pullRequest.getNumber(), pullRequest.getTitle(), issue.getNumber(), issue.getTitle()); |
| 32 | + |
| 33 | + issue.getLabels().stream().map(GHLabel::getName).filter(l -> l.startsWith("team/")).forEach(teamLabelsToAdd::add); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if (!teamLabelsToAdd.isEmpty()) { |
| 38 | + logger.infov("Adding labels {0} to PR {1}", String.join(",", teamLabelsToAdd), pullRequest.getNumber()); |
| 39 | + pullRequest.addLabels(teamLabelsToAdd.toArray(new String[0])); |
| 40 | + } else { |
| 41 | + logger.infov("No team labels found for PR {0}", pullRequest.getNumber()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +} |
0 commit comments