Skip to content

Commit

Permalink
Added support for branch in list commits bb onprem API (#215)
Browse files Browse the repository at this point in the history
* added support for branch in list commits bb onprem API
  • Loading branch information
mohitg0795 committed Aug 17, 2022
1 parent 43b3e0a commit 09a4fde
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scm/driver/stash/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions scm/driver/stash/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func TestGitListCommits(t *testing.T) {

gock.New("http:https://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")
Expand Down
54 changes: 54 additions & 0 deletions scm/driver/stash/integration/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 09a4fde

Please sign in to comment.