Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for branch in list commits bb onprem API #215

Merged
merged 4 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohitg0795 what happens if there is no opts.ref passed or its an empty string. Does the URL still work ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If its empty string, then it shows results of default branch. After your comment, I added an integration test as well for the same.

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://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
27 changes: 27 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,30 @@ 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)
}
}
}