diff --git a/scm/driver/stash/repo.go b/scm/driver/stash/repo.go index 70f4c01de..070bfd373 100644 --- a/scm/driver/stash/repo.go +++ b/scm/driver/stash/repo.go @@ -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. diff --git a/scm/driver/stash/repo_test.go b/scm/driver/stash/repo_test.go index 83924a8ba..ee337dc24 100644 --- a/scm/driver/stash/repo_test.go +++ b/scm/driver/stash/repo_test.go @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { diff --git a/scm/driver/stash/testdata/content_list.json b/scm/driver/stash/testdata/content_list.json index ec0447795..4d5f72873 100644 --- a/scm/driver/stash/testdata/content_list.json +++ b/scm/driver/stash/testdata/content_list.json @@ -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 diff --git a/scm/driver/stash/testdata/content_list.json.golden b/scm/driver/stash/testdata/content_list.json.golden index 72d6fcf30..f02920ac4 100644 --- a/scm/driver/stash/testdata/content_list.json.golden +++ b/scm/driver/stash/testdata/content_list.json.golden @@ -98,5 +98,9 @@ { "path": "testdata/commit.json", "kind": "file" + }, + { + "path": "testdata/default_branch.json", + "kind": "file" } ] diff --git a/scm/driver/stash/testdata/default_branch.json b/scm/driver/stash/testdata/default_branch.json new file mode 100644 index 000000000..ceeec302e --- /dev/null +++ b/scm/driver/stash/testdata/default_branch.json @@ -0,0 +1,8 @@ +{ + "id": "refs/heads/feature_branch", + "displayId": "feature_branch", + "type": "BRANCH", + "latestCommit": "c567b3f4a2980299e2a1148360f23ffb0f4c9764", + "latestChangeset": "c567b3f4a2980299e2a1148360f23ffb0f4c9764", + "isDefault": true +} \ No newline at end of file diff --git a/scm/driver/stash/testdata/repo.json.golden b/scm/driver/stash/testdata/repo.json.golden index e7257362f..66414388a 100644 --- a/scm/driver/stash/testdata/repo.json.golden +++ b/scm/driver/stash/testdata/repo.json.golden @@ -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",