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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "net/http"
  7. "net/url"
  8. "testing"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/models/unittest"
  12. user_model "code.gitea.io/gitea/models/user"
  13. "code.gitea.io/gitea/modules/git"
  14. pull_service "code.gitea.io/gitea/services/pull"
  15. repo_service "code.gitea.io/gitea/services/repository"
  16. files_service "code.gitea.io/gitea/services/repository/files"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestAPIPullUpdate(t *testing.T) {
  20. onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
  21. // Create PR to test
  22. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  23. org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26}).(*user_model.User)
  24. pr := createOutdatedPR(t, user, org26)
  25. // Test GetDiverging
  26. diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr)
  27. assert.NoError(t, err)
  28. assert.EqualValues(t, 1, diffCount.Behind)
  29. assert.EqualValues(t, 1, diffCount.Ahead)
  30. assert.NoError(t, pr.LoadBaseRepo())
  31. assert.NoError(t, pr.LoadIssue())
  32. session := loginUser(t, "user2")
  33. token := getTokenForLoggedInUser(t, session)
  34. req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/pulls/%d/update?token="+token, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Issue.Index)
  35. session.MakeRequest(t, req, http.StatusOK)
  36. // Test GetDiverging after update
  37. diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr)
  38. assert.NoError(t, err)
  39. assert.EqualValues(t, 0, diffCount.Behind)
  40. assert.EqualValues(t, 2, diffCount.Ahead)
  41. })
  42. }
  43. func TestAPIPullUpdateByRebase(t *testing.T) {
  44. onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
  45. // Create PR to test
  46. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}).(*user_model.User)
  47. org26 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 26}).(*user_model.User)
  48. pr := createOutdatedPR(t, user, org26)
  49. // Test GetDiverging
  50. diffCount, err := pull_service.GetDiverging(git.DefaultContext, pr)
  51. assert.NoError(t, err)
  52. assert.EqualValues(t, 1, diffCount.Behind)
  53. assert.EqualValues(t, 1, diffCount.Ahead)
  54. assert.NoError(t, pr.LoadBaseRepo())
  55. assert.NoError(t, pr.LoadIssue())
  56. session := loginUser(t, "user2")
  57. token := getTokenForLoggedInUser(t, session)
  58. req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/pulls/%d/update?style=rebase&token="+token, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Issue.Index)
  59. session.MakeRequest(t, req, http.StatusOK)
  60. // Test GetDiverging after update
  61. diffCount, err = pull_service.GetDiverging(git.DefaultContext, pr)
  62. assert.NoError(t, err)
  63. assert.EqualValues(t, 0, diffCount.Behind)
  64. assert.EqualValues(t, 1, diffCount.Ahead)
  65. })
  66. }
  67. func createOutdatedPR(t *testing.T, actor, forkOrg *user_model.User) *models.PullRequest {
  68. baseRepo, err := repo_service.CreateRepository(actor, actor, models.CreateRepoOptions{
  69. Name: "repo-pr-update",
  70. Description: "repo-tmp-pr-update description",
  71. AutoInit: true,
  72. Gitignores: "C,C++",
  73. License: "MIT",
  74. Readme: "Default",
  75. IsPrivate: false,
  76. })
  77. assert.NoError(t, err)
  78. assert.NotEmpty(t, baseRepo)
  79. headRepo, err := repo_service.ForkRepository(git.DefaultContext, actor, forkOrg, repo_service.ForkRepoOptions{
  80. BaseRepo: baseRepo,
  81. Name: "repo-pr-update",
  82. Description: "desc",
  83. })
  84. assert.NoError(t, err)
  85. assert.NotEmpty(t, headRepo)
  86. // create a commit on base Repo
  87. _, err = files_service.CreateOrUpdateRepoFile(git.DefaultContext, baseRepo, actor, &files_service.UpdateRepoFileOptions{
  88. TreePath: "File_A",
  89. Message: "Add File A",
  90. Content: "File A",
  91. IsNewFile: true,
  92. OldBranch: "master",
  93. NewBranch: "master",
  94. Author: &files_service.IdentityOptions{
  95. Name: actor.Name,
  96. Email: actor.Email,
  97. },
  98. Committer: &files_service.IdentityOptions{
  99. Name: actor.Name,
  100. Email: actor.Email,
  101. },
  102. Dates: &files_service.CommitDateOptions{
  103. Author: time.Now(),
  104. Committer: time.Now(),
  105. },
  106. })
  107. assert.NoError(t, err)
  108. // create a commit on head Repo
  109. _, err = files_service.CreateOrUpdateRepoFile(git.DefaultContext, headRepo, actor, &files_service.UpdateRepoFileOptions{
  110. TreePath: "File_B",
  111. Message: "Add File on PR branch",
  112. Content: "File B",
  113. IsNewFile: true,
  114. OldBranch: "master",
  115. NewBranch: "newBranch",
  116. Author: &files_service.IdentityOptions{
  117. Name: actor.Name,
  118. Email: actor.Email,
  119. },
  120. Committer: &files_service.IdentityOptions{
  121. Name: actor.Name,
  122. Email: actor.Email,
  123. },
  124. Dates: &files_service.CommitDateOptions{
  125. Author: time.Now(),
  126. Committer: time.Now(),
  127. },
  128. })
  129. assert.NoError(t, err)
  130. // create Pull
  131. pullIssue := &models.Issue{
  132. RepoID: baseRepo.ID,
  133. Title: "Test Pull -to-update-",
  134. PosterID: actor.ID,
  135. Poster: actor,
  136. IsPull: true,
  137. }
  138. pullRequest := &models.PullRequest{
  139. HeadRepoID: headRepo.ID,
  140. BaseRepoID: baseRepo.ID,
  141. HeadBranch: "newBranch",
  142. BaseBranch: "master",
  143. HeadRepo: headRepo,
  144. BaseRepo: baseRepo,
  145. Type: models.PullRequestGitea,
  146. }
  147. err = pull_service.NewPullRequest(git.DefaultContext, baseRepo, pullIssue, nil, nil, pullRequest, nil)
  148. assert.NoError(t, err)
  149. issue := unittest.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue)
  150. pr, err := models.GetPullRequestByIssueID(issue.ID)
  151. assert.NoError(t, err)
  152. return pr
  153. }