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.

120 lines
4.0 KiB

  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. 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.StatusFound)
  28. req = NewRequest(t, "GET", "/user1/repo1/pulls")
  29. resp := session.MakeRequest(t, req, http.StatusOK)
  30. doc := 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.CommitStatusWarning,
  45. api.CommitStatusSuccess,
  46. }
  47. statesIcons := map[api.CommitStatusState]string{
  48. api.CommitStatusPending: "circle icon yellow",
  49. api.CommitStatusSuccess: "check icon green",
  50. api.CommitStatusError: "warning icon red",
  51. api.CommitStatusFailure: "remove icon red",
  52. api.CommitStatusWarning: "warning sign icon yellow",
  53. }
  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. token := getTokenForLoggedInUser(t, session)
  58. req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/user1/repo1/statuses/%s?token=%s", commitID, token),
  59. api.CreateStatusOption{
  60. State: api.StatusState(status),
  61. TargetURL: "http://test.ci/",
  62. Description: "",
  63. Context: "testci",
  64. },
  65. )
  66. session.MakeRequest(t, req, http.StatusCreated)
  67. req = NewRequestf(t, "GET", "/user1/repo1/pulls/1/commits")
  68. resp = session.MakeRequest(t, req, http.StatusOK)
  69. doc = NewHTMLParser(t, resp.Body)
  70. commitURL, exists = doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href")
  71. assert.True(t, exists)
  72. assert.NotEmpty(t, commitURL)
  73. assert.EqualValues(t, commitID, path.Base(commitURL))
  74. cls, ok := doc.doc.Find("#commits-table tbody tr td.message i.commit-status").Last().Attr("class")
  75. assert.True(t, ok)
  76. assert.EqualValues(t, "commit-status "+statesIcons[status], cls)
  77. }
  78. })
  79. }
  80. func TestPullCreate_EmptyChangesWithCommits(t *testing.T) {
  81. onGiteaRun(t, func(t *testing.T, u *url.URL) {
  82. session := loginUser(t, "user1")
  83. testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
  84. testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1")
  85. testEditFileToNewBranch(t, session, "user1", "repo1", "status1", "status1", "README.md", "# repo1\n\nDescription for repo1")
  86. url := path.Join("user1", "repo1", "compare", "master...status1")
  87. req := NewRequestWithValues(t, "POST", url,
  88. map[string]string{
  89. "_csrf": GetCSRF(t, session, url),
  90. "title": "pull request from status1",
  91. },
  92. )
  93. session.MakeRequest(t, req, http.StatusFound)
  94. req = NewRequest(t, "GET", "/user1/repo1/pulls/1")
  95. resp := session.MakeRequest(t, req, http.StatusOK)
  96. doc := NewHTMLParser(t, resp.Body)
  97. text := strings.TrimSpace(doc.doc.Find(".item.text.green").Text())
  98. assert.EqualValues(t, "This pull request can be merged automatically.", text)
  99. })
  100. }