-
Notifications
You must be signed in to change notification settings - Fork 13
/
repo.go
333 lines (283 loc) · 10.3 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
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
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package database
import (
"database/sql"
"encoding/base64"
"errors"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/lib/pq"
)
var (
// ErrEmptyRepoFullName defines the error type when a
// Repo type has an empty FullName field provided.
ErrEmptyRepoFullName = errors.New("empty repo full_name provided")
// ErrEmptyRepoHash defines the error type when a
// Repo type has an empty Hash field provided.
ErrEmptyRepoHash = errors.New("empty repo hash provided")
// ErrEmptyRepoName defines the error type when a
// Repo type has an empty Name field provided.
ErrEmptyRepoName = errors.New("empty repo name provided")
// ErrEmptyRepoOrg defines the error type when a
// Repo type has an empty Org field provided.
ErrEmptyRepoOrg = errors.New("empty repo org provided")
// ErrEmptyRepoUserID defines the error type when a
// Repo type has an empty UserID field provided.
ErrEmptyRepoUserID = errors.New("empty repo user_id provided")
// ErrEmptyRepoVisibility defines the error type when a
// Repo type has an empty Visibility field provided.
ErrEmptyRepoVisibility = errors.New("empty repo visibility provided")
// ErrExceededTopicsLimit defines the error type when a
// Repo type has Topics field provided that exceeds the database limit.
ErrExceededTopicsLimit = errors.New("exceeded topics limit")
)
// Repo is the database representation of a repo.
type Repo struct {
ID sql.NullInt64 `sql:"id"`
UserID sql.NullInt64 `sql:"user_id"`
Hash sql.NullString `sql:"hash"`
Org sql.NullString `sql:"org"`
Name sql.NullString `sql:"name"`
FullName sql.NullString `sql:"full_name"`
Link sql.NullString `sql:"link"`
Clone sql.NullString `sql:"clone"`
Branch sql.NullString `sql:"branch"`
Topics pq.StringArray `sql:"topics" gorm:"type:varchar(1020)"`
BuildLimit sql.NullInt64 `sql:"build_limit"`
Timeout sql.NullInt64 `sql:"timeout"`
Counter sql.NullInt32 `sql:"counter"`
Visibility sql.NullString `sql:"visibility"`
Private sql.NullBool `sql:"private"`
Trusted sql.NullBool `sql:"trusted"`
Active sql.NullBool `sql:"active"`
AllowPull sql.NullBool `sql:"allow_pull"`
AllowPush sql.NullBool `sql:"allow_push"`
AllowDeploy sql.NullBool `sql:"allow_deploy"`
AllowTag sql.NullBool `sql:"allow_tag"`
AllowComment sql.NullBool `sql:"allow_comment"`
PipelineType sql.NullString `sql:"pipeline_type"`
PreviousName sql.NullString `sql:"previous_name"`
}
// Decrypt will manipulate the existing repo hash by
// base64 decoding that value. Then, a AES-256 cipher
// block is created from the encryption key in order to
// decrypt the base64 decoded secret value.
func (r *Repo) Decrypt(key string) error {
// base64 decode the encrypted repo hash
decoded, err := base64.StdEncoding.DecodeString(r.Hash.String)
if err != nil {
return err
}
// decrypt the base64 decoded repo hash
decrypted, err := decrypt(key, decoded)
if err != nil {
return err
}
// set the decrypted repo hash
r.Hash = sql.NullString{
String: string(decrypted),
Valid: true,
}
return nil
}
// Encrypt will manipulate the existing repo hash by
// creating a AES-256 cipher block from the encryption
// key in order to encrypt the repo hash. Then, the
// repo hash is base64 encoded for transport across
// network boundaries.
func (r *Repo) Encrypt(key string) error {
// encrypt the repo hash
encrypted, err := encrypt(key, []byte(r.Hash.String))
if err != nil {
return err
}
// base64 encode the encrypted repo hash to make it network safe
r.Hash = sql.NullString{
String: base64.StdEncoding.EncodeToString(encrypted),
Valid: true,
}
return nil
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Repo type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (r *Repo) Nullify() *Repo {
if r == nil {
return nil
}
// check if the ID field should be false
if r.ID.Int64 == 0 {
r.ID.Valid = false
}
// check if the UserID field should be false
if r.UserID.Int64 == 0 {
r.UserID.Valid = false
}
// check if the Hash field should be false
if len(r.Hash.String) == 0 {
r.Hash.Valid = false
}
// check if the Org field should be false
if len(r.Org.String) == 0 {
r.Org.Valid = false
}
// check if the Name field should be false
if len(r.Name.String) == 0 {
r.Name.Valid = false
}
// check if the FullName field should be false
if len(r.FullName.String) == 0 {
r.FullName.Valid = false
}
// check if the Link field should be false
if len(r.Link.String) == 0 {
r.Link.Valid = false
}
// check if the Clone field should be false
if len(r.Clone.String) == 0 {
r.Clone.Valid = false
}
// check if the Branch field should be false
if len(r.Branch.String) == 0 {
r.Branch.Valid = false
}
// check if the BuildLimit field should be false
if r.BuildLimit.Int64 == 0 {
r.BuildLimit.Valid = false
}
// check if the Timeout field should be false
if r.Timeout.Int64 == 0 {
r.Timeout.Valid = false
}
// check if the Visibility field should be false
if len(r.Visibility.String) == 0 {
r.Visibility.Valid = false
}
// check if the PipelineType field should be false
if len(r.PipelineType.String) == 0 {
r.PipelineType.Valid = false
}
// check if the PreviousName field should be false
if len(r.PreviousName.String) == 0 {
r.PreviousName.Valid = false
}
return r
}
// ToLibrary converts the Repo type
// to a library Repo type.
func (r *Repo) ToLibrary() *library.Repo {
repo := new(library.Repo)
repo.SetID(r.ID.Int64)
repo.SetUserID(r.UserID.Int64)
repo.SetHash(r.Hash.String)
repo.SetOrg(r.Org.String)
repo.SetName(r.Name.String)
repo.SetFullName(r.FullName.String)
repo.SetLink(r.Link.String)
repo.SetClone(r.Clone.String)
repo.SetBranch(r.Branch.String)
repo.SetTopics(r.Topics)
repo.SetBuildLimit(r.BuildLimit.Int64)
repo.SetTimeout(r.Timeout.Int64)
repo.SetCounter(int(r.Counter.Int32))
repo.SetVisibility(r.Visibility.String)
repo.SetPrivate(r.Private.Bool)
repo.SetTrusted(r.Trusted.Bool)
repo.SetActive(r.Active.Bool)
repo.SetAllowPull(r.AllowPull.Bool)
repo.SetAllowPush(r.AllowPush.Bool)
repo.SetAllowDeploy(r.AllowDeploy.Bool)
repo.SetAllowTag(r.AllowTag.Bool)
repo.SetAllowComment(r.AllowComment.Bool)
repo.SetPipelineType(r.PipelineType.String)
repo.SetPreviousName(r.PreviousName.String)
return repo
}
// Validate verifies the necessary fields for
// the Repo type are populated correctly.
func (r *Repo) Validate() error {
// verify the UserID field is populated
if r.UserID.Int64 <= 0 {
return ErrEmptyRepoUserID
}
// verify the Hash field is populated
if len(r.Hash.String) == 0 {
return ErrEmptyRepoHash
}
// verify the Org field is populated
if len(r.Org.String) == 0 {
return ErrEmptyRepoOrg
}
// verify the Name field is populated
if len(r.Name.String) == 0 {
return ErrEmptyRepoName
}
// verify the FullName field is populated
if len(r.FullName.String) == 0 {
return ErrEmptyRepoFullName
}
// verify the Visibility field is populated
if len(r.Visibility.String) == 0 {
return ErrEmptyRepoVisibility
}
// calculate total size of favorites while sanitizing entries
total := 0
for i, t := range r.Topics {
r.Topics[i] = sanitize(t)
total += len(t)
}
// verify the Favorites field is within the database constraints
// len is to factor in number of comma separators included in the database field,
// removing 1 due to the last item not having an appended comma
if (total + len(r.Topics) - 1) > constants.TopicsMaxSize {
return ErrExceededTopicsLimit
}
// ensure that all Repo string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
r.Org = sql.NullString{String: sanitize(r.Org.String), Valid: r.Org.Valid}
r.Name = sql.NullString{String: sanitize(r.Name.String), Valid: r.Name.Valid}
r.FullName = sql.NullString{String: sanitize(r.FullName.String), Valid: r.FullName.Valid}
r.Link = sql.NullString{String: sanitize(r.Link.String), Valid: r.Link.Valid}
r.Clone = sql.NullString{String: sanitize(r.Clone.String), Valid: r.Clone.Valid}
r.Branch = sql.NullString{String: sanitize(r.Branch.String), Valid: r.Branch.Valid}
r.Visibility = sql.NullString{String: sanitize(r.Visibility.String), Valid: r.Visibility.Valid}
r.PipelineType = sql.NullString{String: sanitize(r.PipelineType.String), Valid: r.PipelineType.Valid}
return nil
}
// RepoFromLibrary converts the library Repo type
// to a database repo type.
func RepoFromLibrary(r *library.Repo) *Repo {
repo := &Repo{
ID: sql.NullInt64{Int64: r.GetID(), Valid: true},
UserID: sql.NullInt64{Int64: r.GetUserID(), Valid: true},
Hash: sql.NullString{String: r.GetHash(), Valid: true},
Org: sql.NullString{String: r.GetOrg(), Valid: true},
Name: sql.NullString{String: r.GetName(), Valid: true},
FullName: sql.NullString{String: r.GetFullName(), Valid: true},
Link: sql.NullString{String: r.GetLink(), Valid: true},
Clone: sql.NullString{String: r.GetClone(), Valid: true},
Branch: sql.NullString{String: r.GetBranch(), Valid: true},
Topics: pq.StringArray(r.GetTopics()),
BuildLimit: sql.NullInt64{Int64: r.GetBuildLimit(), Valid: true},
Timeout: sql.NullInt64{Int64: r.GetTimeout(), Valid: true},
Counter: sql.NullInt32{Int32: int32(r.GetCounter()), Valid: true},
Visibility: sql.NullString{String: r.GetVisibility(), Valid: true},
Private: sql.NullBool{Bool: r.GetPrivate(), Valid: true},
Trusted: sql.NullBool{Bool: r.GetTrusted(), Valid: true},
Active: sql.NullBool{Bool: r.GetActive(), Valid: true},
AllowPull: sql.NullBool{Bool: r.GetAllowPull(), Valid: true},
AllowPush: sql.NullBool{Bool: r.GetAllowPush(), Valid: true},
AllowDeploy: sql.NullBool{Bool: r.GetAllowDeploy(), Valid: true},
AllowTag: sql.NullBool{Bool: r.GetAllowTag(), Valid: true},
AllowComment: sql.NullBool{Bool: r.GetAllowComment(), Valid: true},
PipelineType: sql.NullString{String: r.GetPipelineType(), Valid: true},
PreviousName: sql.NullString{String: r.GetPreviousName(), Valid: true},
}
return repo.Nullify()
}