Skip to content

Commit

Permalink
added fix for issue jenkins-x#15
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Feb 22, 2019
1 parent fff0b07 commit 47eed25
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 13 deletions.
40 changes: 27 additions & 13 deletions scm/driver/stash/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,6 @@ func (s *repositoryService) FindHook(ctx context.Context, repo string, id string

// FindPerms returns the repository permissions.
func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Perm, *scm.Response, error) {
// TODO(bradrydzewski) we should be able to determine permissions
// using the combined repository, project and global permission
// endpoints.
//
// /rest/api/1.0/projects/PRJ/repos/my-repo/permissions/users
// /rest/api/1.0/projects/PRJ/permissions/users
// /rest/api/1.0/admin/permissions/users

// HACK: test if the user has read access to the repository.
_, _, err := s.Find(ctx, repo)
if err != nil {
Expand All @@ -136,17 +128,30 @@ func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Pe

// HACK: test if the user has admin access to the repository.
_, _, err = s.ListHooks(ctx, repo, scm.ListOptions{})
if err != nil {
if err == nil {
return &scm.Perm{
Pull: true,
Push: false,
Admin: false,
Push: true,
Admin: true,
}, nil, nil
}
// HACK: test if the user has write access to the repository.
_, name := scm.Split(repo)
repos, _, _ := s.listWrite(ctx, repo)
for _, repo := range repos {
if repo.Name == name {
return &scm.Perm{
Pull: true,
Push: true,
Admin: false,
}, nil, nil
}
}

return &scm.Perm{
Pull: true,
Push: true,
Admin: true,
Push: false,
Admin: false,
}, nil, nil
}

Expand All @@ -162,6 +167,15 @@ func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*
return convertRepositoryList(out), res, err
}

// 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&project=%s&name=%s", namespace, name)
out := new(repositories)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertRepositoryList(out), res, err
}

// ListHooks returns a list or repository hooks.
func (s *repositoryService) ListHooks(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Hook, *scm.Response, error) {
namespace, name := scm.Split(repo)
Expand Down
60 changes: 60 additions & 0 deletions scm/driver/stash/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func TestRepositoryPerms(t *testing.T) {
t.Errorf("Unexpected Results")
t.Log(diff)
}

if gock.IsPending() {
t.Errorf("pending API requests")
}
}

func TestRepositoryPerms_ReadOnly(t *testing.T) {
Expand All @@ -107,6 +111,12 @@ func TestRepositoryPerms_ReadOnly(t *testing.T) {
Reply(404).
Type("application/json")

gock.New("http://example.com:7990").
Get("/rest/api/1.0/repos").
Reply(404).
Type("application/json").
File("testdata/repo.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Repositories.FindPerms(context.Background(), "PRJ/my-repo")
if err != nil {
Expand All @@ -123,6 +133,56 @@ func TestRepositoryPerms_ReadOnly(t *testing.T) {
t.Errorf("Unexpected Results")
t.Log(diff)
}

if gock.IsPending() {
t.Errorf("pending API requests")
}
}

func TestRepositoryPerms_Write(t *testing.T) {
defer gock.Off()

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

gock.New("http://example.com:7990").
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/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("project", "PRJ").
MatchParam("name", "my-repo").
Reply(200).
Type("application/json").
File("testdata/repos.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Repositories.FindPerms(context.Background(), "PRJ/my-repo")
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) {
Expand Down

0 comments on commit 47eed25

Please sign in to comment.