Skip to content

Commit

Permalink
fix issue where write permission on bitbucket server wasn't working a…
Browse files Browse the repository at this point in the history
…t repo level

add unit test
  • Loading branch information
eoinmcafee00 committed Jun 17, 2021
1 parent f685c90 commit d1bdb2e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
8 changes: 4 additions & 4 deletions scm/driver/stash/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
46 changes: 45 additions & 1 deletion scm/driver/stash/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down Expand Up @@ -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()

Expand Down
41 changes: 41 additions & 0 deletions scm/driver/stash/testdata/repos.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions scm/driver/stash/testdata/repos.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]

0 comments on commit d1bdb2e

Please sign in to comment.