diff --git a/src/uk/org/floop/jenkins_pmd/Pipelines.groovy b/src/uk/org/floop/jenkins_pmd/Pipelines.groovy index 4c9faa8..52ff03b 100644 --- a/src/uk/org/floop/jenkins_pmd/Pipelines.groovy +++ b/src/uk/org/floop/jenkins_pmd/Pipelines.groovy @@ -3,6 +3,7 @@ import groovy.json.JsonOutput import groovy.json.JsonSlurper import hudson.FilePath +import org.apache.http.HttpEntity import org.apache.http.HttpHost import org.apache.http.HttpResponse import org.apache.http.client.fluent.Executor @@ -36,14 +37,33 @@ "${response.getStatusLine()} : ${EntityUtils.toString(response.getEntity())}" } + private MultipartEntityBuilder createDrafterBody(String draftsetId) { + MultipartEntityBuilder.create() + .addTextBody('__endpoint-type', 'grafter-server.destination/draftset-update') + .addTextBody('__endpoint', JsonOutput.toJson([ + url: "http://localhost:3001/v1/draftset/${draftsetId}/data", + headers: [Authorization: "Basic ${basicAuth}"] + ])) + } + + private def execAndWait(String apiPath, HttpEntity body) { + HttpResponse response = getExec().execute( + Request.Post(apiBase.resolve(apiPath)) + .addHeader("Accept", "application/json") + .userAgent(PMDConfig.UA) + .body(body) + ).returnResponse() + if (response.getStatusLine().statusCode == 202) { + def jobObj = new JsonSlurper().parse(EntityUtils.toByteArray(response.getEntity())) + this.pmd.drafter.waitForJob(apiBase.resolve(jobObj['finished-job'] as String), jobObj['restart-id'] as String) + } else { + throw new PipelineException("Failed pipeline import: ${errorMsg(response)}") + } + } + def dataCube(String draftsetId, String observationsFilename, String datasetName, String datasetPath, String mapping) { String path = "/v1/pipelines/ons-table2qb.core/data-cube/import" - MultipartEntityBuilder body = MultipartEntityBuilder.create() - body.addTextBody('__endpoint-type', 'grafter-server.destination/draftset-update') - body.addTextBody('__endpoint', JsonOutput.toJson([ - url: "http://localhost:3001/v1/draftset/${draftsetId}/data", - headers: [Authorization: "Basic ${basicAuth}"] - ])) + MultipartEntityBuilder body = createDrafterBody(draftsetId) body.addBinaryBody( 'observations-csv', new FilePath(new File(observationsFilename)).read(), @@ -68,18 +88,20 @@ ContentType.create('text/csv', 'UTF-8'), mapping ) - HttpResponse response = getExec().execute( - Request.Post(apiBase.resolve(path)) - .addHeader("Accept", "application/json") - .userAgent(PMDConfig.UA) - .body(body.build()) - ).returnResponse() - if (response.getStatusLine().statusCode == 202) { - def jobObj = new JsonSlurper().parse(EntityUtils.toByteArray(response.getEntity())) - this.pmd.drafter.waitForJob(apiBase.resolve(jobObj['finished-job'] as String), jobObj['restart-id'] as String) - } else { - throw new PipelineException("Failed pipeline import: ${errorMsg(response)}") - } + execAndWait(path, body.build()) + } + + def codelist(String draftsetId, String codelistFilename, String codelistName) { + String path = "/v1/pipelines/ons-table2qb.core/codelist/import" + MultipartEntityBuilder body = createDrafterBody(draftsetId) + body.addBinaryBody( + 'codelist-csv', + new FilePath(new File(codelistFilename)).read(), + ContentType.create('text/csv', 'UTF-8'), + codelistFilename + ) + body.addTextBody('codelist-name', codelistName) + execAndWait(path, body.build()) } } diff --git a/vars/uploadCodelist.groovy b/vars/uploadCodelist.groovy index b4134ee..0488b1b 100644 --- a/vars/uploadCodelist.groovy +++ b/vars/uploadCodelist.groovy @@ -1,19 +1,8 @@ +import uk.org.floop.jenkins_pmd.PMD + def call(String csv, String name) { - configFileProvider([configFile(fileId: 'pmdConfig', variable: 'configfile')]) { - def config = readJSON(text: readFile(file: configfile)) - String PMD = config['pmd_api'] - String credentials = config['credentials'] - String PIPELINE = config['pipeline_api'] - String baseURI = config['base_uri'] - - def draft = jobDraft.find() - - string codelistGraph = "${baseURI}/graph/${util.slugise(name)}" - drafter.deleteGraph(PMD, credentials, draft.id, codelistGraph) - - runPipeline("${PIPELINE}/ons-table2qb.core/codelist/import", - draft.id, credentials, [[name: 'codelist-csv', - file: [name: csv, type: 'text/csv']], - [name: 'codelist-name', value: name]]) - } + PMD pmd = pmdConfig('pmd') + def draft = jobDraft.find() + pmd.drafter.deleteGraph(draft.id as String, "${pmd.config.base_uri}/graph/${util.slugize(name)}") + pmd.pipelines.codelist(draft.id as String, csv, name) }