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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2019 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. "fmt"
  7. "net/http"
  8. "net/url"
  9. "path"
  10. "strings"
  11. "testing"
  12. "code.gitea.io/gitea/models"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "github.com/stretchr/testify/assert"
  15. )
  16. func TestPullCreate_CommitStatus(t *testing.T) {
  17. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  18. session := loginUser(t, "user1")
  19. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  20. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
  21. url := path.Join("user1", "repo1", "compare", "master...status1")
  22. req := NewRequestWithValues(t, "POST", url,
  23. map[string]string{
  24. "_csrf": GetCSRF(t, session, url),
  25. "title": "pull request from status1",
  26. },
  27. )
  28. session.MakeRequest(t, req, http.StatusFound)
  29. req = NewRequest(t, "GET", "/user1/repo1/pulls")
  30. resp := session.MakeRequest(t, req, http.StatusOK)
  31. doc := NewHTMLParser(t, resp.Body)
  32. // Request repository commits page
  33. req = NewRequest(t, "GET", "/user1/repo1/pulls/1/commits")
  34. resp = session.MakeRequest(t, req, http.StatusOK)
  35. doc = NewHTMLParser(t, resp.Body)
  36. // Get first commit URL
  37. commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  38. assert.True(t, exists)
  39. assert.NotEmpty(t, commitURL)
  40. commitID := path.Base(commitURL)
  41. statusList := []models.CommitStatusState{
  42. models.CommitStatusPending,
  43. models.CommitStatusError,
  44. models.CommitStatusFailure,
  45. models.CommitStatusWarning,
  46. models.CommitStatusSuccess,
  47. }
  48. statesIcons := map[models.CommitStatusState]string{
  49. models.CommitStatusPending: "circle icon yellow",
  50. models.CommitStatusSuccess: "check icon green",
  51. models.CommitStatusError: "warning icon red",
  52. models.CommitStatusFailure: "remove icon red",
  53. models.CommitStatusWarning: "warning sign icon yellow",
  54. }
  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. token := getTokenForLoggedInUser(t, session)
  59. req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/user1/repo1/statuses/%s?token=%s", commitID, token),
  60. api.CreateStatusOption{
  61. State: api.StatusState(status),
  62. TargetURL: "http://test.ci/",
  63. Description: "",
  64. Context: "testci",
  65. },
  66. )
  67. session.MakeRequest(t, req, http.StatusCreated)
  68. req = NewRequestf(t, "GET", "/user1/repo1/pulls/1/commits")
  69. resp = session.MakeRequest(t, req, http.StatusOK)
  70. doc = NewHTMLParser(t, resp.Body)
  71. commitURL, exists = doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  72. assert.True(t, exists)
  73. assert.NotEmpty(t, commitURL)
  74. assert.EqualValues(t, commitID, path.Base(commitURL))
  75. cls, ok := doc.doc.Find("#commits-table tbody tr td.message i.commit-status").Last().Attr("class")
  76. assert.True(t, ok)
  77. assert.EqualValues(t, "commit-status "+statesIcons[status], cls)
  78. }
  79. })
  80. }
  81. func TestPullCreate_EmptyChangesWithCommits(t *testing.T) {
  82. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  83. session := loginUser(t, "user1")
  84. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  85. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
  86. testEditFileToNewBranch(t, session, "user1", "repo1", "status1", "status1", "README.md", "# repo1\n\nDescription for repo1")
  87. url := path.Join("user1", "repo1", "compare", "master...status1")
  88. req := NewRequestWithValues(t, "POST", url,
  89. map[string]string{
  90. "_csrf": GetCSRF(t, session, url),
  91. "title": "pull request from status1",
  92. },
  93. )
  94. session.MakeRequest(t, req, http.StatusFound)
  95. req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
  96. resp := session.MakeRequest(t, req, http.StatusOK)
  97. doc := NewHTMLParser(t, resp.Body)
  98. text := strings.TrimSpace(doc.doc.Find(".item.text.green").Text())
  99. assert.EqualValues(t, "This pull request can be merged automatically.", text)
  100. })
  101. }