Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) add ListCommits for gitea and stash #98

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions scm/driver/gitea/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.Lis
}

func (s *gitService) ListCommits(ctx context.Context, repo string, _ scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
path := fmt.Sprintf("api/v1/repos/%s/commits", repo)
out := []*commitInfo{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertCommitList(out), res, err
}

func (s *gitService) ListTags(ctx context.Context, repo string, _ scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
Expand Down Expand Up @@ -142,13 +145,13 @@ func convertBranch(src *branch) *scm.Reference {
}
}

// func convertCommitList(src []*commit) []*scm.Commit {
// dst := []*scm.Commit{}
// for _, v := range src {
// dst = append(dst, convertCommitInfo(v))
// }
// return dst
// }
func convertCommitList(src []*commitInfo) []*scm.Commit {
dst := []*scm.Commit{}
for _, v := range src {
dst = append(dst, convertCommitInfo(v))
}
return dst
}

func convertCommitInfo(src *commitInfo) *scm.Commit {
return &scm.Commit{
Expand Down
25 changes: 20 additions & 5 deletions scm/driver/gitea/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestGitFindCommit(t *testing.T) {
Get("/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630").
Reply(200).
Type("application/json").
File("testdata/commits.json")
File("testdata/commit.json")

client, _ := New("https://try.gitea.io")
got, _, err := client.Git.FindCommit(
Expand All @@ -37,7 +37,7 @@ func TestGitFindCommit(t *testing.T) {
}

want := new(scm.Commit)
raw, _ := ioutil.ReadFile("testdata/commits.json.golden")
raw, _ := ioutil.ReadFile("testdata/commit.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
Expand All @@ -47,10 +47,25 @@ func TestGitFindCommit(t *testing.T) {
}

func TestGitListCommits(t *testing.T) {
gock.New("https://try.gitea.io").
Get("/api/v1/repos/go-gitea/gitea/commits").
Reply(200).
Type("application/json").
File("testdata/commits.json")

client, _ := New("https://try.gitea.io")
_, _, err := client.Git.ListCommits(context.Background(), "go-gitea/gitea", scm.CommitListOptions{})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
got, _, err := client.Git.ListCommits(context.Background(), "go-gitea/gitea", scm.CommitListOptions{})
if err != nil {
t.Error(err)
}

want := []*scm.Commit{}
raw, _ := ioutil.ReadFile("testdata/commits.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

Expand Down
1 change: 1 addition & 0 deletions scm/driver/gitea/testdata/commit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630","sha":"c43399cad8766ee521b873a32c1652407c5a4630","html_url":"https://try.gitea.io/gitea/gitea/commits/c43399cad8766ee521b873a32c1652407c5a4630","commit":{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630","author":{"name":"Lewis Cowles","email":"[email protected]","date":"2018-09-09T03:36:08Z"},"committer":{"name":"Lunny Xiao","email":"[email protected]","date":"2018-09-09T03:36:08Z"},"message":"Fixes repo branch endpoint summary (#4893)","tree":{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/trees/c43399cad8766ee521b873a32c1652407c5a4630","sha":"c43399cad8766ee521b873a32c1652407c5a4630"}},"author":null,"committer":{"id":3,"login":"lunny","full_name":"Lunny Xiao","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/271fc56bcea89c6f69ab0024b59b3f81?d=identicon","language":"zh-CN","username":"lunny"},"parents":[{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/d293a2b9d6722dffde7998c953c3087e47a38a83","sha":"d293a2b9d6722dffde7998c953c3087e47a38a83"}]}
11 changes: 11 additions & 0 deletions scm/driver/gitea/testdata/commit.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"committer": {
"name": "Lunny Xiao",
"login": "lunny",
"email": "[email protected]",
"avatar": "https://secure.gravatar.com/avatar/271fc56bcea89c6f69ab0024b59b3f81?d=identicon"
},
"link": "https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630",
"sha": "c43399cad8766ee521b873a32c1652407c5a4630",
"message": "Fixes repo branch endpoint summary (#4893)"
}
4 changes: 3 additions & 1 deletion scm/driver/gitea/testdata/commits.json
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630","sha":"c43399cad8766ee521b873a32c1652407c5a4630","html_url":"https://try.gitea.io/gitea/gitea/commits/c43399cad8766ee521b873a32c1652407c5a4630","commit":{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630","author":{"name":"Lewis Cowles","email":"[email protected]","date":"2018-09-09T03:36:08Z"},"committer":{"name":"Lunny Xiao","email":"[email protected]","date":"2018-09-09T03:36:08Z"},"message":"Fixes repo branch endpoint summary (#4893)","tree":{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/trees/c43399cad8766ee521b873a32c1652407c5a4630","sha":"c43399cad8766ee521b873a32c1652407c5a4630"}},"author":null,"committer":{"id":3,"login":"lunny","full_name":"Lunny Xiao","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/271fc56bcea89c6f69ab0024b59b3f81?d=identicon","language":"zh-CN","username":"lunny"},"parents":[{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/d293a2b9d6722dffde7998c953c3087e47a38a83","sha":"d293a2b9d6722dffde7998c953c3087e47a38a83"}]}
[
{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630","sha":"c43399cad8766ee521b873a32c1652407c5a4630","html_url":"https://try.gitea.io/gitea/gitea/commits/c43399cad8766ee521b873a32c1652407c5a4630","commit":{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630","author":{"name":"Lewis Cowles","email":"[email protected]","date":"2018-09-09T03:36:08Z"},"committer":{"name":"Lunny Xiao","email":"[email protected]","date":"2018-09-09T03:36:08Z"},"message":"Fixes repo branch endpoint summary (#4893)","tree":{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/trees/c43399cad8766ee521b873a32c1652407c5a4630","sha":"c43399cad8766ee521b873a32c1652407c5a4630"}},"author":null,"committer":{"id":3,"login":"lunny","full_name":"Lunny Xiao","email":"[email protected]","avatar_url":"https://secure.gravatar.com/avatar/271fc56bcea89c6f69ab0024b59b3f81?d=identicon","language":"zh-CN","username":"lunny"},"parents":[{"url":"https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/d293a2b9d6722dffde7998c953c3087e47a38a83","sha":"d293a2b9d6722dffde7998c953c3087e47a38a83"}]}
]
24 changes: 13 additions & 11 deletions scm/driver/gitea/testdata/commits.json.golden
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"committer": {
"name": "Lunny Xiao",
"login": "lunny",
"email": "[email protected]",
"avatar": "https://secure.gravatar.com/avatar/271fc56bcea89c6f69ab0024b59b3f81?d=identicon"
},
"link": "https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630",
"sha": "c43399cad8766ee521b873a32c1652407c5a4630",
"message": "Fixes repo branch endpoint summary (#4893)"
}
[
{
"committer": {
"name": "Lunny Xiao",
"login": "lunny",
"email": "[email protected]",
"avatar": "https://secure.gravatar.com/avatar/271fc56bcea89c6f69ab0024b59b3f81?d=identicon"
},
"link": "https://try.gitea.io/api/v1/repos/gitea/gitea/git/commits/c43399cad8766ee521b873a32c1652407c5a4630",
"sha": "c43399cad8766ee521b873a32c1652407c5a4630",
"message": "Fixes repo branch endpoint summary (#4893)"
}
]
7 changes: 6 additions & 1 deletion scm/driver/stash/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.Lis
}

func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits", namespace, name)
out := new(commits)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
return convertCommitList(out), res, err
}

func (s *gitService) ListTags(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
Expand Down
23 changes: 20 additions & 3 deletions scm/driver/stash/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,27 @@ func TestGitFindTag(t *testing.T) {
}

func TestGitListCommits(t *testing.T) {
defer gock.Off()

gock.New("http:https://example.com:7990").
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/commits").
Reply(200).
Type("application/json").
File("testdata/commits.json")

client, _ := New("http:https://example.com:7990")
_, _, err := client.Git.ListCommits(context.Background(), "PRJ/my-repo", scm.CommitListOptions{Ref: "master", Page: 1, Size: 30})
if err != scm.ErrNotSupported {
t.Errorf("Expect Not Supported error")
got, _, err := client.Git.ListCommits(context.Background(), "PRJ/my-repo", scm.CommitListOptions{})
if err != nil {
t.Error(err)
}

want := []*scm.Commit{}
raw, _ := ioutil.ReadFile("testdata/commits.json.golden")
json.Unmarshal(raw, &want)

if diff := cmp.Diff(got, want); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}

Expand Down
73 changes: 72 additions & 1 deletion scm/driver/stash/testdata/commits.json
Original file line number Diff line number Diff line change
@@ -1 +1,72 @@
{}
{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"id": "131cb13f4aed12e725177bc4b7c28db67839bf9f",
"displayId": "131cb13f4ae",
"author": {
"name": "jcitizen",
"emailAddress": "[email protected]",
"id": 1,
"displayName": "Jane Citizen",
"active": true,
"slug": "jcitizen",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http:https://example.com:7990/users/jcitizen"
}
]
}
},
"authorTimestamp": 1530720102000,
"committer": {
"name": "jcitizen",
"emailAddress": "[email protected]",
"id": 1,
"displayName": "Jane Citizen",
"active": true,
"slug": "jcitizen",
"type": "NORMAL",
"links": {
"self": [
{
"href": "http:https://example.com:7990/users/jcitizen"
}
]
}
},
"committerTimestamp": 1530720102000,
"message": "update files",
"parents": [
{
"id": "4f4b0ef1714a5b6cafdaf2f53c7f5f5b38fb9348",
"displayId": "4f4b0ef1714",
"author": {
"name": "Jane Citizen",
"emailAddress": "[email protected]"
},
"authorTimestamp": 1530719890000,
"committer": {
"name": "Jane Citizen",
"emailAddress": "[email protected]"
},
"committerTimestamp": 1530719890000,
"message": "update files",
"parents": [
{
"id": "f636fe22d302c852df1a68fff2d744039fe55b3d",
"displayId": "f636fe22d30"
}
]
}
]
}
],
"start": 0,
"authorCount": 1,
"totalCount": 1
}
22 changes: 21 additions & 1 deletion scm/driver/stash/testdata/commits.json.golden
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
[]
[
{
"Sha": "131cb13f4aed12e725177bc4b7c28db67839bf9f",
"Message": "update files",
"Author": {
"Name": "Jane Citizen",
"Email": "[email protected]",
"Date": "2018-07-04T09:01:42-07:00",
"Login": "jcitizen",
"Avatar": "https://www.gravatar.com/avatar/9e26471d35a78862c17e467d87cddedf.jpg"
},
"Committer": {
"Name": "Jane Citizen",
"Email": "[email protected]",
"Date": "2018-07-04T09:01:42-07:00",
"Login": "jcitizen",
"Avatar": "https://www.gravatar.com/avatar/9e26471d35a78862c17e467d87cddedf.jpg"
},
"Link": ""
}
]