diff --git a/scm/driver/stash/git.go b/scm/driver/stash/git.go index 2c38c4cd4..c3576c1f0 100644 --- a/scm/driver/stash/git.go +++ b/scm/driver/stash/git.go @@ -83,9 +83,9 @@ func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.Comm namespace, name := scm.Split(repo) var requestPath string if opts.Path != "" { - requestPath = fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits?path=%s", namespace, name, opts.Path) + requestPath = fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits?until=%s&path=%s", namespace, name, opts.Ref, opts.Path) } else { - requestPath = fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits", namespace, name) + requestPath = fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits?until=%s", namespace, name, opts.Ref) } out := new(commits) res, err := s.client.do(ctx, "GET", requestPath, nil, out) diff --git a/scm/driver/stash/git_test.go b/scm/driver/stash/git_test.go index dfa37bfba..bf4d8e990 100644 --- a/scm/driver/stash/git_test.go +++ b/scm/driver/stash/git_test.go @@ -98,6 +98,7 @@ func TestGitListCommits(t *testing.T) { gock.New("http://example.com:7990"). Get("/rest/api/1.0/projects/PRJ/repos/my-repo/commits"). + MatchParam("until", ""). Reply(200). Type("application/json"). File("testdata/commits.json") diff --git a/scm/driver/stash/integration/git_test.go b/scm/driver/stash/integration/git_test.go index bd5933f25..6b4c18b82 100644 --- a/scm/driver/stash/integration/git_test.go +++ b/scm/driver/stash/integration/git_test.go @@ -62,3 +62,57 @@ func TestGetLatestCommitOfBranch(t *testing.T) { } } } + +func TestGetLatestCommitOfNonDefaultBranch(t *testing.T) { + if token == "" { + t.Skip("Skipping, Acceptance test") + } + client, _ = stash.New(endpoint) + client.Client = &http.Client{ + Transport: &transport.BasicAuth{ + Username: username, + Password: token, + }, + } + + commits, response, err := client.Git.ListCommits(context.Background(), repoID, scm.CommitListOptions{Ref: "main", Path: "do-not-touch.txt"}) + + if err != nil { + t.Errorf("GetLatestCommitOfFile got an error %v", err) + } else { + if response.Status != http.StatusOK { + t.Errorf("GetLatestCommitOfFile did not get a 200 back %v", response.Status) + } + + if commits[0].Sha != "76fb1762048a277596d3fa330b3da140cd12d361" { + t.Errorf("Got the commitId %s instead of the top commit of the file", commits[0].Sha) + } + } +} + +func TestGetLatestCommitOfBranchWhenNoRefPassed(t *testing.T) { + if token == "" { + t.Skip("Skipping, Acceptance test") + } + client, _ = stash.New(endpoint) + client.Client = &http.Client{ + Transport: &transport.BasicAuth{ + Username: username, + Password: token, + }, + } + + commits, response, err := client.Git.ListCommits(context.Background(), repoID, scm.CommitListOptions{Path: "README"}) + + if err != nil { + t.Errorf("GetLatestCommitOfFile got an error %v", err) + } else { + if response.Status != http.StatusOK { + t.Errorf("GetLatestCommitOfFile did not get a 200 back %v", response.Status) + } + + if commits[0].Sha != "2cc4dbe084f0d66761318b305c408cb0ea300c9a" { + t.Errorf("Got the commitId %s instead of the top commit of the file", commits[0].Sha) + } + } +} \ No newline at end of file