@@ -197,6 +197,7 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab
197197 isDraft := payload .ObjectAttributes .WorkInProgress
198198 branch := payload .ObjectAttributes .SourceBranch
199199 commitSha := payload .ObjectAttributes .LastCommit .ID
200+ action := payload .ObjectAttributes .Action
200201 //defaultBranch := payload.Repository.DefaultBranch
201202 //actor := payload.User.Username
202203 //discussionId := ""
@@ -208,6 +209,30 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab
208209 log .Printf ("GetGithubService error: %v" , glerr )
209210 return fmt .Errorf ("error getting ghService to post error comment" )
210211 }
212+
213+ // here we check if pr was merged and automatic deletion is enabled, to avoid errors when
214+ // pr is merged and the branch does not exist we handle that gracefully
215+ if action == "merge" {
216+ sourceBranch , _ , err := glService .GetBranchName (prNumber )
217+ if err != nil {
218+ utils .InitCommentReporter (glService , prNumber , fmt .Sprintf (":x: Could not retrieve PR details, error: %v" , err ))
219+ log .Printf ("Could not retrieve PR details error: %v" , err )
220+ return fmt .Errorf ("Could not retrieve PR details: %v" , err )
221+ }
222+
223+ branchExists , err := glService .CheckBranchExists (sourceBranch )
224+ if err != nil {
225+ utils .InitCommentReporter (glService , prNumber , fmt .Sprintf (":x: Could not check if branch exists, error: %v" , err ))
226+ log .Printf ("Could not check if branch exists, error: %v" , err )
227+ return fmt .Errorf ("Could not check if branch exists: %v" , err )
228+
229+ }
230+ if ! branchExists {
231+ log .Printf ("automatic branch deletion is configured, ignoring pr closed event" )
232+ return nil
233+ }
234+ }
235+
211236 comment , err := glService .PublishComment (prNumber , fmt .Sprintf ("Report for pull request (%v)" , commitSha ))
212237 discussionId := comment .DiscussionId
213238
0 commit comments