From e5439430933411531dfc01fd18a70fd071f9fa3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauris=20Buk=C5=A1is-Haberkorns?= Date: Sun, 18 Nov 2018 22:36:58 +0200 Subject: [PATCH 1/2] Support for git refs listing API --- gitea/repo_refs.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 gitea/repo_refs.go diff --git a/gitea/repo_refs.go b/gitea/repo_refs.go new file mode 100644 index 0000000..981dca0 --- /dev/null +++ b/gitea/repo_refs.go @@ -0,0 +1,69 @@ +// Copyright 2018 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 ( + "encoding/json" + "errors" + "fmt" + "strings" +) + +// Reference represents a Git reference. +type Reference struct { + Ref string `json:"ref"` + URL string `json:"url"` + Object *GitObject `json:"object"` +} + +// GitObject represents a Git object. +type GitObject struct { + Type string `json:"type"` + SHA string `json:"sha"` + URL string `json:"url"` +} + +// GetRepoRef get one ref's information of one repository +func (c *Client) GetRepoRef(user, repo, ref string) (*Reference, error) { + ref = strings.TrimPrefix(ref, "refs/") + r := new(Reference) + err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/refs/%s", user, repo, ref), nil, nil, &r) + if _, ok := err.(*json.UnmarshalTypeError); ok { + // Multiple refs + return nil, errors.New("no exact match found for this ref") + } else if err != nil { + return nil, err + } + + return r, nil +} + +// GetRepoRefs get list of ref's information of one repository +func (c *Client) GetRepoRefs(user, repo, ref string) ([]*Reference, error) { + ref = strings.TrimPrefix(ref, "refs/") + resp, err := c.getResponse("GET", fmt.Sprintf("/repos/%s/%s/git/refs/%s", user, repo, ref), nil, nil) + if err != nil { + return nil, err + } + + // Aattemt to unmarshal single returned ref. + r := new(Reference) + refErr := json.Unmarshal(resp, r) + if refErr == nil { + return []*Reference{r}, nil + } + + // Attempt to unmarshal multiple refs. + var rs []*Reference + refsErr := json.Unmarshal(resp, &rs) + if refsErr == nil { + if len(rs) == 0 { + return nil, errors.New("unexpected response: an array of refs with length 0") + } + return rs, nil + } + + return nil, fmt.Errorf("unmarshalling failed for both single and multiple refs: %s and %s", refErr, refsErr) +} From ff7594b9019633bd6356cf38a3f977b60d70f00b Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Mon, 19 Nov 2018 13:45:36 +0200 Subject: [PATCH 2/2] Fix typo --- gitea/repo_refs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gitea/repo_refs.go b/gitea/repo_refs.go index 981dca0..b946a10 100644 --- a/gitea/repo_refs.go +++ b/gitea/repo_refs.go @@ -48,7 +48,7 @@ func (c *Client) GetRepoRefs(user, repo, ref string) ([]*Reference, error) { return nil, err } - // Aattemt to unmarshal single returned ref. + // Attempt to unmarshal single returned ref. r := new(Reference) refErr := json.Unmarshal(resp, r) if refErr == nil {