From ffaf5e0d1e75166ed55a8cd7dff51765aa351c48 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.gitea.io> Date: Sun, 9 Feb 2020 01:08:43 +0000 Subject: [PATCH] Add ListRepoCommits (#266) Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: spawn2kill Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/266 Reviewed-by: Andrew Thornton Reviewed-by: Lunny Xiao --- Makefile | 2 +- gitea/repo_commit.go | 26 ++++++++++++++++++++++++++ gitea/repo_commit_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 gitea/repo_commit_test.go diff --git a/Makefile b/Makefile index a9e9f8b..f0a5e05 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,7 @@ test: .PHONY: test-instance test-instance: - rm -r ${WORK_DIR}/test 2> /dev/null; \ + rm -f -r ${WORK_DIR}/test 2> /dev/null; \ mkdir -p ${WORK_DIR}/test/conf/ ${WORK_DIR}/test/data/ wget "https://dl.gitea.io/gitea/master/gitea-master-linux-amd64" -O ${WORK_DIR}/test/gitea-master; \ chmod +x ${WORK_DIR}/test/gitea-master; \ diff --git a/gitea/repo_commit.go b/gitea/repo_commit.go index c22c253..e6fad60 100644 --- a/gitea/repo_commit.go +++ b/gitea/repo_commit.go @@ -7,6 +7,7 @@ package gitea import ( "fmt" + "net/url" ) // Identity for a person's identity like an author or committer @@ -51,3 +52,28 @@ func (c *Client) GetSingleCommit(user, repo, commitID string) (*Commit, error) { commit := new(Commit) return commit, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/git/commits/%s", user, repo, commitID), nil, nil, &commit) } + +// ListCommitOptions list commit options +type ListCommitOptions struct { + ListOptions + //SHA or branch to start listing commits from (usually 'master') + SHA string +} + +// QueryEncode turns options into querystring argument +func (opt *ListCommitOptions) QueryEncode() string { + query := opt.ListOptions.getURLQuery() + if opt.SHA != "" { + query.Add("sha", opt.SHA) + } + return query.Encode() +} + +// ListRepoCommits return list of commits from a repo +func (c *Client) ListRepoCommits(user, repo string, opt ListCommitOptions) ([]*Commit, error) { + link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/commits", user, repo)) + opt.setDefaults() + commits := make([]*Commit, 0, opt.PageSize) + link.RawQuery = opt.QueryEncode() + return commits, c.getParsedResponse("GET", link.String(), nil, nil, &commits) +} diff --git a/gitea/repo_commit_test.go b/gitea/repo_commit_test.go new file mode 100644 index 0000000..a339ad7 --- /dev/null +++ b/gitea/repo_commit_test.go @@ -0,0 +1,25 @@ +// 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 TestListRepoCommits(t *testing.T) { + log.Println("== TestListRepoCommits ==") + c := newTestClient() + + repo, err := createTestRepo(t, "ListRepoCommits", c) + assert.NoError(t, err) + + l, err := c.ListRepoCommits(repo.Owner.UserName, repo.Name, ListCommitOptions{}) + assert.NoError(t, err) + assert.Len(t, l, 1) + assert.EqualValues(t, "Initial commit", l[0].RepoCommit.Message) +}