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.

branches.go 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/Unknwon/com"
  13. )
  14. const (
  15. // ProtectedBranchRepoID protected Repo ID
  16. ProtectedBranchRepoID = "GITEA_REPO_ID"
  17. )
  18. // ProtectedBranch struct
  19. type ProtectedBranch struct {
  20. ID int64 `xorm:"pk autoincr"`
  21. RepoID int64 `xorm:"UNIQUE(s)"`
  22. BranchName string `xorm:"UNIQUE(s)"`
  23. CanPush bool `xorm:"NOT NULL DEFAULT false"`
  24. EnableWhitelist bool
  25. WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
  26. WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
  27. Created time.Time `xorm:"-"`
  28. CreatedUnix int64 `xorm:"created"`
  29. Updated time.Time `xorm:"-"`
  30. UpdatedUnix int64 `xorm:"updated"`
  31. }
  32. // IsProtected returns if the branch is protected
  33. func (protectBranch *ProtectedBranch) IsProtected() bool {
  34. return protectBranch.ID > 0
  35. }
  36. // CanUserPush returns if some user could push to this protected branch
  37. func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
  38. if !protectBranch.EnableWhitelist {
  39. return false
  40. }
  41. if base.Int64sContains(protectBranch.WhitelistUserIDs, userID) {
  42. return true
  43. }
  44. if len(protectBranch.WhitelistTeamIDs) == 0 {
  45. return false
  46. }
  47. in, err := IsUserInTeams(userID, protectBranch.WhitelistTeamIDs)
  48. if err != nil {
  49. log.Error(1, "IsUserInTeams:", err)
  50. return false
  51. }
  52. return in
  53. }
  54. // GetProtectedBranchByRepoID getting protected branch by repo ID
  55. func GetProtectedBranchByRepoID(RepoID int64) ([]*ProtectedBranch, error) {
  56. protectedBranches := make([]*ProtectedBranch, 0)
  57. return protectedBranches, x.Where("repo_id = ?", RepoID).Desc("updated_unix").Find(&protectedBranches)
  58. }
  59. // GetProtectedBranchBy getting protected branch by ID/Name
  60. func GetProtectedBranchBy(repoID int64, BranchName string) (*ProtectedBranch, error) {
  61. rel := &ProtectedBranch{RepoID: repoID, BranchName: strings.ToLower(BranchName)}
  62. has, err := x.Get(rel)
  63. if err != nil {
  64. return nil, err
  65. }
  66. if !has {
  67. return nil, nil
  68. }
  69. return rel, nil
  70. }
  71. // GetProtectedBranchByID getting protected branch by ID
  72. func GetProtectedBranchByID(id int64) (*ProtectedBranch, error) {
  73. rel := &ProtectedBranch{ID: id}
  74. has, err := x.Get(rel)
  75. if err != nil {
  76. return nil, err
  77. }
  78. if !has {
  79. return nil, nil
  80. }
  81. return rel, nil
  82. }
  83. // UpdateProtectBranch saves branch protection options of repository.
  84. // If ID is 0, it creates a new record. Otherwise, updates existing record.
  85. // This function also performs check if whitelist user and team's IDs have been changed
  86. // to avoid unnecessary whitelist delete and regenerate.
  87. func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, whitelistUserIDs, whitelistTeamIDs []int64) (err error) {
  88. if err = repo.GetOwner(); err != nil {
  89. return fmt.Errorf("GetOwner: %v", err)
  90. }
  91. hasUsersChanged := !util.IsSliceInt64Eq(protectBranch.WhitelistUserIDs, whitelistUserIDs)
  92. if hasUsersChanged {
  93. protectBranch.WhitelistUserIDs = make([]int64, 0, len(whitelistUserIDs))
  94. for _, userID := range whitelistUserIDs {
  95. has, err := hasAccess(x, userID, repo, AccessModeWrite)
  96. if err != nil {
  97. return fmt.Errorf("HasAccess [user_id: %d, repo_id: %d]: %v", userID, protectBranch.RepoID, err)
  98. } else if !has {
  99. continue // Drop invalid user ID
  100. }
  101. protectBranch.WhitelistUserIDs = append(protectBranch.WhitelistUserIDs, userID)
  102. }
  103. }
  104. // if the repo is in an orgniziation
  105. hasTeamsChanged := !util.IsSliceInt64Eq(protectBranch.WhitelistTeamIDs, whitelistTeamIDs)
  106. if hasTeamsChanged {
  107. teams, err := GetTeamsWithAccessToRepo(repo.OwnerID, repo.ID, AccessModeWrite)
  108. if err != nil {
  109. return fmt.Errorf("GetTeamsWithAccessToRepo [org_id: %d, repo_id: %d]: %v", repo.OwnerID, repo.ID, err)
  110. }
  111. protectBranch.WhitelistTeamIDs = make([]int64, 0, len(teams))
  112. for i := range teams {
  113. if teams[i].HasWriteAccess() && com.IsSliceContainsInt64(whitelistTeamIDs, teams[i].ID) {
  114. protectBranch.WhitelistTeamIDs = append(protectBranch.WhitelistTeamIDs, teams[i].ID)
  115. }
  116. }
  117. }
  118. // Make sure protectBranch.ID is not 0 for whitelists
  119. if protectBranch.ID == 0 {
  120. if _, err = x.Insert(protectBranch); err != nil {
  121. return fmt.Errorf("Insert: %v", err)
  122. }
  123. return nil
  124. }
  125. if _, err = x.Id(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
  126. return fmt.Errorf("Update: %v", err)
  127. }
  128. return nil
  129. }
  130. // GetProtectedBranches get all protected branches
  131. func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) {
  132. protectedBranches := make([]*ProtectedBranch, 0)
  133. return protectedBranches, x.Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
  134. }
  135. // IsProtectedBranch checks if branch is protected
  136. func (repo *Repository) IsProtectedBranch(branchName string, doer *User) (bool, error) {
  137. protectedBranch := &ProtectedBranch{
  138. RepoID: repo.ID,
  139. BranchName: branchName,
  140. }
  141. has, err := x.Get(protectedBranch)
  142. if err != nil {
  143. return true, err
  144. } else if has {
  145. return !protectedBranch.CanUserPush(doer.ID), nil
  146. }
  147. return false, nil
  148. }
  149. // DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
  150. func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {
  151. protectedBranch := &ProtectedBranch{
  152. RepoID: repo.ID,
  153. ID: id,
  154. }
  155. sess := x.NewSession()
  156. defer sess.Close()
  157. if err = sess.Begin(); err != nil {
  158. return err
  159. }
  160. if affected, err := sess.Delete(protectedBranch); err != nil {
  161. return err
  162. } else if affected != 1 {
  163. return fmt.Errorf("delete protected branch ID(%v) failed", id)
  164. }
  165. return sess.Commit()
  166. }