|
| 1 | +package org.hibernate.infra.bot; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.HashSet; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Locale; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +import org.hibernate.infra.bot.config.DeploymentConfig; |
| 10 | +import org.hibernate.infra.bot.config.RepositoryConfig; |
| 11 | +import org.hibernate.infra.bot.util.CommitMessages; |
| 12 | + |
| 13 | +import org.jboss.logging.Logger; |
| 14 | + |
| 15 | +import io.quarkiverse.githubapp.ConfigFile; |
| 16 | +import io.quarkiverse.githubapp.event.CheckRun; |
| 17 | +import io.quarkiverse.githubapp.event.CheckSuite; |
| 18 | +import io.quarkiverse.githubapp.event.PullRequest; |
| 19 | +import jakarta.inject.Inject; |
| 20 | +import org.kohsuke.github.GHEventPayload; |
| 21 | +import org.kohsuke.github.GHIssueState; |
| 22 | +import org.kohsuke.github.GHPullRequest; |
| 23 | +import org.kohsuke.github.GHPullRequestCommitDetail; |
| 24 | +import org.kohsuke.github.GHRepository; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Marco Belladelli |
| 28 | + */ |
| 29 | +public class EditPullRequestBodyAddIssueLinks { |
| 30 | + private static final Logger LOG = Logger.getLogger( EditPullRequestBodyAddIssueLinks.class ); |
| 31 | + |
| 32 | + private static final String START_MARKER = "<!-- Hibernate GitHub Bot issue links start -->"; |
| 33 | + |
| 34 | + private static final String END_MARKER = "<!-- Hibernate GitHub Bot issue links end -->"; |
| 35 | + |
| 36 | + private static final String EDITOR_WARNING = "<!-- THIS SECTION IS AUTOMATICALLY GENERATED, ANY MANUAL CHANGES WILL BE LOST -->\n"; |
| 37 | + |
| 38 | + private static final String LINK_TEMPLATE = "https://hibernate.atlassian.net/browse/%s"; |
| 39 | + |
| 40 | + @Inject |
| 41 | + DeploymentConfig deploymentConfig; |
| 42 | + |
| 43 | + void pullRequestChanged( |
| 44 | + @PullRequest.Opened @PullRequest.Reopened @PullRequest.Edited @PullRequest.Synchronize |
| 45 | + GHEventPayload.PullRequest payload, |
| 46 | + @ConfigFile( "hibernate-github-bot.yml" ) RepositoryConfig repositoryConfig) throws IOException { |
| 47 | + editPullRequestBodyAddIssueLinks( payload.getRepository(), repositoryConfig, payload.getPullRequest() ); |
| 48 | + } |
| 49 | + |
| 50 | + private void editPullRequestBodyAddIssueLinks( |
| 51 | + GHRepository repository, |
| 52 | + RepositoryConfig repositoryConfig, |
| 53 | + GHPullRequest pullRequest) throws IOException { |
| 54 | + if ( repositoryConfig == null || repositoryConfig.jira == null |
| 55 | + || repositoryConfig.jira.getIssueKeyPattern().isEmpty() |
| 56 | + || repositoryConfig.jira.getInsertLinksInPullRequests().isEmpty() |
| 57 | + || repositoryConfig.jira.getInsertLinksInPullRequests().get().equals( Boolean.FALSE ) ) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if ( !shouldCheck( repository, pullRequest ) ) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + final Set<String> issueKeys = new HashSet<>(); |
| 66 | + // Collect all issue keys from commit messages |
| 67 | + repositoryConfig.jira.getIssueKeyPattern().ifPresent( issueKeyPattern -> { |
| 68 | + for ( GHPullRequestCommitDetail commitDetails : pullRequest.listCommits() ) { |
| 69 | + final GHPullRequestCommitDetail.Commit commit = commitDetails.getCommit(); |
| 70 | + final List<String> commitIssueKeys = CommitMessages.extractIssueKeys( |
| 71 | + issueKeyPattern, |
| 72 | + commit.getMessage() |
| 73 | + ); |
| 74 | + issueKeys.addAll( commitIssueKeys ); |
| 75 | + } |
| 76 | + } ); |
| 77 | + |
| 78 | + if ( issueKeys.isEmpty() ) { |
| 79 | + LOG.debug( "Found no issue keys in commits, terminating." ); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + final String originalBody = pullRequest.getBody(); |
| 84 | + final StringBuilder sb = new StringBuilder(); |
| 85 | + if ( originalBody != null ) { |
| 86 | + // Check if the body already contains the link section |
| 87 | + final int startIndex = originalBody.indexOf( START_MARKER ); |
| 88 | + final int endIndex = startIndex > -1 ? originalBody.indexOf( END_MARKER ) : -1; |
| 89 | + if ( startIndex > -1 && endIndex > -1 ) { |
| 90 | + // Remove the whole section, it will be re-appended at the end of the body |
| 91 | + sb.append( originalBody.substring( 0, startIndex ).trim() ); |
| 92 | + final String following = originalBody.substring( endIndex + END_MARKER.length() ).trim(); |
| 93 | + if ( following.length() > 0 ) { |
| 94 | + sb.append( "\n\n" ); |
| 95 | + sb.append( following ); |
| 96 | + } |
| 97 | + } |
| 98 | + else { |
| 99 | + sb.append( originalBody.trim() ); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + final String body = sb.toString(); |
| 104 | + final String linksSection = constructLinksSection( issueKeys, body ); |
| 105 | + if ( linksSection == null ) { |
| 106 | + // All issue links were already found in the request body, nothing to do |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + final String newBody = body.length() == 0 |
| 111 | + ? linksSection |
| 112 | + : body + "\n\n" + linksSection; |
| 113 | + if ( !deploymentConfig.isDryRun() ) { |
| 114 | + pullRequest.setBody( newBody ); |
| 115 | + } |
| 116 | + else { |
| 117 | + LOG.info( "Pull request #" + pullRequest.getNumber() + " - Updated PR body: " + newBody ); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private String constructLinksSection(Set<String> issueKeys, String originalBody) { |
| 122 | + final String lowerCaseBody = originalBody.toLowerCase( Locale.ROOT ); |
| 123 | + final StringBuilder sb = new StringBuilder(); |
| 124 | + for ( String key : issueKeys ) { |
| 125 | + if ( !lowerCaseBody.contains( key.toLowerCase( Locale.ROOT ) ) ) { |
| 126 | + // Only add links for issue keys that are not already found |
| 127 | + // in the original PR body |
| 128 | + sb.append( String.format( LINK_TEMPLATE, key ) ).append( '\n' ); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if ( sb.isEmpty() ) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + return START_MARKER + "\n" + EDITOR_WARNING + sb + END_MARKER; |
| 137 | + } |
| 138 | + |
| 139 | + private boolean shouldCheck(GHRepository repository, GHPullRequest pullRequest) { |
| 140 | + return !GHIssueState.CLOSED.equals( pullRequest.getState() ) |
| 141 | + && repository.getId() == pullRequest.getBase().getRepository().getId(); |
| 142 | + } |
| 143 | +} |
0 commit comments