From d1bdb2e3598b2db3d5cc732d7fc86da6ea6ae333 Mon Sep 17 00:00:00 2001 From: Eoin McAfee Date: Wed, 16 Jun 2021 18:10:25 +0100 Subject: [PATCH] fix issue where write permission on bitbucket server wasn't working at repo level add unit test --- scm/driver/stash/repo.go | 8 ++-- scm/driver/stash/repo_test.go | 46 ++++++++++++++++++++- scm/driver/stash/testdata/repos.json | 41 ++++++++++++++++++ scm/driver/stash/testdata/repos.json.golden | 13 ++++++ 4 files changed, 103 insertions(+), 5 deletions(-) diff --git a/scm/driver/stash/repo.go b/scm/driver/stash/repo.go index 17e17362e..e7754b459 100644 --- a/scm/driver/stash/repo.go +++ b/scm/driver/stash/repo.go @@ -137,10 +137,10 @@ func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Pe }, nil, nil } // HACK: test if the user has write access to the repository. - _, name := scm.Split(repo) + namespace, _ := scm.Split(repo) repos, _, _ := s.listWrite(ctx, repo) for _, repo := range repos { - if repo.Name == name { + if repo.Namespace == namespace { return &scm.Perm{ Pull: true, Push: true, @@ -170,8 +170,8 @@ func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]* // listWrite returns the user repository list. func (s *repositoryService) listWrite(ctx context.Context, repo string) ([]*scm.Repository, *scm.Response, error) { - namespace, name := scm.Split(repo) - path := fmt.Sprintf("rest/api/1.0/repos?size=1000&permission=REPO_WRITE&projectname=%s&name=%s", namespace, name) + _, name := scm.Split(repo) + path := fmt.Sprintf("rest/api/1.0/repos?size=1000&permission=REPO_WRITE&name=%s", name) out := new(repositories) res, err := s.client.do(ctx, "GET", path, nil, out) return convertRepositoryList(out), res, err diff --git a/scm/driver/stash/repo_test.go b/scm/driver/stash/repo_test.go index dff71f07a..83924a8ba 100644 --- a/scm/driver/stash/repo_test.go +++ b/scm/driver/stash/repo_test.go @@ -157,7 +157,6 @@ func TestRepositoryPerms_Write(t *testing.T) { Get("/rest/api/1.0/repos"). MatchParam("size", "1000"). MatchParam("permission", "REPO_WRITE"). - MatchParam("projectname", "PRJ"). MatchParam("name", "my-repo"). Reply(200). Type("application/json"). @@ -185,6 +184,51 @@ func TestRepositoryPerms_Write(t *testing.T) { } } +func TestRepositoryPermsDifferentProjectName_Write(t *testing.T) { + defer gock.Off() + + gock.New("http://example.com:7990"). + Get("/rest/api/1.0/projects/PRJ/repos/quux"). + Reply(200). + Type("application/json"). + File("testdata/repo.json") + + gock.New("http://example.com:7990"). + Get("/rest/api/1.0/projects/PRJ/repos/quux/webhooks"). + Reply(404). + Type("application/json") + + gock.New("http://example.com:7990"). + Get("/rest/api/1.0/repos"). + MatchParam("size", "1000"). + MatchParam("permission", "REPO_WRITE"). + MatchParam("name", "quux"). + Reply(200). + Type("application/json"). + File("testdata/repos.json") + + client, _ := New("http://example.com:7990") + got, _, err := client.Repositories.FindPerms(context.Background(), "PRJ/quux") + if err != nil { + t.Error(err) + } + + want := &scm.Perm{ + Pull: true, + Push: true, + Admin: false, + } + + if diff := cmp.Diff(got, want); diff != "" { + t.Errorf("Unexpected Results") + t.Log(diff) + } + + if gock.IsPending() { + t.Errorf("pending API requests") + } +} + func TestRepositoryPerms_Forbidden(t *testing.T) { defer gock.Off() diff --git a/scm/driver/stash/testdata/repos.json b/scm/driver/stash/testdata/repos.json index 46f119c9b..68bc40157 100644 --- a/scm/driver/stash/testdata/repos.json +++ b/scm/driver/stash/testdata/repos.json @@ -43,6 +43,47 @@ } ] } + }, + { + "slug": "quux", + "id": 2, + "name": "quux", + "scmId": "git", + "state": "AVAILABLE", + "statusMessage": "Available", + "forkable": true, + "project": { + "key": "PRJ", + "id": 2, + "name": "different_name", + "public": false, + "type": "NORMAL", + "links": { + "self": [ + { + "href": "http://example.com:7990/projects/PRJ" + } + ] + } + }, + "public": false, + "links": { + "clone": [ + { + "href": "ssh://git@example.com:7999/prj/quux.git", + "name": "ssh" + }, + { + "href": "http://jcitizen@example.com:7990/scm/prj/quux.git", + "name": "http" + } + ], + "self": [ + { + "href": "http://example.com:7990/projects/PRJ/repos/quux/browse" + } + ] + } } ], "start": 0 diff --git a/scm/driver/stash/testdata/repos.json.golden b/scm/driver/stash/testdata/repos.json.golden index ef009866b..18e4d4018 100644 --- a/scm/driver/stash/testdata/repos.json.golden +++ b/scm/driver/stash/testdata/repos.json.golden @@ -11,5 +11,18 @@ "Link": "http://example.com:7990/projects/PRJ/repos/my-repo/browse", "Created": "0001-01-01T00:00:00Z", "Updated": "0001-01-01T00:00:00Z" + }, + { + "ID": "2", + "Namespace": "PRJ", + "Name": "quux", + "Perm": null, + "Branch": "master", + "Private": true, + "Clone": "http://example.com:7990/scm/prj/quux.git", + "CloneSSH": "ssh://git@example.com:7999/prj/quux.git", + "Link": "http://example.com:7990/projects/PRJ/repos/quux/browse", + "Created": "0001-01-01T00:00:00Z", + "Updated": "0001-01-01T00:00:00Z" } ] \ No newline at end of file