-
Notifications
You must be signed in to change notification settings - Fork 13
/
build.go
306 lines (260 loc) · 8.72 KB
/
build.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
// Copyright (c) 2020 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"
"errors"
"github.com/go-vela/types/library"
)
var (
// ErrEmptyBuildNumber defines the error type when a
// Build type has an empty Number field provided.
ErrEmptyBuildNumber = errors.New("empty build number provided")
// ErrEmptyBuildRepoID defines the error type when a
// Build type has an empty `RepoID` field provided.
ErrEmptyBuildRepoID = errors.New("empty build repo_id provided")
)
// Build is the database representation of a build for a pipeline.
type Build struct {
ID sql.NullInt64 `sql:"id"`
RepoID sql.NullInt64 `sql:"repo_id"`
Number sql.NullInt32 `sql:"number"`
Parent sql.NullInt32 `sql:"parent"`
Event sql.NullString `sql:"event"`
Status sql.NullString `sql:"status"`
Error sql.NullString `sql:"error"`
Enqueued sql.NullInt64 `sql:"enqueued"`
Created sql.NullInt64 `sql:"created"`
Started sql.NullInt64 `sql:"started"`
Finished sql.NullInt64 `sql:"finished"`
Deploy sql.NullString `sql:"deploy"`
Clone sql.NullString `sql:"clone"`
Source sql.NullString `sql:"source"`
Title sql.NullString `sql:"title"`
Message sql.NullString `sql:"message"`
Commit sql.NullString `sql:"commit"`
Sender sql.NullString `sql:"sender"`
Author sql.NullString `sql:"author"`
Email sql.NullString `sql:"email"`
Link sql.NullString `sql:"link"`
Branch sql.NullString `sql:"branch"`
Ref sql.NullString `sql:"ref"`
BaseRef sql.NullString `sql:"base_ref"`
Host sql.NullString `sql:"host"`
Runtime sql.NullString `sql:"runtime"`
Distribution sql.NullString `sql:"distribution"`
}
// Crop prepares the Build type for inserting into the database by
// trimming values that may exceed the database column limit.
func (b *Build) Crop() *Build {
// trim the Title field to 1000 characters
if len(b.Title.String) > 1000 {
b.Title = sql.NullString{String: b.Title.String[:1000], Valid: true}
}
// trim the Message field to 2000 characters
if len(b.Message.String) > 2000 {
b.Message = sql.NullString{String: b.Message.String[:2000], Valid: true}
}
return b
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Build type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (b *Build) Nullify() *Build {
if b == nil {
return nil
}
// check if the ID field should be false
if b.ID.Int64 == 0 {
b.ID.Valid = false
}
// check if the RepoID field should be false
if b.RepoID.Int64 == 0 {
b.RepoID.Valid = false
}
// check if the Number field should be false
if b.Number.Int32 == 0 {
b.Number.Valid = false
}
// check if the Parent field should be false
if b.Parent.Int32 == 0 {
b.Parent.Valid = false
}
// check if the Event field should be false
if len(b.Event.String) == 0 {
b.Event.Valid = false
}
// check if the Status field should be false
if len(b.Status.String) == 0 {
b.Status.Valid = false
}
// check if the Error field should be false
if len(b.Error.String) == 0 {
b.Error.Valid = false
}
// check if the Enqueued field should be false
if b.Enqueued.Int64 == 0 {
b.Enqueued.Valid = false
}
// check if the Created field should be false
if b.Created.Int64 == 0 {
b.Created.Valid = false
}
// check if the Started field should be false
if b.Started.Int64 == 0 {
b.Started.Valid = false
}
// check if the Finished field should be false
if b.Finished.Int64 == 0 {
b.Finished.Valid = false
}
// check if the Deploy field should be false
if len(b.Deploy.String) == 0 {
b.Deploy.Valid = false
}
// check if the Clone field should be false
if len(b.Clone.String) == 0 {
b.Clone.Valid = false
}
// check if the Source field should be false
if len(b.Source.String) == 0 {
b.Source.Valid = false
}
// check if the Title field should be false
if len(b.Title.String) == 0 {
b.Title.Valid = false
}
// check if the Message field should be false
if len(b.Message.String) == 0 {
b.Message.Valid = false
}
// check if the Commit field should be false
if len(b.Commit.String) == 0 {
b.Commit.Valid = false
}
// check if the Sender field should be false
if len(b.Sender.String) == 0 {
b.Sender.Valid = false
}
// check if the Author field should be false
if len(b.Author.String) == 0 {
b.Author.Valid = false
}
// check if the Email field should be false
if len(b.Email.String) == 0 {
b.Email.Valid = false
}
// check if the Link field should be false
if len(b.Link.String) == 0 {
b.Link.Valid = false
}
// check if the Branch field should be false
if len(b.Branch.String) == 0 {
b.Branch.Valid = false
}
// check if the Ref field should be false
if len(b.Ref.String) == 0 {
b.Ref.Valid = false
}
// check if the BaseRef field should be false
if len(b.BaseRef.String) == 0 {
b.BaseRef.Valid = false
}
// check if the Host field should be false
if len(b.Host.String) == 0 {
b.Host.Valid = false
}
// check if the Runtime field should be false
if len(b.Runtime.String) == 0 {
b.Runtime.Valid = false
}
// check if the Distribution field should be false
if len(b.Distribution.String) == 0 {
b.Distribution.Valid = false
}
return b
}
// ToLibrary converts the Build type
// to a library Build type.
func (b *Build) ToLibrary() *library.Build {
build := new(library.Build)
build.SetID(b.ID.Int64)
build.SetRepoID(b.RepoID.Int64)
build.SetNumber(int(b.Number.Int32))
build.SetParent(int(b.Parent.Int32))
build.SetEvent(b.Event.String)
build.SetStatus(b.Status.String)
build.SetError(b.Error.String)
build.SetEnqueued(b.Enqueued.Int64)
build.SetCreated(b.Created.Int64)
build.SetStarted(b.Started.Int64)
build.SetFinished(b.Finished.Int64)
build.SetDeploy(b.Deploy.String)
build.SetClone(b.Clone.String)
build.SetSource(b.Source.String)
build.SetTitle(b.Title.String)
build.SetMessage(b.Message.String)
build.SetCommit(b.Commit.String)
build.SetSender(b.Sender.String)
build.SetAuthor(b.Author.String)
build.SetEmail(b.Email.String)
build.SetLink(b.Link.String)
build.SetBranch(b.Branch.String)
build.SetRef(b.Ref.String)
build.SetBaseRef(b.BaseRef.String)
build.SetHost(b.Host.String)
build.SetRuntime(b.Runtime.String)
build.SetDistribution(b.Distribution.String)
return build
}
// Validate verifies the necessary fields for
// the Build type are populated correctly.
func (b *Build) Validate() error {
// verify the RepoID field is populated
if b.RepoID.Int64 <= 0 {
return ErrEmptyBuildRepoID
}
// verify the Number field is populated
if b.Number.Int32 <= 0 {
return ErrEmptyBuildNumber
}
return nil
}
// BuildFromLibrary converts the libray Build type
// to a database build type.
func BuildFromLibrary(b *library.Build) *Build {
build := &Build{
ID: sql.NullInt64{Int64: b.GetID(), Valid: true},
RepoID: sql.NullInt64{Int64: b.GetRepoID(), Valid: true},
Number: sql.NullInt32{Int32: int32(b.GetNumber()), Valid: true},
Parent: sql.NullInt32{Int32: int32(b.GetParent()), Valid: true},
Event: sql.NullString{String: b.GetEvent(), Valid: true},
Status: sql.NullString{String: b.GetStatus(), Valid: true},
Error: sql.NullString{String: b.GetError(), Valid: true},
Enqueued: sql.NullInt64{Int64: b.GetEnqueued(), Valid: true},
Created: sql.NullInt64{Int64: b.GetCreated(), Valid: true},
Started: sql.NullInt64{Int64: b.GetStarted(), Valid: true},
Finished: sql.NullInt64{Int64: b.GetFinished(), Valid: true},
Deploy: sql.NullString{String: b.GetDeploy(), Valid: true},
Clone: sql.NullString{String: b.GetClone(), Valid: true},
Source: sql.NullString{String: b.GetSource(), Valid: true},
Title: sql.NullString{String: b.GetTitle(), Valid: true},
Message: sql.NullString{String: b.GetMessage(), Valid: true},
Commit: sql.NullString{String: b.GetCommit(), Valid: true},
Sender: sql.NullString{String: b.GetSender(), Valid: true},
Author: sql.NullString{String: b.GetAuthor(), Valid: true},
Email: sql.NullString{String: b.GetEmail(), Valid: true},
Link: sql.NullString{String: b.GetLink(), Valid: true},
Branch: sql.NullString{String: b.GetBranch(), Valid: true},
Ref: sql.NullString{String: b.GetRef(), Valid: true},
BaseRef: sql.NullString{String: b.GetBaseRef(), Valid: true},
Host: sql.NullString{String: b.GetHost(), Valid: true},
Runtime: sql.NullString{String: b.GetRuntime(), Valid: true},
Distribution: sql.NullString{String: b.GetDistribution(), Valid: true},
}
return build.Nullify()
}