Skip to content

Commit 4dae08d

Browse files
authored
handle automating branch deletion (#1659)
1 parent 2c989f6 commit 4dae08d

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

backend/controllers/github.go

+29-5
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
468468
isDraft := payload.PullRequest.GetDraft()
469469
commitSha := payload.PullRequest.Head.GetSHA()
470470
branch := payload.PullRequest.Head.GetRef()
471+
action := *payload.Action
471472

472473
link, err := models.DB.GetGithubAppInstallationLink(installationId)
473474
if err != nil {
@@ -476,13 +477,36 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
476477
}
477478
organisationId := link.OrganisationId
478479

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)
482490
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)
485494
}
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 {
486510
utils.InitCommentReporter(ghService, prNumber, fmt.Sprintf(":x: Could not load digger config, error: %v", err))
487511
log.Printf("getDiggerConfigForPR error: %v", err)
488512
return fmt.Errorf("error getting digger config")

libs/ci/github/github.go

+11
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ func (svc GithubService) GetBranchName(prNumber int) (string, string, error) {
334334
return pr.Head.GetRef(), pr.Head.GetSHA(), nil
335335
}
336336

337+
func (svc GithubService) CheckBranchExists(branchName string) (bool, error) {
338+
_, resp, err := svc.Client.Repositories.GetBranch(context.Background(), svc.Owner, svc.RepoName, branchName, 3)
339+
if err != nil {
340+
if resp != nil && resp.StatusCode == 404 {
341+
return false, nil
342+
}
343+
return false, err
344+
}
345+
return true, nil
346+
}
347+
337348
func ConvertGithubPullRequestEventToJobs(payload *github.PullRequestEvent, impactedProjects []digger_config.Project, requestedProject *digger_config.Project, config digger_config.DiggerConfig) ([]scheduler.Job, bool, error) {
338349
workflows := config.Workflows
339350
jobs := make([]scheduler.Job, 0)

0 commit comments

Comments
 (0)