Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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