Skip to content

Commit

Permalink
fix: add IsMember for github + bitbucket server
Browse files Browse the repository at this point in the history
  • Loading branch information
jstrachan committed Jul 30, 2019
1 parent bb5eeca commit edcb503
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 130 deletions.
4 changes: 4 additions & 0 deletions scm/driver/bitbucket/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type organizationService struct {
client *wrapper
}

func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
panic("implement me")
}

func (s *organizationService) ListTeams(ctx context.Context, org string, ops scm.ListOptions) ([]*scm.Team, *scm.Response, error) {
panic("implement me")
}
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/fake/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type organizationService struct {
data *Data
}

func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
panic("implement me")
}

func (s *organizationService) Find(context.Context, string) (*scm.Organization, *scm.Response, error) {
panic("implement me")
}
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/gitea/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type organizationService struct {
client *wrapper
}

func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
panic("implement me")
}

func (s *organizationService) ListTeams(ctx context.Context, org string, ops scm.ListOptions) ([]*scm.Team, *scm.Response, error) {
panic("implement me")
}
Expand Down
18 changes: 18 additions & 0 deletions scm/driver/github/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ type teamMember struct {
Login string `json:"login"`
}

func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s/members/%s", org, user)
res, err := s.client.do(ctx, "GET", path, nil, nil)
if err != nil {
return false, res, err
}
code := res.Status
if code == 204 {
return true, res, nil
} else if code == 404 {
return false, res, nil
} else if code == 302 {
return false, res, fmt.Errorf("requester is not %s org member", org)
}
// Should be unreachable.
return false, res, fmt.Errorf("unexpected status: %d", code)
}

func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organization, *scm.Response, error) {
path := fmt.Sprintf("orgs/%s", name)
out := new(organization)
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/gitlab/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ type organizationService struct {
client *wrapper
}

func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
panic("implement me")
}

func (s *organizationService) ListTeams(ctx context.Context, org string, ops scm.ListOptions) ([]*scm.Team, *scm.Response, error) {
panic("implement me")
}
Expand Down
4 changes: 4 additions & 0 deletions scm/driver/gogs/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type organizationService struct {
client *wrapper
}

func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
panic("implement me")
}

func (s *organizationService) ListTeams(ctx context.Context, org string, ops scm.ListOptions) ([]*scm.Team, *scm.Response, error) {
panic("implement me")
}
Expand Down
3 changes: 2 additions & 1 deletion scm/driver/stash/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ type gitService struct {
}

func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *scm.Response, error) {
panic("implement me")
// TODO implement
return ref, nil, nil
}

func (s *gitService) DeleteRef(ctx context.Context, repo, ref string) (*scm.Response, error) {
Expand Down
3 changes: 2 additions & 1 deletion scm/driver/stash/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func (s *issueService) ListEvents(context.Context, string, int, scm.ListOptions)
}

func (s *issueService) ListLabels(context.Context, string, int, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
panic("implement me")
// TODO implement this
return nil, nil, nil
}

func (s *issueService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
Expand Down
20 changes: 20 additions & 0 deletions scm/driver/stash/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package stash

import (
"context"
"fmt"

"github.com/jenkins-x/go-scm/scm"
)
Expand All @@ -22,6 +23,25 @@ func (s *organizationService) ListTeamMembers(ctx context.Context, id int, role
panic("implement me")
}

func (s *organizationService) IsMember(ctx context.Context, org string, user string) (bool, *scm.Response, error) {
opts := scm.ListOptions{
Size: 1000,
}
path := fmt.Sprintf("rest/api/1.0/projects/%s/permissions/users?%s", org, encodeListOptions(opts))
out := new(participants)
res, err := s.client.do(ctx, "GET", path, nil, out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
for _, participant := range out.Values {
if participant.User.Name == user || participant.User.Slug == user {
return true, res, err
}
}
return false, res, err
}

func (s *organizationService) Find(ctx context.Context, name string) (*scm.Organization, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
Expand Down
22 changes: 14 additions & 8 deletions scm/driver/stash/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,20 @@ func convertPullRequest(from *pullRequest) *scm.PullRequest {
from.FromRef.Repository.Slug,
)
return &scm.PullRequest{
Number: from.ID,
Title: from.Title,
Body: from.Description,
Sha: from.FromRef.LatestCommit,
Ref: fmt.Sprintf("refs/pull-requests/%d/from", from.ID),
Source: from.FromRef.DisplayID,
Target: from.ToRef.DisplayID,
Fork: fork,
Number: from.ID,
Title: from.Title,
Body: from.Description,
Sha: from.FromRef.LatestCommit,
Ref: fmt.Sprintf("refs/pull-requests/%d/from", from.ID),
Source: from.FromRef.DisplayID,
Target: from.ToRef.DisplayID,
Fork: fork,
Base: scm.PullRequestBranch{
Ref: from.FromRef.LatestCommit,
},
Head: scm.PullRequestBranch{
Sha: from.FromRef.LatestCommit,
},
Link: extractSelfLink(from.Links.Self),
Closed: from.Closed,
Merged: from.State == "MERGED",
Expand Down
47 changes: 44 additions & 3 deletions scm/driver/stash/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ type status struct {
Desc string `json:"description"`
}

type participants struct {
pagination
Values []*participant `json:"values"`
}

type participant struct {
User user `json:"user"`
Permission string `json:"permission"`
}

type repositoryService struct {
client *wrapper
}
Expand All @@ -105,15 +115,38 @@ func (s *repositoryService) FindUserPermission(ctx context.Context, repo string,
}

func (s *repositoryService) IsCollaborator(ctx context.Context, repo, user string) (bool, *scm.Response, error) {
panic("implement me")
users, resp, err := s.ListCollaborators(ctx, repo)
if err != nil {
return false, resp, err
}
for _, u := range users {
if u.Name == user || u.Login == user {
return true, resp, err
}
}
return false, resp, err
}

func (s *repositoryService) ListCollaborators(ctx context.Context, repo string) ([]scm.User, *scm.Response, error) {
panic("implement me")
namespace, name := scm.Split(repo)
opts := scm.ListOptions{
Size: 1000,
}
//path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/participants?role=PARTICIPANT&%s", namespace, name, encodeListOptions(opts))
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/permissions/users?%s", namespace, name, encodeListOptions(opts))
//path := fmt.Sprintf("rest/api/1.0/projects/%s/permissions/users?%s", namespace, encodeListOptions(opts))
out := new(participants)
res, err := s.client.do(ctx, "GET", path, nil, out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertParticipants(out), res, err
}

func (s *repositoryService) ListLabels(context.Context, string, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
panic("implement me")
// TODO implement me!
return nil, nil, nil
}

// Find returns the repository by name.
Expand Down Expand Up @@ -369,3 +402,11 @@ func convertState(from string) scm.State {
return scm.StateUnknown
}
}

func convertParticipants(participants *participants) []scm.User {
answer := []scm.User{}
for _, p := range participants.Values {
answer = append(answer, *convertUser(&p.User))
}
return answer
}
3 changes: 3 additions & 0 deletions scm/driver/stash/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,8 @@ type Error struct {
}

func (e *Error) Error() string {
if len(e.Errors) == 0 {
return "No message available"
}
return e.Errors[0].Message
}
6 changes: 6 additions & 0 deletions scm/driver/stash/testdata/pr.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"Ref": "refs/pull-requests/1/from",
"Source": "feature/x",
"Target": "master",
"Base": {
"Ref": "131cb13f4aed12e725177bc4b7c28db67839bf9f"
},
"Head": {
"Sha": "131cb13f4aed12e725177bc4b7c28db67839bf9f"
},
"Fork": "PRJ/my-repo",
"Link": "http://example.com:7990/projects/PRJ/repos/my-repo/pull-requests/1",
"Closed": false,
Expand Down
48 changes: 27 additions & 21 deletions scm/driver/stash/testdata/prs.json.golden
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
[
{
"Number": 1,
"Title": "Updated Files",
"Body": "* added LICENSE\r\n* update files\r\n* update files",
"Sha": "131cb13f4aed12e725177bc4b7c28db67839bf9f",
"Ref": "refs/pull-requests/1/from",
"Source": "feature/x",
"Target": "master",
"Fork": "PRJ/my-repo",
"Link": "http://example.com:7990/projects/PRJ/repos/my-repo/pull-requests/1",
"Closed": false,
"Merged": false,
"Author": {
"Login": "jcitizen",
"Name": "Jane Citizen",
"Email": "[email protected]",
"Avatar": "https://www.gravatar.com/avatar/9e26471d35a78862c17e467d87cddedf.jpg"
},
"Created": "2018-07-04T22:01:10-07:00",
"Updated": "2018-07-04T22:01:10-07:00"
}
{
"Number": 1,
"Title": "Updated Files",
"Body": "* added LICENSE\r\n* update files\r\n* update files",
"Sha": "131cb13f4aed12e725177bc4b7c28db67839bf9f",
"Ref": "refs/pull-requests/1/from",
"Source": "feature/x",
"Target": "master",
"Base": {
"Ref": "131cb13f4aed12e725177bc4b7c28db67839bf9f"
},
"Head": {
"Sha": "131cb13f4aed12e725177bc4b7c28db67839bf9f"
},
"Fork": "PRJ/my-repo",
"Link": "http://example.com:7990/projects/PRJ/repos/my-repo/pull-requests/1",
"Closed": false,
"Merged": false,
"Author": {
"Login": "jcitizen",
"Name": "Jane Citizen",
"Email": "[email protected]",
"Avatar": "https://www.gravatar.com/avatar/9e26471d35a78862c17e467d87cddedf.jpg"
},
"Created": "2018-07-04T22:01:10-07:00",
"Updated": "2018-07-04T22:01:10-07:00"
}
]
Loading

0 comments on commit edcb503

Please sign in to comment.