From 7ddbf1a0151e16d15738261fc9f6b5ce5669bb16 Mon Sep 17 00:00:00 2001 From: Norwin Date: Thu, 17 Dec 2020 12:46:51 +0800 Subject: [PATCH] Fix GetCombinedStatus() (#470) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/470 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Lunny Xiao Co-Authored-By: Norwin Co-Committed-By: Norwin --- gitea/status.go | 16 ++++++--- gitea/status_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 gitea/status_test.go diff --git a/gitea/status.go b/gitea/status.go index 7c23b89..be43655 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -65,11 +65,11 @@ type ListStatusesOption struct { ListOptions } -// ListStatuses returns all statuses for a given Commit -func (c *Client) ListStatuses(owner, repo, sha string, opt ListStatusesOption) ([]*Status, *Response, error) { +// ListStatuses returns all statuses for a given Commit by ref +func (c *Client) ListStatuses(owner, repo, ref string, opt ListStatusesOption) ([]*Status, *Response, error) { opt.setDefaults() statuses := make([]*Status, 0, opt.PageSize) - resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, sha, opt.getURLQuery().Encode()), nil, nil, &statuses) + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses?%s", owner, repo, ref, opt.getURLQuery().Encode()), jsonHeader, nil, &statuses) return statuses, resp, err } @@ -85,8 +85,14 @@ type CombinedStatus struct { } // GetCombinedStatus returns the CombinedStatus for a given Commit -func (c *Client) GetCombinedStatus(owner, repo, sha string) (*CombinedStatus, *Response, error) { +func (c *Client) GetCombinedStatus(owner, repo, ref string) (*CombinedStatus, *Response, error) { status := new(CombinedStatus) - resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, sha), nil, nil, status) + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, ref), jsonHeader, nil, status) + + // gitea api return empty body if nothing here jet + if resp != nil && resp.StatusCode == 200 && err != nil { + return status, resp, nil + } + return status, resp, err } diff --git a/gitea/status_test.go b/gitea/status_test.go new file mode 100644 index 0000000..2bb3358 --- /dev/null +++ b/gitea/status_test.go @@ -0,0 +1,79 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package gitea + +import ( + "log" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestCommitStatus(t *testing.T) { + log.Println("== TestCommitStatus ==") + c := newTestClient() + user, _, err := c.GetMyUserInfo() + assert.NoError(t, err) + + var repoName = "CommitStatuses" + origRepo, err := createTestRepo(t, repoName, c) + if !assert.NoError(t, err) { + return + } + + commits, _, _ := c.ListRepoCommits(user.UserName, repoName, ListCommitOptions{ + ListOptions: ListOptions{}, + SHA: origRepo.DefaultBranch, + }) + if !assert.Len(t, commits, 1) { + return + } + sha := commits[0].SHA + + combiStats, resp, err := c.GetCombinedStatus(user.UserName, repoName, sha) + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.NotNil(t, combiStats) + assert.EqualValues(t, 0, combiStats.TotalCount) + + statuses, resp, err := c.ListStatuses(user.UserName, repoName, sha, ListStatusesOption{}) + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.NotNil(t, statuses) + assert.Len(t, statuses, 0) + + createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "start testing", "ultraCI", StatusPending) + createStatus(t, c, user.UserName, repoName, sha, "https://more.secure", "just a warning", "warn/bot", StatusWarning) + createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "test failed", "ultraCI", StatusFailure) + createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "start testing", "ultraCI", StatusPending) + createStatus(t, c, user.UserName, repoName, sha, "http://dummy.test", "test passed", "ultraCI", StatusSuccess) + + statuses, resp, err = c.ListStatuses(user.UserName, repoName, sha, ListStatusesOption{}) + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.NotNil(t, statuses) + assert.Len(t, statuses, 5) + + combiStats, resp, err = c.GetCombinedStatus(user.UserName, repoName, sha) + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.NotNil(t, combiStats) + assert.EqualValues(t, 2, combiStats.TotalCount) + assert.EqualValues(t, StatusState("warning"), combiStats.State) + assert.Len(t, combiStats.Statuses, 2) +} + +func createStatus(t *testing.T, c *Client, userName, repoName, sha, url, desc, context string, state StatusState) { + stats, resp, err := c.CreateStatus(userName, repoName, sha, CreateStatusOption{ + State: state, + TargetURL: url, + Description: desc, + Context: context, + }) + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.NotNil(t, stats) + assert.EqualValues(t, state, stats.State) +}