@@ -325,137 +325,152 @@ func (g gitHubService) Send(notification Notification, dest Destination) error {
325
325
return fmt .Errorf ("GitHub.repoURL (%s) does not have a `/`" , notification .GitHub .repoURL )
326
326
}
327
327
328
- var (
329
- sendStatus bool
330
- sendDeployment bool
331
- sendComment bool
332
- )
328
+ // match previous behavior:
329
+ // send all notifications if a destination is not specified
330
+ sendAll := dest .Recipient == ""
333
331
334
- switch dest .Recipient {
335
- case "status" :
336
- sendStatus = true
337
- case "deployment" :
338
- sendDeployment = true
339
- case "comment" :
340
- sendComment = true
341
- default :
342
- // match previous behavior: send all notifications if a destination is
343
- // not specified
344
- sendStatus = true
345
- sendDeployment = true
346
- sendComment = true
332
+ if dest .Recipient == "status" || sendAll {
333
+ err := g .sendStatus (u [0 ], u [1 ], notification )
334
+ if err != nil {
335
+ return err
336
+ }
347
337
}
348
338
349
- if notification .GitHub .Status != nil && sendStatus {
350
- // maximum is 140 characters
351
- description := trunc (notification .Message , 140 )
352
- _ , _ , err := g .client .Repositories .CreateStatus (
353
- context .Background (),
354
- u [0 ],
355
- u [1 ],
356
- notification .GitHub .revision ,
357
- & github.RepoStatus {
358
- State : & notification .GitHub .Status .State ,
359
- Description : & description ,
360
- Context : & notification .GitHub .Status .Label ,
361
- TargetURL : & notification .GitHub .Status .TargetURL ,
362
- },
363
- )
339
+ if dest .Recipient == "deployment" || sendAll {
340
+ err := g .sendDeployment (u [0 ], u [1 ], notification )
364
341
if err != nil {
365
342
return err
366
343
}
367
344
}
368
345
369
- if notification .GitHub .Deployment != nil && sendDeployment {
370
- // maximum is 140 characters
371
- description := trunc (notification .Message , 140 )
372
- deployments , _ , err := g .client .Repositories .ListDeployments (
373
- context .Background (),
374
- u [0 ],
375
- u [1 ],
376
- & github.DeploymentsListOptions {
377
- Ref : notification .GitHub .revision ,
378
- Environment : notification .GitHub .Deployment .Environment ,
379
- },
380
- )
346
+ if dest .Recipient == "comment" || sendAll {
347
+ err := g .sendComment (u [0 ], u [1 ], notification )
381
348
if err != nil {
382
349
return err
383
350
}
351
+ }
384
352
385
- // if no reference is provided, use the revision
386
- ref := notification .GitHub .Deployment .Reference
387
- if ref == "" {
388
- ref = notification .GitHub .revision
389
- }
353
+ return nil
354
+ }
390
355
391
- var deployment * github.Deployment
392
- if len (deployments ) != 0 {
393
- deployment = deployments [0 ]
394
- } else {
395
- deployment , _ , err = g .client .Repositories .CreateDeployment (
396
- context .Background (),
397
- u [0 ],
398
- u [1 ],
399
- & github.DeploymentRequest {
400
- Ref : & ref ,
401
- Environment : & notification .GitHub .Deployment .Environment ,
402
- RequiredContexts : & notification .GitHub .Deployment .RequiredContexts ,
403
- AutoMerge : notification .GitHub .Deployment .AutoMerge ,
404
- TransientEnvironment : notification .GitHub .Deployment .TransientEnvironment ,
405
- },
406
- )
407
- if err != nil {
408
- return err
409
- }
410
- }
411
- _ , _ , err = g .client .Repositories .CreateDeploymentStatus (
356
+ func (g gitHubService ) sendStatus (owner , repo string , notification Notification ) error {
357
+ if notification .GitHub .Status == nil {
358
+ return nil
359
+ }
360
+
361
+ // maximum is 140 characters
362
+ description := trunc (notification .Message , 140 )
363
+ _ , _ , err := g .client .Repositories .CreateStatus (
364
+ context .Background (),
365
+ owner ,
366
+ repo ,
367
+ notification .GitHub .revision ,
368
+ & github.RepoStatus {
369
+ State : & notification .GitHub .Status .State ,
370
+ Description : & description ,
371
+ Context : & notification .GitHub .Status .Label ,
372
+ TargetURL : & notification .GitHub .Status .TargetURL ,
373
+ },
374
+ )
375
+ return err
376
+ }
377
+
378
+ func (g gitHubService ) sendDeployment (owner , repo string , notification Notification ) error {
379
+ if notification .GitHub .Deployment == nil {
380
+ return nil
381
+ }
382
+
383
+ // maximum is 140 characters
384
+ description := trunc (notification .Message , 140 )
385
+ deployments , _ , err := g .client .Repositories .ListDeployments (
386
+ context .Background (),
387
+ owner ,
388
+ repo ,
389
+ & github.DeploymentsListOptions {
390
+ Ref : notification .GitHub .revision ,
391
+ Environment : notification .GitHub .Deployment .Environment ,
392
+ },
393
+ )
394
+ if err != nil {
395
+ return err
396
+ }
397
+
398
+ // if no reference is provided, use the revision
399
+ ref := notification .GitHub .Deployment .Reference
400
+ if ref == "" {
401
+ ref = notification .GitHub .revision
402
+ }
403
+
404
+ var deployment * github.Deployment
405
+ if len (deployments ) != 0 {
406
+ deployment = deployments [0 ]
407
+ } else {
408
+ deployment , _ , err = g .client .Repositories .CreateDeployment (
412
409
context .Background (),
413
- u [0 ],
414
- u [1 ],
415
- * deployment .ID ,
416
- & github.DeploymentStatusRequest {
417
- State : & notification .GitHub .Deployment .State ,
418
- LogURL : & notification .GitHub .Deployment .LogURL ,
419
- Description : & description ,
420
- Environment : & notification .GitHub .Deployment .Environment ,
421
- EnvironmentURL : & notification .GitHub .Deployment .EnvironmentURL ,
410
+ owner ,
411
+ repo ,
412
+ & github.DeploymentRequest {
413
+ Ref : & ref ,
414
+ Environment : & notification .GitHub .Deployment .Environment ,
415
+ RequiredContexts : & notification .GitHub .Deployment .RequiredContexts ,
416
+ AutoMerge : notification .GitHub .Deployment .AutoMerge ,
417
+ TransientEnvironment : notification .GitHub .Deployment .TransientEnvironment ,
422
418
},
423
419
)
424
420
if err != nil {
425
421
return err
426
422
}
427
423
}
424
+ _ , _ , err = g .client .Repositories .CreateDeploymentStatus (
425
+ context .Background (),
426
+ owner ,
427
+ repo ,
428
+ * deployment .ID ,
429
+ & github.DeploymentStatusRequest {
430
+ State : & notification .GitHub .Deployment .State ,
431
+ LogURL : & notification .GitHub .Deployment .LogURL ,
432
+ Description : & description ,
433
+ Environment : & notification .GitHub .Deployment .Environment ,
434
+ EnvironmentURL : & notification .GitHub .Deployment .EnvironmentURL ,
435
+ },
436
+ )
428
437
429
- if notification .GitHub .PullRequestComment != nil && sendComment {
430
- // maximum is 65536 characters
431
- body := trunc (notification .GitHub .PullRequestComment .Content , 65536 )
432
- comment := & github.IssueComment {
433
- Body : & body ,
434
- }
438
+ return err
439
+ }
440
+
441
+ func (g gitHubService ) sendComment (owner , repo string , notification Notification ) error {
442
+ if notification .GitHub .PullRequestComment == nil {
443
+ return nil
444
+ }
445
+
446
+ // maximum is 65536 characters
447
+ body := trunc (notification .GitHub .PullRequestComment .Content , 65536 )
448
+ comment := & github.IssueComment {
449
+ Body : & body ,
450
+ }
451
+
452
+ prs , _ , err := g .client .PullRequests .ListPullRequestsWithCommit (
453
+ context .Background (),
454
+ owner ,
455
+ repo ,
456
+ notification .GitHub .revision ,
457
+ nil ,
458
+ )
459
+ if err != nil {
460
+ return err
461
+ }
435
462
436
- prs , _ , err := g .client .PullRequests .ListPullRequestsWithCommit (
463
+ for _ , pr := range prs {
464
+ _ , _ , err = g .client .Issues .CreateComment (
437
465
context .Background (),
438
- u [ 0 ] ,
439
- u [ 1 ] ,
440
- notification . GitHub . revision ,
441
- nil ,
466
+ owner ,
467
+ repo ,
468
+ pr . GetNumber () ,
469
+ comment ,
442
470
)
443
471
if err != nil {
444
472
return err
445
473
}
446
-
447
- for _ , pr := range prs {
448
- _ , _ , err = g .client .Issues .CreateComment (
449
- context .Background (),
450
- u [0 ],
451
- u [1 ],
452
- pr .GetNumber (),
453
- comment ,
454
- )
455
- if err != nil {
456
- return err
457
- }
458
- }
459
474
}
460
475
461
476
return nil
0 commit comments