Add ListRepoCommits #266
2 Makefile
2
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; \ | ||||
|
@@ -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) | ||||
} | ||||
|
25 gitea/repo_commit_test.go Normal file
25
gitea/repo_commit_test.go Normal file @@ -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) | ||||
} |
Reference in New Issue
Block a user