-
Notifications
You must be signed in to change notification settings - Fork 13
/
repo.go
210 lines (177 loc) · 5.96 KB
/
repo.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
// 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 (
// ErrEmptyRepoFullName defines the error type when a
// Repo type has an empty FullName field provided.
ErrEmptyRepoFullName = errors.New("empty repo full_name provided")
// ErrEmptyRepoHash defines the error type when a
// Repo type has an empty Hash field provided.
ErrEmptyRepoHash = errors.New("empty repo hash provided")
// ErrEmptyRepoName defines the error type when a
// Repo type has an empty Name field provided.
ErrEmptyRepoName = errors.New("empty repo name provided")
// ErrEmptyRepoOrg defines the error type when a
// Repo type has an empty Org field provided.
ErrEmptyRepoOrg = errors.New("empty repo org provided")
// ErrEmptyRepoUserID defines the error type when a
// Repo type has an empty UserID field provided.
ErrEmptyRepoUserID = errors.New("empty repo user_id provided")
)
// Repo is the database representation of a repo.
type Repo struct {
ID sql.NullInt64 `sql:"id"`
UserID sql.NullInt64 `sql:"user_id"`
Hash sql.NullString `sql:"hash"`
Org sql.NullString `sql:"org"`
Name sql.NullString `sql:"name"`
FullName sql.NullString `sql:"full_name"`
Link sql.NullString `sql:"link"`
Clone sql.NullString `sql:"clone"`
Branch sql.NullString `sql:"branch"`
Timeout sql.NullInt64 `sql:"timeout"`
Visibility sql.NullString `sql:"visibility"`
Private sql.NullBool `sql:"private"`
Trusted sql.NullBool `sql:"trusted"`
Active sql.NullBool `sql:"active"`
AllowPull sql.NullBool `sql:"allow_pull"`
AllowPush sql.NullBool `sql:"allow_push"`
AllowDeploy sql.NullBool `sql:"allow_deploy"`
AllowTag sql.NullBool `sql:"allow_tag"`
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the Repo type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (r *Repo) Nullify() *Repo {
if r == nil {
return nil
}
// check if the ID field should be false
if r.ID.Int64 == 0 {
r.ID.Valid = false
}
// check if the UserID field should be false
if r.UserID.Int64 == 0 {
r.UserID.Valid = false
}
// check if the Hash field should be false
if len(r.Hash.String) == 0 {
r.Hash.Valid = false
}
// check if the Org field should be false
if len(r.Org.String) == 0 {
r.Org.Valid = false
}
// check if the Name field should be false
if len(r.Name.String) == 0 {
r.Name.Valid = false
}
// check if the FullName field should be false
if len(r.FullName.String) == 0 {
r.FullName.Valid = false
}
// check if the Link field should be false
if len(r.Link.String) == 0 {
r.Link.Valid = false
}
// check if the Clone field should be false
if len(r.Clone.String) == 0 {
r.Clone.Valid = false
}
// check if the Branch field should be false
if len(r.Branch.String) == 0 {
r.Branch.Valid = false
}
// check if the Timeout field should be false
if r.Timeout.Int64 == 0 {
r.Timeout.Valid = false
}
// check if the Visibility field should be false
if len(r.Visibility.String) == 0 {
r.Visibility.Valid = false
}
return r
}
// ToLibrary converts the Repo type
// to a library Repo type.
func (r *Repo) ToLibrary() *library.Repo {
repo := new(library.Repo)
repo.SetID(r.ID.Int64)
repo.SetUserID(r.UserID.Int64)
repo.SetHash(r.Hash.String)
repo.SetOrg(r.Org.String)
repo.SetName(r.Name.String)
repo.SetFullName(r.FullName.String)
repo.SetLink(r.Link.String)
repo.SetClone(r.Clone.String)
repo.SetBranch(r.Branch.String)
repo.SetTimeout(r.Timeout.Int64)
repo.SetVisibility(r.Visibility.String)
repo.SetPrivate(r.Private.Bool)
repo.SetTrusted(r.Trusted.Bool)
repo.SetActive(r.Active.Bool)
repo.SetAllowPull(r.AllowPull.Bool)
repo.SetAllowPush(r.AllowPush.Bool)
repo.SetAllowDeploy(r.AllowDeploy.Bool)
repo.SetAllowTag(r.AllowTag.Bool)
return repo
}
// Validate verifies the necessary fields for
// the Repo type are populated correctly.
func (r *Repo) Validate() error {
// verify the UserID field is populated
if r.UserID.Int64 <= 0 {
return ErrEmptyRepoUserID
}
// verify the Hash field is populated
if len(r.Hash.String) == 0 {
return ErrEmptyRepoHash
}
// verify the Org field is populated
if len(r.Org.String) == 0 {
return ErrEmptyRepoOrg
}
// verify the Name field is populated
if len(r.Name.String) == 0 {
return ErrEmptyRepoName
}
// verify the FullName field is populated
if len(r.FullName.String) == 0 {
return ErrEmptyRepoFullName
}
return nil
}
// RepoFromLibrary converts the libray Repo type
// to a database repo type.
func RepoFromLibrary(r *library.Repo) *Repo {
repo := &Repo{
ID: sql.NullInt64{Int64: r.GetID(), Valid: true},
UserID: sql.NullInt64{Int64: r.GetUserID(), Valid: true},
Hash: sql.NullString{String: r.GetHash(), Valid: true},
Org: sql.NullString{String: r.GetOrg(), Valid: true},
Name: sql.NullString{String: r.GetName(), Valid: true},
FullName: sql.NullString{String: r.GetFullName(), Valid: true},
Link: sql.NullString{String: r.GetLink(), Valid: true},
Clone: sql.NullString{String: r.GetClone(), Valid: true},
Branch: sql.NullString{String: r.GetBranch(), Valid: true},
Timeout: sql.NullInt64{Int64: r.GetTimeout(), Valid: true},
Visibility: sql.NullString{String: r.GetVisibility(), Valid: true},
Private: sql.NullBool{Bool: r.GetPrivate(), Valid: true},
Trusted: sql.NullBool{Bool: r.GetTrusted(), Valid: true},
Active: sql.NullBool{Bool: r.GetActive(), Valid: true},
AllowPull: sql.NullBool{Bool: r.GetAllowPull(), Valid: true},
AllowPush: sql.NullBool{Bool: r.GetAllowPush(), Valid: true},
AllowDeploy: sql.NullBool{Bool: r.GetAllowDeploy(), Valid: true},
AllowTag: sql.NullBool{Bool: r.GetAllowTag(), Valid: true},
}
return repo.Nullify()
}