diff --git a/src/uk/org/floop/jenkins_pmd/Drafter.groovy b/src/uk/org/floop/jenkins_pmd/Drafter.groovy index f6f96dc..fb99d11 100644 --- a/src/uk/org/floop/jenkins_pmd/Drafter.groovy +++ b/src/uk/org/floop/jenkins_pmd/Drafter.groovy @@ -96,7 +96,7 @@ def jobObj = new JsonSlurper().parse(EntityUtils.toByteArray(response.getEntity())) waitForJob(apiBase.resolve(jobObj['finished-job'] as String), jobObj['restart-id'] as String) } else { - throw new DrafterException("Problem deleting draftset ${jobObj['message']}") + throw new DrafterException("Problem deleting draftset ${errorMsg(response)}") } } @@ -166,6 +166,37 @@ } + def publishDraftset(String id) { + String path = "/v1/draftset/${id}/publish" + Executor exec = getExec() + HttpResponse response = exec.execute( + Request.Post(apiBase.resolve(path)) + .addHeader("Accept", "application/json") + .userAgent(PMDConfig.UA) + ).returnResponse() + if (response.getStatusLine().statusCode == 202) { + def jobObj = new JsonSlurper().parse(EntityUtils.toByteArray(response.getEntity())) + waitForJob(apiBase.resolve(jobObj['finished-job'] as String), jobObj['restart-id'] as String) + if (pmd.config.empty_cache) { + exec.execute( + Request.Put(pmd.config.empty_cache) + .addHeader("Accept", "application/json") + .userAgent(PMDConfig.UA)) + } + if (pmd.config.sync_search) { + exec.execute( + Request.Put(pmd.config.sync_search) + .addHeader("Accept", "application/json") + .userAgent(PMDConfig.UA)) + } + + } else { + throw new DrafterException("Problem publishing draftset ${errorMsg(response)}") + } + + + } + } class DrafterException extends Throwable { diff --git a/src/uk/org/floop/jenkins_pmd/PMDConfig.groovy b/src/uk/org/floop/jenkins_pmd/PMDConfig.groovy index f49f5c7..01c1911 100644 --- a/src/uk/org/floop/jenkins_pmd/PMDConfig.groovy +++ b/src/uk/org/floop/jenkins_pmd/PMDConfig.groovy @@ -7,5 +7,7 @@ String pipeline_api String default_mapping String base_uri + String empty_cache + String sync_search } diff --git a/test/integration/groovy/uk/org/floop/jenkins_pmd/DrafterTests.groovy b/test/integration/groovy/uk/org/floop/jenkins_pmd/DrafterTests.groovy index b643b57..911f6b4 100644 --- a/test/integration/groovy/uk/org/floop/jenkins_pmd/DrafterTests.groovy +++ b/test/integration/groovy/uk/org/floop/jenkins_pmd/DrafterTests.groovy @@ -200,4 +200,40 @@ } + @Test + void "publish draftset"() { + stubFor(post(urlMatching("/v1/draftset/.*/publish")) + .withHeader("Accept", equalTo("application/json")) + .withBasicAuth("admin", "admin") + .willReturn(aResponse() + .withStatus(202) + .withBodyFile("publicationJob.json") + .withHeader("Content-Type", "application/json"))) + stubFor(post("/v1/draftsets?display-name=project") + .withHeader("Accept", equalTo("application/json")) + .withBasicAuth("admin", "admin") + .willReturn(seeOther("/v1/draftset/4e376c57-6816-404a-8945-94849299f2a0"))) + stubFor(get(urlMatching("/v1/draftsets.*")) + .withHeader("Accept", equalTo("application/json")) + .withBasicAuth("admin", "admin") + .willReturn(ok() + .withBodyFile("listDraftsets.json") + .withHeader("Content-Type", "application/json"))) + stubFor(get('/v1/status/finished-jobs/2c4111e5-a299-4526-8327-bad5996de400') + .withHeader('Accept', equalTo('application/json')) + .withBasicAuth('admin', 'admin') + .willReturn(ok().withBodyFile('finishedPublicationJobOk.json'))) + final CpsFlowDefinition flow = new CpsFlowDefinition(''' + node { + jobDraft.publish() + }'''.stripIndent(), true) + final WorkflowJob workflowJob = rule.createProject(WorkflowJob, 'project') + workflowJob.definition = flow + + final WorkflowRun firstResult = rule.buildAndAssertSuccess(workflowJob) + verify(postRequestedFor(urlEqualTo("/v1/draftset/4e376c57-6816-404a-8945-94849299f2a0/publish"))) + rule.assertLogContains('Publishing job draft', firstResult) + } + + } diff --git a/test/resources/__files/finishedPublicationJobOk.json b/test/resources/__files/finishedPublicationJobOk.json new file mode 100644 index 0000000..cf3033a --- /dev/null +++ b/test/resources/__files/finishedPublicationJobOk.json @@ -0,0 +1,4 @@ +{ + "type": "ok", + "restart-id": "b1b26596-2dca-4e52-883c-7fdcb8b4be97" +} \ No newline at end of file diff --git a/test/resources/__files/publicationJob.json b/test/resources/__files/publicationJob.json new file mode 100644 index 0000000..b1e0a53 --- /dev/null +++ b/test/resources/__files/publicationJob.json @@ -0,0 +1,5 @@ +{ + "type": "ok", + "restart-id": "b1b26596-2dca-4e52-883c-7fdcb8b4be97", + "finished-job": "/v1/status/finished-jobs/2c4111e5-a299-4526-8327-bad5996de400" +} \ No newline at end of file