forked from xanzy/go-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_requests_test.go
213 lines (185 loc) · 7.69 KB
/
merge_requests_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package gitlab
import (
"fmt"
"log"
"net/http"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
ajk = BasicUser{
ID: 3614858,
Name: "Alex Kalderimis",
Username: "alexkalderimis",
State: "active",
AvatarURL: "https://assets.gitlab-static.net/uploads/-/system/user/avatar/3614858/avatar.png",
WebURL: "https://gitlab.com/alexkalderimis",
}
tk = BasicUser{
ID: 2535118,
Name: "Thong Kuah",
Username: "tkuah",
State: "active",
AvatarURL: "https://secure.gravatar.com/avatar/f7b51bdd49a4914d29504d7ff4c3f7b9?s=80&d=identicon",
WebURL: "https://gitlab.com/tkuah",
}
getOpts = GetMergeRequestsOptions{}
labels = Labels{
"GitLab Enterprise Edition",
"backend",
"database",
"database::reviewed",
"design management",
"feature",
"frontend",
"group::knowledge",
"missed:12.1",
}
pipelineCreation = time.Date(2019, 8, 19, 9, 50, 58, 157000000, time.UTC)
pipelineUpdate = time.Date(2019, 8, 19, 19, 22, 29, 647000000, time.UTC)
pipelineBasic = PipelineInfo{
ID: 77056819,
SHA: "8e0b45049b6253b8984cde9241830d2851168142",
Ref: "delete-designs-v2",
Status: "success",
WebURL: "https://gitlab.com/gitlab-org/gitlab-ee/pipelines/77056819",
CreatedAt: &pipelineCreation,
UpdatedAt: &pipelineUpdate,
}
pipelineStarted = time.Date(2019, 8, 19, 9, 51, 6, 545000000, time.UTC)
pipelineFinished = time.Date(2019, 8, 19, 19, 22, 29, 632000000, time.UTC)
pipelineDetailed = Pipeline{
ID: 77056819,
SHA: "8e0b45049b6253b8984cde9241830d2851168142",
Ref: "delete-designs-v2",
Status: "success",
WebURL: "https://gitlab.com/gitlab-org/gitlab-ee/pipelines/77056819",
BeforeSHA: "3fe568caacb261b63090886f5b879ca0d9c6f4c3",
Tag: false,
User: &ajk,
CreatedAt: &pipelineCreation,
UpdatedAt: &pipelineUpdate,
StartedAt: &pipelineStarted,
FinishedAt: &pipelineFinished,
Duration: 4916,
Coverage: "82.68",
DetailedStatus: &DetailedStatus{
Icon: "status_warning",
Text: "passed",
Label: "passed with warnings",
Group: "success-with-warnings",
Tooltip: "passed",
HasDetails: true,
DetailsPath: "/gitlab-org/gitlab-ee/pipelines/77056819",
Favicon: "https://gitlab.com/assets/ci_favicons/favicon_status_success-8451333011eee8ce9f2ab25dc487fe24a8758c694827a582f17f42b0a90446a2.png",
},
}
)
func TestGetMergeRequest(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
path := "/api/v4/projects/namespace/name/merge_requests/123"
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
mustWriteHTTPResponse(t, w, "testdata/get_merge_request.json")
})
mergeRequest, _, err := client.MergeRequests.GetMergeRequest("namespace/name", 123, &getOpts)
require.NoError(t, err)
require.Equal(t, mergeRequest.ID, 33092005)
require.Equal(t, mergeRequest.SHA, "8e0b45049b6253b8984cde9241830d2851168142")
require.Equal(t, mergeRequest.IID, 14656)
require.Equal(t, mergeRequest.Reference, "!14656")
require.Equal(t, mergeRequest.ProjectID, 278964)
require.Equal(t, mergeRequest.SourceBranch, "delete-designs-v2")
require.Equal(t, mergeRequest.TaskCompletionStatus.Count, 9)
require.Equal(t, mergeRequest.TaskCompletionStatus.CompletedCount, 8)
require.Equal(t, mergeRequest.Title, "Add deletion support for designs")
require.Equal(t, mergeRequest.Description,
"## What does this MR do?\r\n\r\nThis adds the capability to destroy/hide designs.")
require.Equal(t, mergeRequest.WebURL,
"https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/14656")
require.Equal(t, mergeRequest.MergeStatus, "can_be_merged")
require.Equal(t, mergeRequest.Author, &ajk)
require.Equal(t, mergeRequest.Assignee, &tk)
require.Equal(t, mergeRequest.Assignees, []*BasicUser{&tk})
require.Equal(t, mergeRequest.Labels, labels)
require.Equal(t, mergeRequest.Squash, true)
require.Equal(t, mergeRequest.UserNotesCount, 245)
require.Equal(t, mergeRequest.Pipeline, &pipelineBasic)
require.Equal(t, mergeRequest.HeadPipeline, &pipelineDetailed)
mrCreation := time.Date(2019, 7, 11, 22, 34, 43, 500000000, time.UTC)
require.Equal(t, mergeRequest.CreatedAt, &mrCreation)
mrUpdate := time.Date(2019, 8, 20, 9, 9, 56, 690000000, time.UTC)
require.Equal(t, mergeRequest.UpdatedAt, &mrUpdate)
require.Equal(t, mergeRequest.HasConflicts, true)
}
func TestListProjectMergeRequests(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
path := "/api/v4/projects/278964/merge_requests"
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testParams(t, r, "with_labels_details=true&with_merge_status_recheck=true")
mustWriteHTTPResponse(t, w, "testdata/get_merge_requests.json")
})
opts := ListProjectMergeRequestsOptions{
WithLabelsDetails: Bool(true),
WithMergeStatusRecheck: Bool(true),
}
mergeRequests, _, err := client.MergeRequests.ListProjectMergeRequests(278964, &opts)
require.NoError(t, err)
require.Equal(t, 20, len(mergeRequests))
validStates := []string{"opened", "closed", "locked", "merged"}
mergeStatuses := []string{"can_be_merged", "cannot_be_merged"}
allCreatedBefore := time.Date(2019, 8, 21, 0, 0, 0, 0, time.UTC)
allCreatedAfter := time.Date(2019, 8, 17, 0, 0, 0, 0, time.UTC)
for _, mr := range mergeRequests {
require.Equal(t, 278964, mr.ProjectID)
require.Contains(t, validStates, mr.State)
assert.Less(t, mr.CreatedAt.Unix(), allCreatedBefore.Unix())
assert.Greater(t, mr.CreatedAt.Unix(), allCreatedAfter.Unix())
assert.LessOrEqual(t, mr.CreatedAt.Unix(), mr.UpdatedAt.Unix())
assert.LessOrEqual(t, mr.TaskCompletionStatus.CompletedCount, mr.TaskCompletionStatus.Count)
require.Contains(t, mergeStatuses, mr.MergeStatus)
// list requests do not provide these fields:
assert.Nil(t, mr.Pipeline)
assert.Nil(t, mr.HeadPipeline)
assert.Equal(t, "", mr.DiffRefs.HeadSha)
}
}
func TestCreateMergeRequestPipeline(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/merge_requests/1/pipelines", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":1, "status":"pending"}`)
})
pipeline, _, err := client.MergeRequests.CreateMergeRequestPipeline(1, 1)
if err != nil {
t.Errorf("MergeRequests.CreateMergeRequestPipeline returned error: %v", err)
}
assert.Equal(t, 1, pipeline.ID)
assert.Equal(t, "pending", pipeline.Status)
}
func TestGetMergeRequestParticipants(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/projects/1/merge_requests/5/participants", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testURL(t, r, "/api/v4/projects/1/merge_requests/5/participants")
fmt.Fprint(w, `[{"id":1,"name":"User1","username":"User1","state":"active","avatar_url":"","web_url":"https://localhost/User1"},
{"id":2,"name":"User2","username":"User2","state":"active","avatar_url":"https://localhost/uploads/-/system/user/avatar/2/avatar.png","web_url":"https://localhost/User2"}]`)
})
mergeRequestParticipants, _, err := client.MergeRequests.GetMergeRequestParticipants("1", 5)
if err != nil {
log.Fatal(err)
}
want := []*BasicUser{{ID: 1, Name: "User1", Username: "User1", State: "active", AvatarURL: "", WebURL: "https://localhost/User1"},
{ID: 2, Name: "User2", Username: "User2", State: "active", AvatarURL: "https://localhost/uploads/-/system/user/avatar/2/avatar.png", WebURL: "https://localhost/User2"}}
if !reflect.DeepEqual(want, mergeRequestParticipants) {
t.Errorf("Issues.GetMergeRequestParticipants returned %+v, want %+v", mergeRequestParticipants, want)
}
}