Skip to content

Commit 17896b7

Browse files
committed
Use same "new line" char sequence when comparing the license agreement
1 parent aba04af commit 17896b7

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.hibernate.infra.bot.config.RepositoryConfig;
1919
import org.hibernate.infra.bot.util.CommitMessages;
2020
import org.hibernate.infra.bot.util.GlobMatcher;
21+
import org.hibernate.infra.bot.util.Patterns;
2122

2223
import org.jboss.logging.Logger;
2324

@@ -283,12 +284,12 @@ static class LicenseCheck extends PullRequestCheck {
283284

284285
protected LicenseCheck(String agreementText) {
285286
super( "Contribution — License agreement" );
286-
this.agreementText = agreementText;
287+
this.agreementText = Patterns.sanitizeNewLines( agreementText );
287288
}
288289

289290
@Override
290291
public void perform(PullRequestCheckRunContext context, PullRequestCheckRunOutput output) {
291-
String body = context.pullRequest.getBody();
292+
String body = Patterns.sanitizeNewLines( context.pullRequest.getBody() );
292293
PullRequestCheckRunRule rule = output.rule( "The pull request description must contain the license agreement text." );
293294
if ( body != null && body.contains( agreementText ) ) {
294295
rule.passed();

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

+2-15
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public class EditPullRequestBodyAddTaskList {
3434
private static final String START_MARKER = "<!-- Hibernate GitHub Bot task list start -->";
3535

3636
private static final String END_MARKER = "<!-- Hibernate GitHub Bot task list end -->";
37-
private static final Set<Character> REGEX_ESCAPE_CHARS = Set.of( '(', ')', '[', ']', '{', '}', '\\', '.', '?', '*', '+' );
3837

3938
@Inject
4039
DeploymentConfig deploymentConfig;
@@ -114,21 +113,9 @@ else if ( currentTasks != null ) {
114113
}
115114

116115
private boolean tasksAreTheSame(String currentTasks, String tasks) {
117-
StringBuilder sb = new StringBuilder();
118-
for ( char c : tasks.trim().toCharArray() ) {
119-
if ( REGEX_ESCAPE_CHARS.contains( c ) ) {
120-
sb.append( '\\' );
121-
}
122-
if ( c == '\n' ) {
123-
sb.append( '\\' ).append( 'n' );
124-
}
125-
else {
126-
sb.append( c );
127-
}
128-
}
129116

130-
return Patterns.compile( sb.toString().replace( "- \\[ \\]", "- \\[.\\]" ) )
131-
.matcher( currentTasks.trim() )
117+
return Patterns.compile( Patterns.escapeSpecialCharacters( Patterns.sanitizeNewLines( tasks.trim() ) ).replace( "- \\[ \\]", "- \\[.\\]" ) )
118+
.matcher( Patterns.sanitizeNewLines( currentTasks.trim() ) )
132119
.matches();
133120
}
134121

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
package org.hibernate.infra.bot.util;
22

3+
import java.util.Set;
34
import java.util.regex.Pattern;
45

56
public class Patterns {
7+
private static final Set<Character> REGEX_ESCAPE_CHARS = Set.of( '(', ')', '[', ']', '{', '}', '\\', '.', '?', '*', '+' );
68

79
public static Pattern compile(String pattern) {
810
return Pattern.compile(pattern, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
911
}
1012

13+
/**
14+
* We want to replace any new line character sequences ({@code \n \r \n\r}) with a more predictable one
15+
* which should allow us to use {@link String#contains(CharSequence)} in our text-based checks.
16+
* <p>
17+
* Apparently GitHub body/config files may be returned containing various combinations of these new line character sequences,
18+
* and we may end up failing the checks when we shouldn't.
19+
*/
20+
public static String sanitizeNewLines(String value) {
21+
return value == null ? null : value.replaceAll("\\R", "\n");
22+
}
23+
24+
public static String escapeSpecialCharacters(String value) {
25+
if ( value == null ) {
26+
return null;
27+
}
28+
StringBuilder sb = new StringBuilder();
29+
for ( char c : value.toCharArray() ) {
30+
if ( REGEX_ESCAPE_CHARS.contains( c ) ) {
31+
sb.append( '\\' );
32+
}
33+
if ( c == '\n' ) {
34+
sb.append( '\\' ).append( 'n' );
35+
}
36+
else {
37+
sb.append( c );
38+
}
39+
}
40+
return sb.toString();
41+
}
42+
1143
private Patterns() {
1244
}
1345
}

0 commit comments

Comments
 (0)