Skip to content

Commit

Permalink
Add support for git notes in git publisher
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
laudrup committed Oct 12, 2017
1 parent 4d2f35f commit 8857b4d
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class GitPublisherContext extends AbstractContext {
boolean pushMerge
boolean forcePush
List<Node> tags = []
List<Node> notes = []
List<Node> branches = []

GitPublisherContext(JobManagement jobManagement) {
Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,7 @@ class PublisherContext extends AbstractExtensibleContext {
pushOnlyIfSuccess(context.pushOnlyIfSuccess)
forcePush(context.forcePush)
tagsToPush(context.tags)
notesToPush(context.notes)
branchesToPush(context.branches)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -2594,6 +2594,10 @@ class PublisherContextSpec extends Specification {
create()
update()
}
note('origin', 'test') {
namespace('test')
replace()
}
branch('origin', 'master')
}

Expand All @@ -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]) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8857b4d

Please sign in to comment.