2017-05-07 10:40:31 -04:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2017-05-07 10:40:31 -04:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
import (
|
2022-11-30 11:41:49 -05:00
|
|
|
"fmt"
|
2017-05-07 10:40:31 -04:00
|
|
|
"net/http"
|
2019-08-08 22:13:03 -04:00
|
|
|
"net/http/httptest"
|
2017-05-07 10:40:31 -04:00
|
|
|
"path"
|
2022-11-30 11:41:49 -05:00
|
|
|
"sync"
|
2017-05-07 10:40:31 -04:00
|
|
|
"testing"
|
|
|
|
|
2021-07-24 12:03:58 -04:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-08-08 22:13:03 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 06:21:34 -04:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2022-09-02 15:18:23 -04:00
|
|
|
"code.gitea.io/gitea/tests"
|
2017-06-17 00:49:45 -04:00
|
|
|
|
2017-05-07 10:40:31 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRepoCommits(t *testing.T) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2017-05-07 10:40:31 -04:00
|
|
|
|
2017-06-17 00:49:45 -04:00
|
|
|
session := loginUser(t, "user2")
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
// Request repository commits page
|
2017-10-29 22:04:25 -04:00
|
|
|
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
|
2017-07-07 15:36:47 -04:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
2017-05-07 10:40:31 -04:00
|
|
|
|
2017-06-17 12:29:59 -04:00
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
2017-05-07 10:40:31 -04:00
|
|
|
commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
|
|
|
|
assert.True(t, exists)
|
|
|
|
assert.NotEmpty(t, commitURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2017-05-07 10:40:31 -04:00
|
|
|
|
2017-06-17 00:49:45 -04:00
|
|
|
session := loginUser(t, "user2")
|
2017-05-07 10:40:31 -04:00
|
|
|
|
|
|
|
// Request repository commits page
|
2017-10-29 22:04:25 -04:00
|
|
|
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
|
2017-07-07 15:36:47 -04:00
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
2017-05-07 10:40:31 -04:00
|
|
|
|
2017-06-17 12:29:59 -04:00
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
2017-05-07 10:40:31 -04:00
|
|
|
// Get first commit URL
|
|
|
|
commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
|
|
|
|
assert.True(t, exists)
|
|
|
|
assert.NotEmpty(t, commitURL)
|
|
|
|
|
|
|
|
// Call API to add status for commit
|
2022-05-07 13:05:52 -04:00
|
|
|
t.Run("CreateStatus", doAPICreateCommitStatus(NewAPITestContext(t, "user2", "repo1"), path.Base(commitURL), api.CommitStatusState(state)))
|
2017-05-07 10:40:31 -04:00
|
|
|
|
2017-10-29 22:04:25 -04:00
|
|
|
req = NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
|
2017-07-07 15:36:47 -04:00
|
|
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
2017-05-07 10:40:31 -04:00
|
|
|
|
2017-06-17 12:29:59 -04:00
|
|
|
doc = NewHTMLParser(t, resp.Body)
|
2017-05-07 10:40:31 -04:00
|
|
|
// Check if commit status is displayed in message column
|
2022-08-09 17:55:29 -04:00
|
|
|
sel := doc.doc.Find("#commits-table tbody tr td.message a.commit-statuses-trigger .commit-status")
|
2021-06-07 01:27:09 -04:00
|
|
|
assert.Equal(t, 1, sel.Length())
|
2017-05-07 10:40:31 -04:00
|
|
|
for _, class := range classes {
|
|
|
|
assert.True(t, sel.HasClass(class))
|
|
|
|
}
|
2019-08-08 22:13:03 -04:00
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
// By SHA
|
2019-08-08 22:13:03 -04:00
|
|
|
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/statuses")
|
2021-07-13 03:14:14 -04:00
|
|
|
reqOne := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/status")
|
|
|
|
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
|
2022-12-27 08:12:49 -05:00
|
|
|
|
|
|
|
// By short SHA
|
|
|
|
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)[:10]+"/statuses")
|
|
|
|
reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)[:10]+"/status")
|
|
|
|
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
|
2021-07-13 03:14:14 -04:00
|
|
|
|
2022-01-20 12:46:10 -05:00
|
|
|
// By Ref
|
2019-08-08 22:13:03 -04:00
|
|
|
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/statuses")
|
2021-07-13 03:14:14 -04:00
|
|
|
reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/status")
|
|
|
|
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
|
2019-08-08 22:13:03 -04:00
|
|
|
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/v1.1/statuses")
|
2021-07-13 03:14:14 -04:00
|
|
|
reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/v1.1/status")
|
|
|
|
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
|
2019-08-08 22:13:03 -04:00
|
|
|
}
|
|
|
|
|
2021-07-13 03:14:14 -04:00
|
|
|
func testRepoCommitsWithStatus(t *testing.T, resp, respOne *httptest.ResponseRecorder, state string) {
|
|
|
|
var statuses []*api.CommitStatus
|
|
|
|
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &statuses))
|
|
|
|
var status api.CombinedStatus
|
|
|
|
assert.NoError(t, json.Unmarshal(respOne.Body.Bytes(), &status))
|
|
|
|
assert.NotNil(t, status)
|
|
|
|
|
|
|
|
if assert.Len(t, statuses, 1) {
|
|
|
|
assert.Equal(t, api.CommitStatusState(state), statuses[0].State)
|
|
|
|
assert.Equal(t, setting.AppURL+"api/v1/repos/user2/repo1/statuses/65f1bf27bc3bf70f64657658635e66094edbcb4d", statuses[0].URL)
|
|
|
|
assert.Equal(t, "http://test.ci/", statuses[0].TargetURL)
|
|
|
|
assert.Equal(t, "", statuses[0].Description)
|
|
|
|
assert.Equal(t, "testci", statuses[0].Context)
|
|
|
|
|
|
|
|
assert.Len(t, status.Statuses, 1)
|
|
|
|
assert.Equal(t, statuses[0], status.Statuses[0])
|
|
|
|
assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", status.SHA)
|
2019-08-08 22:13:03 -04:00
|
|
|
}
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoCommitsWithStatusPending(t *testing.T) {
|
2022-08-09 17:55:29 -04:00
|
|
|
doTestRepoCommitWithStatus(t, "pending", "octicon-dot-fill", "yellow")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoCommitsWithStatusSuccess(t *testing.T) {
|
2022-08-09 17:55:29 -04:00
|
|
|
doTestRepoCommitWithStatus(t, "success", "octicon-check", "green")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoCommitsWithStatusError(t *testing.T) {
|
2022-08-09 17:55:29 -04:00
|
|
|
doTestRepoCommitWithStatus(t, "error", "gitea-exclamation", "red")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoCommitsWithStatusFailure(t *testing.T) {
|
2022-08-09 17:55:29 -04:00
|
|
|
doTestRepoCommitWithStatus(t, "failure", "octicon-x", "red")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoCommitsWithStatusWarning(t *testing.T) {
|
2022-08-09 17:55:29 -04:00
|
|
|
doTestRepoCommitWithStatus(t, "warning", "gitea-exclamation", "yellow")
|
2017-05-07 10:40:31 -04:00
|
|
|
}
|
2022-11-30 11:41:49 -05:00
|
|
|
|
|
|
|
func TestRepoCommitsStatusParallel(t *testing.T) {
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
|
|
|
// Request repository commits page
|
|
|
|
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
doc := NewHTMLParser(t, resp.Body)
|
|
|
|
// Get first commit URL
|
|
|
|
commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
|
|
|
|
assert.True(t, exists)
|
|
|
|
assert.NotEmpty(t, commitURL)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
2022-12-01 22:15:36 -05:00
|
|
|
go func(parentT *testing.T, i int) {
|
|
|
|
parentT.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) {
|
2022-11-30 11:41:49 -05:00
|
|
|
runBody := doAPICreateCommitStatus(NewAPITestContext(t, "user2", "repo1"), path.Base(commitURL), api.CommitStatusState("pending"))
|
|
|
|
runBody(t)
|
|
|
|
wg.Done()
|
|
|
|
})
|
|
|
|
}(t, i)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|