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/timeutil"
  13. "code.gitea.io/gitea/modules/util"
  14. "github.com/gobwas/glob"
  15. )
  16. // ProtectedBranch struct
  17. type ProtectedBranch struct {
  18. ID int64 `xorm:"pk autoincr"`
  19. RepoID int64 `xorm:"UNIQUE(s)"`
  20. BranchName string `xorm:"UNIQUE(s)"`
  21. CanPush bool `xorm:"NOT NULL DEFAULT false"`
  22. EnableWhitelist bool
  23. WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
  24. WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
  25. EnableMergeWhitelist bool `xorm:"NOT NULL DEFAULT false"`
  26. WhitelistDeployKeys bool `xorm:"NOT NULL DEFAULT false"`
  27. MergeWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
  28. MergeWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
  29. EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
  30. StatusCheckContexts []string `xorm:"JSON TEXT"`
  31. EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"`
  32. ApprovalsWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
  33. ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
  34. RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
  35. BlockOnRejectedReviews bool `xorm:"NOT NULL DEFAULT false"`
  36. BlockOnOfficialReviewRequests bool `xorm:"NOT NULL DEFAULT false"`
  37. BlockOnOutdatedBranch bool `xorm:"NOT NULL DEFAULT false"`
  38. DismissStaleApprovals bool `xorm:"NOT NULL DEFAULT false"`
  39. RequireSignedCommits bool `xorm:"NOT NULL DEFAULT false"`
  40. ProtectedFilePatterns string `xorm:"TEXT"`
  41. CreatedUnix timeutil.TimeStamp `xorm:"created"`
  42. UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
  43. }
  44. // IsProtected returns if the branch is protected
  45. func (protectBranch *ProtectedBranch) IsProtected() bool {
  46. return protectBranch.ID > 0
  47. }
  48. // CanUserPush returns if some user could push to this protected branch
  49. func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
  50. if !protectBranch.CanPush {
  51. return false
  52. }
  53. if !protectBranch.EnableWhitelist {
  54. if user, err := GetUserByID(userID); err != nil {
  55. log.Error("GetUserByID: %v", err)
  56. return false
  57. } else if repo, err := GetRepositoryByID(protectBranch.RepoID); err != nil {
  58. log.Error("GetRepositoryByID: %v", err)
  59. return false
  60. } else if writeAccess, err := HasAccessUnit(user, repo, UnitTypeCode, AccessModeWrite); err != nil {
  61. log.Error("HasAccessUnit: %v", err)
  62. return false
  63. } else {
  64. return writeAccess
  65. }
  66. }
  67. if base.Int64sContains(protectBranch.WhitelistUserIDs, userID) {
  68. return true
  69. }
  70. if len(protectBranch.WhitelistTeamIDs) == 0 {
  71. return false
  72. }
  73. in, err := IsUserInTeams(userID, protectBranch.WhitelistTeamIDs)
  74. if err != nil {
  75. log.Error("IsUserInTeams: %v", err)
  76. return false
  77. }
  78. return in
  79. }
  80. // IsUserMergeWhitelisted checks if some user is whitelisted to merge to this branch
  81. func (protectBranch *ProtectedBranch) IsUserMergeWhitelisted(userID int64, permissionInRepo Permission) bool {
  82. if !protectBranch.EnableMergeWhitelist {
  83. // Then we need to fall back on whether the user has write permission
  84. return permissionInRepo.CanWrite(UnitTypeCode)
  85. }
  86. if base.Int64sContains(protectBranch.MergeWhitelistUserIDs, userID) {
  87. return true
  88. }
  89. if len(protectBranch.MergeWhitelistTeamIDs) == 0 {
  90. return false
  91. }
  92. in, err := IsUserInTeams(userID, protectBranch.MergeWhitelistTeamIDs)
  93. if err != nil {
  94. log.Error("IsUserInTeams: %v", err)
  95. return false
  96. }
  97. return in
  98. }
  99. // IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
  100. func (protectBranch *ProtectedBranch) IsUserOfficialReviewer(user *User) (bool, error) {
  101. return protectBranch.isUserOfficialReviewer(x, user)
  102. }
  103. func (protectBranch *ProtectedBranch) isUserOfficialReviewer(e Engine, user *User) (bool, error) {
  104. repo, err := getRepositoryByID(e, protectBranch.RepoID)
  105. if err != nil {
  106. return false, err
  107. }
  108. if !protectBranch.EnableApprovalsWhitelist {
  109. // Anyone with write access is considered official reviewer
  110. writeAccess, err := hasAccessUnit(e, user, repo, UnitTypeCode, AccessModeWrite)
  111. if err != nil {
  112. return false, err
  113. }
  114. return writeAccess, nil
  115. }
  116. if base.Int64sContains(protectBranch.ApprovalsWhitelistUserIDs, user.ID) {
  117. return true, nil
  118. }
  119. inTeam, err := isUserInTeams(e, user.ID, protectBranch.ApprovalsWhitelistTeamIDs)
  120. if err != nil {
  121. return false, err
  122. }
  123. return inTeam, nil
  124. }
  125. // HasEnoughApprovals returns true if pr has enough granted approvals.
  126. func (protectBranch *ProtectedBranch) HasEnoughApprovals(pr *PullRequest) bool {
  127. if protectBranch.RequiredApprovals == 0 {
  128. return true
  129. }
  130. return protectBranch.GetGrantedApprovalsCount(pr) >= protectBranch.RequiredApprovals
  131. }
  132. // GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
  133. func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest) int64 {
  134. sess := x.Where("issue_id = ?", pr.IssueID).
  135. And("type = ?", ReviewTypeApprove).
  136. And("official = ?", true).
  137. And("dismissed = ?", false)
  138. if protectBranch.DismissStaleApprovals {
  139. sess = sess.And("stale = ?", false)
  140. }
  141. approvals, err := sess.Count(new(Review))
  142. if err != nil {
  143. log.Error("GetGrantedApprovalsCount: %v", err)
  144. return 0
  145. }
  146. return approvals
  147. }
  148. // MergeBlockedByRejectedReview returns true if merge is blocked by rejected reviews
  149. func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(pr *PullRequest) bool {
  150. if !protectBranch.BlockOnRejectedReviews {
  151. return false
  152. }
  153. rejectExist, err := x.Where("issue_id = ?", pr.IssueID).
  154. And("type = ?", ReviewTypeReject).
  155. And("official = ?", true).
  156. And("dismissed = ?", false).
  157. Exist(new(Review))
  158. if err != nil {
  159. log.Error("MergeBlockedByRejectedReview: %v", err)
  160. return true
  161. }
  162. return rejectExist
  163. }
  164. // MergeBlockedByOfficialReviewRequests block merge because of some review request to official reviewer
  165. // of from official review
  166. func (protectBranch *ProtectedBranch) MergeBlockedByOfficialReviewRequests(pr *PullRequest) bool {
  167. if !protectBranch.BlockOnOfficialReviewRequests {
  168. return false
  169. }
  170. has, err := x.Where("issue_id = ?", pr.IssueID).
  171. And("type = ?", ReviewTypeRequest).
  172. And("official = ?", true).
  173. Exist(new(Review))
  174. if err != nil {
  175. log.Error("MergeBlockedByOfficialReviewRequests: %v", err)
  176. return true
  177. }
  178. return has
  179. }
  180. // MergeBlockedByOutdatedBranch returns true if merge is blocked by an outdated head branch
  181. func (protectBranch *ProtectedBranch) MergeBlockedByOutdatedBranch(pr *PullRequest) bool {
  182. return protectBranch.BlockOnOutdatedBranch && pr.CommitsBehind > 0
  183. }
  184. // GetProtectedFilePatterns parses a semicolon separated list of protected file patterns and returns a glob.Glob slice
  185. func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
  186. extarr := make([]glob.Glob, 0, 10)
  187. for _, expr := range strings.Split(strings.ToLower(protectBranch.ProtectedFilePatterns), ";") {
  188. expr = strings.TrimSpace(expr)
  189. if expr != "" {
  190. if g, err := glob.Compile(expr, '.', '/'); err != nil {
  191. log.Info("Invalid glob expression '%s' (skipped): %v", expr, err)
  192. } else {
  193. extarr = append(extarr, g)
  194. }
  195. }
  196. }
  197. return extarr
  198. }
  199. // MergeBlockedByProtectedFiles returns true if merge is blocked by protected files change
  200. func (protectBranch *ProtectedBranch) MergeBlockedByProtectedFiles(pr *PullRequest) bool {
  201. glob := protectBranch.GetProtectedFilePatterns()
  202. if len(glob) == 0 {
  203. return false
  204. }
  205. return len(pr.ChangedProtectedFiles) > 0
  206. }
  207. // IsProtectedFile return if path is protected
  208. func (protectBranch *ProtectedBranch) IsProtectedFile(patterns []glob.Glob, path string) bool {
  209. if len(patterns) == 0 {
  210. patterns = protectBranch.GetProtectedFilePatterns()
  211. if len(patterns) == 0 {
  212. return false
  213. }
  214. }
  215. lpath := strings.ToLower(strings.TrimSpace(path))
  216. r := false
  217. for _, pat := range patterns {
  218. if pat.Match(lpath) {
  219. r = true
  220. break
  221. }
  222. }
  223. return r
  224. }
  225. // GetProtectedBranchBy getting protected branch by ID/Name
  226. func GetProtectedBranchBy(repoID int64, branchName string) (*ProtectedBranch, error) {
  227. return getProtectedBranchBy(x, repoID, branchName)
  228. }
  229. func getProtectedBranchBy(e Engine, repoID int64, branchName string) (*ProtectedBranch, error) {
  230. rel := &ProtectedBranch{RepoID: repoID, BranchName: branchName}
  231. has, err := e.Get(rel)
  232. if err != nil {
  233. return nil, err
  234. }
  235. if !has {
  236. return nil, nil
  237. }
  238. return rel, nil
  239. }
  240. // WhitelistOptions represent all sorts of whitelists used for protected branches
  241. type WhitelistOptions struct {
  242. UserIDs []int64
  243. TeamIDs []int64
  244. MergeUserIDs []int64
  245. MergeTeamIDs []int64
  246. ApprovalsUserIDs []int64
  247. ApprovalsTeamIDs []int64
  248. }
  249. // UpdateProtectBranch saves branch protection options of repository.
  250. // If ID is 0, it creates a new record. Otherwise, updates existing record.
  251. // This function also performs check if whitelist user and team's IDs have been changed
  252. // to avoid unnecessary whitelist delete and regenerate.
  253. func UpdateProtectBranch(repo *Repository, protectBranch *ProtectedBranch, opts WhitelistOptions) (err error) {
  254. if err = repo.GetOwner(); err != nil {
  255. return fmt.Errorf("GetOwner: %v", err)
  256. }
  257. whitelist, err := updateUserWhitelist(repo, protectBranch.WhitelistUserIDs, opts.UserIDs)
  258. if err != nil {
  259. return err
  260. }
  261. protectBranch.WhitelistUserIDs = whitelist
  262. whitelist, err = updateUserWhitelist(repo, protectBranch.MergeWhitelistUserIDs, opts.MergeUserIDs)
  263. if err != nil {
  264. return err
  265. }
  266. protectBranch.MergeWhitelistUserIDs = whitelist
  267. whitelist, err = updateApprovalWhitelist(repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
  268. if err != nil {
  269. return err
  270. }
  271. protectBranch.ApprovalsWhitelistUserIDs = whitelist
  272. // if the repo is in an organization
  273. whitelist, err = updateTeamWhitelist(repo, protectBranch.WhitelistTeamIDs, opts.TeamIDs)
  274. if err != nil {
  275. return err
  276. }
  277. protectBranch.WhitelistTeamIDs = whitelist
  278. whitelist, err = updateTeamWhitelist(repo, protectBranch.MergeWhitelistTeamIDs, opts.MergeTeamIDs)
  279. if err != nil {
  280. return err
  281. }
  282. protectBranch.MergeWhitelistTeamIDs = whitelist
  283. whitelist, err = updateTeamWhitelist(repo, protectBranch.ApprovalsWhitelistTeamIDs, opts.ApprovalsTeamIDs)
  284. if err != nil {
  285. return err
  286. }
  287. protectBranch.ApprovalsWhitelistTeamIDs = whitelist
  288. // Make sure protectBranch.ID is not 0 for whitelists
  289. if protectBranch.ID == 0 {
  290. if _, err = x.Insert(protectBranch); err != nil {
  291. return fmt.Errorf("Insert: %v", err)
  292. }
  293. return nil
  294. }
  295. if _, err = x.ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
  296. return fmt.Errorf("Update: %v", err)
  297. }
  298. return nil
  299. }
  300. // GetProtectedBranches get all protected branches
  301. func (repo *Repository) GetProtectedBranches() ([]*ProtectedBranch, error) {
  302. protectedBranches := make([]*ProtectedBranch, 0)
  303. return protectedBranches, x.Find(&protectedBranches, &ProtectedBranch{RepoID: repo.ID})
  304. }
  305. // GetBranchProtection get the branch protection of a branch
  306. func (repo *Repository) GetBranchProtection(branchName string) (*ProtectedBranch, error) {
  307. return GetProtectedBranchBy(repo.ID, branchName)
  308. }
  309. // IsProtectedBranch checks if branch is protected
  310. func (repo *Repository) IsProtectedBranch(branchName string) (bool, error) {
  311. protectedBranch := &ProtectedBranch{
  312. RepoID: repo.ID,
  313. BranchName: branchName,
  314. }
  315. has, err := x.Exist(protectedBranch)
  316. if err != nil {
  317. return true, err
  318. }
  319. return has, 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 util.IsInt64InSlice(teams[i].ID, newWhitelist) {
  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{}
  436. has, err := x.ID(id).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, olderThan time.Duration) {
  479. // Nothing to do for shutdown or terminate
  480. log.Trace("Doing: DeletedBranchesCleanup")
  481. deleteBefore := time.Now().Add(-olderThan)
  482. _, err := x.Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch))
  483. if err != nil {
  484. log.Error("DeletedBranchesCleanup: %v", err)
  485. }
  486. }