Skip to content

fix gitlab automatic deletion bug #1905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ee/backend/controllers/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab
isDraft := payload.ObjectAttributes.WorkInProgress
branch := payload.ObjectAttributes.SourceBranch
commitSha := payload.ObjectAttributes.LastCommit.ID
action := payload.ObjectAttributes.Action
//defaultBranch := payload.Repository.DefaultBranch
//actor := payload.User.Username
//discussionId := ""
Expand All @@ -208,6 +209,30 @@ func handlePullRequestEvent(gitlabProvider utils.GitlabProvider, payload *gitlab
log.Printf("GetGithubService error: %v", glerr)
return fmt.Errorf("error getting ghService to post error comment")
}

// here we check if pr was merged and automatic deletion is enabled, to avoid errors when
// pr is merged and the branch does not exist we handle that gracefully
if action == "merge" {
sourceBranch, _, err := glService.GetBranchName(prNumber)
if err != nil {
utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: Could not retrieve PR details, error: %v", err))
log.Printf("Could not retrieve PR details error: %v", err)
return fmt.Errorf("Could not retrieve PR details: %v", err)
}

branchExists, err := glService.CheckBranchExists(sourceBranch)
if err != nil {
utils.InitCommentReporter(glService, prNumber, fmt.Sprintf(":x: Could not check if branch exists, error: %v", err))
log.Printf("Could not check if branch exists, error: %v", err)
return fmt.Errorf("Could not check if branch exists: %v", err)

}
if !branchExists {
log.Printf("automatic branch deletion is configured, ignoring pr closed event")
return nil
}
}

comment, err := glService.PublishComment(prNumber, fmt.Sprintf("Report for pull request (%v)", commitSha))
discussionId := comment.DiscussionId

Expand Down
23 changes: 22 additions & 1 deletion libs/ci/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,28 @@ func (gitlabService GitLabService) GetApprovals(prNumber int) ([]string, error)

func (gitlabService GitLabService) GetBranchName(prNumber int) (string, string, error) {
//TODO implement me
return "", "", nil
projectId := *gitlabService.Context.ProjectId
log.Printf("CheckBranchExists prNumber : %d, projectId: %d \n", prNumber, projectId)
options := go_gitlab.GetMergeRequestsOptions{}
pr, _, err := gitlabService.Client.MergeRequests.GetMergeRequest(projectId, prNumber, &options)
if err != nil {
log.Printf("error while getting branch name for pr: %v", err)
return "", "", err
}
return pr.SourceBranch, pr.SHA, nil
}

func (gitlabService GitLabService) CheckBranchExists(branchName string) (bool, error) {
projectId := *gitlabService.Context.ProjectId
log.Printf("CheckBranchExists branchName : %v, projectId: %d \n", branchName, projectId)
_, resp, err := gitlabService.Client.Branches.GetBranch(projectId, branchName)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
return false, nil
}
return false, err
}
return true, nil
}

func (svc GitLabService) SetOutput(prNumber int, key string, value string) error {
Expand Down
Loading