From 8857b4d72d3386cd391ccbc81e9897aaa8700564 Mon Sep 17 00:00:00 2001 From: Kasper Laudrup Date: Thu, 12 Oct 2017 19:30:19 +0200 Subject: [PATCH] Add support for git notes in git publisher Added support for publishing git notes in the post-build gitpublisher. This is more or less copy-paste from the implementation of the git tag implementation. --- .../publisher/GitPublisherContext.groovy | 19 ++++ .../publisher/NoteToPushContext.groovy | 30 +++++ .../helpers/publisher/PublisherContext.groovy | 1 + .../publisher/PublisherContextSpec.groovy | 104 +++++++++++++++++- 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/NoteToPushContext.groovy diff --git a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/GitPublisherContext.groovy b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/GitPublisherContext.groovy index 66bbe119d..bc64a3e83 100644 --- a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/GitPublisherContext.groovy +++ b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/GitPublisherContext.groovy @@ -12,6 +12,7 @@ class GitPublisherContext extends AbstractContext { boolean pushMerge boolean forcePush List tags = [] + List notes = [] List branches = [] GitPublisherContext(JobManagement jobManagement) { @@ -60,6 +61,24 @@ class GitPublisherContext extends AbstractContext { } } + /** + * Adds a note to push to a remote repository. Can be called multiple times to push more notes. + */ + void note(String targetRepo, String message, @DslContext(NoteToPushContext) Closure closure = null) { + checkNotNullOrEmpty(targetRepo, 'targetRepo must be specified') + checkNotNullOrEmpty(message, 'message must be specified') + + NoteToPushContext context = new NoteToPushContext() + ContextHelper.executeInContext(closure, context) + + notes << NodeBuilder.newInstance().'hudson.plugins.git.GitPublisher_-NoteToPush' { + targetRepoName(targetRepo) + noteMsg(message) + noteNamespace(context.namespace ?: 'master') + noteReplace(context.replace) + } + } + /** * Adds a branch to push to a remote repository. Can be called multiple times to push more branches. */ diff --git a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/NoteToPushContext.groovy b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/NoteToPushContext.groovy new file mode 100644 index 000000000..49f9c8a90 --- /dev/null +++ b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/NoteToPushContext.groovy @@ -0,0 +1,30 @@ +package javaposse.jobdsl.dsl.helpers.publisher + +import javaposse.jobdsl.dsl.Context + +class NoteToPushContext implements Context { + String message + String namespace + boolean replace + + /** + * Sets the content of the note. + */ + void message(String message) { + this.message = message + } + + /** + * If set, sets the namespace of the note. + */ + void namespace(String namespace) { + this.namespace = namespace + } + + /** + * If set, replaces an existing note. Defaults to {@code false}. + */ + void replace(boolean replace = true) { + this.replace = replace + } +} diff --git a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContext.groovy b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContext.groovy index 27e6fe1e0..cae53b7ed 100644 --- a/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContext.groovy +++ b/job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContext.groovy @@ -945,6 +945,7 @@ class PublisherContext extends AbstractExtensibleContext { pushOnlyIfSuccess(context.pushOnlyIfSuccess) forcePush(context.forcePush) tagsToPush(context.tags) + notesToPush(context.notes) branchesToPush(context.branches) } } diff --git a/job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContextSpec.groovy b/job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContextSpec.groovy index f025658f4..ab77ecaa9 100644 --- a/job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContextSpec.groovy +++ b/job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContextSpec.groovy @@ -2575,7 +2575,7 @@ class PublisherContextSpec extends Specification { then: context.publisherNodes.size() == 1 context.publisherNodes[0].name() == 'hudson.plugins.git.GitPublisher' - context.publisherNodes[0].children().size() == 6 + context.publisherNodes[0].children().size() == 7 context.publisherNodes[0].configVersion[0].value() == 2 context.publisherNodes[0].pushMerge[0].value() == false context.publisherNodes[0].pushOnlyIfSuccess[0].value() == false @@ -2594,6 +2594,10 @@ class PublisherContextSpec extends Specification { create() update() } + note('origin', 'test') { + namespace('test') + replace() + } branch('origin', 'master') } @@ -2614,6 +2618,14 @@ class PublisherContextSpec extends Specification { createTag[0].value() == true updateTag[0].value() == true } + notesToPush.size() == 1 + notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'.size() == 1 + with(notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'[0]) { + targetRepoName[0].value() == 'origin' + noteNamespace[0].value() == 'test' + noteMsg[0].value() == 'test' + noteReplace[0].value() == true + } branchesToPush.size() == 1 branchesToPush[0].'hudson.plugins.git.GitPublisher_-BranchToPush'.size() == 1 with(branchesToPush[0].'hudson.plugins.git.GitPublisher_-BranchToPush'[0]) { @@ -2687,6 +2699,96 @@ class PublisherContextSpec extends Specification { thrown(DslScriptException) } + def 'call git with minimal note options'() { + when: + context.git { + note('origin', 'test') + } + + then: + context.publisherNodes.size() == 1 + with(context.publisherNodes[0]) { + name() == 'hudson.plugins.git.GitPublisher' + configVersion[0].value() == 2 + pushMerge[0].value() == false + pushOnlyIfSuccess[0].value() == false + forcePush[0].value() == false + notesToPush.size() == 1 + notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'.size() == 1 + with(notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'[0]) { + targetRepoName[0].value() == 'origin' + noteMsg[0].value() == 'test' + noteNamespace[0].value() == 'master' + noteReplace[0].value() == false + } + } + 1 * jobManagement.requireMinimumPluginVersion('git', '2.5.3') + } + + def 'call git with note replace'() { + when: + context.git { + note('origin', 'test') { + replace() + } + } + + then: + context.publisherNodes.size() == 1 + with(context.publisherNodes[0]) { + name() == 'hudson.plugins.git.GitPublisher' + configVersion[0].value() == 2 + pushMerge[0].value() == false + pushOnlyIfSuccess[0].value() == false + forcePush[0].value() == false + notesToPush.size() == 1 + notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'.size() == 1 + with(notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'[0]) { + targetRepoName[0].value() == 'origin' + noteMsg[0].value() == 'test' + noteNamespace[0].value() == 'master' + noteReplace[0].value() == true + } + } + 1 * jobManagement.requireMinimumPluginVersion('git', '2.5.3') + } + + def 'call git without note targetRepoName'() { + when: + context.git { + note(null, 'test') + } + + then: + thrown(DslScriptException) + + when: + context.git { + note('', 'test') + } + + then: + thrown(DslScriptException) + } + + def 'call git without note message'() { + when: + context.git { + note('origin', null) + } + + then: + thrown(DslScriptException) + + when: + context.git { + note('origin', '') + } + + then: + thrown(DslScriptException) + } + def 'call git without branch targetRepoName'() { when: context.git {