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.

pull_update_test.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 integrations
  5. import (
  6. "fmt"
  7. "net/url"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/repofiles"
  12. repo_module "code.gitea.io/gitea/modules/repository"
  13. pull_service "code.gitea.io/gitea/services/pull"
  14. repo_service "code.gitea.io/gitea/services/repository"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestPullUpdate(t *testing.T) {
  18. onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
  19. //Create PR to test
  20. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  21. org26 := models.AssertExistsAndLoadBean(t, &models.User{ID: 26}).(*models.User)
  22. pr := createOutdatedPR(t, user, org26)
  23. //Test GetDiverging
  24. diffCount, err := pull_service.GetDiverging(pr)
  25. assert.NoError(t, err)
  26. assert.EqualValues(t, 1, diffCount.Behind)
  27. assert.EqualValues(t, 1, diffCount.Ahead)
  28. message := fmt.Sprintf("Merge branch '%s' into %s", pr.BaseBranch, pr.HeadBranch)
  29. err = pull_service.Update(pr, user, message)
  30. assert.NoError(t, err)
  31. //Test GetDiverging after update
  32. diffCount, err = pull_service.GetDiverging(pr)
  33. assert.NoError(t, err)
  34. assert.EqualValues(t, 0, diffCount.Behind)
  35. assert.EqualValues(t, 2, diffCount.Ahead)
  36. })
  37. }
  38. func createOutdatedPR(t *testing.T, actor, forkOrg *models.User) *models.PullRequest {
  39. baseRepo, err := repo_service.CreateRepository(actor, actor, models.CreateRepoOptions{
  40. Name: "repo-pr-update",
  41. Description: "repo-tmp-pr-update description",
  42. AutoInit: true,
  43. Gitignores: "C,C++",
  44. License: "MIT",
  45. Readme: "Default",
  46. IsPrivate: false,
  47. })
  48. assert.NoError(t, err)
  49. assert.NotEmpty(t, baseRepo)
  50. headRepo, err := repo_module.ForkRepository(actor, forkOrg, baseRepo, "repo-pr-update", "desc")
  51. assert.NoError(t, err)
  52. assert.NotEmpty(t, headRepo)
  53. //create a commit on base Repo
  54. _, err = repofiles.CreateOrUpdateRepoFile(baseRepo, actor, &repofiles.UpdateRepoFileOptions{
  55. TreePath: "File_A",
  56. Message: "Add File A",
  57. Content: "File A",
  58. IsNewFile: true,
  59. OldBranch: "master",
  60. NewBranch: "master",
  61. Author: &repofiles.IdentityOptions{
  62. Name: actor.Name,
  63. Email: actor.Email,
  64. },
  65. Committer: &repofiles.IdentityOptions{
  66. Name: actor.Name,
  67. Email: actor.Email,
  68. },
  69. Dates: &repofiles.CommitDateOptions{
  70. Author: time.Now(),
  71. Committer: time.Now(),
  72. },
  73. })
  74. assert.NoError(t, err)
  75. //create a commit on head Repo
  76. _, err = repofiles.CreateOrUpdateRepoFile(headRepo, actor, &repofiles.UpdateRepoFileOptions{
  77. TreePath: "File_B",
  78. Message: "Add File on PR branch",
  79. Content: "File B",
  80. IsNewFile: true,
  81. OldBranch: "master",
  82. NewBranch: "newBranch",
  83. Author: &repofiles.IdentityOptions{
  84. Name: actor.Name,
  85. Email: actor.Email,
  86. },
  87. Committer: &repofiles.IdentityOptions{
  88. Name: actor.Name,
  89. Email: actor.Email,
  90. },
  91. Dates: &repofiles.CommitDateOptions{
  92. Author: time.Now(),
  93. Committer: time.Now(),
  94. },
  95. })
  96. assert.NoError(t, err)
  97. //create Pull
  98. pullIssue := &models.Issue{
  99. RepoID: baseRepo.ID,
  100. Title: "Test Pull -to-update-",
  101. PosterID: actor.ID,
  102. Poster: actor,
  103. IsPull: true,
  104. }
  105. pullRequest := &models.PullRequest{
  106. HeadRepoID: headRepo.ID,
  107. BaseRepoID: baseRepo.ID,
  108. HeadBranch: "newBranch",
  109. BaseBranch: "master",
  110. HeadRepo: headRepo,
  111. BaseRepo: baseRepo,
  112. Type: models.PullRequestGitea,
  113. }
  114. err = pull_service.NewPullRequest(baseRepo, pullIssue, nil, nil, pullRequest, nil)
  115. assert.NoError(t, err)
  116. issue := models.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue)
  117. pr, err := models.GetPullRequestByIssueID(issue.ID)
  118. assert.NoError(t, err)
  119. return pr
  120. }