Browse Source

Add ListRepoCommits (#266)

Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: spawn2kill <spawn2kill@noreply.gitea.io>
Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/266
Reviewed-by: Andrew Thornton <art27@cantab.net>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
pull/283/head
parent
commit
ffaf5e0d1e
3 changed files with 52 additions and 1 deletions
  1. +1
    -1
      Makefile
  2. +26
    -0
      gitea/repo_commit.go
  3. +25
    -0
      gitea/repo_commit_test.go

+ 1
- 1
Makefile View File

@@ -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; \


+ 26
- 0
gitea/repo_commit.go View File

@@ -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
- 0
gitea/repo_commit_test.go View 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)
}

Loading…
Cancel
Save