Skip to content

Commit

Permalink
Merge pull request #182 from DeepakPatankar/deepak/add-path-as-a-para…
Browse files Browse the repository at this point in the history
…m-in-list-branches

Add the support to fetch commit of a particular file
  • Loading branch information
TP Honey authored May 16, 2022
2 parents 7664143 + 1ff5fea commit bb2f62e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions scm/driver/stash/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions scm/driver/stash/integration/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit bb2f62e

Please sign in to comment.