Skip to content

Commit

Permalink
lab: Add assign reviewer functionality
Browse files Browse the repository at this point in the history
The 'mr create' and 'mr edit' commands have the ability to assign and
unassign GitLab users to Merge Requests.  Add similar functionality to
allow adding GitLab users as Reviewers of Merge Requests.

Signed-off-by: Prarit Bhargava <prarit@redhat.com>
  • Loading branch information
prarit committed Mar 24, 2021
1 parent ac1fdb8 commit 7b53f9a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var mrCreateCmd = &cobra.Command{
func init() {
mrCreateCmd.Flags().StringArrayP("message", "m", []string{}, "use the given <msg>; multiple -m are concatenated as separate paragraphs")
mrCreateCmd.Flags().StringSliceP("assignee", "a", []string{}, "set assignee by username; can be specified multiple times for multiple assignees")
mrCreateCmd.Flags().StringSliceP("reviewer", "r", []string{}, "set reviewer by username; can be specified multiple times for multiple reviewers")
mrCreateCmd.Flags().StringSliceP("label", "l", []string{}, "add label <label>; can be specified multiple times for multiple labels")
mrCreateCmd.Flags().BoolP("remove-source-branch", "d", false, "remove source branch from remote after merge")
mrCreateCmd.Flags().BoolP("squash", "s", false, "squash commits when merging")
Expand Down Expand Up @@ -114,6 +115,11 @@ func runMRCreate(cmd *cobra.Command, args []string) {
if err != nil {
log.Fatal(err)
}
reviewers, err := cmd.Flags().GetStringSlice("reviewer")
if err != nil {
log.Fatal(err)
}

filename, err := cmd.Flags().GetString("file")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -282,6 +288,7 @@ func runMRCreate(cmd *cobra.Command, args []string) {
Title: &title,
Description: &body,
AssigneeIDs: getUserIDs(assignees),
ReviewerIDs: getUserIDs(reviewers),
RemoveSourceBranch: &removeSourceBranch,
Squash: &squash,
AllowCollaboration: &allowCollaboration,
Expand Down
36 changes: 36 additions & 0 deletions cmd/mr_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,
log.Fatal(err)
}

// get the reviewers to add
reviewers, err := cmd.Flags().GetStringSlice("review")
if err != nil {
log.Fatal(err)
}

// get the reviewers to remove
unreviewers, err := cmd.Flags().GetStringSlice("unreview")
if err != nil {
log.Fatal(err)
}

filename, err := cmd.Flags().GetString("file")
if err != nil {
log.Fatal(err)
Expand All @@ -124,6 +136,12 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,
log.Fatal(err)
}

currentReviewers := mrGetCurrentReviewers(mr)
reviewerIDs, reviewersChanged, err := getUpdateUsers(currentReviewers, reviewers, unreviewers)
if err != nil {
log.Fatal(err)
}

milestoneName, err := cmd.Flags().GetString("milestone")
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -233,6 +251,10 @@ lab MR edit <id>:<comment_id> # update a comment on MR`,
opts.AssigneeIDs = assigneeIDs
}

if reviewersChanged {
opts.ReviewerIDs = reviewerIDs
}

if updateMilestone {
opts.MilestoneID = &milestoneID
}
Expand Down Expand Up @@ -261,6 +283,18 @@ func mrGetCurrentAssignees(mr *gitlab.MergeRequest) []string {
return currentAssignees
}

// mrGetCurrentReviewers returns a string slice of the current reviewers'
// usernames
func mrGetCurrentReviewers(mr *gitlab.MergeRequest) []string {
currentReviewers := make([]string, len(mr.Reviewers))
if len(mr.Reviewers) > 0 && mr.Reviewers[0].Username != "" {
for i, a := range mr.Reviewers {
currentReviewers[i] = a.Username
}
}
return currentReviewers
}

// getBranchName considers the possible ambiguity of different branch names
func getBranchName(project, branch string) (string, error) {
opts := &gitlab.ListBranchesOptions{
Expand Down Expand Up @@ -313,6 +347,8 @@ func init() {
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")
mrEditCmd.Flags().StringSliceP("review", "r", []string{}, "add an reviewer by username")
mrEditCmd.Flags().StringSliceP("unreview", "", []string{}, "remove an reviewer by username")
mrEditCmd.Flags().SortFlags = false

mrCmd.AddCommand(mrEditCmd)
Expand Down

0 comments on commit 7b53f9a

Please sign in to comment.