Skip to content

Commit

Permalink
fetch branch for bitbucket onprem
Browse files Browse the repository at this point in the history
  • Loading branch information
devkimittal committed Feb 8, 2023
1 parent ca3776c commit 2a53c8e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
14 changes: 13 additions & 1 deletion scm/driver/stash/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,19 @@ func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Reposit
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s", namespace, name)
out := new(repository)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertRepository(out), res, err
outputRepo := convertRepository(out)

branch := new(branch)
pathBranch := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/branches/default", namespace, name)
_, errBranch := s.client.do(ctx, "GET", pathBranch, nil, branch)
if errBranch == nil {
outputRepo.Branch = branch.DisplayID
}
if err == nil {
err = errBranch
}

return outputRepo, res, err
}

// FindHook returns a repository hook.
Expand Down
30 changes: 30 additions & 0 deletions scm/driver/stash/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func TestRepositoryFind(t *testing.T) {
Type("application/json").
File("testdata/repo.json")

gock.New("http://example.com:7990").
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/branches/default").
Reply(200).
Type("application/json").
File("testdata/default_branch.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Repositories.Find(context.Background(), "PRJ/my-repo")
if err != nil {
Expand Down Expand Up @@ -76,6 +82,12 @@ func TestRepositoryPerms(t *testing.T) {
Type("application/json").
File("testdata/webhooks.json")

gock.New("http://example.com:7990").
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/branches/default").
Reply(200).
Type("application/json").
File("testdata/default_branch.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Repositories.FindPerms(context.Background(), "PRJ/my-repo")
if err != nil {
Expand Down Expand Up @@ -117,6 +129,12 @@ func TestRepositoryPerms_ReadOnly(t *testing.T) {
Type("application/json").
File("testdata/repo.json")

gock.New("http://example.com:7990").
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/branches/default").
Reply(200).
Type("application/json").
File("testdata/default_branch.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Repositories.FindPerms(context.Background(), "PRJ/my-repo")
if err != nil {
Expand Down Expand Up @@ -162,6 +180,12 @@ func TestRepositoryPerms_Write(t *testing.T) {
Type("application/json").
File("testdata/repos.json")

gock.New("http://example.com:7990").
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/branches/default").
Reply(200).
Type("application/json").
File("testdata/default_branch.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Repositories.FindPerms(context.Background(), "PRJ/my-repo")
if err != nil {
Expand Down Expand Up @@ -207,6 +231,12 @@ func TestRepositoryPermsDifferentProjectName_Write(t *testing.T) {
Type("application/json").
File("testdata/repos.json")

gock.New("http://example.com:7990").
Get("/rest/api/1.0/projects/PRJ/repos/quux/branches/default").
Reply(200).
Type("application/json").
File("testdata/default_branch.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Repositories.FindPerms(context.Background(), "PRJ/quux")
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion scm/driver/stash/testdata/content_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"testdata/branches.json.golden",
"testdata/changes.json",
"testdata/changes.json.golden",
"testdata/commit.json"
"testdata/commit.json",
"testdata/default_branch.json"
],
"start": 0,
"nextPageStart": 25
Expand Down
6 changes: 5 additions & 1 deletion scm/driver/stash/testdata/content_list.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,9 @@
{
"path": "testdata/commit.json",
"kind": "file"
}
},
{
"path": "testdata/default_branch.json",
"kind": "file"
},
]
8 changes: 8 additions & 0 deletions scm/driver/stash/testdata/default_branch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "refs/heads/feature_branch",
"displayId": "feature_branch",
"type": "BRANCH",
"latestCommit": "c567b3f4a2980299e2a1148360f23ffb0f4c9764",
"latestChangeset": "c567b3f4a2980299e2a1148360f23ffb0f4c9764",
"isDefault": true
}
2 changes: 1 addition & 1 deletion scm/driver/stash/testdata/repo.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"Namespace": "PRJ",
"Name": "my-repo",
"Perm": null,
"Branch": "master",
"Branch": "feature_branch",
"Private": true,
"Clone": "http://example.com:7990/scm/prj/my-repo.git",
"CloneSSH": "ssh://git@example.com:7999/prj/my-repo.git",
Expand Down

0 comments on commit 2a53c8e

Please sign in to comment.