@@ -468,6 +468,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
468
468
isDraft := payload .PullRequest .GetDraft ()
469
469
commitSha := payload .PullRequest .Head .GetSHA ()
470
470
branch := payload .PullRequest .Head .GetRef ()
471
+ action := * payload .Action
471
472
472
473
link , err := models .DB .GetGithubAppInstallationLink (installationId )
473
474
if err != nil {
@@ -476,13 +477,36 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
476
477
}
477
478
organisationId := link .OrganisationId
478
479
479
- diggerYmlStr , ghService , config , projectsGraph , _ , _ , err := getDiggerConfigForPR (gh , installationId , repoFullName , repoOwner , repoName , cloneURL , prNumber )
480
- if err != nil {
481
- ghService , _ , err := utils .GetGithubService (gh , installationId , repoFullName , repoOwner , repoName )
480
+ ghService , _ , ghServiceErr := utils .GetGithubService (gh , installationId , repoFullName , repoOwner , repoName )
481
+ if ghServiceErr != nil {
482
+ log .Printf ("GetGithubService error: %v" , err )
483
+ return fmt .Errorf ("error getting ghService to post error comment" )
484
+ }
485
+
486
+ // here we check if pr was closed and automatic deletion is enabled, to avoid errors when
487
+ // pr is merged and the branch does not exist we handle that gracefully
488
+ if action == "closed" {
489
+ branchName , _ , err := ghService .GetBranchName (prNumber )
482
490
if err != nil {
483
- log .Printf ("GetGithubService error: %v" , err )
484
- return fmt .Errorf ("error getting ghService to post error comment" )
491
+ utils .InitCommentReporter (ghService , prNumber , fmt .Sprintf (":x: Could not retrive PR details, error: %v" , err ))
492
+ log .Printf ("Could not retrive PR details error: %v" , err )
493
+ return fmt .Errorf ("Could not retrive PR details: %v" , err )
485
494
}
495
+ branchExists , err := ghService .CheckBranchExists (branchName )
496
+ if err != nil {
497
+ utils .InitCommentReporter (ghService , prNumber , fmt .Sprintf (":x: Could not check if branch exists, error: %v" , err ))
498
+ log .Printf ("Could not check if branch exists, error: %v" , err )
499
+ return fmt .Errorf ("Could not check if branch exists: %v" , err )
500
+
501
+ }
502
+ if ! branchExists {
503
+ log .Printf ("automating branch deletion is configured, ignoring pr closed event" )
504
+ return nil
505
+ }
506
+ }
507
+
508
+ diggerYmlStr , ghService , config , projectsGraph , _ , _ , err := getDiggerConfigForPR (gh , installationId , repoFullName , repoOwner , repoName , cloneURL , prNumber )
509
+ if err != nil {
486
510
utils .InitCommentReporter (ghService , prNumber , fmt .Sprintf (":x: Could not load digger config, error: %v" , err ))
487
511
log .Printf ("getDiggerConfigForPR error: %v" , err )
488
512
return fmt .Errorf ("error getting digger config" )
0 commit comments