-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathresources.go
166 lines (135 loc) · 3.68 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
package resource
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/google/go-github/v28/github"
"github.com/peterbourgon/mergemap"
)
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"`
}
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"`
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"`
RawDescription json.RawMessage `json:"description"`
RawAutoMerge json.RawMessage `json:"auto_merge"`
RawPayload json.RawMessage `json:"payload"`
}
// 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.RawDescription != nil {
p.Description = github.String(getStringOrStringFromFile(p.RawDescription))
}
var payload map[string]interface{}
json.Unmarshal(p.RawPayload, &payload)
if p.PayloadPath != nil {
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 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))
}