Skip to content

Commit 7ddbf1a

Browse files
Norwinnoerw6543
authored andcommitted
Fix GetCombinedStatus() (#470)
fix broken GetCombinedStatus weirdly enough, gitea API responds to .../status properly, if ref has a status. but if there is no status, an empty response is returned. ../statuses handles both correctly.. 🙃 fix ListStatuses rename sha to ref and use jsonHeader on GetCombinedStatus Add Tests enable tests for GetCombinedStatus() next fix final fix Co-authored-by: Norwin Roosen <[email protected]> Co-authored-by: 6543 <[email protected]> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/470 Reviewed-by: 6543 <[email protected]> Reviewed-by: Lunny Xiao <[email protected]> Co-Authored-By: Norwin <[email protected]> Co-Committed-By: Norwin <[email protected]>
1 parent cd52f00 commit 7ddbf1a

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

gitea/status.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ type ListStatusesOption struct {
6565
ListOptions
6666
}
6767

68-
// ListStatuses returns all statuses for a given Commit
69-
func (c *Client) ListStatuses(owner, repo, sha string, opt ListStatusesOption) ([]*Status, *Response, error) {
68+
// ListStatuses returns all statuses for a given Commit by ref
69+
func (c *Client) ListStatuses(owner, repo, ref string, opt ListStatusesOption) ([]*Status, *Response, error) {
7070
opt.setDefaults()
7171
statuses := make([]*Status, 0, opt.PageSize)
72-
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, sha, opt.getURLQuery().Encode()), nil, nil, &statuses)
72+
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, ref, opt.getURLQuery().Encode()), jsonHeader, nil, &statuses)
7373
return statuses, resp, err
7474
}
7575

@@ -85,8 +85,14 @@ type CombinedStatus struct {
8585
}
8686

8787
// GetCombinedStatus returns the CombinedStatus for a given Commit
88-
func (c *Client) GetCombinedStatus(owner, repo, sha string) (*CombinedStatus, *Response, error) {
88+
func (c *Client) GetCombinedStatus(owner, repo, ref string) (*CombinedStatus, *Response, error) {
8989
status := new(CombinedStatus)
90-
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, sha), nil, nil, status)
90+
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, ref), jsonHeader, nil, status)
91+
92+
// gitea api return empty body if nothing here jet
93+
if resp != nil && resp.StatusCode == 200 && err != nil {
94+
return status, resp, nil
95+
}
96+
9197
return status, resp, err
9298
}

gitea/status_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2020 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+
5+
package gitea
6+
7+
import (
8+
"log"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCommitStatus(t *testing.T) {
15+
log.Println("== TestCommitStatus ==")
16+
c := newTestClient()
17+
user, _, err := c.GetMyUserInfo()
18+
assert.NoError(t, err)
19+
20+
var repoName = "CommitStatuses"
21+
origRepo, err := createTestRepo(t, repoName, c)
22+
if !assert.NoError(t, err) {
23+
return
24+
}
25+
26+
commits, _, _ := c.ListRepoCommits(user.UserName, repoName, ListCommitOptions{
27+
ListOptions: ListOptions{},
28+
SHA: origRepo.DefaultBranch,
29+
})
30+
if !assert.Len(t, commits, 1) {
31+
return
32+
}
33+
sha := commits[0].SHA
34+
35+
combiStats, resp, err := c.GetCombinedStatus(user.UserName, repoName, sha)
36+
assert.NoError(t, err)
37+
assert.NotNil(t, resp)
38+
assert.NotNil(t, combiStats)
39+
assert.EqualValues(t, 0, combiStats.TotalCount)
40+
41+
statuses, resp, err := c.ListStatuses(user.UserName, repoName, sha, ListStatusesOption{})
42+
assert.NoError(t, err)
43+
assert.NotNil(t, resp)
44+
assert.NotNil(t, statuses)
45+
assert.Len(t, statuses, 0)
46+
47+
createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "start testing", "ultraCI", StatusPending)
48+
createStatus(t, c, user.UserName, repoName, sha, "https://more.secure", "just a warning", "warn/bot", StatusWarning)
49+
createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "test failed", "ultraCI", StatusFailure)
50+
createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "start testing", "ultraCI", StatusPending)
51+
createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "test passed", "ultraCI", StatusSuccess)
52+
53+
statuses, resp, err = c.ListStatuses(user.UserName, repoName, sha, ListStatusesOption{})
54+
assert.NoError(t, err)
55+
assert.NotNil(t, resp)
56+
assert.NotNil(t, statuses)
57+
assert.Len(t, statuses, 5)
58+
59+
combiStats, resp, err = c.GetCombinedStatus(user.UserName, repoName, sha)
60+
assert.NoError(t, err)
61+
assert.NotNil(t, resp)
62+
assert.NotNil(t, combiStats)
63+
assert.EqualValues(t, 2, combiStats.TotalCount)
64+
assert.EqualValues(t, StatusState("warning"), combiStats.State)
65+
assert.Len(t, combiStats.Statuses, 2)
66+
}
67+
68+
func createStatus(t *testing.T, c *Client, userName, repoName, sha, url, desc, context string, state StatusState) {
69+
stats, resp, err := c.CreateStatus(userName, repoName, sha, CreateStatusOption{
70+
State: state,
71+
TargetURL: url,
72+
Description: desc,
73+
Context: context,
74+
})
75+
assert.NoError(t, err)
76+
assert.NotNil(t, resp)
77+
assert.NotNil(t, stats)
78+
assert.EqualValues(t, state, stats.State)
79+
}

0 commit comments

Comments
 (0)