Skip to content

Commit a3314c7

Browse files
authored
Add support for Draft on the MR hook (#1459)
* Add support for Draft on the MR hook * Apply spotless formatting
1 parent a3ea4d0 commit a3314c7

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/merge/MergeRequestHookTriggerHandlerImpl.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.function.Predicate;
2929
import java.util.logging.Level;
3030
import java.util.logging.Logger;
31+
import java.util.regex.Pattern;
3132
import org.apache.commons.lang.StringUtils;
3233

3334
/**
@@ -133,7 +134,7 @@ private boolean isExecutable(Job<?, ?> job, MergeRequestHook hook) {
133134
return true;
134135
} else {
135136
if (isLastCommitNotYetBuild(job, hook)) {
136-
return isNewCommitPushed(hook) || isBecameNoWip(hook);
137+
return isNewCommitPushed(hook) || isChangedToNotDraft(hook);
137138
}
138139
}
139140
}
@@ -305,15 +306,41 @@ private boolean isAllowedByConfig(MergeRequestObjectAttributes objectAttributes)
305306
return triggerConfig.test(objectAttributes);
306307
}
307308

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) {
309315
MergeRequestChangedTitle changedTitle = Optional.of(hook)
310316
.map(MergeRequestHook::getChanges)
311317
.map(MergeRequestChanges::getTitle)
312318
.orElse(new MergeRequestChangedTitle());
313319
String current = changedTitle.getCurrent() != null ? changedTitle.getCurrent() : "";
314320
String previous = changedTitle.getPrevious() != null ? changedTitle.getPrevious() : "";
321+
boolean wasDraft = hasDraftIndicator(previous) && !hasDraftIndicator(current);
315322

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();
317344
}
318345

319346
private boolean isForcedByAddedLabel(MergeRequestHook hook) {

0 commit comments

Comments
 (0)