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.4KB

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