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_status_test.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. api "code.gitea.io/gitea/modules/structs"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestPullCreate_CommitStatus(t *testing.T) {
  16. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  17. session := loginUser(t, "user1")
  18. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  19. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
  20. url := path.Join("user1", "repo1", "compare", "master...status1")
  21. req := NewRequestWithValues(t, "POST", url,
  22. map[string]string{
  23. "_csrf": GetCSRF(t, session, url),
  24. "title": "pull request from status1",
  25. },
  26. )
  27. session.MakeRequest(t, req, http.StatusOK)
  28. req = NewRequest(t, "GET", "/user1/repo1/pulls")
  29. resp := session.MakeRequest(t, req, http.StatusOK)
  30. NewHTMLParser(t, resp.Body)
  31. // Request repository commits page
  32. req = NewRequest(t, "GET", "/user1/repo1/pulls/1/commits")
  33. resp = session.MakeRequest(t, req, http.StatusOK)
  34. doc := NewHTMLParser(t, resp.Body)
  35. // Get first commit URL
  36. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  37. assert.True(t, exists)
  38. assert.NotEmpty(t, commitURL)
  39. commitID := path.Base(commitURL)
  40. statusList := []api.CommitStatusState{
  41. api.CommitStatusPending,
  42. api.CommitStatusError,
  43. api.CommitStatusFailure,
  44. api.CommitStatusSuccess,
  45. api.CommitStatusWarning,
  46. }
  47. statesIcons := map[api.CommitStatusState]string{
  48. api.CommitStatusPending: "octicon-dot-fill",
  49. api.CommitStatusSuccess: "octicon-check",
  50. api.CommitStatusError: "gitea-exclamation",
  51. api.CommitStatusFailure: "octicon-x",
  52. api.CommitStatusWarning: "gitea-exclamation",
  53. }
  54. testCtx := NewAPITestContext(t, "user1", "repo1", auth_model.AccessTokenScopeWriteRepository)
  55. // Update commit status, and check if icon is updated as well
  56. for _, status := range statusList {
  57. // Call API to add status for commit
  58. t.Run("CreateStatus", doAPICreateCommitStatus(testCtx, commitID, api.CreateStatusOption{
  59. State: status,
  60. TargetURL: "http://test.ci/",
  61. Description: "",
  62. Context: "testci",
  63. }))
  64. req = NewRequestf(t, "GET", "/user1/repo1/pulls/1/commits")
  65. resp = session.MakeRequest(t, req, http.StatusOK)
  66. doc = NewHTMLParser(t, resp.Body)
  67. commitURL, exists = doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  68. assert.True(t, exists)
  69. assert.NotEmpty(t, commitURL)
  70. assert.EqualValues(t, commitID, path.Base(commitURL))
  71. cls, ok := doc.doc.Find("#commits-table tbody tr td.message .commit-status").Last().Attr("class")
  72. assert.True(t, ok)
  73. assert.Contains(t, cls, statesIcons[status])
  74. }
  75. })
  76. }
  77. func doAPICreateCommitStatus(ctx APITestContext, commitID string, data api.CreateStatusOption) func(*testing.T) {
  78. return func(t *testing.T) {
  79. req := NewRequestWithJSON(
  80. t,
  81. http.MethodPost,
  82. fmt.Sprintf("/api/v1/repos/%s/%s/statuses/%s?token=%s", ctx.Username, ctx.Reponame, commitID, ctx.Token),
  83. data,
  84. )
  85. if ctx.ExpectedCode != 0 {
  86. ctx.Session.MakeRequest(t, req, ctx.ExpectedCode)
  87. return
  88. }
  89. ctx.Session.MakeRequest(t, req, http.StatusCreated)
  90. }
  91. }
  92. func TestPullCreate_EmptyChangesWithDifferentCommits(t *testing.T) {
  93. // Merge must continue if commits SHA are different, even if content is same
  94. // Reason: gitflow and merging master back into develop, where is high possibility, there are no changes
  95. // but just commit saying "Merge branch". And this meta commit can be also tagged,
  96. // so we need to have this meta commit also in develop branch.
  97. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  98. session := loginUser(t, "user1")
  99. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  100. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
  101. testEditFileToNewBranch(t, session, "user1", "repo1", "status1", "status1", "README.md", "# repo1\n\nDescription for repo1")
  102. url := path.Join("user1", "repo1", "compare", "master...status1")
  103. req := NewRequestWithValues(t, "POST", url,
  104. map[string]string{
  105. "_csrf": GetCSRF(t, session, url),
  106. "title": "pull request from status1",
  107. },
  108. )
  109. session.MakeRequest(t, req, http.StatusOK)
  110. req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
  111. resp := session.MakeRequest(t, req, http.StatusOK)
  112. doc := NewHTMLParser(t, resp.Body)
  113. text := strings.TrimSpace(doc.doc.Find(".merge-section").Text())
  114. assert.Contains(t, text, "This pull request can be merged automatically.")
  115. })
  116. }
  117. func TestPullCreate_EmptyChangesWithSameCommits(t *testing.T) {
  118. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  119. session := loginUser(t, "user1")
  120. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  121. testCreateBranch(t, session, "user1", "repo1", "branch/master", "status1", http.StatusSeeOther)
  122. url := path.Join("user1", "repo1", "compare", "master...status1")
  123. req := NewRequestWithValues(t, "POST", url,
  124. map[string]string{
  125. "_csrf": GetCSRF(t, session, url),
  126. "title": "pull request from status1",
  127. },
  128. )
  129. session.MakeRequest(t, req, http.StatusOK)
  130. req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
  131. resp := session.MakeRequest(t, req, http.StatusOK)
  132. doc := NewHTMLParser(t, resp.Body)
  133. text := strings.TrimSpace(doc.doc.Find(".merge-section").Text())
  134. assert.Contains(t, text, "This branch is already included in the target branch. There is nothing to merge.")
  135. })
  136. }