Skip to content

Commit

Permalink
mr show: Display output for approvers and reviewers
Browse files Browse the repository at this point in the history
Several users have requested that the list of approvers and reviewers be
added to the output of the 'lab mr show' command.  This list is useful for
authors to know who to request reviews from, and to see the eligible list
of approvers.

Add Approvers, Approval Groups, and Reviewers to the output of 'lab mr
show'.

Signed-off-by: Prarit Bhargava <[email protected]>
  • Loading branch information
prarit committed Mar 11, 2021
1 parent 73fd2bc commit 8c8e502
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
53 changes: 48 additions & 5 deletions cmd/mr_show.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,18 @@ func printMR(mr *gitlab.MergeRequest, project string, renderMarkdown bool) {
milestone := "None"
labels := "None"
approvedByUsers := "None"
approvers := "None"
approverGroups := "None"
reviewers := "None"
subscribed := "No"
state := map[string]string{
"opened": "Open",
"closed": "Closed",
"merged": "Merged",
}[mr.State]

var _tmpStringArray []string

if state == "Open" && mr.MergeStatus == "cannot_be_merged" {
state = "Open (Needs Rebase)"
}
Expand Down Expand Up @@ -158,12 +163,47 @@ func printMR(mr *gitlab.MergeRequest, project string, renderMarkdown bool) {
log.Fatal(err)
}

approvedBys, err := lab.GetMRApprovedBys(project, mr.IID)
approvalConfig, err := lab.GetMRApprovalsConfiguration(project, mr.IID)
if err != nil {
log.Fatal(err)
}
if len(approvedBys) > 0 {
approvedByUsers = strings.Join(approvedBys, ", ")

for _, approvedby := range approvalConfig.ApprovedBy {
_tmpStringArray = append(_tmpStringArray, approvedby.User.Username)
}
if len(_tmpStringArray) > 0 {
approvedByUsers = strings.Join(_tmpStringArray, ", ")
_tmpStringArray = nil
}

// An argument could be made to separate these two fields into their own
// entries, however, at a high level they essentially the users that can
// approve the MR
for _, approvers := range approvalConfig.Approvers {
_tmpStringArray = append(_tmpStringArray, approvers.User.Username)
}
for _, suggestedApprovers := range approvalConfig.SuggestedApprovers {
_tmpStringArray = append(_tmpStringArray, suggestedApprovers.Username)
}
if len(_tmpStringArray) > 0 {
approvers = strings.Join(_tmpStringArray, ", ")
_tmpStringArray = nil
}

for _, approversGroups := range approvalConfig.ApproverGroups {
_tmpStringArray = append(_tmpStringArray, approversGroups.Group.Name)
}
if len(_tmpStringArray) > 0 {
approverGroups = strings.Join(_tmpStringArray, ", ")
_tmpStringArray = nil
}

for _, reviewerUsers := range mr.Reviewers {
_tmpStringArray = append(_tmpStringArray, reviewerUsers.Name)
}
if len(_tmpStringArray) > 0 {
reviewers = strings.Join(_tmpStringArray, ", ")
_tmpStringArray = nil
}

if mr.Subscribed {
Expand All @@ -181,15 +221,18 @@ Status: %s
Assignee: %s
Author: %s
Approved By: %s
Approvers: %s
Approval Groups: %s
Reviewers: %s
Milestone: %s
Labels: %s
Issues Closed by this MR: %s
Subscribed: %s
WebURL: %s
`,
mr.IID, mr.Title, mr.Description, project, mr.SourceBranch,
mr.TargetBranch, state, assignee,
mr.Author.Username, approvedByUsers, milestone, labels,
mr.TargetBranch, state, assignee, mr.Author.Username,
approvedByUsers, approvers, approverGroups, reviewers, milestone, labels,
strings.Trim(strings.Replace(fmt.Sprint(closingIssues), " ", ",", -1), "[]"),
subscribed, mr.WebURL,
)
Expand Down
3 changes: 3 additions & 0 deletions cmd/mr_show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Status: Open
Assignee: zaquestion
Author: zaquestion
Approved By: None
Approvers: None
Approval Groups: None
Reviewers: None
Milestone: 1.0
Labels: documentation
Issues Closed by this MR:
Expand Down
14 changes: 4 additions & 10 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -1635,24 +1635,18 @@ func MoveIssue(project string, issueNum int, dest string) (string, error) {
return fmt.Sprintf("%s/issues/%d", destProject.WebURL, issue.IID), nil
}

func GetMRApprovedBys(project string, mrNum int) ([]string, error) {
var retArray []string

func GetMRApprovalsConfiguration(project string, mrNum int) (*gitlab.MergeRequestApprovals, error) {
p, err := FindProject(project)
if err != nil {
return retArray, err
return nil, err
}

configuration, _, err := lab.MergeRequestApprovals.GetConfiguration(p.ID, mrNum)
if err != nil {
return retArray, err
}

for _, approvedby := range configuration.ApprovedBy {
retArray = append(retArray, approvedby.User.Username)
return nil, err
}

return retArray, err
return configuration, err
}

func ResolveMRDiscussion(project string, mrNum int, discussionID string, noteID int) (string, error) {
Expand Down

0 comments on commit 8c8e502

Please sign in to comment.