-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.go
184 lines (132 loc) · 4.2 KB
/
trigger.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
package main
import (
"fmt"
"net/http"
"strings"
)
type triggerAction struct {
userGitCommit string
userOverrides map[string]string
userRuleName string
userUploadToken string
userVerbose bool
ciInfo *ciInfo
rtInfo *rtInfo
validated bool
}
//-----------------------------------------------------------------------------
func newTriggerAction(uploadToken, ruleName, gitCommit string, verbose bool, overrides map[string]string) *triggerAction {
return &triggerAction{
rtInfo: detectRTInfo(),
userGitCommit: gitCommit,
userOverrides: overrides,
userRuleName: ruleName,
userUploadToken: uploadToken,
userVerbose: verbose}
}
//-----------------------------------------------------------------------------
func (ta *triggerAction) gitCommit() string {
return ta.userGitCommit
}
func (ta *triggerAction) ruleName() string {
return ta.userRuleName
}
func (ta *triggerAction) uploadToken() string {
return ta.userUploadToken
}
func (ta *triggerAction) version() string {
return ta.rtInfo.version()
}
//-----------------------------------------------------------------------------
func (ta *triggerAction) perform() error {
return ta.triggerRunWithRetry()
}
func (ta *triggerAction) validate() error {
if ta.validated {
return nil
}
ta.ciInfo = detectCIInfo(false)
ta.validated = true
return nil
}
//-----------------------------------------------------------------------------
func (ta *triggerAction) authorization() string {
return fmt.Sprintf("Upload-Token %s", ta.userUploadToken)
}
func (ta *triggerAction) checkTriggerStatus(resp *http.Response) error {
status := resp.StatusCode
if status == 401 {
return fmt.Errorf("Upload token is invalid or missing!")
}
if status < 200 || status > 299 {
return fmt.Errorf("Unable to trigger run on Waldo, HTTP status: %d", status)
}
return nil
}
func (ta *triggerAction) contentType() string {
return jsonContentType
}
func (ta *triggerAction) makePayload() string {
payload := ""
appendIfNotEmpty(&payload, "agentName", agentName)
appendIfNotEmpty(&payload, "agentVersion", agentVersion)
appendIfNotEmpty(&payload, "arch", ta.rtInfo.arch)
appendIfNotEmpty(&payload, "ci", ta.ciInfo.provider.string())
appendIfNotEmpty(&payload, "gitSha", ta.userGitCommit)
appendIfNotEmpty(&payload, "platform", ta.rtInfo.platform)
appendIfNotEmpty(&payload, "ruleName", ta.userRuleName)
appendIfNotEmpty(&payload, "wrapperName", ta.userOverrides["wrapperName"])
appendIfNotEmpty(&payload, "wrapperVersion", ta.userOverrides["wrapperVersion"])
payload = "{" + payload + "}"
return payload
}
func (ta *triggerAction) makeURL() string {
triggerURL := ta.userOverrides["apiTriggerEndpoint"]
if len(triggerURL) == 0 {
triggerURL = defaultAPITriggerEndpoint
}
return triggerURL
}
func (ta *triggerAction) triggerRun(retryAllowed bool) (bool, error) {
fmt.Printf("Triggering run on Waldo…\n")
url := ta.makeURL()
body := ta.makePayload()
client := &http.Client{}
req, err := http.NewRequest("POST", url, strings.NewReader(body))
if err != nil {
return false, fmt.Errorf("Unable to trigger run on Waldo, error: %v, url: %q", err, url)
}
req.Header.Add("Authorization", ta.authorization())
req.Header.Add("Content-Type", ta.contentType())
req.Header.Add("User-Agent", ta.userAgent())
dumpRequest(ta.userVerbose, req, true)
resp, err := client.Do(req)
if err != nil {
return retryAllowed, fmt.Errorf("Unable to trigger run on Waldo, error: %v, url: %q", err, url)
}
dumpResponse(ta.userVerbose, resp, true)
defer resp.Body.Close()
return retryAllowed && shouldRetry(resp), ta.checkTriggerStatus(resp)
}
func (ta *triggerAction) triggerRunWithRetry() error {
for attempts := 1; attempts <= maxNetworkAttempts; attempts++ {
retry, err := ta.triggerRun(attempts < maxNetworkAttempts)
if !retry || err == nil {
return err
}
emitError(err)
fmt.Printf("\nFailed trigger attempts: %d -- retrying…\n\n", attempts)
}
return nil
}
func (ta *triggerAction) userAgent() string {
ci := ta.ciInfo.provider.string()
if ci == "Unknown" {
ci = "Go Agent"
}
version := ta.userOverrides["wrapperVersion"]
if len(version) == 0 {
version = agentVersion
}
return fmt.Sprintf("Waldo %s v%s", ci, version)
}