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

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