GitLabgitlab-org
cli
main
1//go:build !integration
3package approvers
5import (
6 "testing"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 "go.uber.org/mock/gomock"
12 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 gitlabtesting "gitlab.com/gitlab-org/api/client-go/v3/testing"
15 "gitlab.com/gitlab-org/cli/internal/testing/cmdtest"
16)
18func TestMrApprovers(t *testing.T) {
19 type testCase struct {
20 name string
21 cli string
22 expectedOut string
23 wantErr bool
24 wantStderr string
25 setupMock func(tc *gitlabtesting.TestClient)
26 }
28 testMR := &gitlab.MergeRequest{
29 BasicMergeRequest: gitlab.BasicMergeRequest{
30 ID: 123,
31 IID: 123,
32 ProjectID: 3,
33 Title: "test mr title",
34 Description: "test mr description",
35 State: "opened",
36 },
37 }
39 approvalState := &gitlab.MergeRequestApprovalState{
40 ApprovalRulesOverwritten: true,
41 Rules: []*gitlab.MergeRequestApprovalRule{
42 {
43 ID: 239,
44 Name: "All Members",
45 RuleType: "any_approver",
46 EligibleApprovers: []*gitlab.BasicUser{
47 {
48 ID: 1,
49 Username: "approver_1",
50 Name: "Abc Approver",
51 State: "active",
52 },
53 {
54 ID: 6,
55 Username: "approver_2",
56 Name: "Bar Approver",
57 State: "active",
58 },
59 },
60 ApprovalsRequired: 1,
61 ApprovedBy: []*gitlab.BasicUser{
62 {
63 ID: 1232,
64 Username: "foo_reviewer",
65 Name: "Foo Reviewer",
66 State: "active",
67 },
68 },
69 Approved: true,
70 },
71 },
72 }
74 testCases := []testCase{
75 {
76 name: "List approvers by MR ID",
77 cli: "123",
78 // Note: trailing tabs are added by the table renderer
79 expectedOut: "\nListing merge request !123 eligible approvers:\n" +
80 "Approval rules overwritten.\n" +
81 "Rule \"All Members\" sufficient approvals (1/1 required):\n" +
82 "Name\tUsername\tApproved\n" +
83 "Abc Approver\tapprover_1\t-\t\n" +
84 "Bar Approver\tapprover_2\t-\t\n" +
85 "Foo Reviewer\tfoo_reviewer\t👍\t\n\n",
86 setupMock: func(tc *gitlabtesting.TestClient) {
87 tc.MockMergeRequests.EXPECT().
88 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any()).
89 Return(testMR, nil, nil)
90 tc.MockMergeRequestApprovals.EXPECT().
91 GetApprovalState("OWNER/REPO", int64(123), gomock.Any()).
92 Return(approvalState, nil, nil)
93 },
94 },
95 }
97 for _, tc := range testCases {
98 t.Run(tc.name, func(t *testing.T) {
99 // GIVEN
100 testClient := gitlabtesting.NewTestClient(t)
101 tc.setupMock(testClient)
102 exec := cmdtest.SetupCmdForTest(
103 t,
104 NewCmdApprovers,
105 false,
106 cmdtest.WithGitLabClient(testClient.Client),
107 )
109 // WHEN
110 out, err := exec(tc.cli)
112 // THEN
113 if tc.wantErr {
114 require.Error(t, err)
115 assert.Contains(t, err.Error(), tc.wantStderr)
116 return
117 }
118 require.NoError(t, err)
119 assert.Equal(t, tc.expectedOut, out.OutBuf.String())
120 assert.Empty(t, out.ErrBuf.String())
121 })
122 }
123}
125func TestMrApprovers_JSON(t *testing.T) {
126 t.Parallel()
128 testMR := &gitlab.MergeRequest{
129 BasicMergeRequest: gitlab.BasicMergeRequest{
130 ID: 123,
131 IID: 123,
132 ProjectID: 3,
133 Title: "test mr title",
134 Description: "test mr description",
135 State: "opened",
136 },
137 }
139 approvalState := &gitlab.MergeRequestApprovalState{
140 ApprovalRulesOverwritten: true,
141 Rules: []*gitlab.MergeRequestApprovalRule{
142 {
143 ID: 239,
144 Name: "All Members",
145 RuleType: "any_approver",
146 EligibleApprovers: []*gitlab.BasicUser{
147 {
148 ID: 1,
149 Username: "approver_1",
150 Name: "Abc Approver",
151 State: "active",
152 },
153 },
154 ApprovalsRequired: 1,
155 ApprovedBy: []*gitlab.BasicUser{
156 {
157 ID: 1232,
158 Username: "foo_reviewer",
159 Name: "Foo Reviewer",
160 State: "active",
161 },
162 },
163 Approved: true,
164 },
165 },
166 }
168 testClient := gitlabtesting.NewTestClient(t)
169 testClient.MockMergeRequests.EXPECT().
170 GetMergeRequest("OWNER/REPO", int64(123), gomock.Any()).
171 Return(testMR, nil, nil)
172 testClient.MockMergeRequestApprovals.EXPECT().
173 GetApprovalState("OWNER/REPO", int64(123), gomock.Any()).
174 Return(approvalState, nil, nil)
176 exec := cmdtest.SetupCmdForTest(
177 t,
178 NewCmdApprovers,
179 false,
180 cmdtest.WithGitLabClient(testClient.Client),
181 )
183 out, err := exec("123 --output json")
184 require.NoError(t, err)
186 assert.Contains(t, out.String(), `"approval_rules_overwritten":true`)
187 assert.Contains(t, out.String(), `"name":"All Members"`)
188 assert.Contains(t, out.String(), `"username":"approver_1"`)
189 assert.Empty(t, out.Stderr())
190}
Diagnostics8 git calls · 312 ms
CallStatusSizeTime0312 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200746 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=746 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=f3d413a22af5a9c2fe9aa49709b1cb36d1351a7f git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=746 git.pkt.payload.max=719 git.sideband.band1.bytes=719 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=719 git.pack.trailer=330baf80c59a4208b55120dff7dd824b7952b790Open pack map
fetchtree:0200177 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=299 git.response.bytes=177 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=68b5b6c350c2dcda1ba34de2038766c0140ad9c4 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=177 git.pkt.payload.max=150 git.sideband.band1.bytes=150 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=150 git.pack.trailer=b99879e3fb3f4355e99db5756f2c4058158bd8fcOpen pack map
fetch2001.3 KiB299 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1364 git.response.cache=MISS git.response.duration_ms=299 git.fetch.wants=309cd65cf76b139745ad11b7f9b59ef784de0385 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1364 git.pkt.payload.max=1337 git.sideband.band1.bytes=1337 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e3082d4f6a15db
- Request
- 26989103…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format