From 8a8597dbdbe0e75837d4f0ab5816f13fd1a58561 Mon Sep 17 00:00:00 2001 From: Mohit Garg Date: Mon, 21 Feb 2022 15:14:55 +0530 Subject: [PATCH] GitHub list commits fix (#152) * fixing commit list options for github list commits API * added integration tests * fix UT --- scm/driver/github/git_test.go | 2 +- scm/driver/github/integration/git_test.go | 32 +++++++++++++++++++++-- scm/driver/github/util.go | 2 +- scm/driver/github/util_test.go | 2 +- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/scm/driver/github/git_test.go b/scm/driver/github/git_test.go index 9c0a6c530..65c2562c9 100644 --- a/scm/driver/github/git_test.go +++ b/scm/driver/github/git_test.go @@ -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). diff --git a/scm/driver/github/integration/git_test.go b/scm/driver/github/integration/git_test.go index c6192cc91..2915ad2f8 100644 --- a/scm/driver/github/integration/git_test.go +++ b/scm/driver/github/integration/git_test.go @@ -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)) } } @@ -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{ @@ -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 // diff --git a/scm/driver/github/util.go b/scm/driver/github/util.go index 7f441b751..5d5fb4741 100644 --- a/scm/driver/github/util.go +++ b/scm/driver/github/util.go @@ -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) diff --git a/scm/driver/github/util_test.go b/scm/driver/github/util_test.go index 3cc66bdc9..46a87888e 100644 --- a/scm/driver/github/util_test.go +++ b/scm/driver/github/util_test.go @@ -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)