From 1ff5fea1a4598410eb408e598c95e15a21f163e2 Mon Sep 17 00:00:00 2001 From: DeepakPatankar Date: Mon, 16 May 2022 15:45:22 +0530 Subject: [PATCH] Add the support to fetch commit of a file --- scm/driver/stash/git.go | 9 ++++++-- scm/driver/stash/integration/git_test.go | 26 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/scm/driver/stash/git.go b/scm/driver/stash/git.go index a06f74909..2c38c4cd4 100644 --- a/scm/driver/stash/git.go +++ b/scm/driver/stash/git.go @@ -81,9 +81,14 @@ 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) { namespace, name := scm.Split(repo) - path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits", namespace, name) + var requestPath string + if opts.Path != "" { + requestPath = fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits?path=%s", namespace, name, opts.Path) + } else { + requestPath = 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) + res, err := s.client.do(ctx, "GET", requestPath, nil, out) copyPagination(out.pagination, res) return convertCommitList(out), res, err } diff --git a/scm/driver/stash/integration/git_test.go b/scm/driver/stash/integration/git_test.go index d03277fee..bd5933f25 100644 --- a/scm/driver/stash/integration/git_test.go +++ b/scm/driver/stash/integration/git_test.go @@ -34,5 +34,31 @@ func TestCreateBranch(t *testing.T) { if response.Status != http.StatusOK { t.Errorf("CreateBranch did not get a 200 back %v", response.Status) } +} +func TestGetLatestCommitOfBranch(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: "master", 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) + } + } }