-
Notifications
You must be signed in to change notification settings - Fork 13
/
step.go
212 lines (179 loc) · 5.78 KB
/
step.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
// Copyright (c) 2019 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 (
// ErrEmptyStepBuildID defines the error type when a
// Step type has an empty BuildID field provided.
ErrEmptyStepBuildID = errors.New("empty step build_id provided")
// ErrEmptyStepName defines the error type when a
// Step type has an empty Name field provided.
ErrEmptyStepName = errors.New("empty step name provided")
// ErrEmptyStepNumber defines the error type when a
// Step type has an empty Number field provided.
ErrEmptyStepNumber = errors.New("empty step number provided")
// ErrEmptyStepRepoID defines the error type when a
// Step type has an empty RepoID field provided.
ErrEmptyStepRepoID = errors.New("empty step repo_id provided")
)
// Step is the database representation of a step in a build.
type Step struct {
ID sql.NullInt64 `sql:"id"`
BuildID sql.NullInt64 `sql:"build_id"`
RepoID sql.NullInt64 `sql:"repo_id"`
Number sql.NullInt32 `sql:"number"`
Name sql.NullString `sql:"name"`
Stage sql.NullString `sql:"stage"`
Status sql.NullString `sql:"status"`
Error sql.NullString `sql:"error"`
ExitCode sql.NullInt32 `sql:"exit_code"`
Created sql.NullInt64 `sql:"created"`
Started sql.NullInt64 `sql:"started"`
Finished sql.NullInt64 `sql:"finished"`
Host sql.NullString `sql:"host"`
Runtime sql.NullString `sql:"runtime"`
Distribution sql.NullString `sql:"distribution"`
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Step type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (s *Step) Nullify() *Step {
if s == nil {
return nil
}
// check if the ID field should be false
if s.ID.Int64 == 0 {
s.ID.Valid = false
}
// check if the BuildID field should be false
if s.BuildID.Int64 == 0 {
s.BuildID.Valid = false
}
// check if the RepoID field should be false
if s.RepoID.Int64 == 0 {
s.RepoID.Valid = false
}
// check if the Number field should be false
if s.Number.Int32 == 0 {
s.Number.Valid = false
}
// check if the Name field should be false
if len(s.Name.String) == 0 {
s.Name.Valid = false
}
// check if the Stage field should be false
if len(s.Stage.String) == 0 {
s.Stage.Valid = false
}
// check if the Status field should be false
if len(s.Status.String) == 0 {
s.Status.Valid = false
}
// check if the Error field should be false
if len(s.Error.String) == 0 {
s.Error.Valid = false
}
// check if the ExitCode field should be false
if s.ExitCode.Int32 == 0 {
s.ExitCode.Valid = false
}
// check if Created field should be false
if s.Created.Int64 == 0 {
s.Created.Valid = false
}
// check if Started field should be false
if s.Started.Int64 == 0 {
s.Started.Valid = false
}
// check if Finished field should be false
if s.Finished.Int64 == 0 {
s.Finished.Valid = false
}
// check if the Host field should be false
if len(s.Host.String) == 0 {
s.Host.Valid = false
}
// check if the Runtime field should be false
if len(s.Runtime.String) == 0 {
s.Runtime.Valid = false
}
// check if the Distribution field should be false
if len(s.Distribution.String) == 0 {
s.Distribution.Valid = false
}
return s
}
// ToLibrary converts the Step type
// to a library Step type.
func (s *Step) ToLibrary() *library.Step {
n := int(s.Number.Int32)
e := int(s.ExitCode.Int32)
return &library.Step{
ID: &s.ID.Int64,
BuildID: &s.BuildID.Int64,
RepoID: &s.RepoID.Int64,
Number: &n,
Name: &s.Name.String,
Stage: &s.Stage.String,
Status: &s.Status.String,
Error: &s.Error.String,
ExitCode: &e,
Created: &s.Created.Int64,
Started: &s.Started.Int64,
Finished: &s.Finished.Int64,
Host: &s.Host.String,
Runtime: &s.Runtime.String,
Distribution: &s.Distribution.String,
}
}
// Validate verifies the necessary fields for
// the Step type are populated correctly.
func (s *Step) Validate() error {
// verify the BuildID field is populated
if s.BuildID.Int64 <= 0 {
return ErrEmptyStepBuildID
}
// verify the RepoID field is populated
if s.RepoID.Int64 <= 0 {
return ErrEmptyStepRepoID
}
// verify the Number field is populated
if s.Number.Int32 <= 0 {
return ErrEmptyStepNumber
}
// verify the Name field is populated
if len(s.Name.String) == 0 {
return ErrEmptyStepName
}
return nil
}
// StepFromLibrary converts the library Step type
// to a database Step type.
func StepFromLibrary(s *library.Step) *Step {
step := &Step{
ID: sql.NullInt64{Int64: s.GetID(), Valid: true},
BuildID: sql.NullInt64{Int64: s.GetBuildID(), Valid: true},
RepoID: sql.NullInt64{Int64: s.GetRepoID(), Valid: true},
Number: sql.NullInt32{Int32: int32(s.GetNumber()), Valid: true},
Name: sql.NullString{String: s.GetName(), Valid: true},
Stage: sql.NullString{String: s.GetStage(), Valid: true},
Status: sql.NullString{String: s.GetStatus(), Valid: true},
Error: sql.NullString{String: s.GetError(), Valid: true},
ExitCode: sql.NullInt32{Int32: int32(s.GetExitCode()), Valid: true},
Created: sql.NullInt64{Int64: s.GetCreated(), Valid: true},
Started: sql.NullInt64{Int64: s.GetStarted(), Valid: true},
Finished: sql.NullInt64{Int64: s.GetFinished(), Valid: true},
Host: sql.NullString{String: s.GetHost(), Valid: true},
Runtime: sql.NullString{String: s.GetRuntime(), Valid: true},
Distribution: sql.NullString{String: s.GetDistribution(), Valid: true},
}
return step.Nullify()
}