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

branches.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. "context"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/timeutil"
  14. "code.gitea.io/gitea/modules/util"
  15. "github.com/gobwas/glob"
  16. "github.com/unknwon/com"
  17. )
  18. const (
  19. // ProtectedBranchRepoID protected Repo ID
  20. ProtectedBranchRepoID = "GITEA_REPO_ID"
  21. // ProtectedBranchPRID protected Repo PR ID
  22. ProtectedBranchPRID = "GITEA_PR_ID"
  23. )
  24. // ProtectedBranch struct
  25. type ProtectedBranch struct {
  26. ID int64 `xorm:"pk autoincr"`
  27. RepoID int64 `xorm:"UNIQUE(s)"`
  28. BranchName string `xorm:"UNIQUE(s)"`
  29. CanPush bool `xorm:"NOT NULL DEFAULT false"`
  30. EnableWhitelist bool
  31. WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
  32. WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
  33. EnableMergeWhitelist bool `xorm:"NOT NULL DEFAULT false"`
  34. WhitelistDeployKeys bool `xorm:"NOT NULL DEFAULT false"`
  35. MergeWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
  36. MergeWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
  37. EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
  38. StatusCheckContexts []string `xorm:"JSON TEXT"`
  39. EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"`
  40. ApprovalsWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
  41. ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
  42. RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
  43. BlockOnRejectedReviews bool `xorm:"NOT NULL DEFAULT false"`
  44. DismissStaleApprovals bool `xorm:"NOT NULL DEFAULT false"`
  45. RequireSignedCommits bool `xorm:"NOT NULL DEFAULT false"`
  46. ProtectedFilePatterns string `xorm:"TEXT"`
  47. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  48. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  49. }
  50. // IsProtected returns if the branch is protected
  51. func (protectBranch *ProtectedBranch) IsProtected() bool {
  52. return protectBranch.ID > 0
  53. }
  54. // CanUserPush returns if some user could push to this protected branch
  55. func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
  56. if !protectBranch.CanPush {
  57. return false
  58. }
  59. if !protectBranch.EnableWhitelist {
  60. if user, err := GetUserByID(userID); err != nil {
  61. log.Error("GetUserByID: %v", err)
  62. return false
  63. } else if repo, err := GetRepositoryByID(protectBranch.RepoID); err != nil {
  64. log.Error("GetRepositoryByID: %v", err)
  65. return false
  66. } else if writeAccess, err := HasAccessUnit(user, repo, UnitTypeCode, AccessModeWrite); err != nil {
  67. log.Error("HasAccessUnit: %v", err)
  68. return false
  69. } else {
  70. return writeAccess
  71. }
  72. }
  73. if base.Int64sContains(protectBranch.WhitelistUserIDs, userID) {
  74. return true
  75. }
  76. if len(protectBranch.WhitelistTeamIDs) == 0 {
  77. return false
  78. }
  79. in, err := IsUserInTeams(userID, protectBranch.WhitelistTeamIDs)
  80. if err != nil {
  81. log.Error("IsUserInTeams: %v", err)
  82. return false
  83. }
  84. return in
  85. }
  86. // IsUserMergeWhitelisted checks if some user is whitelisted to merge to this branch
  87. func (protectBranch *ProtectedBranch) IsUserMergeWhitelisted(userID int64) bool {
  88. if !protectBranch.EnableMergeWhitelist {
  89. return true
  90. }
  91. if base.Int64sContains(protectBranch.MergeWhitelistUserIDs, userID) {
  92. return true
  93. }
  94. if len(protectBranch.MergeWhitelistTeamIDs) == 0 {
  95. return false
  96. }
  97. in, err := IsUserInTeams(userID, protectBranch.MergeWhitelistTeamIDs)
  98. if err != nil {
  99. log.Error("IsUserInTeams: %v", err)
  100. return false
  101. }
  102. return in
  103. }
  104. // IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
  105. func (protectBranch *ProtectedBranch) IsUserOfficialReviewer(user *User) (bool, error) {
  106. return protectBranch.isUserOfficialReviewer(x, user)
  107. }
  108. func (protectBranch *ProtectedBranch) isUserOfficialReviewer(e Engine, user *User) (bool, error) {
  109. repo, err := getRepositoryByID(e, protectBranch.RepoID)
  110. if err != nil {
  111. return false, err
  112. }
  113. if !protectBranch.EnableApprovalsWhitelist {
  114. // Anyone with write access is considered official reviewer
  115. writeAccess, err := hasAccessUnit(e, user, repo, UnitTypeCode, AccessModeWrite)
  116. if err != nil {
  117. return false, err
  118. }
  119. return writeAccess, nil
  120. }
  121. if base.Int64sContains(protectBranch.ApprovalsWhitelistUserIDs, user.ID) {
  122. return true, nil
  123. }
  124. inTeam, err := isUserInTeams(e, user.ID, protectBranch.ApprovalsWhitelistTeamIDs)
  125. if err != nil {
  126. return false, err
  127. }
  128. return inTeam, nil
  129. }
  130. // HasEnoughApprovals returns true if pr has enough granted approvals.
  131. func (protectBranch *ProtectedBranch) HasEnoughApprovals(pr *PullRequest) bool {
  132. if protectBranch.RequiredApprovals == 0 {
  133. return true
  134. }
  135. return protectBranch.GetGrantedApprovalsCount(pr) >= protectBranch.RequiredApprovals
  136. }
  137. // GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
  138. func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest) int64 {
  139. sess := x.Where("issue_id = ?", pr.IssueID).
  140. And("type = ?", ReviewTypeApprove).
  141. And("official = ?", true)
  142. if protectBranch.DismissStaleApprovals {
  143. sess = sess.And("stale = ?", false)
  144. }
  145. approvals, err := sess.Count(new(Review))
  146. if err != nil {
  147. log.Error("GetGrantedApprovalsCount: %v", err)
  148. return 0
  149. }
  150. return approvals
  151. }
  152. // MergeBlockedByRejectedReview returns true if merge is blocked by rejected reviews
  153. func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(pr *PullRequest) bool {
  154. if !protectBranch.BlockOnRejectedReviews {
  155. return false
  156. }
  157. rejectExist, err := x.Where("issue_id = ?", pr.IssueID).
  158. And("type = ?", ReviewTypeReject).
  159. And("official = ?", true).
  160. Exist(new(Review))
  161. if err != nil {
  162. log.Error("MergeBlockedByRejectedReview: %v", err)
  163. return true
  164. }
  165. return rejectExist
  166. }
  167. // GetProtectedFilePatterns parses a semicolon separated list of protected file patterns and returns a glob.Glob slice
  168. func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
  169. extarr := make([]glob.Glob, 0, 10)
  170. for _, expr := range strings.Split(strings.ToLower(protectBranch.ProtectedFilePatterns), ";") {
  171. expr = strings.TrimSpace(expr)
  172. if expr != "" {
  173. if g, err := glob.Compile(expr, '.', '/'); err != nil {
  174. log.Info("Invalid glob expresion '%s' (skipped): %v", expr, err)
  175. } else {
  176. extarr = append(extarr, g)
  177. }
  178. }
  179. }
  180. return extarr
  181. }
  182. // GetProtectedBranchByRepoID getting protected branch by repo ID
  183. func GetProtectedBranchByRepoID(repoID int64) ([]*ProtectedBranch, error) {
  184. protectedBranches := make([]*ProtectedBranch, 0)
  185. return protectedBranches, x.Where("repo_id = ?", repoID).Desc("updated_unix").Find(&protectedBranches)
  186. }
  187. // GetProtectedBranchBy getting protected branch by ID/Name
  188. func GetProtectedBranchBy(repoID int64, branchName string) (*ProtectedBranch, error) {
  189. return getProtectedBranchBy(x, repoID, branchName)
  190. }
  191. func getProtectedBranchBy(e Engine, repoID int64, branchName string) (*ProtectedBranch, error) {
  192. rel := &ProtectedBranch{RepoID: repoID, BranchName: branchName}
  193. has, err := e.Get(rel)
  194. if err != nil {
  195. return nil, err
  196. }
  197. if !has {
  198. return nil, nil
  199. }
  200. return rel, nil
  201. }
  202. // GetProtectedBranchByID getting protected branch by ID
  203. func GetProtectedBranchByID(id int64) (*ProtectedBranch, error) {
  204. rel := &ProtectedBranch{ID: id}
  205. has, err := x.Get(rel)
  206. if err != nil {
  207. return nil, err
  208. }
  209. if !has {
  210. return nil, nil
  211. }
  212. return rel, nil
  213. }
  214. // WhitelistOptions represent all sorts of whitelists used for protected branches
  215. type WhitelistOptions struct {
  216. UserIDs []int64
  217. TeamIDs []int64
  218. MergeUserIDs []int64
  219. MergeTeamIDs []int64
  220. ApprovalsUserIDs []int64
  221. ApprovalsTeamIDs []int64
  222. }
  223. // UpdateProtectBranch saves branch protection options of repository.
  224. // If ID is 0, it creates a new record. Otherwise, updates existing record.
  225. // This function also performs check if whitelist user and team's IDs have been changed
  226. // to avoid unnecessary whitelist delete and regenerate.
  227. func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, opts WhitelistOptions) (err error) {
  228. if err = repo.GetOwner(); err != nil {
  229. return fmt.Errorf("GetOwner: %v", err)
  230. }
  231. whitelist, err := updateUserWhitelist(repo, protectBranch.WhitelistUserIDs, opts.UserIDs)
  232. if err != nil {
  233. return err
  234. }
  235. protectBranch.WhitelistUserIDs = whitelist
  236. whitelist, err = updateUserWhitelist(repo, protectBranch.MergeWhitelistUserIDs, opts.MergeUserIDs)
  237. if err != nil {
  238. return err
  239. }
  240. protectBranch.MergeWhitelistUserIDs = whitelist
  241. whitelist, err = updateApprovalWhitelist(repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
  242. if err != nil {
  243. return err
  244. }
  245. protectBranch.ApprovalsWhitelistUserIDs = whitelist
  246. // if the repo is in an organization
  247. whitelist, err = updateTeamWhitelist(repo, protectBranch.WhitelistTeamIDs, opts.TeamIDs)
  248. if err != nil {
  249. return err
  250. }
  251. protectBranch.WhitelistTeamIDs = whitelist
  252. whitelist, err = updateTeamWhitelist(repo, protectBranch.MergeWhitelistTeamIDs, opts.MergeTeamIDs)
  253. if err != nil {
  254. return err
  255. }
  256. protectBranch.MergeWhitelistTeamIDs = whitelist
  257. whitelist, err = updateTeamWhitelist(repo, protectBranch.ApprovalsWhitelistTeamIDs, opts.ApprovalsTeamIDs)
  258. if err != nil {
  259. return err
  260. }
  261. protectBranch.ApprovalsWhitelistTeamIDs = whitelist
  262. // Make sure protectBranch.ID is not 0 for whitelists
  263. if protectBranch.ID == 0 {
  264. if _, err = x.Insert(protectBranch); err != nil {
  265. return fmt.Errorf("Insert: %v", err)
  266. }
  267. return nil
  268. }
  269. if _, err = x.ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
  270. return fmt.Errorf("Update: %v", err)
  271. }
  272. return nil
  273. }
  274. // GetProtectedBranches get all protected branches
  275. func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) {
  276. protectedBranches := make([]*ProtectedBranch, 0)
  277. return protectedBranches, x.Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
  278. }
  279. // GetBranchProtection get the branch protection of a branch
  280. func (repo *Repository) GetBranchProtection(branchName string) (*ProtectedBranch, error) {
  281. return GetProtectedBranchBy(repo.ID, branchName)
  282. }
  283. // IsProtectedBranch checks if branch is protected
  284. func (repo *Repository) IsProtectedBranch(branchName string, doer *User) (bool, error) {
  285. if doer == nil {
  286. return true, nil
  287. }
  288. protectedBranch := &ProtectedBranch{
  289. RepoID: repo.ID,
  290. BranchName: branchName,
  291. }
  292. has, err := x.Exist(protectedBranch)
  293. if err != nil {
  294. return true, err
  295. }
  296. return has, nil
  297. }
  298. // IsProtectedBranchForPush checks if branch is protected for push
  299. func (repo *Repository) IsProtectedBranchForPush(branchName string, doer *User) (bool, error) {
  300. if doer == nil {
  301. return true, nil
  302. }
  303. protectedBranch := &ProtectedBranch{
  304. RepoID: repo.ID,
  305. BranchName: branchName,
  306. }
  307. has, err := x.Get(protectedBranch)
  308. if err != nil {
  309. return true, err
  310. } else if has {
  311. return !protectedBranch.CanUserPush(doer.ID), nil
  312. }
  313. return false, nil
  314. }
  315. // updateApprovalWhitelist checks whether the user whitelist changed and returns a whitelist with
  316. // the users from newWhitelist which have explicit read or write access to the repo.
  317. func updateApprovalWhitelist(repo *Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
  318. hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
  319. if !hasUsersChanged {
  320. return currentWhitelist, nil
  321. }
  322. whitelist = make([]int64, 0, len(newWhitelist))
  323. for _, userID := range newWhitelist {
  324. if reader, err := repo.IsReader(userID); err != nil {
  325. return nil, err
  326. } else if !reader {
  327. continue
  328. }
  329. whitelist = append(whitelist, userID)
  330. }
  331. return
  332. }
  333. // updateUserWhitelist checks whether the user whitelist changed and returns a whitelist with
  334. // the users from newWhitelist which have write access to the repo.
  335. func updateUserWhitelist(repo *Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
  336. hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
  337. if !hasUsersChanged {
  338. return currentWhitelist, nil
  339. }
  340. whitelist = make([]int64, 0, len(newWhitelist))
  341. for _, userID := range newWhitelist {
  342. user, err := GetUserByID(userID)
  343. if err != nil {
  344. return nil, fmt.Errorf("GetUserByID [user_id: %d, repo_id: %d]: %v", userID, repo.ID, err)
  345. }
  346. perm, err := GetUserRepoPermission(repo, user)
  347. if err != nil {
  348. return nil, fmt.Errorf("GetUserRepoPermission [user_id: %d, repo_id: %d]: %v", userID, repo.ID, err)
  349. }
  350. if !perm.CanWrite(UnitTypeCode) {
  351. continue // Drop invalid user ID
  352. }
  353. whitelist = append(whitelist, userID)
  354. }
  355. return
  356. }
  357. // updateTeamWhitelist checks whether the team whitelist changed and returns a whitelist with
  358. // the teams from newWhitelist which have write access to the repo.
  359. func updateTeamWhitelist(repo *Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
  360. hasTeamsChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
  361. if !hasTeamsChanged {
  362. return currentWhitelist, nil
  363. }
  364. teams, err := GetTeamsWithAccessToRepo(repo.OwnerID, repo.ID, AccessModeRead)
  365. if err != nil {
  366. return nil, fmt.Errorf("GetTeamsWithAccessToRepo [org_id: %d, repo_id: %d]: %v", repo.OwnerID, repo.ID, err)
  367. }
  368. whitelist = make([]int64, 0, len(teams))
  369. for i := range teams {
  370. if com.IsSliceContainsInt64(newWhitelist, teams[i].ID) {
  371. whitelist = append(whitelist, teams[i].ID)
  372. }
  373. }
  374. return
  375. }
  376. // DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
  377. func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {
  378. protectedBranch := &ProtectedBranch{
  379. RepoID: repo.ID,
  380. ID: id,
  381. }
  382. sess := x.NewSession()
  383. defer sess.Close()
  384. if err = sess.Begin(); err != nil {
  385. return err
  386. }
  387. if affected, err := sess.Delete(protectedBranch); err != nil {
  388. return err
  389. } else if affected != 1 {
  390. return fmt.Errorf("delete protected branch ID(%v) failed", id)
  391. }
  392. return sess.Commit()
  393. }
  394. // DeletedBranch struct
  395. type DeletedBranch struct {
  396. ID int64 `xorm:"pk autoincr"`
  397. RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
  398. Name string `xorm:"UNIQUE(s) NOT NULL"`
  399. Commit string `xorm:"UNIQUE(s) NOT NULL"`
  400. DeletedByID int64 `xorm:"INDEX"`
  401. DeletedBy *User `xorm:"-"`
  402. DeletedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  403. }
  404. // AddDeletedBranch adds a deleted branch to the database
  405. func (repo *Repository) AddDeletedBranch(branchName, commit string, deletedByID int64) error {
  406. deletedBranch := &DeletedBranch{
  407. RepoID: repo.ID,
  408. Name: branchName,
  409. Commit: commit,
  410. DeletedByID: deletedByID,
  411. }
  412. sess := x.NewSession()
  413. defer sess.Close()
  414. if err := sess.Begin(); err != nil {
  415. return err
  416. }
  417. if _, err := sess.InsertOne(deletedBranch); err != nil {
  418. return err
  419. }
  420. return sess.Commit()
  421. }
  422. // GetDeletedBranches returns all the deleted branches
  423. func (repo *Repository) GetDeletedBranches() ([]*DeletedBranch, error) {
  424. deletedBranches := make([]*DeletedBranch, 0)
  425. return deletedBranches, x.Where("repo_id = ?", repo.ID).Desc("deleted_unix").Find(&deletedBranches)
  426. }
  427. // GetDeletedBranchByID get a deleted branch by its ID
  428. func (repo *Repository) GetDeletedBranchByID(ID int64) (*DeletedBranch, error) {
  429. deletedBranch := &DeletedBranch{ID: ID}
  430. has, err := x.Get(deletedBranch)
  431. if err != nil {
  432. return nil, err
  433. }
  434. if !has {
  435. return nil, nil
  436. }
  437. return deletedBranch, nil
  438. }
  439. // RemoveDeletedBranch removes a deleted branch from the database
  440. func (repo *Repository) RemoveDeletedBranch(id int64) (err error) {
  441. deletedBranch := &DeletedBranch{
  442. RepoID: repo.ID,
  443. ID: id,
  444. }
  445. sess := x.NewSession()
  446. defer sess.Close()
  447. if err = sess.Begin(); err != nil {
  448. return err
  449. }
  450. if affected, err := sess.Delete(deletedBranch); err != nil {
  451. return err
  452. } else if affected != 1 {
  453. return fmt.Errorf("remove deleted branch ID(%v) failed", id)
  454. }
  455. return sess.Commit()
  456. }
  457. // LoadUser loads the user that deleted the branch
  458. // When there's no user found it returns a NewGhostUser
  459. func (deletedBranch *DeletedBranch) LoadUser() {
  460. user, err := GetUserByID(deletedBranch.DeletedByID)
  461. if err != nil {
  462. user = NewGhostUser()
  463. }
  464. deletedBranch.DeletedBy = user
  465. }
  466. // RemoveDeletedBranch removes all deleted branches
  467. func RemoveDeletedBranch(repoID int64, branch string) error {
  468. _, err := x.Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))
  469. return err
  470. }
  471. // RemoveOldDeletedBranches removes old deleted branches
  472. func RemoveOldDeletedBranches(ctx context.Context) {
  473. // Nothing to do for shutdown or terminate
  474. log.Trace("Doing: DeletedBranchesCleanup")
  475. deleteBefore := time.Now().Add(-setting.Cron.DeletedBranchesCleanup.OlderThan)
  476. _, err := x.Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch))
  477. if err != nil {
  478. log.Error("DeletedBranchesCleanup: %v", err)
  479. }
  480. }