Skip to content

Commit

Permalink
GitHub list commits fix (#152)
Browse files Browse the repository at this point in the history
* fixing commit list options for github list commits API

* added integration tests

* fix UT
  • Loading branch information
mohitg0795 authored Feb 21, 2022
1 parent 3fe1028 commit 8a8597d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion scm/driver/github/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestGitListCommits(t *testing.T) {
Get("/repos/octocat/hello-world/commits").
MatchParam("page", "1").
MatchParam("per_page", "30").
MatchParam("ref", "master").
MatchParam("sha", "master").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
Expand Down
32 changes: 30 additions & 2 deletions scm/driver/github/integration/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ func testCommits(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
t.Run("Find", testCommitFind(client))
t.Run("List", testCommitList(client))
t.Run("List-Default-Branch", testCommitListDefaultBranch(client))
t.Run("List-Non-Default-Branch", testCommitListNonDefaultBranch(client))
}
}

Expand All @@ -130,7 +131,7 @@ func testCommitFind(client *scm.Client) func(t *testing.T) {
}
}

func testCommitList(client *scm.Client) func(t *testing.T) {
func testCommitListDefaultBranch(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
opts := scm.CommitListOptions{
Expand All @@ -152,6 +153,33 @@ func testCommitList(client *scm.Client) func(t *testing.T) {
}
}

func testCommitListNonDefaultBranch(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
opts := scm.CommitListOptions{
Ref: "octocat-patch-1",
}
result, _, err := client.Git.ListCommits(context.Background(), "octocat/Hello-World", opts)
if err != nil {
t.Error(err)
return
}
if len(result) == 0 {
t.Errorf("Want a non-empty commit list")
}

if got, want := result[0].Sha, "b1b3f9723831141a31a1a7252a213e216ea76e56"; got != want {
t.Errorf("Want commit Sha %q, got %q", want, got)
}

for _, commit := range result {
if commit.Sha == "7fd1a60b01f91b314f59955a4e4d4e80d8edf11d" {
t.Run("Commit", testCommit(commit))
}
}
}
}

//
// struct sub-tests
//
Expand Down
2 changes: 1 addition & 1 deletion scm/driver/github/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func encodeCommitListOptions(opts scm.CommitListOptions) string {
params.Set("per_page", strconv.Itoa(opts.Size))
}
if opts.Ref != "" {
params.Set("ref", opts.Ref)
params.Set("sha", opts.Ref)
}
if opts.Path != "" {
params.Set("path", opts.Path)
Expand Down
2 changes: 1 addition & 1 deletion scm/driver/github/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func Test_encodeCommitListOptions(t *testing.T) {
Ref: "master",
Path: "readme.md",
}
want := "page=10&path=readme.md&per_page=30&ref=master"
want := "page=10&path=readme.md&per_page=30&sha=master"
got := encodeCommitListOptions(opts)
if got != want {
t.Errorf("Want encoded commit list options %q, got %q", want, got)
Expand Down

0 comments on commit 8a8597d

Please sign in to comment.