/
JenkinsHTTRequester.groovy
executable file
·100 lines (81 loc) · 3.76 KB
/
JenkinsHTTRequester.groovy
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
// This file is part of the hesperides-jenkins-lib distribution.
// (https://github.com/voyages-sncf-technologies/hesperides-jenkins-lib)
// Copyright (c) 2017 VSCT.
//
// hesperides-jenkins-lib is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, version 3.
//
// hesperides-jenkins-lib is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package com.vsct.dt.hesperides.jenkins.pipelines.http
import static groovy.json.JsonOutput.prettyPrint
import static com.vsct.dt.hesperides.jenkins.pipelines.LogUtils.*
import groovy.json.JsonSlurperClassic
import hudson.EnvVars
import org.jenkinsci.plugins.workflow.steps.EnvironmentExpander
class JenkinsHTTRequester implements Serializable {
private static final long serialVersionUID = 1654684321891394621L
Object steps
boolean ansiColorEnabled
JenkinsHTTRequester(steps) {
// cf. https://github.com/jenkinsci/ansicolor-plugin/issues/117 for context about why this:
def env = EnvironmentExpander.getEffectiveEnvironment(new EnvVars(), null, steps.getContext(EnvironmentExpander), null, null)
this.ansiColorEnabled = 'TERM' in env
this.steps = steps
}
def performRequest(Map args) {
// cf. https://jenkins.io/doc/pipeline/steps/http_request/ & https://github.com/jenkinsci/http-request-plugin
def headers = args.authHeader != null ? [[name: 'Authorization', value: args.authHeader]] : []
def response = this.steps.httpRequest url: args.uri.toString(),
httpMode: args.method,
requestBody:args.body,
acceptType: args.accept == 'JSON' ? 'APPLICATION_JSON' : 'NOT_SET',
contentType: 'APPLICATION_JSON', // Beware ! Default is "application/x-www-form-urlencoded", cf. https://issues.jenkins-ci.org/browse/JENKINS-47356
customHeaders: headers,
ignoreSslErrors: true,
validResponseCodes: '100:600'
steps.echo 'Response header Set-Cookie = ' + tryPrettyPrintJSON(response.headers['Set-Cookie'])
['Deprecation', 'Sunset', 'Link'].findAll { header -> response.headers[header] }
.each { header ->
logWarn header + ': ' + response.headers[header]
}
if (!(response.status in [200, 201, 202, 203, 204, 205, 206])) {
logWarn tryPrettyPrintJSON(tryParseJSON(response.content))
throw new HttpException(response.status, 'HTTP error')
}
if (args.accept == 'JSON') {
tryParseJSON response.content
} else {
response.content
}
}
def logWarn(String msg) {
if (this.ansiColorEnabled) {
this.steps.echo COLOR_RED + msg + COLOR_END
} else {
this.steps.echo msg
}
}
def tryParseJSON(String text) {
try {
new JsonSlurperClassic().parseText(text)
} catch (JsonException) {
text
} catch (IllegalArgumentException) {
text
}
}
def tryPrettyPrintJSON(Object obj) {
try {
prettyPrint obj
} catch (JsonException) {
obj.toString()
}
}
}