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.

repo.go 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package models
  5. import (
  6. "context"
  7. "fmt"
  8. "strconv"
  9. _ "image/jpeg" // Needed for jpeg support
  10. activities_model "code.gitea.io/gitea/models/activities"
  11. admin_model "code.gitea.io/gitea/models/admin"
  12. asymkey_model "code.gitea.io/gitea/models/asymkey"
  13. "code.gitea.io/gitea/models/db"
  14. git_model "code.gitea.io/gitea/models/git"
  15. issues_model "code.gitea.io/gitea/models/issues"
  16. "code.gitea.io/gitea/models/organization"
  17. access_model "code.gitea.io/gitea/models/perm/access"
  18. project_model "code.gitea.io/gitea/models/project"
  19. repo_model "code.gitea.io/gitea/models/repo"
  20. secret_model "code.gitea.io/gitea/models/secret"
  21. system_model "code.gitea.io/gitea/models/system"
  22. "code.gitea.io/gitea/models/unit"
  23. user_model "code.gitea.io/gitea/models/user"
  24. "code.gitea.io/gitea/models/webhook"
  25. "code.gitea.io/gitea/modules/lfs"
  26. "code.gitea.io/gitea/modules/log"
  27. "code.gitea.io/gitea/modules/storage"
  28. "xorm.io/builder"
  29. )
  30. // ItemsPerPage maximum items per page in forks, watchers and stars of a repo
  31. var ItemsPerPage = 40
  32. // Init initialize model
  33. func Init() error {
  34. unit.LoadUnitConfig()
  35. return system_model.Init()
  36. }
  37. // DeleteRepository deletes a repository for a user or organization.
  38. // make sure if you call this func to close open sessions (sqlite will otherwise get a deadlock)
  39. func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
  40. ctx, committer, err := db.TxContext(db.DefaultContext)
  41. if err != nil {
  42. return err
  43. }
  44. defer committer.Close()
  45. sess := db.GetEngine(ctx)
  46. // In case is a organization.
  47. org, err := user_model.GetUserByID(ctx, uid)
  48. if err != nil {
  49. return err
  50. }
  51. repo := &repo_model.Repository{OwnerID: uid}
  52. has, err := sess.ID(repoID).Get(repo)
  53. if err != nil {
  54. return err
  55. } else if !has {
  56. return repo_model.ErrRepoNotExist{
  57. ID: repoID,
  58. UID: uid,
  59. OwnerName: "",
  60. Name: "",
  61. }
  62. }
  63. // Delete Deploy Keys
  64. deployKeys, err := asymkey_model.ListDeployKeys(ctx, &asymkey_model.ListDeployKeysOptions{RepoID: repoID})
  65. if err != nil {
  66. return fmt.Errorf("listDeployKeys: %w", err)
  67. }
  68. needRewriteKeysFile := len(deployKeys) > 0
  69. for _, dKey := range deployKeys {
  70. if err := DeleteDeployKey(ctx, doer, dKey.ID); err != nil {
  71. return fmt.Errorf("deleteDeployKeys: %w", err)
  72. }
  73. }
  74. if cnt, err := sess.ID(repoID).Delete(&repo_model.Repository{}); err != nil {
  75. return err
  76. } else if cnt != 1 {
  77. return repo_model.ErrRepoNotExist{
  78. ID: repoID,
  79. UID: uid,
  80. OwnerName: "",
  81. Name: "",
  82. }
  83. }
  84. if org.IsOrganization() {
  85. teams, err := organization.FindOrgTeams(ctx, org.ID)
  86. if err != nil {
  87. return err
  88. }
  89. for _, t := range teams {
  90. if !organization.HasTeamRepo(ctx, t.OrgID, t.ID, repoID) {
  91. continue
  92. } else if err = removeRepository(ctx, t, repo, false); err != nil {
  93. return err
  94. }
  95. }
  96. }
  97. attachments := make([]*repo_model.Attachment, 0, 20)
  98. if err = sess.Join("INNER", "`release`", "`release`.id = `attachment`.release_id").
  99. Where("`release`.repo_id = ?", repoID).
  100. Find(&attachments); err != nil {
  101. return err
  102. }
  103. releaseAttachments := make([]string, 0, len(attachments))
  104. for i := 0; i < len(attachments); i++ {
  105. releaseAttachments = append(releaseAttachments, attachments[i].RelativePath())
  106. }
  107. if _, err := db.Exec(ctx, "UPDATE `user` SET num_stars=num_stars-1 WHERE id IN (SELECT `uid` FROM `star` WHERE repo_id = ?)", repo.ID); err != nil {
  108. return err
  109. }
  110. if _, err := db.GetEngine(ctx).In("hook_id", builder.Select("id").From("webhook").Where(builder.Eq{"webhook.repo_id": repo.ID})).
  111. Delete(&webhook.HookTask{}); err != nil {
  112. return err
  113. }
  114. if err := db.DeleteBeans(ctx,
  115. &access_model.Access{RepoID: repo.ID},
  116. &activities_model.Action{RepoID: repo.ID},
  117. &repo_model.Collaboration{RepoID: repoID},
  118. &issues_model.Comment{RefRepoID: repoID},
  119. &git_model.CommitStatus{RepoID: repoID},
  120. &git_model.DeletedBranch{RepoID: repoID},
  121. &git_model.LFSLock{RepoID: repoID},
  122. &repo_model.LanguageStat{RepoID: repoID},
  123. &issues_model.Milestone{RepoID: repoID},
  124. &repo_model.Mirror{RepoID: repoID},
  125. &activities_model.Notification{RepoID: repoID},
  126. &git_model.ProtectedBranch{RepoID: repoID},
  127. &git_model.ProtectedTag{RepoID: repoID},
  128. &repo_model.PushMirror{RepoID: repoID},
  129. &repo_model.Release{RepoID: repoID},
  130. &repo_model.RepoIndexerStatus{RepoID: repoID},
  131. &repo_model.Redirect{RedirectRepoID: repoID},
  132. &repo_model.RepoUnit{RepoID: repoID},
  133. &repo_model.Star{RepoID: repoID},
  134. &admin_model.Task{RepoID: repoID},
  135. &repo_model.Watch{RepoID: repoID},
  136. &webhook.Webhook{RepoID: repoID},
  137. &secret_model.Secret{RepoID: repoID},
  138. ); err != nil {
  139. return fmt.Errorf("deleteBeans: %w", err)
  140. }
  141. // Delete Labels and related objects
  142. if err := issues_model.DeleteLabelsByRepoID(ctx, repoID); err != nil {
  143. return err
  144. }
  145. // Delete Pulls and related objects
  146. if err := issues_model.DeletePullsByBaseRepoID(ctx, repoID); err != nil {
  147. return err
  148. }
  149. // Delete Issues and related objects
  150. var attachmentPaths []string
  151. if attachmentPaths, err = issues_model.DeleteIssuesByRepoID(ctx, repoID); err != nil {
  152. return err
  153. }
  154. // Delete issue index
  155. if err := db.DeleteResourceIndex(ctx, "issue_index", repoID); err != nil {
  156. return err
  157. }
  158. if repo.IsFork {
  159. if _, err := db.Exec(ctx, "UPDATE `repository` SET num_forks=num_forks-1 WHERE id=?", repo.ForkID); err != nil {
  160. return fmt.Errorf("decrease fork count: %w", err)
  161. }
  162. }
  163. if _, err := db.Exec(ctx, "UPDATE `user` SET num_repos=num_repos-1 WHERE id=?", uid); err != nil {
  164. return err
  165. }
  166. if len(repo.Topics) > 0 {
  167. if err := repo_model.RemoveTopicsFromRepo(ctx, repo.ID); err != nil {
  168. return err
  169. }
  170. }
  171. if err := project_model.DeleteProjectByRepoID(ctx, repoID); err != nil {
  172. return fmt.Errorf("unable to delete projects for repo[%d]: %w", repoID, err)
  173. }
  174. // Remove LFS objects
  175. var lfsObjects []*git_model.LFSMetaObject
  176. if err = sess.Where("repository_id=?", repoID).Find(&lfsObjects); err != nil {
  177. return err
  178. }
  179. lfsPaths := make([]string, 0, len(lfsObjects))
  180. for _, v := range lfsObjects {
  181. count, err := db.CountByBean(ctx, &git_model.LFSMetaObject{Pointer: lfs.Pointer{Oid: v.Oid}})
  182. if err != nil {
  183. return err
  184. }
  185. if count > 1 {
  186. continue
  187. }
  188. lfsPaths = append(lfsPaths, v.RelativePath())
  189. }
  190. if _, err := db.DeleteByBean(ctx, &git_model.LFSMetaObject{RepositoryID: repoID}); err != nil {
  191. return err
  192. }
  193. // Remove archives
  194. var archives []*repo_model.RepoArchiver
  195. if err = sess.Where("repo_id=?", repoID).Find(&archives); err != nil {
  196. return err
  197. }
  198. archivePaths := make([]string, 0, len(archives))
  199. for _, v := range archives {
  200. archivePaths = append(archivePaths, v.RelativePath())
  201. }
  202. if _, err := db.DeleteByBean(ctx, &repo_model.RepoArchiver{RepoID: repoID}); err != nil {
  203. return err
  204. }
  205. if repo.NumForks > 0 {
  206. if _, err = sess.Exec("UPDATE `repository` SET fork_id=0,is_fork=? WHERE fork_id=?", false, repo.ID); err != nil {
  207. log.Error("reset 'fork_id' and 'is_fork': %v", err)
  208. }
  209. }
  210. // Get all attachments with both issue_id and release_id are zero
  211. var newAttachments []*repo_model.Attachment
  212. if err := sess.Where(builder.Eq{
  213. "repo_id": repo.ID,
  214. "issue_id": 0,
  215. "release_id": 0,
  216. }).Find(&newAttachments); err != nil {
  217. return err
  218. }
  219. newAttachmentPaths := make([]string, 0, len(newAttachments))
  220. for _, attach := range newAttachments {
  221. newAttachmentPaths = append(newAttachmentPaths, attach.RelativePath())
  222. }
  223. if _, err := sess.Where("repo_id=?", repo.ID).Delete(new(repo_model.Attachment)); err != nil {
  224. return err
  225. }
  226. if err = committer.Commit(); err != nil {
  227. return err
  228. }
  229. committer.Close()
  230. if needRewriteKeysFile {
  231. if err := asymkey_model.RewriteAllPublicKeys(); err != nil {
  232. log.Error("RewriteAllPublicKeys failed: %v", err)
  233. }
  234. }
  235. // We should always delete the files after the database transaction succeed. If
  236. // we delete the file but the database rollback, the repository will be broken.
  237. // Remove repository files.
  238. repoPath := repo.RepoPath()
  239. system_model.RemoveAllWithNotice(db.DefaultContext, "Delete repository files", repoPath)
  240. // Remove wiki files
  241. if repo.HasWiki() {
  242. system_model.RemoveAllWithNotice(db.DefaultContext, "Delete repository wiki", repo.WikiPath())
  243. }
  244. // Remove archives
  245. for _, archive := range archivePaths {
  246. system_model.RemoveStorageWithNotice(db.DefaultContext, storage.RepoArchives, "Delete repo archive file", archive)
  247. }
  248. // Remove lfs objects
  249. for _, lfsObj := range lfsPaths {
  250. system_model.RemoveStorageWithNotice(db.DefaultContext, storage.LFS, "Delete orphaned LFS file", lfsObj)
  251. }
  252. // Remove issue attachment files.
  253. for _, attachment := range attachmentPaths {
  254. system_model.RemoveStorageWithNotice(db.DefaultContext, storage.Attachments, "Delete issue attachment", attachment)
  255. }
  256. // Remove release attachment files.
  257. for _, releaseAttachment := range releaseAttachments {
  258. system_model.RemoveStorageWithNotice(db.DefaultContext, storage.Attachments, "Delete release attachment", releaseAttachment)
  259. }
  260. // Remove attachment with no issue_id and release_id.
  261. for _, newAttachment := range newAttachmentPaths {
  262. system_model.RemoveStorageWithNotice(db.DefaultContext, storage.Attachments, "Delete issue attachment", newAttachment)
  263. }
  264. if len(repo.Avatar) > 0 {
  265. if err := storage.RepoAvatars.Delete(repo.CustomAvatarRelativePath()); err != nil {
  266. return fmt.Errorf("Failed to remove %s: %w", repo.Avatar, err)
  267. }
  268. }
  269. return nil
  270. }
  271. type repoChecker struct {
  272. querySQL func(ctx context.Context) ([]map[string][]byte, error)
  273. correctSQL func(ctx context.Context, id int64) error
  274. desc string
  275. }
  276. func repoStatsCheck(ctx context.Context, checker *repoChecker) {
  277. results, err := checker.querySQL(ctx)
  278. if err != nil {
  279. log.Error("Select %s: %v", checker.desc, err)
  280. return
  281. }
  282. for _, result := range results {
  283. id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
  284. select {
  285. case <-ctx.Done():
  286. log.Warn("CheckRepoStats: Cancelled before checking %s for with id=%d", checker.desc, id)
  287. return
  288. default:
  289. }
  290. log.Trace("Updating %s: %d", checker.desc, id)
  291. err = checker.correctSQL(ctx, id)
  292. if err != nil {
  293. log.Error("Update %s[%d]: %v", checker.desc, id, err)
  294. }
  295. }
  296. }
  297. func StatsCorrectSQL(ctx context.Context, sql string, id int64) error {
  298. _, err := db.GetEngine(ctx).Exec(sql, id, id)
  299. return err
  300. }
  301. func repoStatsCorrectNumWatches(ctx context.Context, id int64) error {
  302. return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_watches=(SELECT COUNT(*) FROM `watch` WHERE repo_id=? AND mode<>2) WHERE id=?", id)
  303. }
  304. func repoStatsCorrectNumStars(ctx context.Context, id int64) error {
  305. return StatsCorrectSQL(ctx, "UPDATE `repository` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE repo_id=?) WHERE id=?", id)
  306. }
  307. func labelStatsCorrectNumIssues(ctx context.Context, id int64) error {
  308. return StatsCorrectSQL(ctx, "UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=?) WHERE id=?", id)
  309. }
  310. func labelStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
  311. _, err := db.GetEngine(ctx).Exec("UPDATE `label` SET num_issues=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=id) WHERE repo_id=?", id)
  312. return err
  313. }
  314. func labelStatsCorrectNumClosedIssues(ctx context.Context, id int64) error {
  315. _, err := db.GetEngine(ctx).Exec("UPDATE `label` SET num_closed_issues=(SELECT COUNT(*) FROM `issue_label`,`issue` WHERE `issue_label`.label_id=`label`.id AND `issue_label`.issue_id=`issue`.id AND `issue`.is_closed=?) WHERE `label`.id=?", true, id)
  316. return err
  317. }
  318. func labelStatsCorrectNumClosedIssuesRepo(ctx context.Context, id int64) error {
  319. _, err := db.GetEngine(ctx).Exec("UPDATE `label` SET num_closed_issues=(SELECT COUNT(*) FROM `issue_label`,`issue` WHERE `issue_label`.label_id=`label`.id AND `issue_label`.issue_id=`issue`.id AND `issue`.is_closed=?) WHERE `label`.repo_id=?", true, id)
  320. return err
  321. }
  322. var milestoneStatsQueryNumIssues = "SELECT `milestone`.id FROM `milestone` WHERE `milestone`.num_closed_issues!=(SELECT COUNT(*) FROM `issue` WHERE `issue`.milestone_id=`milestone`.id AND `issue`.is_closed=?) OR `milestone`.num_issues!=(SELECT COUNT(*) FROM `issue` WHERE `issue`.milestone_id=`milestone`.id)"
  323. func milestoneStatsCorrectNumIssuesRepo(ctx context.Context, id int64) error {
  324. e := db.GetEngine(ctx)
  325. results, err := e.Query(milestoneStatsQueryNumIssues+" AND `milestone`.repo_id = ?", true, id)
  326. if err != nil {
  327. return err
  328. }
  329. for _, result := range results {
  330. id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
  331. err = issues_model.UpdateMilestoneCounters(ctx, id)
  332. if err != nil {
  333. return err
  334. }
  335. }
  336. return nil
  337. }
  338. func userStatsCorrectNumRepos(ctx context.Context, id int64) error {
  339. return StatsCorrectSQL(ctx, "UPDATE `user` SET num_repos=(SELECT COUNT(*) FROM `repository` WHERE owner_id=?) WHERE id=?", id)
  340. }
  341. func repoStatsCorrectIssueNumComments(ctx context.Context, id int64) error {
  342. return StatsCorrectSQL(ctx, "UPDATE `issue` SET num_comments=(SELECT COUNT(*) FROM `comment` WHERE issue_id=? AND type=0) WHERE id=?", id)
  343. }
  344. func repoStatsCorrectNumIssues(ctx context.Context, id int64) error {
  345. return repo_model.UpdateRepoIssueNumbers(ctx, id, false, false)
  346. }
  347. func repoStatsCorrectNumPulls(ctx context.Context, id int64) error {
  348. return repo_model.UpdateRepoIssueNumbers(ctx, id, true, false)
  349. }
  350. func repoStatsCorrectNumClosedIssues(ctx context.Context, id int64) error {
  351. return repo_model.UpdateRepoIssueNumbers(ctx, id, false, true)
  352. }
  353. func repoStatsCorrectNumClosedPulls(ctx context.Context, id int64) error {
  354. return repo_model.UpdateRepoIssueNumbers(ctx, id, true, true)
  355. }
  356. func statsQuery(args ...interface{}) func(context.Context) ([]map[string][]byte, error) {
  357. return func(ctx context.Context) ([]map[string][]byte, error) {
  358. return db.GetEngine(ctx).Query(args...)
  359. }
  360. }
  361. // CheckRepoStats checks the repository stats
  362. func CheckRepoStats(ctx context.Context) error {
  363. log.Trace("Doing: CheckRepoStats")
  364. checkers := []*repoChecker{
  365. // Repository.NumWatches
  366. {
  367. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_watches!=(SELECT COUNT(*) FROM `watch` WHERE repo_id=repo.id AND mode<>2)"),
  368. repoStatsCorrectNumWatches,
  369. "repository count 'num_watches'",
  370. },
  371. // Repository.NumStars
  372. {
  373. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_stars!=(SELECT COUNT(*) FROM `star` WHERE repo_id=repo.id)"),
  374. repoStatsCorrectNumStars,
  375. "repository count 'num_stars'",
  376. },
  377. // Repository.NumIssues
  378. {
  379. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_issues!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_pull=?)", false),
  380. repoStatsCorrectNumIssues,
  381. "repository count 'num_issues'",
  382. },
  383. // Repository.NumClosedIssues
  384. {
  385. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_closed_issues!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_closed=? AND is_pull=?)", true, false),
  386. repoStatsCorrectNumClosedIssues,
  387. "repository count 'num_closed_issues'",
  388. },
  389. // Repository.NumPulls
  390. {
  391. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_pull=?)", true),
  392. repoStatsCorrectNumPulls,
  393. "repository count 'num_pulls'",
  394. },
  395. // Repository.NumClosedPulls
  396. {
  397. statsQuery("SELECT repo.id FROM `repository` repo WHERE repo.num_closed_pulls!=(SELECT COUNT(*) FROM `issue` WHERE repo_id=repo.id AND is_closed=? AND is_pull=?)", true, true),
  398. repoStatsCorrectNumClosedPulls,
  399. "repository count 'num_closed_pulls'",
  400. },
  401. // Label.NumIssues
  402. {
  403. statsQuery("SELECT label.id FROM `label` WHERE label.num_issues!=(SELECT COUNT(*) FROM `issue_label` WHERE label_id=label.id)"),
  404. labelStatsCorrectNumIssues,
  405. "label count 'num_issues'",
  406. },
  407. // Label.NumClosedIssues
  408. {
  409. statsQuery("SELECT `label`.id FROM `label` WHERE `label`.num_closed_issues!=(SELECT COUNT(*) FROM `issue_label`,`issue` WHERE `issue_label`.label_id=`label`.id AND `issue_label`.issue_id=`issue`.id AND `issue`.is_closed=?)", true),
  410. labelStatsCorrectNumClosedIssues,
  411. "label count 'num_closed_issues'",
  412. },
  413. // Milestone.Num{,Closed}Issues
  414. {
  415. statsQuery(milestoneStatsQueryNumIssues, true),
  416. issues_model.UpdateMilestoneCounters,
  417. "milestone count 'num_closed_issues' and 'num_issues'",
  418. },
  419. // User.NumRepos
  420. {
  421. statsQuery("SELECT `user`.id FROM `user` WHERE `user`.num_repos!=(SELECT COUNT(*) FROM `repository` WHERE owner_id=`user`.id)"),
  422. userStatsCorrectNumRepos,
  423. "user count 'num_repos'",
  424. },
  425. // Issue.NumComments
  426. {
  427. statsQuery("SELECT `issue`.id FROM `issue` WHERE `issue`.num_comments!=(SELECT COUNT(*) FROM `comment` WHERE issue_id=`issue`.id AND type=0)"),
  428. repoStatsCorrectIssueNumComments,
  429. "issue count 'num_comments'",
  430. },
  431. }
  432. for _, checker := range checkers {
  433. select {
  434. case <-ctx.Done():
  435. log.Warn("CheckRepoStats: Cancelled before %s", checker.desc)
  436. return db.ErrCancelledf("before checking %s", checker.desc)
  437. default:
  438. repoStatsCheck(ctx, checker)
  439. }
  440. }
  441. // FIXME: use checker when stop supporting old fork repo format.
  442. // ***** START: Repository.NumForks *****
  443. e := db.GetEngine(ctx)
  444. results, err := e.Query("SELECT repo.id FROM `repository` repo WHERE repo.num_forks!=(SELECT COUNT(*) FROM `repository` WHERE fork_id=repo.id)")
  445. if err != nil {
  446. log.Error("Select repository count 'num_forks': %v", err)
  447. } else {
  448. for _, result := range results {
  449. id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
  450. select {
  451. case <-ctx.Done():
  452. log.Warn("CheckRepoStats: Cancelled")
  453. return db.ErrCancelledf("during repository count 'num_fork' for repo ID %d", id)
  454. default:
  455. }
  456. log.Trace("Updating repository count 'num_forks': %d", id)
  457. repo, err := repo_model.GetRepositoryByID(ctx, id)
  458. if err != nil {
  459. log.Error("repo_model.GetRepositoryByID[%d]: %v", id, err)
  460. continue
  461. }
  462. _, err = e.SQL("SELECT COUNT(*) FROM `repository` WHERE fork_id=?", repo.ID).Get(&repo.NumForks)
  463. if err != nil {
  464. log.Error("Select count of forks[%d]: %v", repo.ID, err)
  465. continue
  466. }
  467. if _, err = e.ID(repo.ID).Cols("num_forks").Update(repo); err != nil {
  468. log.Error("UpdateRepository[%d]: %v", id, err)
  469. continue
  470. }
  471. }
  472. }
  473. // ***** END: Repository.NumForks *****
  474. return nil
  475. }
  476. func UpdateRepoStats(ctx context.Context, id int64) error {
  477. var err error
  478. for _, f := range []func(ctx context.Context, id int64) error{
  479. repoStatsCorrectNumWatches,
  480. repoStatsCorrectNumStars,
  481. repoStatsCorrectNumIssues,
  482. repoStatsCorrectNumPulls,
  483. repoStatsCorrectNumClosedIssues,
  484. repoStatsCorrectNumClosedPulls,
  485. labelStatsCorrectNumIssuesRepo,
  486. labelStatsCorrectNumClosedIssuesRepo,
  487. milestoneStatsCorrectNumIssuesRepo,
  488. } {
  489. err = f(ctx, id)
  490. if err != nil {
  491. return err
  492. }
  493. }
  494. return nil
  495. }
  496. func updateUserStarNumbers(users []user_model.User) error {
  497. ctx, committer, err := db.TxContext(db.DefaultContext)
  498. if err != nil {
  499. return err
  500. }
  501. defer committer.Close()
  502. for _, user := range users {
  503. if _, err = db.Exec(ctx, "UPDATE `user` SET num_stars=(SELECT COUNT(*) FROM `star` WHERE uid=?) WHERE id=?", user.ID, user.ID); err != nil {
  504. return err
  505. }
  506. }
  507. return committer.Commit()
  508. }
  509. // DoctorUserStarNum recalculate Stars number for all user
  510. func DoctorUserStarNum() (err error) {
  511. const batchSize = 100
  512. for start := 0; ; start += batchSize {
  513. users := make([]user_model.User, 0, batchSize)
  514. if err = db.GetEngine(db.DefaultContext).Limit(batchSize, start).Where("type = ?", 0).Cols("id").Find(&users); err != nil {
  515. return
  516. }
  517. if len(users) == 0 {
  518. break
  519. }
  520. if err = updateUserStarNumbers(users); err != nil {
  521. return
  522. }
  523. }
  524. log.Debug("recalculate Stars number for all user finished")
  525. return err
  526. }
  527. // DeleteDeployKey delete deploy keys
  528. func DeleteDeployKey(ctx context.Context, doer *user_model.User, id int64) error {
  529. key, err := asymkey_model.GetDeployKeyByID(ctx, id)
  530. if err != nil {
  531. if asymkey_model.IsErrDeployKeyNotExist(err) {
  532. return nil
  533. }
  534. return fmt.Errorf("GetDeployKeyByID: %w", err)
  535. }
  536. // Check if user has access to delete this key.
  537. if !doer.IsAdmin {
  538. repo, err := repo_model.GetRepositoryByID(ctx, key.RepoID)
  539. if err != nil {
  540. return fmt.Errorf("GetRepositoryByID: %w", err)
  541. }
  542. has, err := access_model.IsUserRepoAdmin(ctx, repo, doer)
  543. if err != nil {
  544. return fmt.Errorf("GetUserRepoPermission: %w", err)
  545. } else if !has {
  546. return asymkey_model.ErrKeyAccessDenied{
  547. UserID: doer.ID,
  548. KeyID: key.ID,
  549. Note: "deploy",
  550. }
  551. }
  552. }
  553. if _, err := db.DeleteByBean(ctx, &asymkey_model.DeployKey{
  554. ID: key.ID,
  555. }); err != nil {
  556. return fmt.Errorf("delete deploy key [%d]: %w", key.ID, err)
  557. }
  558. // Check if this is the last reference to same key content.
  559. has, err := asymkey_model.IsDeployKeyExistByKeyID(ctx, key.KeyID)
  560. if err != nil {
  561. return err
  562. } else if !has {
  563. if err = asymkey_model.DeletePublicKeys(ctx, key.KeyID); err != nil {
  564. return err
  565. }
  566. }
  567. return nil
  568. }