Skip to content

Commit 171cef6

Browse files
Fix 'Format string argument allowing user controlled parameters' error
1 parent 2b9a912 commit 171cef6

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

src/main/java/stashpullrequestbuilder/stashpullrequestbuilder/StashRepository.java

+23-23
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,17 @@
4949
public class StashRepository {
5050
private static final Logger logger =
5151
Logger.getLogger(MethodHandles.lookup().lookupClass().getName());
52-
private static final String BUILD_START_MARKER = "[*BuildStarted* **%s**] %s into %s";
53-
private static final String BUILD_FINISH_MARKER = "[*BuildFinished* **%s**] %s into %s";
54-
private static final String BUILD_CANCEL_MARKER = "[*BuildCanceled* **%s**] %s into %s";
55-
56-
private static final String BUILD_START_REGEX =
57-
"\\[\\*BuildStarted\\* \\*\\*%s\\*\\*\\] ([0-9a-fA-F]+) into ([0-9a-fA-F]+)";
58-
private static final String BUILD_FINISH_REGEX =
59-
"\\[\\*BuildFinished\\* \\*\\*%s\\*\\*\\] ([0-9a-fA-F]+) into ([0-9a-fA-F]+)";
60-
private static final String BUILD_CANCEL_REGEX =
61-
"\\[\\*BuildCanceled\\* \\*\\*%s\\*\\*\\] ([0-9a-fA-F]+) into ([0-9a-fA-F]+)";
62-
private static final String[] BUILD_REGEXES = {BUILD_START_REGEX, BUILD_FINISH_REGEX, BUILD_CANCEL_REGEX};
52+
private static final String BUILD_START_MESSAGE = "BuildStarted";
53+
private static final String BUILD_FINISH_MESSAGE = "BuildFinished";
54+
private static final String BUILD_CANCEL_MESSAGE = "BuildCanceled";
55+
private static final String[] BUILD_STATUSES = {BUILD_START_MESSAGE, BUILD_FINISH_MESSAGE, BUILD_CANCEL_MESSAGE};
56+
private static final String BUILD_MARKER = "[*%s* **%s**] %s into %s";
57+
58+
private static final String BUILD_STATUS_REGEX =
59+
"\\[\\*%s\\* \\*\\*%s\\*\\*\\] ([0-9a-fA-F]+) into ([0-9a-fA-F]+)";
6360

6461
private static final String BUILD_FINISH_SENTENCE =
65-
BUILD_FINISH_MARKER + " %n%n **[%s](%s)** - Build *#%d* which took *%s*";
62+
" %n%n **[%s](%s)** - Build *#%d* which took *%s*";
6663

6764
private static final String BUILD_SUCCESS_COMMENT = "✓ BUILD SUCCESS";
6865
private static final String BUILD_FAILURE_COMMENT = "✕ BUILD FAILURE";
@@ -148,8 +145,8 @@ private boolean shouldSkip(StashPullRequestResponseValue pullRequest) {
148145

149146
private boolean isStatusMessage(String content) {
150147
String escapedBuildName = Pattern.quote(job.getDisplayName());
151-
for (String pattern : BUILD_REGEXES) {
152-
String buildStatusMessage = String.format(pattern, escapedBuildName);
148+
for (String buildStatus : BUILD_STATUSES) {
149+
String buildStatusMessage = String.format(BUILD_STATUS_REGEX, buildStatus, escapedBuildName);
153150
Matcher matcher =
154151
Pattern.compile(buildStatusMessage, Pattern.CASE_INSENSITIVE).matcher(content);
155152
if (matcher.find()) {
@@ -262,8 +259,8 @@ private List<StashPullRequestBuildTarget> getBuildTargetsWithoutOnlyBuildOnComme
262259

263260
// These will match any start or finish message -- need to check commits
264261
String escapedBuildName = Pattern.quote(job.getDisplayName());
265-
String project_build_start = String.format(BUILD_START_REGEX, escapedBuildName);
266-
String project_build_finished = String.format(BUILD_FINISH_REGEX, escapedBuildName);
262+
String project_build_start = String.format(BUILD_STATUS_REGEX, BUILD_START_MESSAGE, escapedBuildName);
263+
String project_build_finished = String.format(BUILD_STATUS_REGEX, BUILD_FINISH_MESSAGE, escapedBuildName);
267264
Matcher startMatcher =
268265
Pattern.compile(project_build_start, Pattern.CASE_INSENSITIVE).matcher(content);
269266
Matcher finishMatcher =
@@ -313,7 +310,7 @@ private List<StashPullRequestBuildTarget> getBuildTargetsWithoutOnlyBuildOnComme
313310
private String postBuildStartComment(
314311
StashPullRequestResponseValue pullRequest, Integer buildCommandCommentId)
315312
throws StashApiException {
316-
return postBuildStatusComment(pullRequest, buildCommandCommentId, BUILD_START_MARKER);
313+
return postBuildStatusComment(pullRequest, buildCommandCommentId, BUILD_START_MESSAGE);
317314
}
318315

319316
/**
@@ -326,15 +323,15 @@ private String postBuildStartComment(
326323
private String postBuildCancelComment(
327324
StashPullRequestResponseValue pullRequest, Integer buildCommandCommentId)
328325
throws StashApiException {
329-
return postBuildStatusComment(pullRequest, buildCommandCommentId, BUILD_CANCEL_MARKER);
326+
return postBuildStatusComment(pullRequest, buildCommandCommentId, BUILD_CANCEL_MESSAGE);
330327
}
331328

332329
private String postBuildStatusComment(
333-
StashPullRequestResponseValue pullRequest, Integer buildCommandCommentId, String marker)
330+
StashPullRequestResponseValue pullRequest, Integer buildCommandCommentId, String buildMessage)
334331
throws StashApiException {
335332
String sourceCommit = pullRequest.getFromRef().getLatestCommit();
336333
String destinationCommit = pullRequest.getToRef().getLatestCommit();
337-
String comment = format(marker, job.getDisplayName(), sourceCommit, destinationCommit);
334+
String comment = format(BUILD_MARKER, buildMessage, job.getDisplayName(), sourceCommit, destinationCommit);
338335
StashPullRequestComment commentResponse;
339336
commentResponse =
340337
this.client.postPullRequestComment(pullRequest.getId(), comment, buildCommandCommentId);
@@ -582,10 +579,13 @@ public void postFinishedComment(
582579
String message = getMessageForBuildResult(buildResult);
583580
String comment =
584581
format(
585-
BUILD_FINISH_SENTENCE,
582+
BUILD_MARKER,
583+
BUILD_FINISH_MESSAGE,
586584
job.getDisplayName(),
587585
sourceCommit,
588-
destinationCommit,
586+
destinationCommit)
587+
+ format(
588+
BUILD_FINISH_SENTENCE,
589589
message,
590590
buildUrl,
591591
buildNumber,
@@ -684,7 +684,7 @@ private void deletePreviousBuildFinishedComments(StashPullRequestResponseValue p
684684
continue;
685685
}
686686

687-
String project_build_finished = format(BUILD_FINISH_REGEX, job.getDisplayName());
687+
String project_build_finished = format(BUILD_STATUS_REGEX, BUILD_FINISH_MESSAGE, job.getDisplayName());
688688
Matcher finishMatcher =
689689
Pattern.compile(project_build_finished, Pattern.CASE_INSENSITIVE).matcher(content);
690690

0 commit comments

Comments
 (0)