-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot_helper_test.go
330 lines (289 loc) · 9.71 KB
/
robot_helper_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"github.com/opensourceways/robot-framework-lib/client"
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/mock"
)
type mockClient struct {
mock.Mock
successfulCreatePRComment bool
successfulDeletePRComment bool
successfulCheckCLASignature bool
successfulAddPRLabels bool
successfulRemovePRLabels bool
successfulCheckIfPRCreateEvent bool
successfulCheckIfPRSourceCodeUpdateEvent bool
successfulGetPullRequestCommits bool
successfulGetPullRequestLabels bool
successfulListPullRequestComments bool
successfulCheckPermission bool
permission bool
method string
commits []client.PRCommit
prComments []client.PRComment
labels []string
CLAState string
}
func (m *mockClient) CreatePRComment(org, repo, number, comment string) bool {
m.method = "CreatePRComment"
return m.successfulCreatePRComment
}
func (m *mockClient) DeletePRComment(org, repo, commentID string) bool {
m.method = "DeletePRComment"
return m.successfulDeletePRComment
}
func (m *mockClient) CheckCLASignature(urlStr string) (string, bool) {
m.method = "CheckCLASignature"
return m.CLAState, m.successfulCheckCLASignature
}
func (m *mockClient) AddPRLabels(org, repo, number string, labels []string) bool {
m.method = "AddPRLabels"
return m.successfulAddPRLabels
}
func (m *mockClient) RemovePRLabels(org, repo, number string, labels []string) bool {
m.method = "RemovePRLabels"
return m.successfulRemovePRLabels
}
func (m *mockClient) CheckIfPRCreateEvent(evt *client.GenericEvent) bool {
m.method = "CheckIfPRCreateEvent"
return m.successfulCheckIfPRCreateEvent
}
func (m *mockClient) CheckIfPRSourceCodeUpdateEvent(evt *client.GenericEvent) bool {
m.method = "CheckIfPRSourceCodeUpdateEvent"
return m.successfulCheckIfPRSourceCodeUpdateEvent
}
func (m *mockClient) GetPullRequestCommits(org, repo, number string) ([]client.PRCommit, bool) {
m.method = "GetPullRequestCommits"
return m.commits, m.successfulGetPullRequestCommits
}
func (m *mockClient) GetPullRequestLabels(org, repo, number string) ([]string, bool) {
m.method = "GetPullRequestLabels"
return m.labels, m.successfulGetPullRequestLabels
}
func (m *mockClient) ListPullRequestComments(org, repo, number string) ([]client.PRComment, bool) {
m.method = "ListPullRequestComments"
return m.prComments, m.successfulListPullRequestComments
}
func (m *mockClient) CheckPermission(org, repo, username string) (bool, bool) {
m.method = "CheckPermission"
return m.permission, m.successfulCheckPermission
}
const (
org = "org1"
repo = "repo1"
number = "1"
commenter = "commenter1"
labelYes = "label-yes"
labelNo = "label-no"
)
func TestRemoveCLASignGuideComment(t *testing.T) {
mc := new(mockClient)
bot := &robot{cli: mc, cnf: &configuration{
PlaceholderCLASignGuideTitle: "#123",
PlaceholderCLASignPassTitle: "#456",
}}
cli, ok := bot.cli.(*mockClient)
assert.Equal(t, true, ok)
case1 := "ListPullRequestComments"
cli.method = ""
// get comments failed
bot.removeCLASignGuideComment(org, repo, number)
execMethod1 := cli.method
assert.Equal(t, case1, execMethod1)
cli.method = ""
cli.successfulListPullRequestComments = true
// getting comments to remove
bot.removeCLASignGuideComment(org, repo, number)
execMethod2 := cli.method
assert.Equal(t, case1, execMethod2)
cli.method = ""
cli.prComments = []client.PRComment{
{
"123132",
"111123",
},
}
bot.cnf.PlaceholderCLASignGuideTitle = "222"
// not found CLA sign guide comment
bot.removeCLASignGuideComment(org, repo, number)
execMethod3 := cli.method
assert.Equal(t, case1, execMethod3)
case4 := "DeletePRComment"
cli.method = ""
bot.cnf.PlaceholderCLASignGuideTitle = "111"
// delete the CLA sign guide comment
bot.removeCLASignGuideComment(org, repo, number)
execMethod4 := cli.method
assert.Equal(t, case4, execMethod4)
}
func TestWaitCLASignature(t *testing.T) {
mc := new(mockClient)
bot := &robot{cli: mc, cnf: &configuration{
CommentSomeNeedSign: "%s, 24, %s",
PlaceholderCommitter: "ddd",
CommentUpdateLabelFailed: "14",
}}
cli, ok := bot.cli.(*mockClient)
assert.Equal(t, true, ok)
repoCnf := &repoConfig{
CLALabelYes: labelYes,
CLALabelNo: labelNo,
}
case1 := "unsigned users is empty"
cli.method = case1
bot.waitCLASignature(org, repo, number, []string{}, []string{labelYes}, repoCnf)
execMethod1 := cli.method
assert.Equal(t, case1, execMethod1)
case2 := "CreatePRComment"
cli.method = ""
// PR labels contains CLA failed label
bot.waitCLASignature(org, repo, number, []string{"user1"}, []string{labelNo}, repoCnf)
execMethod2 := cli.method
assert.Equal(t, case2, execMethod2)
case3 := "CreatePRComment"
cli.method = ""
cli.successfulAddPRLabels = true
// remove CLA success label, and add CLA failed label
bot.waitCLASignature(org, repo, number, []string{"user1"}, []string{labelYes}, repoCnf)
execMethod3 := cli.method
assert.Equal(t, case3, execMethod3)
}
func TestPassCLASignature(t *testing.T) {
mc := new(mockClient)
bot := &robot{cli: mc, cnf: &configuration{
CommentAllSigned: "%s, 99, %s",
PlaceholderCommitter: "aaa",
CommentUpdateLabelFailed: "53",
}}
cli, ok := bot.cli.(*mockClient)
assert.Equal(t, true, ok)
repoCnf := &repoConfig{
CLALabelYes: labelYes,
CLALabelNo: labelNo,
}
case1 := "CreatePRComment"
cli.method = ""
// PR labels contains CLA failed label and CLA success label
bot.passCLASignature(org, repo, number, []string{"user2"}, []string{labelYes, labelNo}, repoCnf)
execMethod1 := cli.method
assert.Equal(t, case1, execMethod1)
case2 := "CreatePRComment"
cli.method = ""
cli.successfulAddPRLabels = true
// PR labels is empty
bot.passCLASignature(org, repo, number, []string{"user3"}, []string{}, repoCnf)
execMethod2 := cli.method
assert.Equal(t, case2, execMethod2)
}
func TestListContributorNameAndEmail(t *testing.T) {
mc := new(mockClient)
bot := &robot{cli: mc, cnf: &configuration{}}
repoCnf := &repoConfig{}
var commits []client.PRCommit
// PR commits is empty
users, emails := bot.ListContributorNameAndEmail(commits, repoCnf)
assert.Equal(t, []string{}, users)
assert.Equal(t, []string{}, emails)
commits1 := []client.PRCommit{
{
"u1",
"e1",
"u2",
"e2",
},
{
"u1",
"e1",
"u2",
"e2",
},
}
// use author info
users1, emails1 := bot.ListContributorNameAndEmail(commits1, repoCnf)
assert.Equal(t, true, len(users1) == 1 && len(emails1) == 1)
assert.Equal(t, "u1", users1[0])
assert.Equal(t, "e1", emails1[0])
repoCnf.CheckByCommitter = true
// use committer info
users2, emails2 := bot.ListContributorNameAndEmail(commits1, repoCnf)
assert.Equal(t, true, len(users2) == 1 && len(emails2) == 1)
assert.Equal(t, "u2", users2[0])
assert.Equal(t, "e2", emails2[0])
}
func TestCheckCLASignResult(t *testing.T) {
mc := new(mockClient)
bot := &robot{cli: mc, cnf: &configuration{}}
cli, ok := bot.cli.(*mockClient)
assert.Equal(t, true, ok)
repoCnf := &repoConfig{
LitePRCommitter: litePRCommiter{
"e0",
"u0",
},
}
var commits []client.PRCommit
// PR commits is empty
allSigned, signResult := bot.checkCLASignResult(org, repo, number, commits, repoCnf)
assert.Equal(t, false, allSigned)
assert.Equal(t, ([]string)(nil), signResult[0])
assert.Equal(t, ([]string)(nil), signResult[1])
assert.Equal(t, ([]string)(nil), signResult[2])
commits1 := []client.PRCommit{
{
"u3",
"e3",
"u3",
"e3",
},
}
cli.CLAState = client.CLASignStateUnknown
// CLA sever is unavailable
allSigned1, signResult1 := bot.checkCLASignResult(org, repo, number, commits1, repoCnf)
assert.Equal(t, false, allSigned1)
assert.Equal(t, ([]string)(nil), signResult1[0])
assert.Equal(t, ([]string)(nil), signResult1[1])
assert.Equal(t, []string{"u3"}, signResult1[2])
cli.CLAState = client.CLASignStateNo
// CLA sever is available, but not signed
allSigned2, signResult2 := bot.checkCLASignResult(org, repo, number, commits1, repoCnf)
assert.Equal(t, false, allSigned2)
assert.Equal(t, ([]string)(nil), signResult2[0])
assert.Equal(t, []string{"u3"}, signResult2[1])
assert.Equal(t, ([]string)(nil), signResult2[2])
cli.CLAState = client.CLASignStateYes
// CLA sever is available, and signed
allSigned3, signResult3 := bot.checkCLASignResult(org, repo, number, commits1, repoCnf)
assert.Equal(t, true, allSigned3)
assert.Equal(t, []string{"u3"}, signResult3[0])
assert.Equal(t, ([]string)(nil), signResult3[1])
assert.Equal(t, ([]string)(nil), signResult3[2])
commits2 := []client.PRCommit{
{
"u0",
"e0",
"u0",
"e0",
},
}
cli.CLAState = client.CLASignStateYes
// CLA sever is available, and signed, but email is invalid
allSigned4, signResult4 := bot.checkCLASignResult(org, repo, number, commits2, repoCnf)
assert.Equal(t, false, allSigned4)
assert.Equal(t, ([]string)(nil), signResult4[0])
assert.Equal(t, ([]string)(nil), signResult4[1])
assert.Equal(t, []string{"u0"}, signResult4[2])
}