|
28 | 28 | import java.util.function.Predicate;
|
29 | 29 | import java.util.logging.Level;
|
30 | 30 | import java.util.logging.Logger;
|
| 31 | +import java.util.regex.Pattern; |
31 | 32 | import org.apache.commons.lang.StringUtils;
|
32 | 33 |
|
33 | 34 | /**
|
@@ -133,7 +134,7 @@ private boolean isExecutable(Job<?, ?> job, MergeRequestHook hook) {
|
133 | 134 | return true;
|
134 | 135 | } else {
|
135 | 136 | if (isLastCommitNotYetBuild(job, hook)) {
|
136 |
| - return isNewCommitPushed(hook) || isBecameNoWip(hook); |
| 137 | + return isNewCommitPushed(hook) || isChangedToNotDraft(hook); |
137 | 138 | }
|
138 | 139 | }
|
139 | 140 | }
|
@@ -305,15 +306,41 @@ private boolean isAllowedByConfig(MergeRequestObjectAttributes objectAttributes)
|
305 | 306 | return triggerConfig.test(objectAttributes);
|
306 | 307 | }
|
307 | 308 |
|
308 |
| - private boolean isBecameNoWip(MergeRequestHook hook) { |
| 309 | + /** |
| 310 | + * Checks if the MR Title had the 'Draft' keyword removed |
| 311 | + * @param hook The hook |
| 312 | + * @return True if the 'Draft' keyword was removed from the MR title |
| 313 | + */ |
| 314 | + private boolean isChangedToNotDraft(MergeRequestHook hook) { |
309 | 315 | MergeRequestChangedTitle changedTitle = Optional.of(hook)
|
310 | 316 | .map(MergeRequestHook::getChanges)
|
311 | 317 | .map(MergeRequestChanges::getTitle)
|
312 | 318 | .orElse(new MergeRequestChangedTitle());
|
313 | 319 | String current = changedTitle.getCurrent() != null ? changedTitle.getCurrent() : "";
|
314 | 320 | String previous = changedTitle.getPrevious() != null ? changedTitle.getPrevious() : "";
|
| 321 | + boolean wasDraft = hasDraftIndicator(previous) && !hasDraftIndicator(current); |
315 | 322 |
|
316 |
| - return previous.contains("WIP") && !current.contains("WIP"); |
| 323 | + // The support of "WIP" is to be removed in GitLab 14.0 |
| 324 | + // See here: |
| 325 | + // https://docs.gitlab.com/13.12/ee/user/project/merge_requests/drafts.html#mark-merge-requests-as-drafts |
| 326 | + boolean wasWip = previous.contains("WIP") && !current.contains("WIP"); |
| 327 | + |
| 328 | + return wasDraft || wasWip; |
| 329 | + } |
| 330 | + |
| 331 | + /** |
| 332 | + * Checks if given text has the appropriate 'Draft' syntax |
| 333 | + * |
| 334 | + * This is intended to be used on a MR Title. |
| 335 | + * |
| 336 | + * The 'Draft' syntax was based off the following documentation: |
| 337 | + * https://docs.gitlab.com/13.12/ee/user/project/merge_requests/drafts.html#mark-merge-requests-as-drafts |
| 338 | + * @param title The title to check |
| 339 | + * @return true if the title starts with the appropriate 'Draft' syntax, else false. |
| 340 | + */ |
| 341 | + private static boolean hasDraftIndicator(String title) { |
| 342 | + Pattern draftPattern = Pattern.compile("\\s*(Draft:|\\[Draft\\]|\\(Draft\\)).*"); |
| 343 | + return draftPattern.matcher(title).matches(); |
317 | 344 | }
|
318 | 345 |
|
319 | 346 | private boolean isForcedByAddedLabel(MergeRequestHook hook) {
|
|
0 commit comments