|
| 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.Objects; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +import org.hibernate.infra.bot.config.DeploymentConfig; |
| 12 | +import org.hibernate.infra.bot.config.RepositoryConfig; |
| 13 | +import org.hibernate.infra.bot.jira.JiraIssue; |
| 14 | +import org.hibernate.infra.bot.jira.JiraIssues; |
| 15 | +import org.hibernate.infra.bot.jira.JiraRestClient; |
| 16 | +import org.hibernate.infra.bot.util.CommitMessages; |
| 17 | +import org.hibernate.infra.bot.util.Patterns; |
| 18 | + |
| 19 | +import org.jboss.logging.Logger; |
| 20 | + |
| 21 | +import io.quarkiverse.githubapp.ConfigFile; |
| 22 | +import io.quarkiverse.githubapp.event.PullRequest; |
| 23 | +import jakarta.inject.Inject; |
| 24 | +import org.eclipse.microprofile.rest.client.inject.RestClient; |
| 25 | +import org.kohsuke.github.GHEventPayload; |
| 26 | +import org.kohsuke.github.GHIssueState; |
| 27 | +import org.kohsuke.github.GHPullRequest; |
| 28 | +import org.kohsuke.github.GHPullRequestCommitDetail; |
| 29 | +import org.kohsuke.github.GHRepository; |
| 30 | + |
| 31 | +public class EditPullRequestBodyAddTaskList { |
| 32 | + private static final Logger LOG = Logger.getLogger( EditPullRequestBodyAddTaskList.class ); |
| 33 | + |
| 34 | + private static final String START_MARKER = "<!-- Hibernate GitHub Bot task list start -->"; |
| 35 | + |
| 36 | + private static final String END_MARKER = "<!-- Hibernate GitHub Bot task list end -->"; |
| 37 | + |
| 38 | + @Inject |
| 39 | + DeploymentConfig deploymentConfig; |
| 40 | + @RestClient |
| 41 | + JiraRestClient jiraRestClient; |
| 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 |
| 47 | + ) throws IOException { |
| 48 | + addUpdateTaskList( payload.getRepository(), repositoryConfig, payload.getPullRequest() ); |
| 49 | + } |
| 50 | + |
| 51 | + private void addUpdateTaskList( |
| 52 | + GHRepository repository, |
| 53 | + RepositoryConfig repositoryConfig, |
| 54 | + GHPullRequest pullRequest |
| 55 | + ) throws IOException { |
| 56 | + if ( repositoryConfig == null || repositoryConfig.pullRequestTasks == null |
| 57 | + || !repositoryConfig.pullRequestTasks.getEnabled().orElse( Boolean.FALSE ) ) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if ( !shouldCheck( repository, pullRequest ) ) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + final Set<String> issueKeys = new HashSet<>(); |
| 66 | + boolean genericTasksRequired = false; |
| 67 | + if ( repositoryConfig.jira.getIssueKeyPattern().isPresent() ) { |
| 68 | + Pattern issueKeyPattern = repositoryConfig.jira.getIssueKeyPattern().get(); |
| 69 | + |
| 70 | + for ( GHPullRequestCommitDetail commitDetails : pullRequest.listCommits() ) { |
| 71 | + final GHPullRequestCommitDetail.Commit commit = commitDetails.getCommit(); |
| 72 | + final List<String> commitIssueKeys = CommitMessages.extractIssueKeys( |
| 73 | + issueKeyPattern, |
| 74 | + commit.getMessage() |
| 75 | + ); |
| 76 | + issueKeys.addAll( commitIssueKeys ); |
| 77 | + genericTasksRequired = genericTasksRequired || commitIssueKeys.isEmpty(); |
| 78 | + } |
| 79 | + } |
| 80 | + else { |
| 81 | + genericTasksRequired = true; |
| 82 | + } |
| 83 | + |
| 84 | + final String originalBody = Objects.toString( pullRequest.getBody(), "" ); |
| 85 | + final String currentTasks = currentTaskBody( originalBody ); |
| 86 | + final String tasks = generateTaskList( repositoryConfig.pullRequestTasks, genericTasksRequired, issueKeys ); |
| 87 | + |
| 88 | + String body; |
| 89 | + |
| 90 | + if ( currentTasks == null && tasks == null ) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if ( tasks == null ) { |
| 95 | + body = originalBody.replace( currentTasks, "" ); |
| 96 | + } |
| 97 | + else if ( currentTasks != null ) { |
| 98 | + if (tasksAreTheSame( currentTasks, tasks ) ) { |
| 99 | + return; |
| 100 | + } |
| 101 | + body = originalBody.replace( currentTasks, tasks ); |
| 102 | + } |
| 103 | + else { |
| 104 | + body = "%s\n\n---\n%s\n%s\n%s".formatted( originalBody, START_MARKER, tasks, END_MARKER ); |
| 105 | + } |
| 106 | + |
| 107 | + if ( !deploymentConfig.isDryRun() ) { |
| 108 | + pullRequest.setBody( body ); |
| 109 | + } |
| 110 | + else { |
| 111 | + LOG.info( "Pull request #" + pullRequest.getNumber() + " - Updated PR body: " + body ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private boolean tasksAreTheSame(String currentTasks, String tasks) { |
| 116 | + return Patterns.compile( tasks.replace( "- [ ]", "- \\[.\\]" ).replace( "\n", "\\n" ) ) |
| 117 | + .matcher( currentTasks ) |
| 118 | + .matches(); |
| 119 | + } |
| 120 | + |
| 121 | + private String generateTaskList(RepositoryConfig.TaskList taskListConfiguration, boolean genericTasksRequired, Set<String> issueKeys) { |
| 122 | + if ( !genericTasksRequired && issueKeys.isEmpty() ) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + StringBuilder taskList = new StringBuilder(); |
| 126 | + taskList.append( "Please make sure that the following tasks are completed:\n" ); |
| 127 | + if ( genericTasksRequired ) { |
| 128 | + addTasks( taskList, taskListConfiguration.defaultTasks() ); |
| 129 | + } |
| 130 | + if ( !issueKeys.isEmpty() ) { |
| 131 | + JiraIssues issues = jiraRestClient.find( "key IN (" + String.join( ",", issueKeys ) + ") ORDER BY KEY DESC", "issuetype,key" ); |
| 132 | + for ( JiraIssue issue : issues.issues ) { |
| 133 | + taskList.append( "Tasks specific to " ) |
| 134 | + .append( issue.key ) |
| 135 | + .append( " (" ) |
| 136 | + .append( issue.fields.issuetype.name ) |
| 137 | + .append( "):\n" ); |
| 138 | + addTasks( taskList, taskListConfiguration.getTasks().getOrDefault( issue.fields.issuetype.name.toLowerCase( Locale.ROOT ), taskListConfiguration.defaultTasks() ) ); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return taskList.toString(); |
| 143 | + } |
| 144 | + |
| 145 | + private void addTasks(StringBuilder sb, List<String> tasks) { |
| 146 | + for ( String task : tasks ) { |
| 147 | + sb.append( "- [ ] " ) |
| 148 | + .append( task ) |
| 149 | + .append( "\n" ); |
| 150 | + } |
| 151 | + sb.append( "\n" ); |
| 152 | + } |
| 153 | + |
| 154 | + private static String currentTaskBody(String originalBody) { |
| 155 | + // Check if the body already contains the tasks |
| 156 | + final int startIndex = originalBody.indexOf( START_MARKER ); |
| 157 | + final int endIndex = startIndex > -1 ? originalBody.indexOf( END_MARKER ) : -1; |
| 158 | + if ( startIndex > -1 && endIndex > -1 ) { |
| 159 | + return originalBody.substring( startIndex + START_MARKER.length() + 1, endIndex - 1 ); |
| 160 | + } |
| 161 | + else { |
| 162 | + return null; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private boolean shouldCheck(GHRepository repository, GHPullRequest pullRequest) { |
| 167 | + return !GHIssueState.CLOSED.equals( pullRequest.getState() ) |
| 168 | + && repository.getId() == pullRequest.getBase().getRepository().getId(); |
| 169 | + } |
| 170 | +} |
0 commit comments