Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

v136.go 3.2KB

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