Skip to content

Commit 4128ad2

Browse files
mbelladeyrodiere
authored andcommitted
Small improvements to automatic PR issue link editing
General refactoring and cleanup of the auto-edit action
1 parent 72d6c2f commit 4128ad2

File tree

2 files changed

+44
-52
lines changed

2 files changed

+44
-52
lines changed

src/main/java/org/hibernate/infra/bot/EditPullRequestBodyAddIssueLinks.java

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.hibernate.infra.bot;
22

33
import java.io.IOException;
4+
import java.util.ArrayList;
45
import java.util.HashSet;
56
import java.util.List;
67
import java.util.Locale;
8+
import java.util.Objects;
79
import java.util.Set;
810

911
import org.hibernate.infra.bot.config.DeploymentConfig;
@@ -13,8 +15,6 @@
1315
import org.jboss.logging.Logger;
1416

1517
import io.quarkiverse.githubapp.ConfigFile;
16-
import io.quarkiverse.githubapp.event.CheckRun;
17-
import io.quarkiverse.githubapp.event.CheckSuite;
1818
import io.quarkiverse.githubapp.event.PullRequest;
1919
import jakarta.inject.Inject;
2020
import org.kohsuke.github.GHEventPayload;
@@ -80,36 +80,20 @@ private void editPullRequestBodyAddIssueLinks(
8080
return;
8181
}
8282

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-
}
83+
final String originalBody = Objects.toString( pullRequest.getBody(), "" );
84+
85+
// Check if the body already contains the link section
86+
final int startIndex = originalBody.indexOf( START_MARKER );
87+
final int endIndex = startIndex > -1 ? originalBody.indexOf( END_MARKER ) : -1;
88+
final String body = removeLinksSection( originalBody, startIndex, endIndex );
10289

103-
final String body = sb.toString();
10490
final String linksSection = constructLinksSection( issueKeys, body );
10591
if ( linksSection == null ) {
10692
// All issue links were already found in the request body, nothing to do
10793
return;
10894
}
10995

110-
final String newBody = body.length() == 0
111-
? linksSection
112-
: body + "\n\n" + linksSection;
96+
final String newBody = body.isEmpty() ? linksSection : body + "\n\n" + linksSection;
11397
if ( !deploymentConfig.isDryRun() ) {
11498
pullRequest.setBody( newBody );
11599
}
@@ -118,24 +102,46 @@ private void editPullRequestBodyAddIssueLinks(
118102
}
119103
}
120104

121-
private String constructLinksSection(Set<String> issueKeys, String originalBody) {
122-
final String lowerCaseBody = originalBody.toLowerCase( Locale.ROOT );
123-
final StringBuilder sb = new StringBuilder();
105+
private String constructLinksSection(Set<String> issueKeys, String body) {
106+
final String lowerCaseBody = body.toLowerCase( Locale.ROOT );
107+
final List<String> keys = new ArrayList<>( issueKeys.size() );
124108
for ( String key : issueKeys ) {
109+
// Add links for issue keys that are not already found in the original PR body
125110
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' );
111+
keys.add( key );
129112
}
130113
}
131114

132-
if ( sb.isEmpty() ) {
115+
if ( keys.isEmpty() ) {
133116
return null;
134117
}
135118

119+
// Try to pre-size the StringBuilder with the correct capacity
120+
final int linkSize = LINK_TEMPLATE.length() + keys.get( 0 ).length() + 1;
121+
final StringBuilder sb = new StringBuilder( linkSize * keys.size() );
122+
for ( final String key : keys ) {
123+
sb.append( String.format( LINK_TEMPLATE, key ) ).append( '\n' );
124+
}
136125
return START_MARKER + "\n" + EDITOR_WARNING + sb + END_MARKER;
137126
}
138127

128+
private static String removeLinksSection(String originalBody, int startIndex, int endIndex) {
129+
if ( startIndex > -1 && endIndex > -1 ) {
130+
final StringBuilder sb = new StringBuilder();
131+
// Remove the whole section, it will be re-appended at the end of the body
132+
sb.append( originalBody.substring( 0, startIndex ).trim() );
133+
final String following = originalBody.substring( endIndex + END_MARKER.length() ).trim();
134+
if ( !following.isEmpty() ) {
135+
sb.append( "\n\n" );
136+
sb.append( following );
137+
}
138+
return sb.toString();
139+
}
140+
else {
141+
return originalBody.trim();
142+
}
143+
}
144+
139145
private boolean shouldCheck(GHRepository repository, GHPullRequest pullRequest) {
140146
return !GHIssueState.CLOSED.equals( pullRequest.getState() )
141147
&& repository.getId() == pullRequest.getBase().getRepository().getId();

src/test/java/org/hibernate/infra/bot/tests/EditPullRequestBodyAddIssueLinksTest.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
package org.hibernate.infra.bot.tests;
22

3-
import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given;
4-
import static org.assertj.core.api.Assertions.assertThat;
5-
import static org.hibernate.infra.bot.tests.PullRequestMockHelper.mockPagedIterable;
6-
import static org.mockito.ArgumentMatchers.any;
7-
import static org.mockito.Mockito.mock;
8-
import static org.mockito.Mockito.times;
9-
import static org.mockito.Mockito.verify;
10-
import static org.mockito.Mockito.verifyNoMoreInteractions;
11-
import static org.mockito.Mockito.when;
12-
import static org.mockito.Mockito.withSettings;
13-
143
import java.io.IOException;
15-
import java.util.Collections;
164

175
import org.junit.jupiter.api.Test;
186
import org.junit.jupiter.api.extension.ExtendWith;
197

208
import io.quarkiverse.githubapp.testing.GitHubAppTest;
219
import io.quarkus.test.junit.QuarkusTest;
22-
import org.assertj.core.api.InstanceOfAssertFactories;
23-
import org.kohsuke.github.GHCheckRun;
24-
import org.kohsuke.github.GHCheckRunBuilder;
25-
import org.kohsuke.github.GHCommitPointer;
2610
import org.kohsuke.github.GHEvent;
27-
import org.kohsuke.github.GHIssueComment;
2811
import org.kohsuke.github.GHPullRequest;
2912
import org.kohsuke.github.GHRepository;
30-
import org.kohsuke.github.GHUser;
31-
import org.kohsuke.github.PagedIterable;
32-
import org.mockito.Answers;
3313
import org.mockito.ArgumentCaptor;
3414
import org.mockito.junit.jupiter.MockitoExtension;
3515

16+
import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given;
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.mockito.Mockito.times;
19+
import static org.mockito.Mockito.verify;
20+
import static org.mockito.Mockito.when;
21+
3622
@QuarkusTest
3723
@GitHubAppTest
3824
@ExtendWith(MockitoExtension.class)

0 commit comments

Comments
 (0)