-
Notifications
You must be signed in to change notification settings - Fork 13
/
hook.go
190 lines (159 loc) · 5.28 KB
/
hook.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
// Copyright (c) 2021 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 (
// ErrEmptyHookNumber defines the error type when a
// Hook type has an empty Number field provided.
ErrEmptyHookNumber = errors.New("empty webhook number provided")
// ErrEmptyHookRepoID defines the error type when a
// Hook type has an empty RepoID field provided.
ErrEmptyHookRepoID = errors.New("empty webhook repo_id provided")
// ErrEmptyHookSourceID defines the error type when a
// Hook type has an empty SourceID field provided.
ErrEmptyHookSourceID = errors.New("empty webhook source_id provided")
)
// Hook is the database representation of a webhook for a repo.
type Hook struct {
ID sql.NullInt64 `sql:"id"`
RepoID sql.NullInt64 `sql:"repo_id"`
BuildID sql.NullInt64 `sql:"build_id"`
Number sql.NullInt32 `sql:"number"`
SourceID sql.NullString `sql:"source_id"`
Created sql.NullInt64 `sql:"created"`
Host sql.NullString `sql:"host"`
Event sql.NullString `sql:"event"`
Branch sql.NullString `sql:"branch"`
Error sql.NullString `sql:"error"`
Status sql.NullString `sql:"status"`
Link sql.NullString `sql:"link"`
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Hook type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (h *Hook) Nullify() *Hook {
if h == nil {
return nil
}
// check if the ID field should be false
if h.ID.Int64 == 0 {
h.ID.Valid = false
}
// check if the RepoID field should be false
if h.RepoID.Int64 == 0 {
h.RepoID.Valid = false
}
// check if the BuildID field should be false
if h.BuildID.Int64 == 0 {
h.BuildID.Valid = false
}
// check if the Number field should be false
if h.Number.Int32 == 0 {
h.Number.Valid = false
}
// check if the SourceID field should be false
if len(h.SourceID.String) == 0 {
h.SourceID.Valid = false
}
// check if the Created field should be false
if h.Created.Int64 == 0 {
h.Created.Valid = false
}
// check if the Host field should be false
if len(h.Host.String) == 0 {
h.Host.Valid = false
}
// check if the Event field should be false
if len(h.Event.String) == 0 {
h.Event.Valid = false
}
// check if the Branch field should be false
if len(h.Branch.String) == 0 {
h.Branch.Valid = false
}
// check if the Error field should be false
if len(h.Error.String) == 0 {
h.Error.Valid = false
}
// check if the Status field should be false
if len(h.Status.String) == 0 {
h.Status.Valid = false
}
// check if the Link field should be false
if len(h.Link.String) == 0 {
h.Link.Valid = false
}
return h
}
// ToLibrary converts the Hook type
// to a library Hook type.
func (h *Hook) ToLibrary() *library.Hook {
hook := new(library.Hook)
hook.SetID(h.ID.Int64)
hook.SetRepoID(h.RepoID.Int64)
hook.SetBuildID(h.BuildID.Int64)
hook.SetNumber(int(h.Number.Int32))
hook.SetSourceID(h.SourceID.String)
hook.SetCreated(h.Created.Int64)
hook.SetHost(h.Host.String)
hook.SetEvent(h.Event.String)
hook.SetBranch(h.Branch.String)
hook.SetError(h.Error.String)
hook.SetStatus(h.Status.String)
hook.SetLink(h.Link.String)
return hook
}
// Validate verifies the necessary fields for
// the Hook type are populated correctly.
func (h *Hook) Validate() error {
// verify the RepoID field is populated
if h.RepoID.Int64 <= 0 {
return ErrEmptyHookRepoID
}
// verify the Number field is populated
if h.Number.Int32 <= 0 {
return ErrEmptyHookNumber
}
// verify the SourceID field is populated
if len(h.SourceID.String) <= 0 {
return ErrEmptyHookSourceID
}
// ensure that all Hook string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
h.SourceID = sql.NullString{String: sanitize(h.SourceID.String), Valid: true}
h.Host = sql.NullString{String: sanitize(h.Host.String), Valid: true}
h.Event = sql.NullString{String: sanitize(h.Event.String), Valid: true}
h.Branch = sql.NullString{String: sanitize(h.Branch.String), Valid: true}
h.Error = sql.NullString{String: sanitize(h.Error.String), Valid: true}
h.Status = sql.NullString{String: sanitize(h.Status.String), Valid: true}
h.Link = sql.NullString{String: sanitize(h.Link.String), Valid: true}
return nil
}
// HookFromLibrary converts the Hook type
// to a library Hook type.
func HookFromLibrary(h *library.Hook) *Hook {
hook := &Hook{
ID: sql.NullInt64{Int64: h.GetID(), Valid: true},
RepoID: sql.NullInt64{Int64: h.GetRepoID(), Valid: true},
BuildID: sql.NullInt64{Int64: h.GetBuildID(), Valid: true},
Number: sql.NullInt32{Int32: int32(h.GetNumber()), Valid: true},
SourceID: sql.NullString{String: h.GetSourceID(), Valid: true},
Created: sql.NullInt64{Int64: h.GetCreated(), Valid: true},
Host: sql.NullString{String: h.GetHost(), Valid: true},
Event: sql.NullString{String: h.GetEvent(), Valid: true},
Branch: sql.NullString{String: h.GetBranch(), Valid: true},
Error: sql.NullString{String: h.GetError(), Valid: true},
Status: sql.NullString{String: h.GetStatus(), Valid: true},
Link: sql.NullString{String: h.GetLink(), Valid: true},
}
return hook.Nullify()
}