Skip to content

Commit 154952f

Browse files
committed
lab: Add show mr approval rules
The WebUI provides a mechanism to view the status of MR approval rules on a specific MR. Add this functionality to lab. Usage: 'lab mr approval-rule 1234' One thing to note is that, unlike the WebUI, lab currently does not show the 'Commented By' data. This is because getting the data is expensive, and at this time provides little functionality. This can be added if users request it. Signed-off-by: Prarit Bhargava <prarit@redhat.com>
1 parent 2bbd110 commit 154952f

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

cmd/mr_approval_rules.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/MakeNowJust/heredoc/v2"
7+
"github.com/spf13/cobra"
8+
lab "github.com/zaquestion/lab/internal/gitlab"
9+
10+
gitlab "github.com/xanzy/go-gitlab"
11+
)
12+
13+
func mrApprovalRuleShow(approvalState *gitlab.MergeRequestApprovalState) {
14+
for _, rule := range approvalState.Rules {
15+
fmt.Println("Rule:", rule.Name)
16+
if rule.Approved {
17+
fmt.Println(" Approved: Y")
18+
} else {
19+
fmt.Println(" Approved:")
20+
}
21+
22+
if rule.RuleType == "regular" {
23+
users := ""
24+
for u, user := range rule.EligibleApprovers {
25+
users += fmt.Sprintf("%s", user.Username)
26+
if u != (len(rule.EligibleApprovers) - 1) {
27+
users +=","
28+
}
29+
}
30+
fmt.Println(" Approvers:", users)
31+
} else if rule.RuleType == "any_approver" {
32+
fmt.Println(" Approvers: All eligible users")
33+
}
34+
35+
if rule.ApprovalsRequired > 0 {
36+
fmt.Printf(" Approvals: %d of %d\n", len(rule.ApprovedBy), rule.ApprovalsRequired)
37+
} else {
38+
fmt.Println(" Approvals: Optional")
39+
}
40+
41+
if len(rule.ApprovedBy) > 0 {
42+
users := ""
43+
for u, user := range rule.ApprovedBy {
44+
users += fmt.Sprintf("%s", user.Username)
45+
if u != (len(rule.ApprovedBy) - 1) {
46+
users +=","
47+
}
48+
}
49+
fmt.Println(" Approved By:", users)
50+
} else {
51+
fmt.Println(" Approved By:")
52+
}
53+
}
54+
}
55+
56+
var mrApprovalRuleCmd = &cobra.Command{
57+
Use: "approval-rule [remote] [<MR id or branch>]",
58+
Aliases: []string{},
59+
Example: heredoc.Doc(`
60+
lab mr approval-rule 1234`),
61+
PersistentPreRun: labPersistentPreRun,
62+
Run: func(cmd *cobra.Command, args []string) {
63+
rn, id, err := parseArgsWithGitBranchMR(args)
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
68+
approvalState, err := lab.GetMRApprovalState(rn, int(id))
69+
if err != nil {
70+
log.Fatal(err)
71+
}
72+
73+
// default, no options just show the rules
74+
mrApprovalRuleShow(approvalState)
75+
},
76+
}
77+
78+
func init() {
79+
mrCmd.AddCommand(mrApprovalRuleCmd)
80+
}

internal/gitlab/gitlab.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,16 @@ func GetMRApprovalsConfiguration(projID interface{}, id int) (*gitlab.MergeReque
16001600
return configuration, err
16011601
}
16021602

1603+
// GetMRApprovalsState returns the current MR approval rule
1604+
func GetMRApprovalState(projID interface{}, id int) (*gitlab.MergeRequestApprovalState, error) {
1605+
state, _, err := lab.MergeRequestApprovals.GetApprovalState(projID, id)
1606+
if err != nil {
1607+
return nil, err
1608+
}
1609+
1610+
return state, err
1611+
}
1612+
16031613
// ResolveMRDiscussion resolves a discussion (blocking thread) based on its ID
16041614
func ResolveMRDiscussion(projID interface{}, mrID int, discussionID string, noteID int) (string, error) {
16051615
opts := &gitlab.ResolveMergeRequestDiscussionOptions{

0 commit comments

Comments
 (0)