2019-09-18 13:39:45 +08:00
|
|
|
// Copyright 2019 The Gitea Authors.
|
|
|
|
|
// All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-09-18 13:39:45 +08:00
|
|
|
|
|
|
|
|
package pull
|
|
|
|
|
|
|
|
|
|
import (
|
2022-01-19 23:26:57 +00:00
|
|
|
"context"
|
2026-01-16 10:31:12 +01:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2022-01-19 23:26:57 +00:00
|
|
|
|
2021-09-24 19:32:56 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2025-06-09 12:05:33 +08:00
|
|
|
"code.gitea.io/gitea/modules/commitstatus"
|
2024-01-28 04:09:51 +08:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2025-09-14 02:01:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/glob"
|
2023-05-17 16:11:13 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-09-18 13:39:45 +08:00
|
|
|
)
|
|
|
|
|
|
2020-01-22 11:46:04 +08:00
|
|
|
// MergeRequiredContextsCommitStatus returns a commit status state for given required contexts
|
2025-06-09 12:05:33 +08:00
|
|
|
func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus, requiredContexts []string) commitstatus.CommitStatusState {
|
|
|
|
|
if len(commitStatuses) == 0 {
|
|
|
|
|
return commitstatus.CommitStatusPending
|
|
|
|
|
}
|
2023-05-17 16:11:13 +08:00
|
|
|
|
2025-06-09 12:05:33 +08:00
|
|
|
if len(requiredContexts) == 0 {
|
|
|
|
|
return git_model.CalcCommitStatus(commitStatuses).State
|
|
|
|
|
}
|
2020-01-22 11:46:04 +08:00
|
|
|
|
2025-06-09 12:05:33 +08:00
|
|
|
requiredContextsGlob := make(map[string]glob.Glob, len(requiredContexts))
|
|
|
|
|
for _, ctx := range requiredContexts {
|
|
|
|
|
if gp, err := glob.Compile(ctx); err != nil {
|
|
|
|
|
log.Error("glob.Compile %s failed. Error: %v", ctx, err)
|
|
|
|
|
} else {
|
|
|
|
|
requiredContextsGlob[ctx] = gp
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-22 11:46:04 +08:00
|
|
|
|
2025-06-09 12:05:33 +08:00
|
|
|
requiredCommitStatuses := make([]*git_model.CommitStatus, 0, len(commitStatuses))
|
2025-06-22 17:31:46 -06:00
|
|
|
allRequiredContextsMatched := true
|
2025-06-09 12:05:33 +08:00
|
|
|
for _, gp := range requiredContextsGlob {
|
2025-06-22 17:31:46 -06:00
|
|
|
requiredContextMatched := false
|
2025-06-09 12:05:33 +08:00
|
|
|
for _, commitStatus := range commitStatuses {
|
|
|
|
|
if gp.Match(commitStatus.Context) {
|
|
|
|
|
requiredCommitStatuses = append(requiredCommitStatuses, commitStatus)
|
2025-06-22 17:31:46 -06:00
|
|
|
requiredContextMatched = true
|
2024-03-08 13:02:13 +08:00
|
|
|
}
|
2020-01-22 11:46:04 +08:00
|
|
|
}
|
2025-06-22 17:31:46 -06:00
|
|
|
allRequiredContextsMatched = allRequiredContextsMatched && requiredContextMatched
|
2023-05-17 16:11:13 +08:00
|
|
|
}
|
2025-06-09 12:05:33 +08:00
|
|
|
if len(requiredCommitStatuses) == 0 {
|
|
|
|
|
return commitstatus.CommitStatusPending
|
|
|
|
|
}
|
2023-05-17 16:11:13 +08:00
|
|
|
|
2025-06-09 12:05:33 +08:00
|
|
|
returnedStatus := git_model.CalcCommitStatus(requiredCommitStatuses).State
|
2025-06-22 17:31:46 -06:00
|
|
|
if allRequiredContextsMatched {
|
2025-06-09 12:05:33 +08:00
|
|
|
return returnedStatus
|
2019-09-30 04:33:40 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-09 12:05:33 +08:00
|
|
|
if returnedStatus == commitstatus.CommitStatusFailure {
|
|
|
|
|
return commitstatus.CommitStatusFailure
|
|
|
|
|
}
|
|
|
|
|
// even if part of success, return pending
|
|
|
|
|
return commitstatus.CommitStatusPending
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsPullCommitStatusPass returns if all required status checks PASS
|
2022-06-13 17:37:59 +08:00
|
|
|
func IsPullCommitStatusPass(ctx context.Context, pr *issues_model.PullRequest) (bool, error) {
|
2023-01-16 16:00:22 +08:00
|
|
|
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
|
|
|
|
|
if err != nil {
|
2026-01-16 10:31:12 +01:00
|
|
|
return false, fmt.Errorf("GetLatestCommitStatus: %w", err)
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|
2023-01-16 16:00:22 +08:00
|
|
|
if pb == nil || !pb.EnableStatusCheck {
|
2019-09-18 13:39:45 +08:00
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-19 23:26:57 +00:00
|
|
|
state, err := GetPullRequestCommitStatusState(ctx, pr)
|
2020-01-22 11:46:04 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
return state.IsSuccess(), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetPullRequestCommitStatusState returns pull request merged commit status state
|
2025-06-09 12:05:33 +08:00
|
|
|
func GetPullRequestCommitStatusState(ctx context.Context, pr *issues_model.PullRequest) (commitstatus.CommitStatusState, error) {
|
2020-02-18 19:34:08 +00:00
|
|
|
// Ensure HeadRepo is loaded
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadHeadRepo(ctx); err != nil {
|
2026-01-16 10:31:12 +01:00
|
|
|
return "", fmt.Errorf("LoadHeadRepo: %w", err)
|
2020-02-18 19:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-18 13:39:45 +08:00
|
|
|
// check if all required status checks are successful
|
2024-01-28 04:09:51 +08:00
|
|
|
headGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo)
|
2019-09-18 13:39:45 +08:00
|
|
|
if err != nil {
|
2026-01-16 10:31:12 +01:00
|
|
|
return "", fmt.Errorf("OpenRepository: %w", err)
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|
2022-01-19 23:26:57 +00:00
|
|
|
defer closer.Close()
|
2019-09-18 13:39:45 +08:00
|
|
|
|
2025-10-25 10:08:25 -07:00
|
|
|
if pr.Flow == issues_model.PullRequestFlowGithub {
|
|
|
|
|
if exist, err := git_model.IsBranchExist(ctx, pr.HeadRepo.ID, pr.HeadBranch); err != nil {
|
2026-01-16 10:31:12 +01:00
|
|
|
return "", fmt.Errorf("IsBranchExist: %w", err)
|
2025-10-25 10:08:25 -07:00
|
|
|
} else if !exist {
|
|
|
|
|
return "", errors.New("Head branch does not exist, can not merge")
|
|
|
|
|
}
|
2021-07-28 17:42:56 +08:00
|
|
|
}
|
2025-07-16 21:33:33 +08:00
|
|
|
if pr.Flow == issues_model.PullRequestFlowAGit && !gitrepo.IsReferenceExist(ctx, pr.HeadRepo, pr.GetGitHeadRefName()) {
|
2020-01-22 11:46:04 +08:00
|
|
|
return "", errors.New("Head branch does not exist, can not merge")
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-28 17:42:56 +08:00
|
|
|
var sha string
|
2022-06-13 17:37:59 +08:00
|
|
|
if pr.Flow == issues_model.PullRequestFlowGithub {
|
2021-07-28 17:42:56 +08:00
|
|
|
sha, err = headGitRepo.GetBranchCommitID(pr.HeadBranch)
|
|
|
|
|
} else {
|
2025-07-16 21:33:33 +08:00
|
|
|
sha, err = headGitRepo.GetRefCommitID(pr.GetGitHeadRefName())
|
2021-07-28 17:42:56 +08:00
|
|
|
}
|
2019-09-18 13:39:45 +08:00
|
|
|
if err != nil {
|
2021-07-28 17:42:56 +08:00
|
|
|
return "", err
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadBaseRepo(ctx); err != nil {
|
2026-01-16 10:31:12 +01:00
|
|
|
return "", fmt.Errorf("LoadBaseRepo: %w", err)
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-27 03:00:22 +08:00
|
|
|
commitStatuses, err := git_model.GetLatestCommitStatus(ctx, pr.BaseRepo.ID, sha, db.ListOptionsAll)
|
2019-09-18 13:39:45 +08:00
|
|
|
if err != nil {
|
2026-01-16 10:31:12 +01:00
|
|
|
return "", fmt.Errorf("GetLatestCommitStatus: %w", err)
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 16:00:22 +08:00
|
|
|
pb, err := git_model.GetFirstMatchProtectedBranchRule(ctx, pr.BaseRepoID, pr.BaseBranch)
|
|
|
|
|
if err != nil {
|
2026-01-16 10:31:12 +01:00
|
|
|
return "", fmt.Errorf("LoadProtectedBranch: %w", err)
|
2022-05-07 19:05:52 +02:00
|
|
|
}
|
|
|
|
|
var requiredContexts []string
|
2023-01-16 16:00:22 +08:00
|
|
|
if pb != nil {
|
|
|
|
|
requiredContexts = pb.StatusCheckContexts
|
2022-05-07 19:05:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return MergeRequiredContextsCommitStatus(commitStatuses, requiredContexts), nil
|
2019-09-18 13:39:45 +08:00
|
|
|
}
|