forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
repo.go
237 lines (205 loc) · 6.95 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
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gogs
import (
"context"
"fmt"
"strconv"
"time"
"github.com/jenkins-x/go-scm/scm"
)
type repositoryService struct {
client *wrapper
}
func (s *repositoryService) Create(context.Context, *scm.RepositoryInput) (*scm.Repository, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *repositoryService) FindCombinedStatus(ctx context.Context, repo, ref string) (*scm.CombinedStatus, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *repositoryService) FindUserPermission(ctx context.Context, repo string, user string) (string, *scm.Response, error) {
return "", nil, scm.ErrNotSupported
}
func (s *repositoryService) IsCollaborator(ctx context.Context, repo, user string) (bool, *scm.Response, error) {
return false, nil, scm.ErrNotSupported
}
func (s *repositoryService) ListCollaborators(ctx context.Context, repo string, ops scm.ListOptions) ([]scm.User, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *repositoryService) ListLabels(context.Context, string, scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *repositoryService) Find(ctx context.Context, repo string) (*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s", repo)
out := new(repository)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertRepository(out), res, err
}
func (s *repositoryService) FindHook(ctx context.Context, repo string, id string) (*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/hooks/%s", repo, id)
out := new(hook)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertHook(out), res, err
}
func (s *repositoryService) FindPerms(ctx context.Context, repo string) (*scm.Perm, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s", repo)
out := new(repository)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertRepository(out).Perm, res, err
}
func (s *repositoryService) List(ctx context.Context, _ scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("api/v1/user/repos")
out := []*repository{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertRepositoryList(out), res, err
}
func (s *repositoryService) ListOrganisation(ctx context.Context, org string, opts scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("api/v1/orgs/%s/repos", org)
out := []*repository{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertRepositoryList(out), res, err
}
func (s *repositoryService) ListUser(ctx context.Context, username string, opts scm.ListOptions) ([]*scm.Repository, *scm.Response, error) {
path := fmt.Sprintf("api/v1/users/%s/repos", username)
out := []*repository{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertRepositoryList(out), res, err
}
func (s *repositoryService) ListHooks(ctx context.Context, repo string, _ scm.ListOptions) ([]*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/hooks", repo)
out := []*hook{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertHookList(out), res, err
}
func (s *repositoryService) ListStatus(context.Context, string, string, scm.ListOptions) ([]*scm.Status, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *repositoryService) CreateHook(ctx context.Context, repo string, input *scm.HookInput) (*scm.Hook, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/hooks", repo)
in := new(hook)
in.Type = "gogs"
in.Active = true
in.Config.Secret = input.Secret
in.Config.ContentType = "json"
in.Config.URL = input.Target
in.Events = append(
input.NativeEvents,
convertHookEvent(input.Events)...,
)
out := new(hook)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertHook(out), res, err
}
func (s *repositoryService) CreateStatus(context.Context, string, string, *scm.StatusInput) (*scm.Status, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *repositoryService) DeleteHook(ctx context.Context, repo string, id string) (*scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/hooks/%s", repo, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
//
// native data structures
//
type (
// gogs repository resource.
repository struct {
ID int `json:"id"`
Owner user `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Private bool `json:"private"`
Fork bool `json:"fork"`
HTMLURL string `json:"html_url"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
DefaultBranch string `json:"default_branch"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Permissions perm `json:"permissions"`
}
// gogs permissions details.
perm struct {
Admin bool `json:"admin"`
Push bool `json:"push"`
Pull bool `json:"pull"`
}
// gogs hook resource.
hook struct {
ID int `json:"id"`
Type string `json:"type"`
Events []string `json:"events"`
Active bool `json:"active"`
Config hookConfig `json:"config"`
}
// gogs hook configuration details.
hookConfig struct {
URL string `json:"url"`
ContentType string `json:"content_type"`
Secret string `json:"secret"`
}
)
//
// native data structure conversion
//
func convertRepositoryList(src []*repository) []*scm.Repository {
var dst []*scm.Repository
for _, v := range src {
dst = append(dst, convertRepository(v))
}
return dst
}
func convertRepository(src *repository) *scm.Repository {
return &scm.Repository{
ID: strconv.Itoa(src.ID),
Namespace: userLogin(&src.Owner),
Name: src.Name,
Perm: convertPerm(src.Permissions),
Branch: src.DefaultBranch,
Private: src.Private,
Clone: src.CloneURL,
CloneSSH: src.SSHURL,
}
}
func convertPerm(src perm) *scm.Perm {
return &scm.Perm{
Push: src.Push,
Pull: src.Pull,
Admin: src.Admin,
}
}
func convertHookList(src []*hook) []*scm.Hook {
var dst []*scm.Hook
for _, v := range src {
dst = append(dst, convertHook(v))
}
return dst
}
func convertHook(from *hook) *scm.Hook {
return &scm.Hook{
ID: strconv.Itoa(from.ID),
Active: from.Active,
Target: from.Config.URL,
Events: from.Events,
}
}
func convertHookEvent(from scm.HookEvents) []string {
var events []string
if from.PullRequest {
events = append(events, "pull_request")
}
if from.Issue {
events = append(events, "issues")
}
if from.IssueComment || from.PullRequestComment {
events = append(events, "issue_comment")
}
if from.Branch || from.Tag {
events = append(events, "create")
events = append(events, "delete")
}
if from.Push {
events = append(events, "push")
}
return events
}