Skip to content

Commit

Permalink
Merge pull request drone#75 from jstrachan/stuff
Browse files Browse the repository at this point in the history
fix: add support for the users API on the fake provider
  • Loading branch information
jenkins-x-bot authored Feb 18, 2020
2 parents cc0bf0c + d47e1b5 commit 010ed53
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 32 deletions.
4 changes: 4 additions & 0 deletions scm/driver/fake/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type Data struct {
PullRequestsCreated map[int]*scm.PullRequestInput
PullRequestID int
CreateRepositories []*scm.RepositoryInput
Repositories []*scm.Repository
CurrentUser scm.User
Users []*scm.User
Hooks map[string][]*scm.Hook

//All Labels That Exist In The Repo
RepoLabelsExisting []string
Expand Down
5 changes: 4 additions & 1 deletion scm/driver/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
// scm operations have been performed
func NewDefault() (*scm.Client, *Data) {
data := NewData()
data.CurrentUser.Login = "dummy"
data.CurrentUser.Name = "dummy"

client := &wrapper{new(scm.Client)}
client.BaseURL = &url.URL{
Host: "fake.com",
Expand All @@ -25,11 +28,11 @@ func NewDefault() (*scm.Client, *Data) {
client.PullRequests = &pullService{client: client, data: data}
client.Repositories = &repositoryService{client: client, data: data}
client.Reviews = &reviewService{client: client, data: data}
client.Users = &userService{client: client, data: data}

// TODO
/*
client.Contents = &contentService{client}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
*/
return client.Client, data
Expand Down
83 changes: 52 additions & 31 deletions scm/driver/fake/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ func (s *repositoryService) FindUserPermission(ctx context.Context, repo string,
// NormLogin normalizes login strings
var NormLogin = strings.ToLower

func (s *repositoryService) FindHook(context.Context, string, string) (*scm.Hook, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) FindPerms(context.Context, string) (*scm.Perm, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) ListOrganisation(context.Context, string, scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) ListUser(context.Context, string, scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) IsCollaborator(ctx context.Context, repo, login string) (bool, *scm.Response, error) {
f := s.data
normed := NormLogin(login)
Expand All @@ -57,28 +73,17 @@ func (s *repositoryService) ListCollaborators(ctx context.Context, repo string)
return result, nil, nil
}

func (s *repositoryService) Find(context.Context, string) (*scm.Repository, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) FindHook(context.Context, string, string) (*scm.Hook, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) FindPerms(context.Context, string) (*scm.Perm, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) List(context.Context, scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) ListOrganisation(context.Context, string, scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
panic("implement me")
func (s *repositoryService) Find(ctx context.Context, fullName string) (*scm.Repository, *scm.Response, error) {
for _, repo := range s.data.Repositories {
if repo.FullName == fullName {
return repo, nil, nil
}
}
return nil, nil, scm.ErrNotFound
}

func (s *repositoryService) ListUser(context.Context, string, scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
panic("implement me")
func (s *repositoryService) List(ctx context.Context, opts scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
return s.data.Repositories, nil, nil
}

func (s *repositoryService) ListLabels(context.Context, string, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
Expand All @@ -90,10 +95,6 @@ func (s *repositoryService) ListLabels(context.Context, string, scm.ListOptions)
return la, nil, nil
}

func (s *repositoryService) ListHooks(context.Context, string, scm.ListOptions) ([]*scm.Hook, *scm.Response, error) {
panic("implement me")
}

func (s *repositoryService) ListStatus(ctx context.Context, repo string, ref string, opt scm.ListOptions) ([]*scm.Status, *scm.Response, error) {
f := s.data
result := make([]*scm.Status, 0, len(f.Statuses))
Expand All @@ -108,14 +109,38 @@ func (s *repositoryService) Create(ctx context.Context, input *scm.RepositoryInp
repo := &scm.Repository{
Namespace: input.Namespace,
Name: input.Name,
FullName: scm.Join(input.Namespace, input.Namespace),
FullName: scm.Join(input.Namespace, input.Name),
Created: time.Now(),
}
s.data.Repositories = append(s.data.Repositories, repo)
return repo, nil, nil
}

func (s *repositoryService) CreateHook(context.Context, string, *scm.HookInput) (*scm.Hook, *scm.Response, error) {
panic("implement me")
func (s *repositoryService) ListHooks(ctx context.Context, fullName string, opts scm.ListOptions) ([]*scm.Hook, *scm.Response, error) {
return s.data.Hooks[fullName], nil, nil
}

func (s *repositoryService) CreateHook(ctx context.Context, fullName string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
hook := &scm.Hook{
ID: "dummy",
Name: input.Name,
Target: input.Target,
Active: true,
}
s.data.Hooks[fullName] = append(s.data.Hooks[fullName], hook)
return hook, nil, nil
}

func (s *repositoryService) DeleteHook(ctx context.Context, fullName string, hookName string) (*scm.Response, error) {
hooks := s.data.Hooks[fullName]
for i, h := range hooks {
if h.Name == hookName {
hooks = append(hooks[0:i], hooks[i+1:]...)
s.data.Hooks[fullName] = hooks
break
}
}
return nil, nil
}

func (s *repositoryService) CreateStatus(ctx context.Context, repo string, ref string, in *scm.StatusInput) (*scm.Status, *scm.Response, error) {
Expand All @@ -134,7 +159,3 @@ func (s *repositoryService) CreateStatus(ctx context.Context, repo string, ref s
s.data.Statuses[ref] = statuses
return status, nil, nil
}

func (s *repositoryService) DeleteHook(context.Context, string, string) (*scm.Response, error) {
panic("implement me")
}
29 changes: 29 additions & 0 deletions scm/driver/fake/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package fake

import (
"context"

"github.com/jenkins-x/go-scm/scm"
)

type userService struct {
client *wrapper
data *Data
}

func (u *userService) Find(ctx context.Context) (*scm.User, *scm.Response, error) {
return &u.data.CurrentUser, nil, nil
}

func (u *userService) FindEmail(ctx context.Context) (string, *scm.Response, error) {
return u.data.CurrentUser.Email, nil, nil
}

func (u *userService) FindLogin(ctx context.Context, login string) (*scm.User, *scm.Response, error) {
for _, user := range u.data.Users {
if user.Login == login {
return user, nil, nil
}
}
return nil, nil, nil
}

0 comments on commit 010ed53

Please sign in to comment.