From 46cef78c4aec48d61510ed30f3f83e3dd59a44e0 Mon Sep 17 00:00:00 2001 From: Prarit Bhargava Date: Sat, 28 Nov 2020 13:21:45 -0500 Subject: [PATCH] mr_show: Display approvals Currently there is no way to use lab to view the list of contributors that have approved an MR. It would be nice if the users that approved an MR were displayed in the 'mr show' output. Display the approvers in a 'Approved By:' section of 'mr show'. Signed-off-by: Prarit Bhargava --- cmd/mr_show.go | 12 +++++++++++- cmd/mr_show_test.go | 1 + internal/gitlab/gitlab.go | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/mr_show.go b/cmd/mr_show.go index 7707448d..59de1b52 100644 --- a/cmd/mr_show.go +++ b/cmd/mr_show.go @@ -116,6 +116,7 @@ func printMR(mr *gitlab.MergeRequest, project string, renderMarkdown bool) { assignee := "None" milestone := "None" labels := "None" + approvedByUsers := "None" state := map[string]string{ "opened": "Open", "closed": "Closed", @@ -145,6 +146,14 @@ func printMR(mr *gitlab.MergeRequest, project string, renderMarkdown bool) { log.Fatal(err) } + approvedBys, err := lab.GetMRApprovedBys(project, mr.IID) + if err != nil { + log.Fatal(err) + } + if len(approvedBys) > 0 { + approvedByUsers = strings.Join(approvedBys, ", ") + } + fmt.Printf(` #%d %s =================================== @@ -155,6 +164,7 @@ Branches: %s->%s Status: %s Assignee: %s Author: %s +Approved By: %s Milestone: %s Labels: %s Issues Closed by this MR: %s @@ -162,7 +172,7 @@ WebURL: %s `, mr.IID, mr.Title, mr.Description, project, mr.SourceBranch, mr.TargetBranch, state, assignee, - mr.Author.Username, milestone, labels, + mr.Author.Username, approvedByUsers, milestone, labels, strings.Trim(strings.Replace(fmt.Sprint(closingIssues), " ", ",", -1), "[]"), mr.WebURL, ) diff --git a/cmd/mr_show_test.go b/cmd/mr_show_test.go index 2c1b0295..13ab2d4a 100644 --- a/cmd/mr_show_test.go +++ b/cmd/mr_show_test.go @@ -37,6 +37,7 @@ Branches: mrtest->master Status: Open Assignee: zaquestion Author: zaquestion +Approved By: None Milestone: 1.0 Labels: documentation Issues Closed by this MR: diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index 1b9831ee..23e9a089 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -1102,3 +1102,23 @@ 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 + + p, err := FindProject(project) + if err != nil { + return retArray, 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 retArray, err +}