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