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 18KB

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