1
1
package org .hibernate .infra .bot ;
2
2
3
3
import java .io .IOException ;
4
+ import java .util .ArrayList ;
4
5
import java .util .HashSet ;
5
6
import java .util .List ;
6
7
import java .util .Locale ;
8
+ import java .util .Objects ;
7
9
import java .util .Set ;
8
10
9
11
import org .hibernate .infra .bot .config .DeploymentConfig ;
13
15
import org .jboss .logging .Logger ;
14
16
15
17
import io .quarkiverse .githubapp .ConfigFile ;
16
- import io .quarkiverse .githubapp .event .CheckRun ;
17
- import io .quarkiverse .githubapp .event .CheckSuite ;
18
18
import io .quarkiverse .githubapp .event .PullRequest ;
19
19
import jakarta .inject .Inject ;
20
20
import org .kohsuke .github .GHEventPayload ;
@@ -80,36 +80,20 @@ private void editPullRequestBodyAddIssueLinks(
80
80
return ;
81
81
}
82
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
- }
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 );
102
89
103
- final String body = sb .toString ();
104
90
final String linksSection = constructLinksSection ( issueKeys , body );
105
91
if ( linksSection == null ) {
106
92
// All issue links were already found in the request body, nothing to do
107
93
return ;
108
94
}
109
95
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 ;
113
97
if ( !deploymentConfig .isDryRun () ) {
114
98
pullRequest .setBody ( newBody );
115
99
}
@@ -118,24 +102,46 @@ private void editPullRequestBodyAddIssueLinks(
118
102
}
119
103
}
120
104
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 () );
124
108
for ( String key : issueKeys ) {
109
+ // Add links for issue keys that are not already found in the original PR body
125
110
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 );
129
112
}
130
113
}
131
114
132
- if ( sb .isEmpty () ) {
115
+ if ( keys .isEmpty () ) {
133
116
return null ;
134
117
}
135
118
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
+ }
136
125
return START_MARKER + "\n " + EDITOR_WARNING + sb + END_MARKER ;
137
126
}
138
127
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
+
139
145
private boolean shouldCheck (GHRepository repository , GHPullRequest pullRequest ) {
140
146
return !GHIssueState .CLOSED .equals ( pullRequest .getState () )
141
147
&& repository .getId () == pullRequest .getBase ().getRepository ().getId ();
0 commit comments