From ef5beaf553b15ae82f510fc0c9d2d407a798c9f9 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Wed, 16 Dec 2020 21:43:33 +0100 Subject: [PATCH 1/7] fix broken GetCombinedStatus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.. 🙃 --- gitea/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/status.go b/gitea/status.go index 7c23b89..1464f43 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -87,6 +87,6 @@ type CombinedStatus struct { // GetCombinedStatus returns the CombinedStatus for a given Commit func (c *Client) GetCombinedStatus(owner, repo, sha 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/statuses", owner, repo, sha), nil, nil, status) return status, resp, err } -- 2.40.1 From 28cd2735d93cde1a2bb7ea58903420921ad2a81f Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 23:42:49 +0100 Subject: [PATCH 2/7] fix ListStatuses --- gitea/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/status.go b/gitea/status.go index 1464f43..0b3eb1d 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -69,7 +69,7 @@ type ListStatusesOption struct { func (c *Client) ListStatuses(owner, repo, sha 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/statuses/%s?%s", owner, repo, sha, opt.getURLQuery().Encode()), jsonHeader, nil, &statuses) return statuses, resp, err } -- 2.40.1 From e3155ce55c3e49013cf2e1b4812570632f75fc4c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 23:43:16 +0100 Subject: [PATCH 3/7] rename sha to ref and use jsonHeader on GetCombinedStatus --- gitea/status.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gitea/status.go b/gitea/status.go index 0b3eb1d..91a7ce6 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -85,8 +85,8 @@ 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/statuses", owner, repo, sha), nil, nil, status) + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/statuses", owner, repo, ref), jsonHeader, nil, status) return status, resp, err } -- 2.40.1 From 64e7ed12fd97f22ccd6902082c5c3d716fcdd294 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 00:47:11 +0100 Subject: [PATCH 4/7] Add Tests --- gitea/status_test.go | 72 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 gitea/status_test.go diff --git a/gitea/status_test.go b/gitea/status_test.go new file mode 100644 index 0000000..b6f9ce5 --- /dev/null +++ b/gitea/status_test.go @@ -0,0 +1,72 @@ +// 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 + + 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, 5, combiStats.TotalCount) + +} + +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) +} -- 2.40.1 From da955d648e9fabc70f37f0349558a14fcb3208b4 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 01:30:42 +0100 Subject: [PATCH 5/7] enable tests for GetCombinedStatus() --- gitea/status_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gitea/status_test.go b/gitea/status_test.go index b6f9ce5..0b40e40 100644 --- a/gitea/status_test.go +++ b/gitea/status_test.go @@ -50,11 +50,11 @@ func TestCommitStatus(t *testing.T) { 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, 5, combiStats.TotalCount) + combiStats, resp, err := c.GetCombinedStatus(user.UserName, repoName, sha) + assert.NoError(t, err) + assert.NotNil(t, resp) + assert.NotNil(t, combiStats) + assert.EqualValues(t, 5, combiStats.TotalCount) } -- 2.40.1 From c8b1e7dc0333459200db026735b003ea20e00ed6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 02:02:12 +0100 Subject: [PATCH 6/7] next fix --- gitea/status.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gitea/status.go b/gitea/status.go index 91a7ce6..2b4d35d 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/statuses/%s?%s", owner, repo, sha, opt.getURLQuery().Encode()), jsonHeader, 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 } @@ -87,6 +87,6 @@ type CombinedStatus struct { // GetCombinedStatus returns the CombinedStatus for a given Commit 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/statuses", owner, repo, ref), jsonHeader, nil, status) + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/commits/%s/status", owner, repo, ref), jsonHeader, nil, status) return status, resp, err } -- 2.40.1 From bfee1fee14932c02f2e9453217efac6034ad2bf0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 02:10:01 +0100 Subject: [PATCH 7/7] final fix --- gitea/status.go | 6 ++++++ gitea/status_test.go | 13 ++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/gitea/status.go b/gitea/status.go index 2b4d35d..be43655 100644 --- a/gitea/status.go +++ b/gitea/status.go @@ -88,5 +88,11 @@ type CombinedStatus struct { 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, 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 index 0b40e40..2bb3358 100644 --- a/gitea/status_test.go +++ b/gitea/status_test.go @@ -32,6 +32,12 @@ func TestCommitStatus(t *testing.T) { } 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) @@ -50,12 +56,13 @@ func TestCommitStatus(t *testing.T) { assert.NotNil(t, statuses) assert.Len(t, statuses, 5) - combiStats, resp, err := c.GetCombinedStatus(user.UserName, repoName, 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, 5, combiStats.TotalCount) - + 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) { -- 2.40.1