forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
repo.go
226 lines (198 loc) · 6.38 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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) {
s.data.Collaborators = append(s.data.Collaborators, user)
if len(s.data.UserPermissions) == 0 {
s.data.UserPermissions = make(map[string]map[string]string)
}
m := s.data.UserPermissions[repo]
if len(m) == 0 {
m = make(map[string]string)
}
alreadyExists := false
if _, ok := m[user]; ok {
alreadyExists = true
}
m[user] = permission
s.data.UserPermissions[repo] = m
// lets add an invitation if the user isn't already in the repo...
if !alreadyExists {
id := int64(len(s.data.Invitations) + 1)
names := strings.SplitN(repo, "/", 2)
owner := ""
repoName := ""
if len(names) == 2 {
owner = names[0]
repoName = names[1]
}
s.data.Invitations = append(s.data.Invitations, &scm.Invitation{
ID: id,
Repo: &scm.Repository{
Namespace: owner,
Name: repoName,
FullName: repo,
},
Invitee: &scm.User{},
Inviter: &scm.User{},
Permissions: permission,
Created: time.Now(),
})
}
return true, alreadyExists, nil, nil
}
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, &scm.Response{Status: 200}, nil
}
}
return nil, &scm.Response{Status: 404}, 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)
namespace := input.Namespace
if namespace == "" {
namespace = s.client.Username
}
fullName := scm.Join(namespace, input.Name)
link := fmt.Sprintf("https://fake.com/%s.git", fullName)
repo := &scm.Repository{
Namespace: namespace,
Name: input.Name,
FullName: fullName,
Link: link,
Clone: link,
Created: time.Now(),
}
s.data.Repositories = append(s.data.Repositories, repo)
return repo, nil, nil
}
func (s *repositoryService) Fork(ctx context.Context, input *scm.RepositoryInput, origRepo string) (*scm.Repository, *scm.Response, error) {
// TODO: Actually make this fork rather than just duplicate Create.
return s.Create(ctx, input)
}
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) {
/* #nosec */
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
}
func (s *repositoryService) Delete(context.Context, string) (*scm.Response, error) {
panic("implement me")
}