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.

138 lines
3.9 KiB

  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/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 TestAPIPullUpdate(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. assert.NoError(t, pr.LoadBaseRepo())
  29. assert.NoError(t, pr.LoadIssue())
  30. session := loginUser(t, "user2")
  31. token := getTokenForLoggedInUser(t, session)
  32. req := NewRequestf(t, "POST", "/api/v1/repos/%s/%s/pulls/%d/update?token="+token, pr.BaseRepo.OwnerName, pr.BaseRepo.Name, pr.Issue.Index)
  33. session.MakeRequest(t, req, http.StatusOK)
  34. //Test GetDiverging after update
  35. diffCount, err = pull_service.GetDiverging(pr)
  36. assert.NoError(t, err)
  37. assert.EqualValues(t, 0, diffCount.Behind)
  38. assert.EqualValues(t, 2, diffCount.Ahead)
  39. })
  40. }
  41. func createOutdatedPR(t *testing.T, actor, forkOrg *models.User) *models.PullRequest {
  42. baseRepo, err := repo_service.CreateRepository(actor, actor, models.CreateRepoOptions{
  43. Name: "repo-pr-update",
  44. Description: "repo-tmp-pr-update description",
  45. AutoInit: true,
  46. Gitignores: "C,C++",
  47. License: "MIT",
  48. Readme: "Default",
  49. IsPrivate: false,
  50. })
  51. assert.NoError(t, err)
  52. assert.NotEmpty(t, baseRepo)
  53. headRepo, err := repo_module.ForkRepository(actor, forkOrg, baseRepo, "repo-pr-update", "desc")
  54. assert.NoError(t, err)
  55. assert.NotEmpty(t, headRepo)
  56. //create a commit on base Repo
  57. _, err = repofiles.CreateOrUpdateRepoFile(baseRepo, actor, &repofiles.UpdateRepoFileOptions{
  58. TreePath: "File_A",
  59. Message: "Add File A",
  60. Content: "File A",
  61. IsNewFile: true,
  62. OldBranch: "master",
  63. NewBranch: "master",
  64. Author: &repofiles.IdentityOptions{
  65. Name: actor.Name,
  66. Email: actor.Email,
  67. },
  68. Committer: &repofiles.IdentityOptions{
  69. Name: actor.Name,
  70. Email: actor.Email,
  71. },
  72. Dates: &repofiles.CommitDateOptions{
  73. Author: time.Now(),
  74. Committer: time.Now(),
  75. },
  76. })
  77. assert.NoError(t, err)
  78. //create a commit on head Repo
  79. _, err = repofiles.CreateOrUpdateRepoFile(headRepo, actor, &repofiles.UpdateRepoFileOptions{
  80. TreePath: "File_B",
  81. Message: "Add File on PR branch",
  82. Content: "File B",
  83. IsNewFile: true,
  84. OldBranch: "master",
  85. NewBranch: "newBranch",
  86. Author: &repofiles.IdentityOptions{
  87. Name: actor.Name,
  88. Email: actor.Email,
  89. },
  90. Committer: &repofiles.IdentityOptions{
  91. Name: actor.Name,
  92. Email: actor.Email,
  93. },
  94. Dates: &repofiles.CommitDateOptions{
  95. Author: time.Now(),
  96. Committer: time.Now(),
  97. },
  98. })
  99. assert.NoError(t, err)
  100. //create Pull
  101. pullIssue := &models.Issue{
  102. RepoID: baseRepo.ID,
  103. Title: "Test Pull -to-update-",
  104. PosterID: actor.ID,
  105. Poster: actor,
  106. IsPull: true,
  107. }
  108. pullRequest := &models.PullRequest{
  109. HeadRepoID: headRepo.ID,
  110. BaseRepoID: baseRepo.ID,
  111. HeadBranch: "newBranch",
  112. BaseBranch: "master",
  113. HeadRepo: headRepo,
  114. BaseRepo: baseRepo,
  115. Type: models.PullRequestGitea,
  116. }
  117. err = pull_service.NewPullRequest(baseRepo, pullIssue, nil, nil, pullRequest, nil)
  118. assert.NoError(t, err)
  119. issue := models.AssertExistsAndLoadBean(t, &models.Issue{Title: "Test Pull -to-update-"}).(*models.Issue)
  120. pr, err := models.GetPullRequestByIssueID(issue.ID)
  121. assert.NoError(t, err)
  122. return pr
  123. }