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.

v136.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2020 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 migrations
  5. import (
  6. "fmt"
  7. "math"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "xorm.io/xorm"
  15. )
  16. func addCommitDivergenceToPulls(x *xorm.Engine) error {
  17. type Repository struct {
  18. ID int64 `xorm:"pk autoincr"`
  19. OwnerID int64 `xorm:"UNIQUE(s) index"`
  20. OwnerName string
  21. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  22. Name string `xorm:"INDEX NOT NULL"`
  23. }
  24. type PullRequest struct {
  25. ID int64 `xorm:"pk autoincr"`
  26. IssueID int64 `xorm:"INDEX"`
  27. Index int64
  28. CommitsAhead int
  29. CommitsBehind int
  30. BaseRepoID int64 `xorm:"INDEX"`
  31. BaseBranch string
  32. HasMerged bool `xorm:"INDEX"`
  33. MergedCommitID string `xorm:"VARCHAR(40)"`
  34. }
  35. if err := x.Sync2(new(PullRequest)); err != nil {
  36. return fmt.Errorf("Sync2: %v", err)
  37. }
  38. last := 0
  39. migrated := 0
  40. batchSize := setting.Database.IterateBufferSize
  41. sess := x.NewSession()
  42. defer sess.Close()
  43. ticker := time.NewTicker(5 * time.Second)
  44. defer ticker.Stop()
  45. count, err := sess.Where("has_merged = ?", false).Count(new(PullRequest))
  46. if err != nil {
  47. return err
  48. }
  49. log.Info("%d Unmerged Pull Request(s) to migrate ...", count)
  50. for {
  51. if err := sess.Begin(); err != nil {
  52. return err
  53. }
  54. results := make([]*PullRequest, 0, batchSize)
  55. err := sess.Where("has_merged = ?", false).OrderBy("id").Limit(batchSize, last).Find(&results)
  56. if err != nil {
  57. return err
  58. }
  59. if len(results) == 0 {
  60. break
  61. }
  62. last += batchSize
  63. for _, pr := range results {
  64. baseRepo := &Repository{ID: pr.BaseRepoID}
  65. has, err := x.Table("repository").Get(baseRepo)
  66. if err != nil {
  67. return fmt.Errorf("Unable to get base repo %d %v", pr.BaseRepoID, err)
  68. }
  69. if !has {
  70. log.Error("Missing base repo with id %d for PR ID %d", pr.BaseRepoID, pr.ID)
  71. continue
  72. }
  73. userPath := filepath.Join(setting.RepoRootPath, strings.ToLower(baseRepo.OwnerName))
  74. repoPath := filepath.Join(userPath, strings.ToLower(baseRepo.Name)+".git")
  75. gitRefName := fmt.Sprintf("refs/pull/%d/head", pr.Index)
  76. divergence, err := git.GetDivergingCommits(repoPath, pr.BaseBranch, gitRefName)
  77. if err != nil {
  78. log.Warn("Could not recalculate Divergence for pull: %d", pr.ID)
  79. pr.CommitsAhead = 0
  80. pr.CommitsBehind = 0
  81. }
  82. pr.CommitsAhead = divergence.Ahead
  83. pr.CommitsBehind = divergence.Behind
  84. if _, err = sess.ID(pr.ID).Cols("commits_ahead", "commits_behind").Update(pr); err != nil {
  85. return fmt.Errorf("Update Cols: %v", err)
  86. }
  87. migrated++
  88. }
  89. if err := sess.Commit(); err != nil {
  90. return err
  91. }
  92. select {
  93. case <-ticker.C:
  94. log.Info(
  95. "%d/%d (%2.0f%%) Pull Request(s) migrated in %d batches. %d PRs Remaining ...",
  96. migrated,
  97. count,
  98. float64(migrated)/float64(count)*100,
  99. int(math.Ceil(float64(migrated)/float64(batchSize))),
  100. count-int64(migrated))
  101. default:
  102. }
  103. }
  104. log.Info("Completed migrating %d Pull Request(s) in: %d batches", count, int(math.Ceil(float64(migrated)/float64(batchSize))))
  105. return nil
  106. }