1
2 package list
3
4 import (
5 "context"
6 "fmt"
7 "reflect"
8 "testing"
9
10 "edge-infra.dev/pkg/lib/logging"
11
12 "github.com/google/go-github/v47/github"
13
14 "edge-infra.dev/pkg/f8n/devinfra/jack/constants"
15 "edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
16 )
17
18 var c = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}}
19 var hp = &plugin.HandlerParams{
20 Ctx: context.Background(),
21 Client: c,
22 Org: "some-org",
23 Repo: "some-repo",
24 Log: *logging.NewLogger(),
25 }
26
27 var relatedIssue = github.Issue{
28 Assignees: []*github.User{},
29 Body: github.String("Related Issue"),
30 Number: github.Int(999),
31 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
32 Repository: &github.Repository{
33 Owner: &github.User{Login: github.String(hp.Org)},
34 Name: github.String(hp.Repo),
35 ID: github.Int64(1234),
36 },
37 }
38
39 const pcBody = "### parents\n<!-- PARENTLIST -->\n*No Issues added yet*\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n*No Issues added yet*\n<!-- ENDCHILDLIST -->"
40
41 func TestHandleNewIssue(t *testing.T) {
42
43 c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}
44 ie := github.IssuesEvent{
45 Action: github.String("opened"),
46 Issue: &github.Issue{
47 Body: github.String(""),
48 Labels: []*github.Label{{Name: github.String(string(constants.Epic))}},
49 Number: github.Int(1),
50 Title: github.String("List Test Issue"),
51 },
52 Repo: &github.Repository{
53 Owner: &github.User{Login: github.String(hp.Org)},
54 Name: github.String(hp.Repo),
55 ID: github.Int64(1234),
56 },
57 }
58
59 handleIssue(*hp, ie)
60
61 ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}}
62 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
63 t.Error("Failed: TestHandleNewIssue -> Test if the new issue body is empty.")
64 }
65
66
67 c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}
68 ie = github.IssuesEvent{
69 Action: github.String("opened"),
70 Issue: &github.Issue{
71 Body: github.String("Nothing to see here"),
72 Labels: []*github.Label{{Name: github.String(string(constants.Epic))}},
73 Number: github.Int(1),
74 Title: github.String("List Test Issue"),
75 },
76 Repo: &github.Repository{
77 Owner: &github.User{Login: github.String(hp.Org)},
78 Name: github.String(hp.Repo),
79 ID: github.Int64(1234),
80 },
81 }
82
83 handleIssue(*hp, ie)
84
85 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}}
86 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
87 t.Error("Failed: TestHandleNewIssue -> Test if the new issue body has NO jack commands.")
88 }
89
90
91 c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}
92 ie = github.IssuesEvent{
93 Action: github.String("opened"),
94 Issue: &github.Issue{
95 Body: github.String("/child #1 /parent #1"),
96 Labels: []*github.Label{{Name: github.String(string(constants.Epic))}},
97 Number: github.Int(1),
98 Title: github.String("List Test Issue"),
99 },
100 Repo: &github.Repository{
101 Owner: &github.User{Login: github.String(hp.Org)},
102 Name: github.String(hp.Repo),
103 ID: github.Int64(1234),
104 },
105 }
106
107 handleIssue(*hp, ie)
108
109 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
110 Comments: 1,
111 Labels: []string{string(constants.Epic)},
112 }}
113 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
114 t.Error("Failed: TestHandleNewIssue -> Test if the new issue body has both /child and /parent commands.")
115 }
116
117
118 c.GithubIssues = &plugin.MockIssueService{
119 Labels: []string{string(constants.Feature)},
120 RelatedIssue: relatedIssue,
121 }
122 ie = github.IssuesEvent{
123 Action: github.String("opened"),
124 Issue: &github.Issue{
125 Body: github.String("/child #2"),
126 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
127 Number: github.Int(1),
128 Title: github.String("List Test Issue"),
129 },
130 Repo: &github.Repository{
131 Owner: &github.User{Login: github.String(hp.Org)},
132 Name: github.String(hp.Repo),
133 ID: github.Int64(1234),
134 },
135 }
136
137 handleIssue(*hp, ie)
138
139 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
140 Body: fmt.Sprintf("/child #2\n%s%s%s", constants.Preamble, "### parents\n<!-- PARENTLIST -->\n*No Issues added yet*\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDCHILDLIST -->", constants.Postamble),
141 Comments: 0,
142 Labels: []string{string(constants.Feature)},
143 RelatedIssue: relatedIssue,
144 }}
145 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
146 t.Error("Failed: TestHandleNewIssue -> Test if the new issue is a parent.")
147 }
148
149
150 c.GithubIssues = &plugin.MockIssueService{
151 Labels: []string{string(constants.Task)},
152 RelatedIssue: relatedIssue,
153 }
154 ie = github.IssuesEvent{
155 Action: github.String("opened"),
156 Issue: &github.Issue{
157 Body: github.String("/parent #2"),
158 Labels: []*github.Label{{Name: github.String(string(constants.Task))}},
159 Number: github.Int(1),
160 Title: github.String("List Test Issue"),
161 },
162 Repo: &github.Repository{
163 Owner: &github.User{Login: github.String(hp.Org)},
164 Name: github.String(hp.Repo),
165 ID: github.Int64(1234),
166 },
167 }
168
169 handleIssue(*hp, ie)
170
171 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
172 Body: fmt.Sprintf("/parent #2\n%s%s%s", constants.Preamble, "### parents\n<!-- PARENTLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDPARENTLIST -->\n<!-- CHILDLIST -->\n\n<!-- ENDCHILDLIST -->", constants.Postamble),
173 Labels: []string{string(constants.Task)},
174 RelatedIssue: relatedIssue,
175 }}
176 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
177 t.Error("Failed: TestHandleNewIssue -> Test if the new issue is a child.")
178 }
179
180
181 c.GithubIssues = &plugin.MockIssueService{
182 Labels: []string{string(constants.Epic)},
183 RelatedIssue: relatedIssue,
184 }
185 ie = github.IssuesEvent{
186 Action: github.String("opened"),
187 Issue: &github.Issue{
188 Body: github.String("/parent #2"),
189 Labels: []*github.Label{{Name: github.String(string(constants.Epic))}},
190 Number: github.Int(1),
191 Title: github.String("List Test Issue"),
192 },
193 Repo: &github.Repository{
194 Owner: &github.User{Login: github.String(hp.Org)},
195 Name: github.String(hp.Repo),
196 ID: github.Int64(1234),
197 },
198 }
199
200 handleIssue(*hp, ie)
201
202 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
203 Comments: 1,
204 Labels: []string{string(constants.Epic)},
205 RelatedIssue: relatedIssue,
206 }}
207 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
208 t.Error("Failed: TestHandleNewIssue -> Test if the new issue is an epic and has a parent.")
209 }
210
211
212 c.GithubIssues = &plugin.MockIssueService{Labels: []string{}}
213 ie = github.IssuesEvent{
214 Action: github.String("opened"),
215 Issue: &github.Issue{
216 Body: github.String("/child #12"),
217 Labels: []*github.Label{},
218 Number: github.Int(1),
219 Title: github.String("Test adding epic"),
220 },
221 Repo: &github.Repository{
222 Owner: &github.User{Login: github.String(hp.Org)},
223 Name: github.String(hp.Repo),
224 ID: github.Int64(1234),
225 },
226 }
227 handleIssue(*hp, ie)
228
229 ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
230 Body: fmt.Sprintf("/child #12\n%s%s%s", constants.Preamble, "<!-- PARENTLIST -->\n\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n- <!-- REPOID=1234 --> #0\n<!-- ENDCHILDLIST -->", constants.Postamble),
231 Comments: 0,
232 Labels: []string{string(constants.Epic)},
233 }}
234
235 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
236 t.Error("Failed test: handleIssue -> Test adding epic to parent")
237 }
238 }
239
240 func TestHandleIssueComment(t *testing.T) {
241
242 c.GithubIssues = &plugin.MockIssueService{}
243 ice := github.IssueCommentEvent{
244 Action: github.String("edited"),
245 }
246
247 handleIssueComment(*hp, ice)
248
249 ct := plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}}
250 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
251 t.Error("Failed: TestHandleIssueComment -> Test if action is NOT created.")
252 }
253
254
255 ice = github.IssueCommentEvent{
256 Action: github.String("created"),
257 Issue: &github.Issue{},
258 Comment: &github.IssueComment{Body: github.String("No relevant commands here.")},
259 }
260
261 handleIssueComment(*hp, ice)
262
263 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
264 t.Error("Failed: TestHandleIssueComment -> Test if no commands are found in the comment.")
265 }
266
267
268 c.GithubIssues = &plugin.MockIssueService{
269 Comments: 0,
270 Labels: []string{string(constants.Task)},
271 }
272 ice = github.IssueCommentEvent{
273 Action: github.String("created"),
274 Comment: &github.IssueComment{Body: github.String("/child #2")},
275 Issue: &github.Issue{
276 Body: github.String("Nothing to see here"),
277 Labels: []*github.Label{{Name: github.String(string(constants.Task))}},
278 Number: github.Int(1),
279 Title: github.String("List Test Issue"),
280 },
281 Repo: &github.Repository{
282 Owner: &github.User{Login: github.String(hp.Org)},
283 Name: github.String(hp.Repo),
284 ID: github.Int64(1234),
285 },
286 }
287
288 handleIssueComment(*hp, ice)
289
290 ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
291 Comments: 1,
292 Labels: []string{string(constants.Task)}},
293 }
294 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
295 t.Error("Failed: TestHandleIssueComment -> Test if comment has child commands while issue is not a parent.")
296 }
297
298
299 c.GithubIssues = &plugin.MockIssueService{
300 Comments: 0,
301 Labels: []string{string(constants.Feature)},
302 RelatedIssue: relatedIssue,
303 }
304 ice = github.IssueCommentEvent{
305 Action: github.String("created"),
306 Comment: &github.IssueComment{Body: github.String("/child -#2")},
307 Issue: &github.Issue{
308 Body: github.String("Nothing to see here"),
309 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
310 Number: github.Int(1),
311 Title: github.String("List Test Issue"),
312 },
313 Repo: &github.Repository{
314 Owner: &github.User{Login: github.String(hp.Org)},
315 Name: github.String(hp.Repo),
316 ID: github.Int64(1234),
317 },
318 }
319
320 handleIssueComment(*hp, ice)
321
322 ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
323 Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, pcBody, constants.Postamble),
324 Comments: 0,
325 Labels: []string{string(constants.Feature)},
326 RelatedIssue: relatedIssue,
327 }}
328
329 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
330 t.Error("Failed: TestHandleIssueComment -> Test child removal.")
331 }
332
333
334 c.GithubIssues = &plugin.MockIssueService{
335 Comments: 0,
336 Labels: []string{string(constants.Feature)},
337 RelatedIssue: relatedIssue,
338 }
339 ice = github.IssueCommentEvent{
340 Action: github.String("created"),
341 Comment: &github.IssueComment{Body: github.String("/parent -#2")},
342 Issue: &github.Issue{
343 Body: github.String("Nothing to see here"),
344 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
345 Number: github.Int(1),
346 Title: github.String("List Test Issue"),
347 },
348 Repo: &github.Repository{
349 Owner: &github.User{Login: github.String(hp.Org)},
350 Name: github.String(hp.Repo),
351 ID: github.Int64(1234),
352 },
353 }
354
355 handleIssueComment(*hp, ice)
356
357 ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
358
359 Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, pcBody, constants.Postamble),
360 Comments: 0,
361 Labels: []string{string(constants.Feature)},
362 RelatedIssue: relatedIssue,
363 }}
364 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
365 t.Error("Failed: TestHandleIssueComment -> Test parent removal.")
366 }
367
368
369 c.GithubIssues = &plugin.MockIssueService{
370 Comments: 0,
371 Labels: []string{string(constants.Feature)},
372 RelatedIssue: relatedIssue,
373 }
374 ice = github.IssueCommentEvent{
375 Action: github.String("created"),
376 Comment: &github.IssueComment{Body: github.String("/child #2")},
377 Issue: &github.Issue{
378 Body: github.String("Nothing to see here"),
379 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
380 Number: github.Int(1),
381 Title: github.String("List Test Issue"),
382 },
383 Repo: &github.Repository{
384 Owner: &github.User{Login: github.String(hp.Org)},
385 Name: github.String(hp.Repo),
386 ID: github.Int64(1234),
387 },
388 }
389
390 handleIssueComment(*hp, ice)
391
392 ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
393 Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, "### parents\n<!-- PARENTLIST -->\n*No Issues added yet*\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDCHILDLIST -->", constants.Postamble),
394 Comments: 0,
395 Labels: []string{string(constants.Feature)},
396 RelatedIssue: relatedIssue,
397 }}
398 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
399 t.Error("Failed: TestHandleIssueComment -> Test child addition.")
400 }
401
402
403 c.GithubIssues = &plugin.MockIssueService{
404 Comments: 0,
405 Labels: []string{string(constants.Feature)},
406 RelatedIssue: relatedIssue,
407 }
408 ice = github.IssueCommentEvent{
409 Action: github.String("created"),
410 Comment: &github.IssueComment{Body: github.String("/parent #2")},
411 Issue: &github.Issue{
412 Body: github.String("Nothing to see here"),
413 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
414 Number: github.Int(1),
415 Title: github.String("List Test Issue"),
416 },
417 Repo: &github.Repository{
418 Owner: &github.User{Login: github.String(hp.Org)},
419 Name: github.String(hp.Repo),
420 ID: github.Int64(1234),
421 },
422 }
423
424 handleIssueComment(*hp, ice)
425
426 ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
427 Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, "### parents\n<!-- PARENTLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n*No Issues added yet*\n<!-- ENDCHILDLIST -->", constants.Postamble),
428 Comments: 0,
429 Labels: []string{string(constants.Feature)},
430 RelatedIssue: relatedIssue,
431 }}
432 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
433 t.Error("Failed: TestHandleIssueComment -> Test parent addition.")
434 }
435
436
437 c.GithubIssues = &plugin.MockIssueService{
438 Comments: 0,
439 Labels: []string{string(constants.Feature)},
440 RelatedIssue: relatedIssue,
441 }
442 ice = github.IssueCommentEvent{
443 Action: github.String("created"),
444 Comment: &github.IssueComment{Body: github.String("/parent #1")},
445 Issue: &github.Issue{
446 Body: github.String("Nothing to see here"),
447 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
448 Number: github.Int(1),
449 Title: github.String("List Test Issue"),
450 },
451 Repo: &github.Repository{
452 Owner: &github.User{Login: github.String(hp.Org)},
453 Name: github.String(hp.Repo),
454 ID: github.Int64(1234),
455 },
456 }
457
458 handleIssueComment(*hp, ice)
459
460 ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
461
462 Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, pcBody, constants.Postamble),
463 Comments: 1,
464 Labels: []string{string(constants.Feature)},
465 RelatedIssue: relatedIssue,
466 }}
467 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
468 t.Error("Failed: TestHandleIssueComment -> Test adding an issue to itself.")
469 }
470
471
472
473 c.GithubIssues = &plugin.MockIssueService{
474 Comments: 0,
475 Labels: []string{string(constants.Feature)},
476 RelatedIssue: relatedIssue,
477 Body: "Nothing to see here\n### parents\n<!-- PARENTLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n*No Issues added yet*\n<!-- ENDCHILDLIST -->",
478 }
479
480
481 ice = github.IssueCommentEvent{
482 Action: github.String("created"),
483 Comment: &github.IssueComment{Body: github.String("/child #999")},
484 Issue: &github.Issue{
485 Body: github.String("Nothing to see here\n<!-- JACKBOT -->\n___\n\n<!-- LIST -->\n### parents\n<!-- PARENTLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n*No Issues added yet*\n<!-- ENDCHILDLIST -->\n<!-- ENDLIST -->\n To add to this list use the commands: \n`/child #{child_number}` to add a child or `/parent #{parent_number}` to add a parent.\n[Click here to find out more about jackbot](https://docs.edge-infra.dev/edge-dev/process/bots/jackbot/)\n___\n<!-- ENDJACKBOT -->"),
486 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
487 Number: github.Int(1),
488 Title: github.String("Test Swappie"),
489 },
490 Repo: &github.Repository{
491 Owner: &github.User{Login: github.String(hp.Org)},
492 Name: github.String(hp.Org),
493 ID: github.Int64(1234),
494 },
495 }
496 handleIssueComment(*hp, ice)
497
498
499 ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
500 Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, "### parents\n<!-- PARENTLIST -->\n*No Issues added yet*\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDCHILDLIST -->", constants.Postamble),
501 Comments: 0,
502 Labels: []string{string(constants.Feature)},
503 RelatedIssue: relatedIssue,
504 }}
505
506 if !reflect.DeepEqual(c.GithubIssues, ct.GithubIssues) {
507 t.Error("Failed: TestIssueComment ==> Swap child to parent")
508 }
509
510
511 c.GithubIssues = &plugin.MockIssueService{
512 Comments: 0,
513 Labels: []string{string(constants.Feature)},
514 RelatedIssue: relatedIssue,
515 Body: "Nothing to see here\n" + constants.Preamble + "### parents\n<!-- PARENTLIST -->\n*No Issues added yet*\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDCHILDLIST -->" + constants.Postamble,
516 }
517 ice = github.IssueCommentEvent{
518 Action: github.String("created"),
519 Comment: &github.IssueComment{Body: github.String("/parent #999")},
520 Issue: &github.Issue{
521 Body: github.String("Nothing to see here\n" + constants.Preamble + "### parents\n<!-- PARENTLIST -->\n*No Issues added yet*\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDCHILDLIST -->" + constants.Postamble),
522 Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
523 Number: github.Int(1),
524 Title: github.String("Test Swip Swap"),
525 },
526 Repo: &github.Repository{
527 Owner: &github.User{Login: github.String(hp.Org)},
528 Name: github.String(hp.Org),
529 ID: github.Int64(1234),
530 },
531 }
532 handleIssueComment(*hp, ice)
533
534 ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
535
536 Body: "Nothing to see here\n" + constants.Preamble + "### parents\n<!-- PARENTLIST -->\n- <!-- REPOID=1234 --> #999\n<!-- ENDPARENTLIST -->\n### children\n<!-- CHILDLIST -->\n*No Issues added yet*\n<!-- ENDCHILDLIST -->" + constants.Postamble,
537 Comments: 0,
538 Labels: []string{string(constants.Feature)},
539 RelatedIssue: relatedIssue,
540 }}
541
542 if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
543 t.Error("Failed: TestIssueComment ==> Swap parent to child")
544 }
545 }
546
View as plain text