diff --git a/vars/drafter.groovy b/vars/drafter.groovy new file mode 100644 index 0000000..bafa751 --- /dev/null +++ b/vars/drafter.groovy @@ -0,0 +1,95 @@ +def listDraftsets(baseUrl, credentials, include) { + def response = httpRequest(acceptType: 'APPLICATION_JSON', + authentication: credentials, + httpMode: 'GET', + url: "${baseUrl}/v1/draftsets?include=${include}") + if (response.status == 200) { + return readJSON(text: response.content) + } else { + error "Problem listing draftsets ${response.status} : ${response.content}" + } +} + +def deleteDraftset(baseUrl, credentials, id) { + def response = httpRequest(acceptType: 'APPLICATION_JSON', + authentication: credentials, + httpMode: 'DELETE', + url: "${baseUrl}/v1/draftset/${jobDraft.id}") + if (response.status == 202) { + def job = readJSON(text: response.content) + drafter.waitForJob( + "${baseUrl}${job['finished-job']}", + credentials, job['restart-id']) + } else { + error "Problem deleting draftset ${response.status} : ${response.content}" + } +} + +def createDraftset(baseUrl, credentials, label) { + String displayName = java.net.URLEncoder.encode(label, "UTF-8") + def response = httpRequest(acceptType: 'APPLICATION_JSON', + authentication: credentials, + httpMode: 'POST', + url: "${baseUrl}/v1/draftsets?display-name=${displayName}") + if (response.status == 200) { + return readJSON(text: response.content) + } else { + error "Problem creating draftset ${response.status} : ${response.content}" + } +} + +def addData(baseUrl, credentials, id, data, type) { + def response = httpRequest(acceptType: 'APPLICATION_JSON', + authentication: credentials, + httpMode: 'PUT', + url: "${baseUrl}/v1/draftset/${id}/data", + requestBody: data, + customHeaders: [[name: 'Content-Type', + value: type]]) + if (response.status == 202) { + def job = readJSON(text: response.content) + drafter.waitForJob( + "${baseUrl}${job['finished-job']}", + credentials, job['restart-id']) + } else { + error "Problem adding data ${response.status} : ${response.content}" + } +} + +def publishDraftset(baseUrl, credentials, id) { + def response = httpRequest(acceptType: 'APPLICATION_JSON', + authentication: credentials, + httpMode: 'POST', + url: "${baseUrl}/v1/draftset/${id}/publish") + if (response.status == 202) { + def job = readJSON(text: response.content) + drafter.waitForJob( + "${baseUrl}${job['finished-job']}", + credentials, job['restart-id']) + } else { + error "Problem publishing draftset ${response.status} : ${response.content}" + } +} + +def waitForJob(pollUrl, credentials, restartId) { + while (true) { + jobResponse = httpRequest(acceptType: 'APPLICATION_JSON', authentication: credentials, + httpMode: 'GET', url: pollUrl, validResponseCodes: '200:404') + if (jobResponse.status == 404) { + if (readJSON(text: jobResponse.content)['restart-id'] != restartId) { + error "Failed waiting for job to finish, restart-id different." + } else { + sleep 10 + } + } else if (jobResponse.status == 200) { + def jobResponseObj = readJSON(text: jobResponse.content) + if (jobResponseObj['restart-id'] != restartId) { + error "Failed waiting for job to finish, restart-id different." + } else if (jobResponseObj.type == 'ok') { + return + } else if (jobResponseObj.type == "error") { + error "Pipeline error in ${jobResponseObj.details?.pipeline?.name}. ${jobResponseObj.message}" + } + } + } +} diff --git a/vars/runPipeline.groovy b/vars/runPipeline.groovy new file mode 100644 index 0000000..33ef699 --- /dev/null +++ b/vars/runPipeline.groovy @@ -0,0 +1,33 @@ +def call(pipelineUrl, draftsetId, credentials, params) { + withCredentials([usernameColonPassword(credentialsId: credentials, variable: 'USERPASS')]) { + String boundary = UUID.randomUUID().toString() + def allParams = [ + [name: '__endpoint-type', value: 'grafter-server.destination/draftset-update'], + [name: '__endpoint', value: groovy.json.JsonOutput.toJson([ + url: "http://localhost:3001/v1/draftset/${draftsetId}/data", + headers: [Authorization: "Basic ${USERPASS.bytes.encodeBase64()}"] + ])]] + params + String body = "" + allParams.each { param -> + body += "--${boundary}\r\n" + body += 'Content-Disposition: form-data; name="' + param.name + '"' + if (param.containsKey('file')) { + body += '; filename="' + param.file.name + '"\r\nContent-Type: "' + param.file.type + '\r\n\r\n' + body += readFile(param.file.name) + '\r\n' + } else { + body += "\r\n\r\n${param.value}\r\n" + } + } + body += "--${boundary}--\r\n" + def importRequest = httpRequest(acceptType: 'APPLICATION_JSON', authentication: credentials, + httpMode: 'POST', url: pipelineUrl, requestBody: body, + customHeaders: [[name: 'Content-Type', value: 'multipart/form-data;boundary="' + boundary + '"']]) + if (importRequest.status == 202) { + def importJob = readJSON(text: importRequest.content) + String jobUrl = new java.net.URI(pipelineUrl).resolve(importJob['finished-job']) as String + drafter.waitForJob(jobUrl, credentials, importJob['restart-id']) + } else { + error "Failed import, ${importRequest.status} : ${importRequest.content}" + } + } +}