Skip to content

Commit

Permalink
mr_edit: Support (un)marking merge requests as draft
Browse files Browse the repository at this point in the history
Gitlab supports marking a merge request as draft to indicate that it is
not ready to be merged. This is supported in the web UI (actions to mark
as draft/ready, disabled merge button in draft MRs), but not explicitly
exposed in the API: Drafts are simply indicated by prefixing the MR title
with "Draft:", "[Draft]" or "(Draft)" (or previously "WIP)[0].

lab therefore kind of supports the feature already, but it still seems
reasonable to export the feature explicitly rather than via manipulating
the title (although that's of course what the implementation does under
the hood).

[0] https://docs.gitlab.com/ee/user/project/merge_requests/work_in_progress_merge_requests.html
  • Loading branch information
fmuellner authored and prarit committed Dec 14, 2020
1 parent a4f346f commit b4f4d18
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,
log.Fatal(err)
}

draft, err := cmd.Flags().GetBool("draft")
if err != nil {
log.Fatal(err)
}

ready, err := cmd.Flags().GetBool("ready")
if err != nil {
log.Fatal(err)
}

if draft && ready {
log.Fatal("--draft and --ready cannot be used together")
}

currentAssignees := mrGetCurrentAssignees(mr)
assigneeIDs, assigneesChanged, err := getUpdateAssignees(currentAssignees, assignees, unassignees)
if err != nil {
Expand All @@ -118,6 +132,29 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,
log.Fatal("aborting: empty mr title")
}

isWIP := strings.EqualFold(title[0:4], "wip:")
isDraft := strings.EqualFold(title[0:6], "draft:") ||
strings.EqualFold(title[0:7], "[draft]") ||
strings.EqualFold(title[0:7], "(draft)")

if ready {
if isWIP {
title = strings.TrimPrefix(title, title[0:4])
} else if isDraft {
if title[0] == '(' || title[0] == '[' {
title = strings.TrimPrefix(title, title[0:7])
} else {
title = strings.TrimPrefix(title, title[0:6])
}
}
}

if draft {
if !isWIP && !isDraft {
title = "Draft: " + title
}
}

abortUpdate := title == mr.Title && body == mr.Description && !labelsChanged && !assigneesChanged
if abortUpdate {
log.Fatal("aborting: no changes")
Expand Down Expand Up @@ -167,6 +204,8 @@ func init() {
mrEditCmd.Flags().StringSliceP("assign", "a", []string{}, "add an assignee by username")
mrEditCmd.Flags().StringSliceP("unassign", "", []string{}, "remove an assignee by username")
mrEditCmd.Flags().Bool("force-linebreak", false, "append 2 spaces to the end of each line to force markdown linebreaks")
mrEditCmd.Flags().Bool("draft", false, "mark the merge request as draft")
mrEditCmd.Flags().Bool("ready", false, "mark the merge request as ready")

mrCmd.AddCommand(mrEditCmd)
carapace.Gen(mrEditCmd).PositionalCompletion(
Expand Down

0 comments on commit b4f4d18

Please sign in to comment.