forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
repo.go
169 lines (147 loc) · 4.92 KB
/
repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package fake
import (
"context"
"fmt"
"math/rand"
"strings"
"time"
"github.com/jenkins-x/go-scm/scm"
)
type repositoryService struct {
client *wrapper
data *Data
}
func (s *repositoryService) FindCombinedStatus(ctx context.Context, repo, ref string) (*scm.CombinedStatus, *scm.Response, error) {
statuses, _, err := s.ListStatus(ctx, repo, ref, scm.ListOptions{})
if err != nil {
return nil, nil, err
}
return &scm.CombinedStatus{
Sha: ref,
Statuses: statuses,
}, nil, nil
}
func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) {
f := s.data
m := f.UserPermissions[repo]
perm := ""
if m != nil {
perm = m[user]
}
return perm, nil, nil
}
// 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) AddCollaborator(ctx context.Context, repo, user, permission string) (bool, bool, *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)
for _, collab := range f.Collaborators {
if NormLogin(collab) == normed {
return true, nil, nil
}
}
return false, nil, nil
}
func (s *repositoryService) ListCollaborators(ctx context.Context, repo string, ops scm.ListOptions) ([]scm.User, *scm.Response, error) {
f := s.data
result := make([]scm.User, 0, len(f.Collaborators))
for _, login := range f.Collaborators {
result = append(result, scm.User{Login: login})
}
return result, nil, nil
}
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) 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) {
f := s.data
la := []*scm.Label{}
for _, l := range f.RepoLabelsExisting {
la = append(la, &scm.Label{Name: l})
}
return la, nil, nil
}
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))
for _, status := range f.Statuses[ref] {
result = append(result, status)
}
return result, nil, nil
}
func (s *repositoryService) Create(ctx context.Context, input *scm.RepositoryInput) (*scm.Repository, *scm.Response, error) {
s.data.CreateRepositories = append(s.data.CreateRepositories, input)
fullName := scm.Join(input.Namespace, input.Name)
repo := &scm.Repository{
Namespace: input.Namespace,
Name: input.Name,
FullName: fullName,
Link: fmt.Sprintf("https://fake.com/%s.git", fullName),
Created: time.Now(),
}
s.data.Repositories = append(s.data.Repositories, repo)
return repo, nil, nil
}
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: fmt.Sprintf("%d", rand.Int()),
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, hookID string) (*scm.Response, error) {
hooks := s.data.Hooks[fullName]
for i, h := range hooks {
if h.ID == hookID {
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) {
statuses := s.data.Statuses[ref]
if statuses == nil {
statuses = []*scm.Status{}
}
status := scm.ConvertStatusInputToStatus(in)
for _, existing := range statuses {
if existing.Label == status.Label {
*existing = *status
return status, nil, nil
}
}
statuses = append(statuses, status)
s.data.Statuses[ref] = statuses
return status, nil, nil
}