forked from KevinSnyderCodes/github-deployment-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.go
203 lines (167 loc) · 4.84 KB
/
resources.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
package resource
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/peterbourgon/mergemap"
"github.com/shipt/go-github/v32/github"
)
type Source struct {
User string `json:"user"`
Repository string `json:"repository"`
AccessToken string `json:"access_token"`
GitHubAPIURL string `json:"github_api_url"`
Environments []string `json:"environments"`
}
type Version struct {
ID string `json:"id"`
Statuses string `json:"status"`
ETag string `json:"etag"`
}
type CheckRequest struct {
Source Source `json:"source"`
Version Version `json:"version"`
}
type InRequest struct {
Source Source `json:"source"`
Version Version `json:"version"`
}
type OutRequest struct {
Source Source `json:"source"`
Params OutParams `json:"params"`
}
type InResponse struct {
Version Version `json:"version"`
Metadata []MetadataPair `json:"metadata"`
}
type OutResponse struct {
Version Version `json:"version"`
Metadata []MetadataPair `json:"metadata"`
}
type OutParams struct {
Type *string `json:"type"`
ID *string
Ref *string
Environment *string
Task *string
State *string
Description *string
AutoMerge *bool
Payload *map[string]interface{}
PayloadPath *string `json:"payload_path"`
LogURL *string
EnvironmentURL *string
RawID json.RawMessage `json:"id"`
RawState json.RawMessage `json:"state"`
RawRef json.RawMessage `json:"ref"`
RawTask json.RawMessage `json:"task"`
RawEnvironment json.RawMessage `json:"environment"`
RawEnvironmentURL json.RawMessage `json:"environment_url"`
RawDescription json.RawMessage `json:"description"`
RawAutoMerge json.RawMessage `json:"auto_merge"`
RawPayload json.RawMessage `json:"payload"`
RawLogURL json.RawMessage `json:"log_url"`
}
// Used to avoid recursion in UnmarshalJSON below.
type outParams OutParams
func (p *OutParams) UnmarshalJSON(b []byte) (err error) {
j := outParams{
Type: github.String("status"),
}
if err = json.Unmarshal(b, &j); err == nil {
*p = OutParams(j)
if p.RawID != nil {
p.ID = github.String(getStringOrStringFromFile(p.RawID))
}
if p.RawState != nil {
p.State = github.String(getStringOrStringFromFile(p.RawState))
}
if p.RawRef != nil {
p.Ref = github.String(getStringOrStringFromFile(p.RawRef))
}
if p.RawTask != nil {
p.Task = github.String(getStringOrStringFromFile(p.RawTask))
}
if p.RawEnvironment != nil {
p.Environment = github.String(getStringOrStringFromFile(p.RawEnvironment))
}
if p.RawEnvironmentURL != nil {
envUrl := os.ExpandEnv(getStringOrStringFromFile(p.RawEnvironmentURL)) // Interpolate ENV variables
p.EnvironmentURL = github.String(envUrl)
}
if p.RawDescription != nil {
p.Description = github.String(getStringOrStringFromFile(p.RawDescription))
}
if p.RawLogURL != nil {
logURL := os.ExpandEnv(getStringOrStringFromFile(p.RawLogURL)) // Interpolate ENV variables
p.LogURL = github.String(logURL)
}
if p.RawAutoMerge != nil {
p.AutoMerge = github.Bool(getBoolOrDefault(p.RawAutoMerge, false))
}
var payload map[string]interface{}
json.Unmarshal(p.RawPayload, &payload)
if p.PayloadPath != nil && *p.PayloadPath != "" {
stringFromFile := fileContents(*p.PayloadPath)
var payloadFromFile map[string]interface{}
json.Unmarshal([]byte(stringFromFile), &payloadFromFile)
payload = mergemap.Merge(payloadFromFile, payload)
}
p.Payload = &payload
return
}
return
}
type MetadataPair struct {
Name string `json:"name"`
Value string `json:"value"`
}
func NewCheckRequest() CheckRequest {
return CheckRequest{}
}
func NewInRequest() InRequest {
return InRequest{}
}
func NewOutRequest() OutRequest {
return OutRequest{}
}
func getStringOrStringFromFile(field json.RawMessage) string {
var rawValue interface{}
if err := json.Unmarshal(field, &rawValue); err == nil {
switch rawValue := rawValue.(type) {
case string:
return rawValue
case map[string]interface{}:
return fileContents(rawValue["file"].(string))
default:
panic("Could not read string out of Params field")
}
}
return ""
}
func getBoolOrDefault(field json.RawMessage, defaultValue bool) bool {
var rawValue interface{}
err := json.Unmarshal(field, &rawValue)
if err != nil {
log.Printf("Error unmarshalling json: %s.\nDefaulting to given default value %t", err, defaultValue)
return defaultValue
}
switch rawValue := rawValue.(type) {
case bool:
return rawValue
default:
log.Printf("Could not parse bool from json; defaulting to false")
return defaultValue
}
}
func fileContents(path string) string {
sourceDir := os.Args[1]
contents, err := ioutil.ReadFile(filepath.Join(sourceDir, path))
if err != nil {
panic(err)
}
return strings.TrimSpace(string(contents))
}