-
Notifications
You must be signed in to change notification settings - Fork 13
/
deployment.go
183 lines (154 loc) · 5.41 KB
/
deployment.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
// SPDX-License-Identifier: Apache-2.0
package database
import (
"database/sql"
"errors"
"fmt"
"github.com/go-vela/types/library"
"github.com/go-vela/types/raw"
"github.com/lib/pq"
)
var (
// ErrEmptyDeploymentNumber defines the error type when a
// Deployment type has an empty Number field provided.
ErrEmptyDeploymentNumber = errors.New("empty deployment number provided")
// ErrEmptyDeploymentRepoID defines the error type when a
// Deployment type has an empty RepoID field provided.
ErrEmptyDeploymentRepoID = errors.New("empty deployment repo_id provided")
)
// Deployment is the database representation of a deployment for a repo.
type Deployment struct {
ID sql.NullInt64 `sql:"id"`
Number sql.NullInt64 `sql:"number"`
RepoID sql.NullInt64 `sql:"repo_id"`
URL sql.NullString `sql:"url"`
Commit sql.NullString `sql:"commit"`
Ref sql.NullString `sql:"ref"`
Task sql.NullString `sql:"task"`
Target sql.NullString `sql:"target"`
Description sql.NullString `sql:"description"`
Payload raw.StringSliceMap `sql:"payload"`
CreatedAt sql.NullInt64 `sql:"created_at"`
CreatedBy sql.NullString `sql:"created_by"`
Builds pq.StringArray `sql:"builds" gorm:"type:varchar(50)"`
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Deployment type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (d *Deployment) Nullify() *Deployment {
if d == nil {
return nil
}
// check if the ID field should be false
if d.ID.Int64 == 0 {
d.ID.Valid = false
}
// check if the Number field should be false
if d.Number.Int64 == 0 {
d.Number.Valid = false
}
// check if the RepoID field should be false
if d.RepoID.Int64 == 0 {
d.RepoID.Valid = false
}
// check if the URL field should be false
if len(d.URL.String) == 0 {
d.URL.Valid = false
}
// check if the Commit field should be false
if len(d.Commit.String) == 0 {
d.Commit.Valid = false
}
// check if the Ref field should be false
if len(d.Ref.String) == 0 {
d.Ref.Valid = false
}
// check if the Task field should be false
if len(d.Task.String) == 0 {
d.Task.Valid = false
}
// check if the Target field should be false
if len(d.Target.String) == 0 {
d.Target.Valid = false
}
// check if the Description field should be false
if len(d.Description.String) == 0 {
d.Description.Valid = false
}
// check if the CreatedAt field should be false
if d.CreatedAt.Int64 == 0 {
d.CreatedAt.Valid = false
}
// check if the CreatedBy field should be false
if len(d.CreatedBy.String) == 0 {
d.CreatedBy.Valid = false
}
return d
}
// ToLibrary converts the Deployment type
// to a library Deployment type.
func (d *Deployment) ToLibrary(builds []*library.Build) *library.Deployment {
deployment := new(library.Deployment)
deployment.SetID(d.ID.Int64)
deployment.SetNumber(d.Number.Int64)
deployment.SetRepoID(d.RepoID.Int64)
deployment.SetURL(d.URL.String)
deployment.SetCommit(d.Commit.String)
deployment.SetRef(d.Ref.String)
deployment.SetTask(d.Task.String)
deployment.SetTarget(d.Target.String)
deployment.SetDescription(d.Description.String)
deployment.SetPayload(d.Payload)
deployment.SetCreatedAt(d.CreatedAt.Int64)
deployment.SetCreatedBy(d.CreatedBy.String)
deployment.SetBuilds(builds)
return deployment
}
// Validate verifies the necessary fields for
// the Deployment type are populated correctly.
func (d *Deployment) Validate() error {
// verify the RepoID field is populated
if d.RepoID.Int64 <= 0 {
return ErrEmptyDeploymentRepoID
}
// verify the Number field is populated
if d.Number.Int64 <= 0 {
return ErrEmptyDeploymentNumber
}
// ensure that all Deployment string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
d.Commit = sql.NullString{String: sanitize(d.Commit.String), Valid: d.Commit.Valid}
d.Ref = sql.NullString{String: sanitize(d.Ref.String), Valid: d.Ref.Valid}
d.Task = sql.NullString{String: sanitize(d.Task.String), Valid: d.Task.Valid}
d.Target = sql.NullString{String: sanitize(d.Target.String), Valid: d.Target.Valid}
d.Description = sql.NullString{String: sanitize(d.Description.String), Valid: d.Description.Valid}
return nil
}
// DeploymentFromLibrary converts the library Deployment type
// to a database Deployment type.
func DeploymentFromLibrary(d *library.Deployment) *Deployment {
buildIDs := []string{}
for _, build := range d.GetBuilds() {
buildIDs = append(buildIDs, fmt.Sprint(build.GetID()))
}
deployment := &Deployment{
ID: sql.NullInt64{Int64: d.GetID(), Valid: true},
Number: sql.NullInt64{Int64: d.GetNumber(), Valid: true},
RepoID: sql.NullInt64{Int64: d.GetRepoID(), Valid: true},
URL: sql.NullString{String: d.GetURL(), Valid: true},
Commit: sql.NullString{String: d.GetCommit(), Valid: true},
Ref: sql.NullString{String: d.GetRef(), Valid: true},
Task: sql.NullString{String: d.GetTask(), Valid: true},
Target: sql.NullString{String: d.GetTarget(), Valid: true},
Description: sql.NullString{String: d.GetDescription(), Valid: true},
Payload: d.GetPayload(),
CreatedAt: sql.NullInt64{Int64: d.GetCreatedAt(), Valid: true},
CreatedBy: sql.NullString{String: d.GetCreatedBy(), Valid: true},
Builds: buildIDs,
}
return deployment.Nullify()
}