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