/
resource_repository.go
355 lines (310 loc) · 10.5 KB
/
resource_repository.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Copyright 2023 The Flux CD contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gitea
import (
"context"
"errors"
"reflect"
"code.gitea.io/sdk/gitea"
"github.com/fluxcd/go-git-providers/gitprovider"
"github.com/fluxcd/go-git-providers/validation"
)
func newUserRepository(ctx *clientContext, apiObj *gitea.Repository, ref gitprovider.RepositoryRef) *userRepository {
return &userRepository{
clientContext: ctx,
r: *apiObj,
ref: ref,
deployKeys: &DeployKeyClient{
clientContext: ctx,
ref: ref,
},
commits: &CommitClient{
clientContext: ctx,
ref: ref,
},
branches: &BranchClient{
clientContext: ctx,
ref: ref,
},
pullRequests: &PullRequestClient{
clientContext: ctx,
ref: ref,
},
files: &FileClient{
clientContext: ctx,
ref: ref,
},
}
}
var _ gitprovider.UserRepository = &userRepository{}
type userRepository struct {
*clientContext
r gitea.Repository // gitea
ref gitprovider.RepositoryRef
deployKeys *DeployKeyClient
commits *CommitClient
branches *BranchClient
pullRequests *PullRequestClient
files *FileClient
trees *TreeClient
}
// Get returns the repository information.
func (r *userRepository) Get() gitprovider.RepositoryInfo {
return repositoryFromAPI(&r.r)
}
// Set sets the repository information.
func (r *userRepository) Set(info gitprovider.RepositoryInfo) error {
if err := info.ValidateInfo(); err != nil {
return err
}
repositoryInfoToAPIObj(&info, &r.r)
return nil
}
// APIObject returns the underlying API object.
func (r *userRepository) APIObject() interface{} {
return &r.r
}
// Repository returns the repository reference.
func (r *userRepository) Repository() gitprovider.RepositoryRef {
return r.ref
}
// DeployKeys returns the deploy key client.
func (r *userRepository) DeployKeys() gitprovider.DeployKeyClient {
return r.deployKeys
}
// DeployTokens returns the deploy token client.
// ErrNoProviderSupport is returned as the provider does not support deploy tokens.
func (r *userRepository) DeployTokens() (gitprovider.DeployTokenClient, error) {
return nil, gitprovider.ErrNoProviderSupport
}
// Commits returns the commit client.
func (r *userRepository) Commits() gitprovider.CommitClient {
return r.commits
}
// Branches returns the branch client.
func (r *userRepository) Branches() gitprovider.BranchClient {
return r.branches
}
// PullRequests returns the pull request client.
func (r *userRepository) PullRequests() gitprovider.PullRequestClient {
return r.pullRequests
}
// Files returns the file client.
func (r *userRepository) Files() gitprovider.FileClient {
return r.files
}
// Trees returns the tree client.
func (r *userRepository) Trees() gitprovider.TreeClient {
return r.trees
}
// Update will apply the desired state in this object to the server.
// Only set fields will be respected (i.e. PATCH behaviour).
// In order to apply changes to this object, use the .Set({Resource}Info) error
// function, or cast .APIObject() to a pointer to the provider-specific type
// and set custom fields there.
//
// ErrNotFound is returned if the resource does not exist.
//
// The internal API object will be overridden with the received server data.
func (r *userRepository) Update(ctx context.Context) error {
// PATCH /repos/{owner}/{repo}
opts := gitea.EditRepoOption{
Name: &r.r.Name,
Description: &r.r.Description,
Website: &r.r.Website,
Private: &r.r.Private,
Template: &r.r.Template,
HasIssues: &r.r.HasIssues,
InternalTracker: r.r.InternalTracker,
ExternalTracker: r.r.ExternalTracker,
HasWiki: &r.r.HasWiki,
ExternalWiki: r.r.ExternalWiki,
DefaultBranch: &r.r.DefaultBranch,
HasPullRequests: &r.r.HasPullRequests,
HasProjects: &r.r.HasProjects,
IgnoreWhitespaceConflicts: &r.r.IgnoreWhitespaceConflicts,
AllowMerge: &r.r.AllowMerge,
AllowRebase: &r.r.AllowRebase,
AllowRebaseMerge: &r.r.AllowRebaseMerge,
AllowSquash: &r.r.AllowSquash,
Archived: &r.r.Archived,
DefaultMergeStyle: &r.r.DefaultMergeStyle,
}
if r.r.Mirror {
opts.MirrorInterval = &r.r.MirrorInterval
}
apiObj, err := updateRepo(r.c, r.ref.GetIdentity(), r.ref.GetRepository(), &opts)
if err != nil {
return err
}
r.r = *apiObj
return nil
}
// Reconcile makes sure the desired state in this object (called "req" here) becomes
// the actual state in the backing Git provider.
//
// If req doesn't exist under the hood, it is created (actionTaken == true).
// If req doesn't equal the actual state, the resource will be updated (actionTaken == true).
// If req is already the actual state, this is a no-op (actionTaken == false).
//
// The internal API object will be overridden with the received server data if actionTaken == true.
func (r *userRepository) Reconcile(ctx context.Context) (bool, error) {
opts := gitea.CreateRepoOption{
Name: r.r.Name,
Description: r.r.Description,
Private: r.r.Private,
Template: r.r.Template,
DefaultBranch: r.r.DefaultBranch,
}
apiObj, err := getRepo(r.c, r.ref.GetIdentity(), r.ref.GetRepository())
if err != nil {
// Create if not found
if errors.Is(err, gitprovider.ErrNotFound) {
orgName := ""
if orgRef, ok := r.ref.(gitprovider.OrgRepositoryRef); ok {
orgName = orgRef.Organization
}
repo, err := createRepo(r.c, orgName, opts)
if err != nil {
return true, err
}
r.r = *repo
return true, nil
}
return false, err
}
// Use wrappers here to extract the "spec" part of the object for comparison
desiredSpec := newGiteaRepositorySpec(&r.r)
actualSpec := newGiteaRepositorySpec(apiObj)
// If desired state already is the actual state, do nothing
if desiredSpec.Equals(actualSpec) {
return false, nil
}
// Otherwise, make the desired state the actual state
return true, r.Update(ctx)
}
// Delete deletes the current resource irreversibly.
//
// ErrNotFound is returned if the resource doesn't exist anymore.
func (r *userRepository) Delete(ctx context.Context) error {
return deleteRepo(r.c, r.ref.GetIdentity(), r.ref.GetRepository(), r.destructiveActions)
}
func newOrgRepository(ctx *clientContext, apiObj *gitea.Repository, ref gitprovider.RepositoryRef) *orgRepository {
return &orgRepository{
userRepository: *newUserRepository(ctx, apiObj, ref),
teamAccess: &TeamAccessClient{
clientContext: ctx,
ref: ref,
},
}
}
var _ gitprovider.OrgRepository = &orgRepository{}
type orgRepository struct {
userRepository
teamAccess *TeamAccessClient
}
// TeamAccess returns the team access client.
func (r *orgRepository) TeamAccess() gitprovider.TeamAccessClient {
return r.teamAccess
}
// validateRepositoryAPI validates the apiObj received from the server, to make sure that it is
// valid for our use.
func validateRepositoryAPI(apiObj *gitea.Repository) error {
return validateAPIObject("Gitea.Repository", func(validator validation.Validator) {
// Make sure name is set
if apiObj.Name == "" {
validator.Required("Name")
}
// Make sure visibility is valid if set
if !apiObj.Private {
v := gitprovider.RepositoryVisibility("public")
validator.Append(gitprovider.ValidateRepositoryVisibility(v), v, "Visibility")
} else {
v := gitprovider.RepositoryVisibility("private")
validator.Append(gitprovider.ValidateRepositoryVisibility(v), v, "Visibility")
}
})
}
func repositoryFromAPI(apiObj *gitea.Repository) gitprovider.RepositoryInfo {
repo := gitprovider.RepositoryInfo{
Description: &apiObj.Description,
DefaultBranch: &apiObj.DefaultBranch,
}
if !apiObj.Private {
repo.Visibility = gitprovider.RepositoryVisibilityVar(gitprovider.RepositoryVisibility("public"))
} else {
repo.Visibility = gitprovider.RepositoryVisibilityVar(gitprovider.RepositoryVisibility("private"))
}
return repo
}
func repositoryToAPI(repo *gitprovider.RepositoryInfo, ref gitprovider.RepositoryRef) gitea.CreateRepoOption {
apiObj := gitea.CreateRepoOption{
Name: *gitprovider.StringVar(ref.GetRepository()),
}
repositoryInfoToCreateOption(repo, &apiObj)
return apiObj
}
func repositoryInfoToCreateOption(repo *gitprovider.RepositoryInfo, apiObj *gitea.CreateRepoOption) {
if repo.Description != nil {
apiObj.Description = *repo.Description
}
if repo.DefaultBranch != nil {
apiObj.DefaultBranch = *repo.DefaultBranch
}
if repo.Visibility != nil {
apiObj.Private = *gitprovider.BoolVar(string(*repo.Visibility) == "private")
}
}
func repositoryInfoToAPIObj(repo *gitprovider.RepositoryInfo, apiObj *gitea.Repository) {
if repo.Description != nil {
apiObj.Description = *repo.Description
}
if repo.DefaultBranch != nil {
apiObj.DefaultBranch = *repo.DefaultBranch
}
if repo.Visibility != nil {
apiObj.Private = *gitprovider.BoolVar(string(*repo.Visibility) == "private")
}
}
// This function copies over the fields that are part of create/update requests of a repository
// i.e. the desired spec of the repository. This allows us to separate "spec" from "status" fields.
// See also: https://gitea.com/api/swagger#/repository/createCurrentUserRepo
func newGiteaRepositorySpec(repo *gitea.Repository) *giteaRepositorySpec {
return &giteaRepositorySpec{
&gitea.Repository{
// Generic
Name: repo.Name,
Description: repo.Description,
Website: repo.Website,
Private: repo.Private,
HasIssues: repo.HasIssues,
HasProjects: repo.HasProjects,
HasWiki: repo.HasWiki,
Internal: repo.Internal,
// Update-specific parameters
DefaultBranch: repo.DefaultBranch,
// Create-specific parameters
// Generic
AllowSquash: repo.AllowSquash,
AllowMerge: repo.AllowMerge,
AllowRebase: repo.AllowRebase,
},
}
}
type giteaRepositorySpec struct {
*gitea.Repository
}
// Equals compares two giteaRepositorySpec objects for equality.
func (s *giteaRepositorySpec) Equals(other *giteaRepositorySpec) bool {
return reflect.DeepEqual(s, other)
}