-
Notifications
You must be signed in to change notification settings - Fork 5
/
models.go
206 lines (187 loc) · 6.74 KB
/
models.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
package resource
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/shurcooL/githubv4"
)
type StatusFilter struct {
Context string
State string
}
// Source represents the configuration for the resource.
type Source struct {
Repository string `json:"repository"`
AccessToken string `json:"access_token"`
OdAdvanced OdAdvanced `json:"od_advanced"`
V3Endpoint string `json:"v3_endpoint"`
V4Endpoint string `json:"v4_endpoint"`
Paths []string `json:"paths"`
IgnorePaths []string `json:"ignore_paths"`
DisableCISkip bool `json:"disable_ci_skip"`
DisableGitLFS bool `json:"disable_git_lfs"`
SkipSSLVerification bool `json:"skip_ssl_verification"`
DisableForks bool `json:"disable_forks"`
IgnoreDrafts bool `json:"ignore_drafts"`
GitCryptKey string `json:"git_crypt_key"`
BaseBranch string `json:"base_branch"`
RequiredReviewApprovals int `json:"required_review_approvals"`
Labels []string `json:"labels"`
States []githubv4.PullRequestState `json:"states"`
StatusFilters []StatusFilter `json:"status_filters"`
}
type OdAdvanced struct {
VaultAddr string `json:"vault_addr"`
VaultApproleRoleId string `json:"vault_approle_role_id"`
VaultApproleSecretId string `json:"vault_approle_secret_id"`
MinRemainingThresholdBeforeUsingAccessTokenAdditional int `json:"min_remaining_threshold_before_using_access_token_additional"`
DataDogApiKey string `json:"datadog_api_key"`
DataDogAppKey string `json:"datadog_app_key"`
DataDogMetricName string `json:"datadog_metric_name"`
DataDogResourcesName string `json:"datadog_resources_name"`
DataDogResourcesType string `json:"datadog_resources_type"`
Debug bool `json:"debug"`
}
// Validate the source configuration.
func (s *Source) Validate() error {
if s.AccessToken == "" {
return errors.New("access_token must be set")
}
if s.Repository == "" {
return errors.New("repository must be set")
}
if s.V3Endpoint != "" && s.V4Endpoint == "" {
return errors.New("v4_endpoint must be set together with v3_endpoint")
}
if s.V4Endpoint != "" && s.V3Endpoint == "" {
return errors.New("v3_endpoint must be set together with v4_endpoint")
}
for _, state := range s.States {
switch state {
case githubv4.PullRequestStateOpen:
case githubv4.PullRequestStateClosed:
case githubv4.PullRequestStateMerged:
default:
return errors.New(fmt.Sprintf("states value \"%s\" must be one of: OPEN, MERGED, CLOSED", state))
}
}
return nil
}
// Metadata output from get/put steps.
type Metadata []*MetadataField
// Add a MetadataField to the Metadata.
func (m *Metadata) Add(name, value string) {
*m = append(*m, &MetadataField{Name: name, Value: value})
}
// MetadataField ...
type MetadataField struct {
Name string `json:"name"`
Value string `json:"value"`
}
// Version communicated with Concourse.
type Version struct {
PR string `json:"pr"`
Commit string `json:"commit"`
CommittedDate time.Time `json:"committed,omitempty"`
ChangedDate time.Time `json:"changed,omitempty"`
ApprovedReviewCount string `json:"approved_review_count"`
State githubv4.PullRequestState `json:"state"`
}
// NewVersion constructs a new Version.
func NewVersion(p *PullRequest, changedDate time.Time) Version {
return Version{
PR: strconv.Itoa(p.Number),
Commit: p.Tip.OID,
ApprovedReviewCount: strconv.Itoa(p.ApprovedReviewCount),
State: p.State,
CommittedDate: p.Tip.CommittedDate.Time,
ChangedDate: changedDate,
}
}
// PullRequest represents a pull request and includes the tip (commit).
type PullRequest struct {
PullRequestObject
Tip CommitObject
ApprovedReviewCount int
Labels []LabelObject
}
// Age: returns a date of the last update to the PR.
// If the job runs every minute, only PRs with an Age() in the last minute will run.
func (p *PullRequest) Age() time.Time {
age := p.Tip.PushedDate
if age == nil {
age = &p.Tip.CommittedDate
}
// handles the case where you're creating a fresh PR:
// there might be a few minutes between the push date and when you open the PR,
// so in that case take the CreatedAt date.
if age.Time.Before(p.CreatedAt.Time) {
age = &p.CreatedAt
}
return age.Time
}
// PullRequestObject represents the GraphQL commit node.
// https://developer.github.com/v4/object/pullrequest/
type PullRequestObject struct {
ID string
Number int
Title string
URL string
BaseRefName string
HeadRefName string
Repository struct {
URL string
}
IsCrossRepository bool
IsDraft bool
State githubv4.PullRequestState
CreatedAt githubv4.DateTime
ClosedAt githubv4.DateTime
MergedAt githubv4.DateTime
}
// UpdatedDate returns the last time a PR was updated, either by commit
// or being closed/merged.
func (p *PullRequest) UpdatedDate() githubv4.DateTime {
date := p.Tip.CommittedDate
switch p.State {
case githubv4.PullRequestStateClosed:
date = p.ClosedAt
case githubv4.PullRequestStateMerged:
date = p.MergedAt
}
return date
}
// CommitObject represents the GraphQL commit node.
// https://developer.github.com/v4/object/commit/
type CommitObject struct {
ID string
OID string
CommittedDate githubv4.DateTime
PushedDate *githubv4.DateTime
Message string
Author struct {
User struct {
Login string
}
Email string
}
Status struct {
Contexts []StatusContext
}
}
type StatusContext struct {
Context string
State string
CreatedAt githubv4.DateTime
}
// ChangedFileObject represents the GraphQL FilesChanged node.
// https://developer.github.com/v4/object/pullrequestchangedfile/
type ChangedFileObject struct {
Path string
}
// LabelObject represents the GraphQL label node.
// https://developer.github.com/v4/object/label
type LabelObject struct {
Name string
}