You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

commit_status.go 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2019 The Gitea Authors.
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package pull
  6. import (
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/git"
  9. "github.com/pkg/errors"
  10. )
  11. // IsCommitStatusContextSuccess returns true if all required status check contexts succeed.
  12. func IsCommitStatusContextSuccess(commitStatuses []*models.CommitStatus, requiredContexts []string) bool {
  13. for _, ctx := range requiredContexts {
  14. var found bool
  15. for _, commitStatus := range commitStatuses {
  16. if commitStatus.Context == ctx {
  17. if commitStatus.State != models.CommitStatusSuccess {
  18. return false
  19. }
  20. found = true
  21. break
  22. }
  23. }
  24. if !found {
  25. return false
  26. }
  27. }
  28. return true
  29. }
  30. // IsPullCommitStatusPass returns if all required status checks PASS
  31. func IsPullCommitStatusPass(pr *models.PullRequest) (bool, error) {
  32. if err := pr.LoadProtectedBranch(); err != nil {
  33. return false, errors.Wrap(err, "GetLatestCommitStatus")
  34. }
  35. if pr.ProtectedBranch == nil || !pr.ProtectedBranch.EnableStatusCheck {
  36. return true, nil
  37. }
  38. // check if all required status checks are successful
  39. headGitRepo, err := git.OpenRepository(pr.HeadRepo.RepoPath())
  40. if err != nil {
  41. return false, errors.Wrap(err, "OpenRepository")
  42. }
  43. if !headGitRepo.IsBranchExist(pr.HeadBranch) {
  44. return false, errors.New("Head branch does not exist, can not merge")
  45. }
  46. sha, err := headGitRepo.GetBranchCommitID(pr.HeadBranch)
  47. if err != nil {
  48. return false, errors.Wrap(err, "GetBranchCommitID")
  49. }
  50. if err := pr.LoadBaseRepo(); err != nil {
  51. return false, errors.Wrap(err, "LoadBaseRepo")
  52. }
  53. commitStatuses, err := models.GetLatestCommitStatus(pr.BaseRepo, sha, 0)
  54. if err != nil {
  55. return false, errors.Wrap(err, "GetLatestCommitStatus")
  56. }
  57. return IsCommitStatusContextSuccess(commitStatuses, pr.ProtectedBranch.StatusCheckContexts), nil
  58. }