1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Merge branch 'master' into whitesource-step

This commit is contained in:
Sven Merk 2019-03-28 16:59:14 +01:00 committed by GitHub
commit 452158256c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 55 deletions

View File

@ -1,7 +1,8 @@
package com.sap.piper.analytics
import com.cloudbees.groovy.cps.NonCPS
import org.jenkinsci.plugins.workflow.steps.MissingContextVariableException
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
class Telemetry implements Serializable{
@ -45,53 +46,39 @@ class Telemetry implements Serializable{
}
protected static void piperOsDefaultReporting(Script steps, Map payload) {
def swaEndpoint = 'https://webanalytics.cfapps.eu10.hana.ondemand.com/tracker/log'
Map swaPayload = [
'idsite': '827e8025-1e21-ae84-c3a3-3f62b70b0130',
'url': 'https://github.com/SAP/jenkins-library',
'action_name': payload.actionName,
'event_type': payload.eventType,
'custom3': payload.step, // custom3 = step name (passed as parameter step)
'custom4': payload.jobUrlSha1, // custom4 = job url hashed (calculated)
'custom5': payload.buildUrlSha1, // custom5 = build url hashed (calculated)
'custom10': payload.stageName // custom10 = stage name
]
// step related parameters
for(def key : [1, 2, 3, 4, 5]){ // custom11 - custom15 = step related parameter 1 - 5 (passed as parameter stepParam1 - stepParam5)
if (payload["stepParam${key}"] != null) swaPayload.put("custom1${key}", payload["stepParam${key}"])
}
try {
def swaCustom = [:]
/* SWA custom parameters:
custom3 = step name (passed as parameter step)
custom4 = job url hashed (calculated)
custom5 = build url hashed (calculated)
custom10 = stage name
custom11 = step related parameter 1 (passed as parameter stepParam1)
custom12 = step related parameter 2 (passed as parameter stepParam2)
custom13 = step related parameter 3 (passed as parameter stepParam3)
custom14 = step related parameter 4 (passed as parameter stepParam4)
custom15 = step related parameter 5 (passed as parameter stepParam5)
*/
def swaUrl = 'https://webanalytics.cfapps.eu10.hana.ondemand.com/tracker/log'
def idsite = '827e8025-1e21-ae84-c3a3-3f62b70b0130'
def url = 'https://github.com/SAP/jenkins-library'
swaCustom.custom3 = payload.step
swaCustom.custom4 = payload.jobUrlSha1
swaCustom.custom5 = payload.buildUrlSha1
swaCustom.custom10 = payload.stageName
swaCustom.custom11 = payload.stepParam1
swaCustom.custom12 = payload.stepParam2
swaCustom.custom13 = payload.stepParam3
swaCustom.custom14 = payload.stepParam4
swaCustom.custom15 = payload.stepParam5
def options = []
options.push("-G")
options.push("-v \"${swaUrl}\"")
options.push("--data-urlencode \"action_name=${payload.actionName}\"")
options.push("--data-urlencode \"idsite=${idsite}\"")
options.push("--data-urlencode \"url=${url}\"")
options.push("--data-urlencode \"event_type=${payload.eventType}\"")
for(def key : ['custom3', 'custom4', 'custom5', 'custom10', 'custom11', 'custom12', 'custom13', 'custom14', 'custom15']){
if (swaCustom[key] != null) options.push("--data-urlencode \"${key}=${swaCustom[key]}\"")
steps.timeout(
time: 10,
unit: 'SECONDS'
){
steps.httpRequest(url: "${swaEndpoint}?${getPayloadString(swaPayload)}", timeout: 5, quiet: true)
}
options.push("--connect-timeout 5")
options.push("--max-time 20")
steps.sh(returnStatus: true, script: "#!/bin/sh +x\ncurl ${options.join(' ')} > /dev/null 2>&1 || echo '[${payload.step}] Telemetry Report to SWA failed!'")
} catch (MissingContextVariableException noNode) {
steps.echo "[${payload.step}] Telemetry Report to SWA skipped, no node available!"
} catch (FlowInterruptedException ignore){
// telemetry reporting timed out. This should not break anything though.
steps.echo "[${payload.step}] Telemetry Report with listener failed: timeout"
}
}
@NonCPS
private static String getPayloadString(Map payload){
return payload
.collect { entry -> return "${entry.key}=${URLEncoder.encode(entry.value.toString(), "UTF-8")}" }
.join('&')
}
}

View File

@ -8,11 +8,12 @@ import static org.junit.Assume.assumeThat
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import static org.hamcrest.Matchers.allOf
import static org.hamcrest.Matchers.containsString
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.empty
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.not
import static org.hamcrest.Matchers.empty
import static org.hamcrest.Matchers.startsWith
import util.JenkinsLoggingRule
import util.JenkinsShellCallRule
@ -122,6 +123,13 @@ class TelemetryTest extends BasePiperTest {
@Test
void testReportingToSWA() {
def httpParams = null
helper.registerAllowedMethod('httpRequest', [Map.class], {m ->
httpParams = m
})
helper.registerAllowedMethod("timeout", [Map.class, Closure.class], { m,c ->
c()
})
// prepare
assumeThat(Telemetry.getInstance().listenerList, is(not(empty())))
// test
@ -134,12 +142,15 @@ class TelemetryTest extends BasePiperTest {
stepParam1: 'something'
])
// asserts
assertThat(jscr.shell, hasItem(containsString('curl -G -v "https://webanalytics.cfapps.eu10.hana.ondemand.com/tracker/log"')))
assertThat(jscr.shell, hasItem(containsString('--data-urlencode "action_name=Piper Library OS"')))
assertThat(jscr.shell, hasItem(containsString('--data-urlencode "event_type=library-os"')))
assertThat(jscr.shell, hasItem(containsString('--data-urlencode "custom3=anyStep"')))
assertThat(jscr.shell, hasItem(containsString('--data-urlencode "custom4=1234"')))
assertThat(jscr.shell, hasItem(containsString('--data-urlencode "custom5=abcd"')))
assertThat(jscr.shell, hasItem(containsString('--data-urlencode "custom11=something"')))
assertThat(httpParams, is(not(null)))
assertThat(httpParams.url.toString(), allOf(
startsWith('https://webanalytics.cfapps.eu10.hana.ondemand.com/tracker/log?'),
containsString('action_name=Piper+Library+OS'),
containsString('event_type=library-os'),
containsString('custom3=anyStep'),
containsString('custom4=1234'),
containsString('custom5=abcd'),
containsString('custom11=something')
))
}
}