|
| 1 | +package org.hibernate.infra.bot; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Locale; |
| 6 | +import java.util.Random; |
| 7 | + |
| 8 | +import org.hibernate.infra.bot.config.DeploymentConfig; |
| 9 | +import org.hibernate.infra.bot.config.RepositoryConfig; |
| 10 | + |
| 11 | +import org.jboss.logging.Logger; |
| 12 | + |
| 13 | +import io.quarkiverse.githubapp.ConfigFile; |
| 14 | +import io.quarkiverse.githubapp.event.PullRequest; |
| 15 | +import jakarta.inject.Inject; |
| 16 | +import org.kohsuke.github.GHEventPayload; |
| 17 | +import org.kohsuke.github.GHFileNotFoundException; |
| 18 | +import org.kohsuke.github.GHIssueState; |
| 19 | +import org.kohsuke.github.GHLabel; |
| 20 | +import org.kohsuke.github.GHPullRequest; |
| 21 | +import org.kohsuke.github.GHRepository; |
| 22 | +import org.kohsuke.github.GHUser; |
| 23 | + |
| 24 | +public class EditPullRequestAddBranchTagLabel { |
| 25 | + private static final Logger LOG = Logger.getLogger( EditPullRequestAddBranchTagLabel.class ); |
| 26 | + |
| 27 | + @Inject |
| 28 | + DeploymentConfig deploymentConfig; |
| 29 | + |
| 30 | + void pullRequestChanged( |
| 31 | + @PullRequest.Opened @PullRequest.Reopened @PullRequest.Edited @PullRequest.Synchronize |
| 32 | + GHEventPayload.PullRequest payload, |
| 33 | + @ConfigFile("hibernate-github-bot.yml") RepositoryConfig repositoryConfig |
| 34 | + ) throws IOException { |
| 35 | + addTagOrLabel( payload.getRepository(), repositoryConfig, payload.getPullRequest() ); |
| 36 | + } |
| 37 | + |
| 38 | + private void addTagOrLabel( |
| 39 | + GHRepository repository, |
| 40 | + RepositoryConfig repositoryConfig, |
| 41 | + GHPullRequest pullRequest |
| 42 | + ) throws IOException { |
| 43 | + if ( repositoryConfig == null || repositoryConfig.branches == null |
| 44 | + || !repositoryConfig.branches.getEnabled().orElse( Boolean.FALSE ) ) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if ( !shouldCheck( repository, pullRequest, repositoryConfig.branches.getIgnore() ) ) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + String base = pullRequest.getBase().getRef(); |
| 53 | + if ( "main".equalsIgnoreCase( base ) ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + if ( repositoryConfig.branches.getTitlePrefix().isPresent() ) { |
| 58 | + String prefix = String.format( Locale.ROOT, repositoryConfig.branches.getTitlePrefix().get(), base ); |
| 59 | + String title = pullRequest.getTitle(); |
| 60 | + if ( !title.startsWith( prefix ) ) { |
| 61 | + if ( !deploymentConfig.isDryRun() ) { |
| 62 | + pullRequest.setTitle( prefix + " " + title ); |
| 63 | + } |
| 64 | + else { |
| 65 | + LOG.info( "Pull request #" + pullRequest.getNumber() + " - Setting title to: " + title ); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if ( repositoryConfig.branches.getLabel().isPresent() ) { |
| 71 | + String labelName = String.format( Locale.ROOT, repositoryConfig.branches.getLabel().get(), base ); |
| 72 | + for ( GHLabel ghLabel : pullRequest.getLabels() ) { |
| 73 | + if ( ghLabel.getName().equals( labelName ) ) { |
| 74 | + return; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + GHLabel label; |
| 79 | + try { |
| 80 | + label = repository.getLabel( base ); |
| 81 | + } |
| 82 | + catch (GHFileNotFoundException e) { |
| 83 | + label = repository.createLabel( |
| 84 | + base, |
| 85 | + String.format( Locale.ROOT, "%06x", new Random().nextInt( 0xffffff + 1 ) ), |
| 86 | + "Label for pull requests targeting [" + base + "] branch." |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + if ( !deploymentConfig.isDryRun() ) { |
| 91 | + pullRequest.addLabels( label ); |
| 92 | + } |
| 93 | + else { |
| 94 | + LOG.info( "Pull request #" + pullRequest.getNumber() + " - Adding label: " + label.getName() ); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private boolean shouldCheck(GHRepository repository, GHPullRequest pullRequest, List<RepositoryConfig.IgnoreConfiguration> ignoredPRConfigurations) throws IOException { |
| 100 | + GHUser author = pullRequest.getUser(); |
| 101 | + String title = pullRequest.getTitle(); |
| 102 | + for ( RepositoryConfig.IgnoreConfiguration ignore : ignoredPRConfigurations ) { |
| 103 | + if ( ignore.getUser().equals( author.getLogin() ) |
| 104 | + && ignore.getTitlePattern().matcher( title ).matches() ) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + return !GHIssueState.CLOSED.equals( pullRequest.getState() ) |
| 109 | + && repository.getId() == pullRequest.getBase().getRepository().getId(); |
| 110 | + } |
| 111 | +} |
0 commit comments