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

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