Skip to content

Commit 8857b4d

Browse files
committed
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.
1 parent 4d2f35f commit 8857b4d

File tree

4 files changed

+153
-1
lines changed

4 files changed

+153
-1
lines changed

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/GitPublisherContext.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class GitPublisherContext extends AbstractContext {
1212
boolean pushMerge
1313
boolean forcePush
1414
List<Node> tags = []
15+
List<Node> notes = []
1516
List<Node> branches = []
1617

1718
GitPublisherContext(JobManagement jobManagement) {
@@ -60,6 +61,24 @@ class GitPublisherContext extends AbstractContext {
6061
}
6162
}
6263

64+
/**
65+
* Adds a note to push to a remote repository. Can be called multiple times to push more notes.
66+
*/
67+
void note(String targetRepo, String message, @DslContext(NoteToPushContext) Closure closure = null) {
68+
checkNotNullOrEmpty(targetRepo, 'targetRepo must be specified')
69+
checkNotNullOrEmpty(message, 'message must be specified')
70+
71+
NoteToPushContext context = new NoteToPushContext()
72+
ContextHelper.executeInContext(closure, context)
73+
74+
notes << NodeBuilder.newInstance().'hudson.plugins.git.GitPublisher_-NoteToPush' {
75+
targetRepoName(targetRepo)
76+
noteMsg(message)
77+
noteNamespace(context.namespace ?: 'master')
78+
noteReplace(context.replace)
79+
}
80+
}
81+
6382
/**
6483
* Adds a branch to push to a remote repository. Can be called multiple times to push more branches.
6584
*/
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package javaposse.jobdsl.dsl.helpers.publisher
2+
3+
import javaposse.jobdsl.dsl.Context
4+
5+
class NoteToPushContext implements Context {
6+
String message
7+
String namespace
8+
boolean replace
9+
10+
/**
11+
* Sets the content of the note.
12+
*/
13+
void message(String message) {
14+
this.message = message
15+
}
16+
17+
/**
18+
* If set, sets the namespace of the note.
19+
*/
20+
void namespace(String namespace) {
21+
this.namespace = namespace
22+
}
23+
24+
/**
25+
* If set, replaces an existing note. Defaults to {@code false}.
26+
*/
27+
void replace(boolean replace = true) {
28+
this.replace = replace
29+
}
30+
}

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContext.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ class PublisherContext extends AbstractExtensibleContext {
945945
pushOnlyIfSuccess(context.pushOnlyIfSuccess)
946946
forcePush(context.forcePush)
947947
tagsToPush(context.tags)
948+
notesToPush(context.notes)
948949
branchesToPush(context.branches)
949950
}
950951
}

job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/helpers/publisher/PublisherContextSpec.groovy

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2575,7 +2575,7 @@ class PublisherContextSpec extends Specification {
25752575
then:
25762576
context.publisherNodes.size() == 1
25772577
context.publisherNodes[0].name() == 'hudson.plugins.git.GitPublisher'
2578-
context.publisherNodes[0].children().size() == 6
2578+
context.publisherNodes[0].children().size() == 7
25792579
context.publisherNodes[0].configVersion[0].value() == 2
25802580
context.publisherNodes[0].pushMerge[0].value() == false
25812581
context.publisherNodes[0].pushOnlyIfSuccess[0].value() == false
@@ -2594,6 +2594,10 @@ class PublisherContextSpec extends Specification {
25942594
create()
25952595
update()
25962596
}
2597+
note('origin', 'test') {
2598+
namespace('test')
2599+
replace()
2600+
}
25972601
branch('origin', 'master')
25982602
}
25992603

@@ -2614,6 +2618,14 @@ class PublisherContextSpec extends Specification {
26142618
createTag[0].value() == true
26152619
updateTag[0].value() == true
26162620
}
2621+
notesToPush.size() == 1
2622+
notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'.size() == 1
2623+
with(notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'[0]) {
2624+
targetRepoName[0].value() == 'origin'
2625+
noteNamespace[0].value() == 'test'
2626+
noteMsg[0].value() == 'test'
2627+
noteReplace[0].value() == true
2628+
}
26172629
branchesToPush.size() == 1
26182630
branchesToPush[0].'hudson.plugins.git.GitPublisher_-BranchToPush'.size() == 1
26192631
with(branchesToPush[0].'hudson.plugins.git.GitPublisher_-BranchToPush'[0]) {
@@ -2687,6 +2699,96 @@ class PublisherContextSpec extends Specification {
26872699
thrown(DslScriptException)
26882700
}
26892701

2702+
def 'call git with minimal note options'() {
2703+
when:
2704+
context.git {
2705+
note('origin', 'test')
2706+
}
2707+
2708+
then:
2709+
context.publisherNodes.size() == 1
2710+
with(context.publisherNodes[0]) {
2711+
name() == 'hudson.plugins.git.GitPublisher'
2712+
configVersion[0].value() == 2
2713+
pushMerge[0].value() == false
2714+
pushOnlyIfSuccess[0].value() == false
2715+
forcePush[0].value() == false
2716+
notesToPush.size() == 1
2717+
notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'.size() == 1
2718+
with(notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'[0]) {
2719+
targetRepoName[0].value() == 'origin'
2720+
noteMsg[0].value() == 'test'
2721+
noteNamespace[0].value() == 'master'
2722+
noteReplace[0].value() == false
2723+
}
2724+
}
2725+
1 * jobManagement.requireMinimumPluginVersion('git', '2.5.3')
2726+
}
2727+
2728+
def 'call git with note replace'() {
2729+
when:
2730+
context.git {
2731+
note('origin', 'test') {
2732+
replace()
2733+
}
2734+
}
2735+
2736+
then:
2737+
context.publisherNodes.size() == 1
2738+
with(context.publisherNodes[0]) {
2739+
name() == 'hudson.plugins.git.GitPublisher'
2740+
configVersion[0].value() == 2
2741+
pushMerge[0].value() == false
2742+
pushOnlyIfSuccess[0].value() == false
2743+
forcePush[0].value() == false
2744+
notesToPush.size() == 1
2745+
notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'.size() == 1
2746+
with(notesToPush[0].'hudson.plugins.git.GitPublisher_-NoteToPush'[0]) {
2747+
targetRepoName[0].value() == 'origin'
2748+
noteMsg[0].value() == 'test'
2749+
noteNamespace[0].value() == 'master'
2750+
noteReplace[0].value() == true
2751+
}
2752+
}
2753+
1 * jobManagement.requireMinimumPluginVersion('git', '2.5.3')
2754+
}
2755+
2756+
def 'call git without note targetRepoName'() {
2757+
when:
2758+
context.git {
2759+
note(null, 'test')
2760+
}
2761+
2762+
then:
2763+
thrown(DslScriptException)
2764+
2765+
when:
2766+
context.git {
2767+
note('', 'test')
2768+
}
2769+
2770+
then:
2771+
thrown(DslScriptException)
2772+
}
2773+
2774+
def 'call git without note message'() {
2775+
when:
2776+
context.git {
2777+
note('origin', null)
2778+
}
2779+
2780+
then:
2781+
thrown(DslScriptException)
2782+
2783+
when:
2784+
context.git {
2785+
note('origin', '')
2786+
}
2787+
2788+
then:
2789+
thrown(DslScriptException)
2790+
}
2791+
26902792
def 'call git without branch targetRepoName'() {
26912793
when:
26922794
context.git {

0 commit comments

Comments
 (0)