gitea源码

pull_status_test.go 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "path"
  9. "strings"
  10. "testing"
  11. auth_model "code.gitea.io/gitea/models/auth"
  12. git_model "code.gitea.io/gitea/models/git"
  13. "code.gitea.io/gitea/models/issues"
  14. repo_model "code.gitea.io/gitea/models/repo"
  15. "code.gitea.io/gitea/models/unittest"
  16. "code.gitea.io/gitea/modules/commitstatus"
  17. "code.gitea.io/gitea/modules/setting"
  18. api "code.gitea.io/gitea/modules/structs"
  19. "code.gitea.io/gitea/modules/test"
  20. "code.gitea.io/gitea/services/pull"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. func TestPullCreate_CommitStatus(t *testing.T) {
  24. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  25. session := loginUser(t, "user1")
  26. testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "")
  27. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
  28. url := path.Join("user1", "repo1", "compare", "master...status1")
  29. req := NewRequestWithValues(t, "POST", url,
  30. map[string]string{
  31. "_csrf": GetUserCSRFToken(t, session),
  32. "title": "pull request from status1",
  33. },
  34. )
  35. session.MakeRequest(t, req, http.StatusOK)
  36. req = NewRequest(t, "GET", "/user1/repo1/pulls")
  37. resp := session.MakeRequest(t, req, http.StatusOK)
  38. NewHTMLParser(t, resp.Body)
  39. // Request repository commits page
  40. req = NewRequest(t, "GET", "/user1/repo1/pulls/1/commits")
  41. resp = session.MakeRequest(t, req, http.StatusOK)
  42. doc := NewHTMLParser(t, resp.Body)
  43. // Get first commit URL
  44. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  45. assert.True(t, exists)
  46. assert.NotEmpty(t, commitURL)
  47. commitID := path.Base(commitURL)
  48. statusList := []commitstatus.CommitStatusState{
  49. commitstatus.CommitStatusPending,
  50. commitstatus.CommitStatusError,
  51. commitstatus.CommitStatusFailure,
  52. commitstatus.CommitStatusSuccess,
  53. commitstatus.CommitStatusWarning,
  54. }
  55. statesIcons := map[commitstatus.CommitStatusState]string{
  56. commitstatus.CommitStatusPending: "octicon-dot-fill",
  57. commitstatus.CommitStatusSuccess: "octicon-check",
  58. commitstatus.CommitStatusError: "gitea-exclamation",
  59. commitstatus.CommitStatusFailure: "octicon-x",
  60. commitstatus.CommitStatusWarning: "gitea-exclamation",
  61. }
  62. testCtx := NewAPITestContext(t, "user1", "repo1", auth_model.AccessTokenScopeWriteRepository)
  63. // Update commit status, and check if icon is updated as well
  64. for _, status := range statusList {
  65. // Call API to add status for commit
  66. t.Run("CreateStatus", doAPICreateCommitStatus(testCtx, commitID, api.CreateStatusOption{
  67. State: status,
  68. TargetURL: "http://test.ci/",
  69. Description: "",
  70. Context: "testci",
  71. }))
  72. req = NewRequest(t, "GET", "/user1/repo1/pulls/1/commits")
  73. resp = session.MakeRequest(t, req, http.StatusOK)
  74. doc = NewHTMLParser(t, resp.Body)
  75. commitURL, exists = doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  76. assert.True(t, exists)
  77. assert.NotEmpty(t, commitURL)
  78. assert.Equal(t, commitID, path.Base(commitURL))
  79. cls, ok := doc.doc.Find("#commits-table tbody tr td.message .commit-status").Last().Attr("class")
  80. assert.True(t, ok)
  81. assert.Contains(t, cls, statesIcons[status])
  82. }
  83. repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: "repo1"})
  84. css := unittest.AssertExistsAndLoadBean(t, &git_model.CommitStatusSummary{RepoID: repo1.ID, SHA: commitID})
  85. assert.Equal(t, commitstatus.CommitStatusSuccess, css.State)
  86. })
  87. }
  88. func doAPICreateCommitStatus(ctx APITestContext, commitID string, data api.CreateStatusOption) func(*testing.T) {
  89. return func(t *testing.T) {
  90. req := NewRequestWithJSON(
  91. t,
  92. http.MethodPost,
  93. fmt.Sprintf("/api/v1/repos/%s/%s/statuses/%s", ctx.Username, ctx.Reponame, commitID),
  94. data,
  95. ).AddTokenAuth(ctx.Token)
  96. if ctx.ExpectedCode != 0 {
  97. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  98. return
  99. }
  100. ctx.Session.MakeRequest(t, req, http.StatusCreated)
  101. }
  102. }
  103. func TestPullCreate_EmptyChangesWithDifferentCommits(t *testing.T) {
  104. // Merge must continue if commits SHA are different, even if content is same
  105. // Reason: gitflow and merging master back into develop, where is high possibility, there are no changes
  106. // but just commit saying "Merge branch". And this meta commit can be also tagged,
  107. // so we need to have this meta commit also in develop branch.
  108. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  109. session := loginUser(t, "user1")
  110. testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "")
  111. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
  112. testEditFile(t, session, "user1", "repo1", "status1", "README.md", "# repo1\n\nDescription for repo1")
  113. url := path.Join("user1", "repo1", "compare", "master...status1")
  114. req := NewRequestWithValues(t, "POST", url,
  115. map[string]string{
  116. "_csrf": GetUserCSRFToken(t, session),
  117. "title": "pull request from status1",
  118. },
  119. )
  120. session.MakeRequest(t, req, http.StatusOK)
  121. req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
  122. resp := session.MakeRequest(t, req, http.StatusOK)
  123. doc := NewHTMLParser(t, resp.Body)
  124. text := strings.TrimSpace(doc.doc.Find(".merge-section").Text())
  125. assert.Contains(t, text, "This pull request can be merged automatically.")
  126. })
  127. }
  128. func TestPullCreate_EmptyChangesWithSameCommits(t *testing.T) {
  129. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  130. session := loginUser(t, "user1")
  131. testRepoFork(t, session, "user2", "repo1", "user1", "repo1", "")
  132. testCreateBranch(t, session, "user1", "repo1", "branch/master", "status1", http.StatusSeeOther)
  133. url := path.Join("user1", "repo1", "compare", "master...status1")
  134. req := NewRequestWithValues(t, "POST", url,
  135. map[string]string{
  136. "_csrf": GetUserCSRFToken(t, session),
  137. "title": "pull request from status1",
  138. },
  139. )
  140. session.MakeRequest(t, req, http.StatusOK)
  141. req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
  142. resp := session.MakeRequest(t, req, http.StatusOK)
  143. doc := NewHTMLParser(t, resp.Body)
  144. text := strings.TrimSpace(doc.doc.Find(".merge-section").Text())
  145. assert.Contains(t, text, "This branch is already included in the target branch. There is nothing to merge.")
  146. })
  147. }
  148. func TestPullStatusDelayCheck(t *testing.T) {
  149. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  150. defer test.MockVariableValue(&setting.Repository.PullRequest.DelayCheckForInactiveDays, 1)()
  151. defer test.MockVariableValue(&pull.AddPullRequestToCheckQueue)()
  152. session := loginUser(t, "user2")
  153. run := func(t *testing.T, fn func(*testing.T)) (issue3 *issues.Issue, checkedPrID int64) {
  154. pull.AddPullRequestToCheckQueue = func(prID int64) {
  155. checkedPrID = prID
  156. }
  157. fn(t)
  158. issue3 = unittest.AssertExistsAndLoadBean(t, &issues.Issue{RepoID: 1, Index: 3})
  159. _ = issue3.LoadPullRequest(t.Context())
  160. return issue3, checkedPrID
  161. }
  162. assertReloadingInterval := func(t *testing.T, interval string) {
  163. req := NewRequest(t, "GET", "/user2/repo1/pulls/3")
  164. resp := session.MakeRequest(t, req, http.StatusOK)
  165. attr := "data-pull-merge-box-reloading-interval"
  166. if interval == "" {
  167. assert.NotContains(t, resp.Body.String(), attr)
  168. } else {
  169. assert.Contains(t, resp.Body.String(), fmt.Sprintf(`%s="%v"`, attr, interval))
  170. }
  171. }
  172. // PR issue3 is merageable at the beginning
  173. issue3, checkedPrID := run(t, func(t *testing.T) {})
  174. assert.Equal(t, issues.PullRequestStatusMergeable, issue3.PullRequest.Status)
  175. assert.Zero(t, checkedPrID)
  176. assertReloadingInterval(t, "") // the PR is mergeable, so no need to reload the merge box
  177. // setting.IsProd = false // it would cause data-race because the queue handlers might be running and reading its value
  178. // assertReloadingInterval(t, "1") // make sure dev mode always do merge box reloading, to make sure the UI logic won't break
  179. // setting.IsProd = true
  180. // when base branch changes, PR status should be updated, but it is inactive for long time, so no real check
  181. issue3, checkedPrID = run(t, func(t *testing.T) {
  182. testEditFile(t, session, "user2", "repo1", "master", "README.md", "new content 1")
  183. })
  184. assert.Equal(t, issues.PullRequestStatusChecking, issue3.PullRequest.Status)
  185. assert.Zero(t, checkedPrID)
  186. assertReloadingInterval(t, "2000") // the PR status is "checking", so try to reload the merge box
  187. // view a PR with status=checking, it starts the real check
  188. issue3, checkedPrID = run(t, func(t *testing.T) {
  189. req := NewRequest(t, "GET", "/user2/repo1/pulls/3")
  190. session.MakeRequest(t, req, http.StatusOK)
  191. })
  192. assert.Equal(t, issues.PullRequestStatusChecking, issue3.PullRequest.Status)
  193. assert.Equal(t, issue3.PullRequest.ID, checkedPrID)
  194. // when base branch changes, still so no real check
  195. issue3, checkedPrID = run(t, func(t *testing.T) {
  196. testEditFile(t, session, "user2", "repo1", "master", "README.md", "new content 2")
  197. })
  198. assert.Equal(t, issues.PullRequestStatusChecking, issue3.PullRequest.Status)
  199. assert.Zero(t, checkedPrID)
  200. // then allow to check PRs without delay, when base branch changes, the PRs will be checked
  201. setting.Repository.PullRequest.DelayCheckForInactiveDays = -1
  202. issue3, checkedPrID = run(t, func(t *testing.T) {
  203. testEditFile(t, session, "user2", "repo1", "master", "README.md", "new content 3")
  204. })
  205. assert.Equal(t, issues.PullRequestStatusChecking, issue3.PullRequest.Status)
  206. assert.Equal(t, issue3.PullRequest.ID, checkedPrID)
  207. })
  208. }