-
Notifications
You must be signed in to change notification settings - Fork 13
/
pipeline.go
230 lines (193 loc) · 7.07 KB
/
pipeline.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
// Copyright (c) 2022 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 (
// ErrEmptyPipelineCommit defines the error type when a
// Pipeline type has an empty Commit field provided.
ErrEmptyPipelineCommit = errors.New("empty pipeline commit provided")
// ErrEmptyPipelineRef defines the error type when a
// Pipeline type has an empty Ref field provided.
ErrEmptyPipelineRef = errors.New("empty pipeline ref provided")
// ErrEmptyPipelineRepoID defines the error type when a
// Pipeline type has an empty RepoID field provided.
ErrEmptyPipelineRepoID = errors.New("empty pipeline repo_id provided")
// ErrEmptyPipelineType defines the error type when a
// Pipeline type has an empty Type field provided.
ErrEmptyPipelineType = errors.New("empty pipeline type provided")
// ErrEmptyPipelineVersion defines the error type when a
// Pipeline type has an empty Version field provided.
ErrEmptyPipelineVersion = errors.New("empty pipeline version provided")
)
// Pipeline is the database representation of a pipeline.
type Pipeline struct {
ID sql.NullInt64 `sql:"id"`
RepoID sql.NullInt64 `sql:"repo_id"`
Commit sql.NullString `sql:"commit"`
Flavor sql.NullString `sql:"flavor"`
Platform sql.NullString `sql:"platform"`
Ref sql.NullString `sql:"ref"`
Type sql.NullString `sql:"type"`
Version sql.NullString `sql:"version"`
ExternalSecrets sql.NullBool `sql:"external_secrets"`
InternalSecrets sql.NullBool `sql:"internal_secrets"`
Services sql.NullBool `sql:"services"`
Stages sql.NullBool `sql:"stages"`
Steps sql.NullBool `sql:"steps"`
Templates sql.NullBool `sql:"templates"`
Data []byte `sql:"data"`
}
// Compress will manipulate the existing data for the
// pipeline by compressing that data. This produces
// a significantly smaller amount of data that is
// stored in the system.
func (p *Pipeline) Compress(level int) error {
// compress the database pipeline data
data, err := compress(level, p.Data)
if err != nil {
return err
}
// overwrite database pipeline data with compressed pipeline data
p.Data = data
return nil
}
// Decompress will manipulate the existing data for the
// pipeline by decompressing that data. This allows us
// to have a significantly smaller amount of data that
// is stored in the system.
func (p *Pipeline) Decompress() error {
// decompress the database pipeline data
data, err := decompress(p.Data)
if err != nil {
return err
}
// overwrite compressed pipeline data with decompressed pipeline data
p.Data = data
return nil
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Pipeline type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (p *Pipeline) Nullify() *Pipeline {
if p == nil {
return nil
}
// check if the ID field should be false
if p.ID.Int64 == 0 {
p.ID.Valid = false
}
// check if the RepoID field should be false
if p.RepoID.Int64 == 0 {
p.RepoID.Valid = false
}
// check if the Commit field should be false
if len(p.Commit.String) == 0 {
p.Commit.Valid = false
}
// check if the Flavor field should be false
if len(p.Flavor.String) == 0 {
p.Flavor.Valid = false
}
// check if the Platform field should be false
if len(p.Platform.String) == 0 {
p.Platform.Valid = false
}
// check if the Ref field should be false
if len(p.Ref.String) == 0 {
p.Ref.Valid = false
}
// check if the Type field should be false
if len(p.Type.String) == 0 {
p.Type.Valid = false
}
// check if the Version field should be false
if len(p.Version.String) == 0 {
p.Version.Valid = false
}
return p
}
// ToLibrary converts the Pipeline type
// to a library Pipeline type.
func (p *Pipeline) ToLibrary() *library.Pipeline {
pipeline := new(library.Pipeline)
pipeline.SetID(p.ID.Int64)
pipeline.SetRepoID(p.RepoID.Int64)
pipeline.SetCommit(p.Commit.String)
pipeline.SetFlavor(p.Flavor.String)
pipeline.SetPlatform(p.Platform.String)
pipeline.SetRef(p.Ref.String)
pipeline.SetType(p.Type.String)
pipeline.SetVersion(p.Version.String)
pipeline.SetExternalSecrets(p.ExternalSecrets.Bool)
pipeline.SetInternalSecrets(p.InternalSecrets.Bool)
pipeline.SetServices(p.Services.Bool)
pipeline.SetStages(p.Stages.Bool)
pipeline.SetSteps(p.Steps.Bool)
pipeline.SetTemplates(p.Templates.Bool)
pipeline.SetData(p.Data)
return pipeline
}
// Validate verifies the necessary fields for
// the Pipeline type are populated correctly.
func (p *Pipeline) Validate() error {
// verify the Commit field is populated
if len(p.Commit.String) == 0 {
return ErrEmptyPipelineCommit
}
// verify the Ref field is populated
if len(p.Ref.String) == 0 {
return ErrEmptyPipelineRef
}
// verify the RepoID field is populated
if p.RepoID.Int64 <= 0 {
return ErrEmptyPipelineRepoID
}
// verify the Type field is populated
if len(p.Type.String) == 0 {
return ErrEmptyPipelineType
}
// verify the Version field is populated
if len(p.Version.String) == 0 {
return ErrEmptyPipelineVersion
}
// ensure that all Pipeline string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
p.Commit = sql.NullString{String: sanitize(p.Commit.String), Valid: p.Commit.Valid}
p.Flavor = sql.NullString{String: sanitize(p.Flavor.String), Valid: p.Flavor.Valid}
p.Platform = sql.NullString{String: sanitize(p.Platform.String), Valid: p.Platform.Valid}
p.Ref = sql.NullString{String: sanitize(p.Ref.String), Valid: p.Ref.Valid}
p.Type = sql.NullString{String: sanitize(p.Type.String), Valid: p.Type.Valid}
p.Version = sql.NullString{String: sanitize(p.Version.String), Valid: p.Version.Valid}
return nil
}
// PipelineFromLibrary converts the library Pipeline type
// to a database Pipeline type.
func PipelineFromLibrary(p *library.Pipeline) *Pipeline {
pipeline := &Pipeline{
ID: sql.NullInt64{Int64: p.GetID(), Valid: true},
RepoID: sql.NullInt64{Int64: p.GetRepoID(), Valid: true},
Commit: sql.NullString{String: p.GetCommit(), Valid: true},
Flavor: sql.NullString{String: p.GetFlavor(), Valid: true},
Platform: sql.NullString{String: p.GetPlatform(), Valid: true},
Ref: sql.NullString{String: p.GetRef(), Valid: true},
Type: sql.NullString{String: p.GetType(), Valid: true},
Version: sql.NullString{String: p.GetVersion(), Valid: true},
ExternalSecrets: sql.NullBool{Bool: p.GetExternalSecrets(), Valid: true},
InternalSecrets: sql.NullBool{Bool: p.GetInternalSecrets(), Valid: true},
Services: sql.NullBool{Bool: p.GetServices(), Valid: true},
Stages: sql.NullBool{Bool: p.GetStages(), Valid: true},
Steps: sql.NullBool{Bool: p.GetSteps(), Valid: true},
Templates: sql.NullBool{Bool: p.GetTemplates(), Valid: true},
Data: p.GetData(),
}
return pipeline.Nullify()
}