Skip to content

Commit 9fd4d8b

Browse files
committed
added @RequiresPlugin annotations to JobParent
1 parent dad6d4b commit 9fd4d8b

File tree

6 files changed

+62
-11
lines changed

6 files changed

+62
-11
lines changed

job-dsl-core/src/ast/groovy/javaposse/jobdsl/dsl/transform/PluginASTTransformation.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PluginASTTransformation implements ASTTransformation {
3232
void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
3333
sourceUnit.AST?.classes*.methods.flatten().each { MethodNode method ->
3434
method.getAnnotations(REQUIRES_PLUGIN_ANNOTATION).each { AnnotationNode requiresPluginAnnotation ->
35-
if (!method.declaringClass.getField('jobManagement')) {
35+
if (!method.declaringClass.getField('jobManagement') && !method.declaringClass.getField('jm')) {
3636
sourceUnit.errorCollector.addError(
3737
"no jobManagement field in $method.declaringClass",
3838
Token.newString(
@@ -43,11 +43,12 @@ class PluginASTTransformation implements ASTTransformation {
4343
sourceUnit
4444
)
4545
}
46+
String jobManagementVariable = method.declaringClass.getField('jobManagement') ? 'jobManagement' : 'jm'
4647

4748
MethodCallExpression pluginCheckStatement
4849
if (requiresPluginAnnotation.members.minimumVersion) {
4950
pluginCheckStatement = new MethodCallExpression(
50-
new VariableExpression('jobManagement'),
51+
new VariableExpression(jobManagementVariable),
5152
new ConstantExpression('requireMinimumPluginVersion'),
5253
new ArgumentListExpression(
5354
requiresPluginAnnotation.members.id,
@@ -56,7 +57,7 @@ class PluginASTTransformation implements ASTTransformation {
5657
)
5758
} else {
5859
pluginCheckStatement = new MethodCallExpression(
59-
new VariableExpression('jobManagement'),
60+
new VariableExpression(jobManagementVariable),
6061
new ConstantExpression('requirePlugin'),
6162
new ArgumentListExpression(
6263
requiresPluginAnnotation.members.id,

job-dsl-core/src/main/groovy/javaposse/jobdsl/dsl/JobParent.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ abstract class JobParent extends Script implements DslFactory {
3434
}
3535

3636
@Override
37+
@RequiresPlugin(id = 'build-flow-plugin')
3738
BuildFlowJob buildFlowJob(String name, @DslContext(BuildFlowJob) Closure closure = null) {
3839
processJob(name, BuildFlowJob, closure)
3940
}
@@ -44,16 +45,19 @@ abstract class JobParent extends Script implements DslFactory {
4445
}
4546

4647
@Override
48+
@RequiresPlugin(id = 'maven-plugin')
4749
MavenJob mavenJob(String name, @DslContext(MavenJob) Closure closure = null) {
4850
processJob(name, MavenJob, closure)
4951
}
5052

5153
@Override
54+
@RequiresPlugin(id = 'jenkins-multijob-plugin')
5255
MultiJob multiJob(String name, @DslContext(MultiJob) Closure closure = null) {
5356
processJob(name, MultiJob, closure)
5457
}
5558

5659
@Override
60+
@RequiresPlugin(id = 'workflow-aggregator')
5761
WorkflowJob workflowJob(String name, @DslContext(WorkflowJob) Closure closure = null) {
5862
processJob(name, WorkflowJob, closure)
5963
}
@@ -91,26 +95,31 @@ abstract class JobParent extends Script implements DslFactory {
9195
}
9296

9397
@Override
98+
@RequiresPlugin(id = 'sectioned-view')
9499
SectionedView sectionedView(String name, @DslContext(SectionedView) Closure closure = null) {
95100
processView(name, SectionedView, closure)
96101
}
97102

98103
@Override
104+
@RequiresPlugin(id = 'nested-view')
99105
NestedView nestedView(String name, @DslContext(NestedView) Closure closure = null) {
100106
processView(name, NestedView, closure)
101107
}
102108

103109
@Override
110+
@RequiresPlugin(id = 'delivery-pipeline-plugin')
104111
DeliveryPipelineView deliveryPipelineView(String name, @DslContext(DeliveryPipelineView) Closure closure = null) {
105112
processView(name, DeliveryPipelineView, closure)
106113
}
107114

108115
@Override
116+
@RequiresPlugin(id = 'build-pipeline-plugin')
109117
BuildPipelineView buildPipelineView(String name, @DslContext(BuildPipelineView) Closure closure = null) {
110118
processView(name, BuildPipelineView, closure)
111119
}
112120

113121
@Override
122+
@RequiresPlugin(id = 'build-monitor-plugin')
114123
BuildMonitorView buildMonitorView(String name, @DslContext(BuildMonitorView) Closure closure = null) {
115124
processView(name, BuildMonitorView, closure)
116125
}
@@ -143,6 +152,7 @@ abstract class JobParent extends Script implements DslFactory {
143152

144153
@Override
145154
@Deprecated
155+
@RequiresPlugin(id = 'cloudbees-folder')
146156
Folder folder(@DslContext(Folder) Closure closure) {
147157
jm.logDeprecationWarning()
148158

@@ -153,6 +163,7 @@ abstract class JobParent extends Script implements DslFactory {
153163
}
154164

155165
@Override
166+
@RequiresPlugin(id = 'cloudbees-folder')
156167
Folder folder(String name, @DslContext(Folder) Closure closure = null) {
157168
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), 'name must be specified')
158169

@@ -165,16 +176,19 @@ abstract class JobParent extends Script implements DslFactory {
165176
folder
166177
}
167178

179+
@RequiresPlugin(id = 'config-file-provider')
168180
ConfigFile customConfigFile(String name, @DslContext(ConfigFile) Closure closure = null) {
169181
processConfigFile(name, ConfigFileType.Custom, closure)
170182
}
171183

184+
@RequiresPlugin(id = 'config-file-provider')
172185
ConfigFile mavenSettingsConfigFile(String name, @DslContext(ConfigFile) Closure closure = null) {
173186
processConfigFile(name, ConfigFileType.MavenSettings, closure)
174187
}
175188

176189
@Override
177190
@Deprecated
191+
@RequiresPlugin(id = 'config-file-provider')
178192
ConfigFile configFile(Map<String, Object> arguments = [:], @DslContext(ConfigFile) Closure closure) {
179193
jm.logDeprecationWarning()
180194

Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
package javaposse.jobdsl.dsl
1+
package javaposse.jobdsl.dsl;
22

3-
import java.lang.annotation.Documented
4-
import java.lang.annotation.ElementType
5-
import java.lang.annotation.Target
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Target;
66

77
/**
88
* Indicates that a plugin must be installed to use the features provided by the annotated DSL method. A minimum
99
* version can be specified as a lower bound for the version of the required plugin.
1010
*/
11-
@Target([ElementType.METHOD])
11+
@Target(ElementType.METHOD)
1212
@Documented
1313
@interface RequiresPlugin {
1414
/**
1515
* The Plugin ID or short name of the required plugin.
1616
*/
17-
String id()
17+
String id();
1818

1919
/**
2020
* The least acceptable version of the required plugin. Optional, any version will be accepted if none is given.
2121
*/
22-
String minimumVersion() default ''
22+
String minimumVersion() default "";
2323
}

job-dsl-core/src/test/groovy/javaposse/jobdsl/dsl/JobParentSpec.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class JobParentSpec extends Specification {
9595
view instanceof BuildPipelineView
9696
parent.referencedViews.contains(view)
9797
view.node.description[0].text() == 'foo'
98+
_ * jobManagement.requirePlugin('build-pipeline-plugin')
9899
}
99100

100101
def 'build pipeline view without closure'() {
@@ -105,6 +106,7 @@ class JobParentSpec extends Specification {
105106
view.name == 'test'
106107
view instanceof BuildPipelineView
107108
parent.referencedViews.contains(view)
109+
_ * jobManagement.requirePlugin('build-pipeline-plugin')
108110
}
109111

110112
def 'build monitor view deprecated variant'() {
@@ -131,6 +133,7 @@ class JobParentSpec extends Specification {
131133
view instanceof BuildMonitorView
132134
parent.referencedViews.contains(view)
133135
view.node.description[0].text() == 'foo'
136+
_ * jobManagement.requirePlugin('build-monitor-plugin')
134137
}
135138

136139
def 'build monitor view without closure'() {
@@ -141,6 +144,7 @@ class JobParentSpec extends Specification {
141144
view.name == 'test'
142145
view instanceof BuildMonitorView
143146
parent.referencedViews.contains(view)
147+
_ * jobManagement.requirePlugin('build-monitor-plugin')
144148
}
145149

146150
def 'sectioned view deprecated variant'() {
@@ -167,6 +171,7 @@ class JobParentSpec extends Specification {
167171
view instanceof SectionedView
168172
parent.referencedViews.contains(view)
169173
view.node.description[0].text() == 'foo'
174+
_ * jobManagement.requirePlugin('sectioned-view')
170175
}
171176

172177
def 'sectioned view without closure'() {
@@ -177,6 +182,7 @@ class JobParentSpec extends Specification {
177182
view.name == 'test'
178183
view instanceof SectionedView
179184
parent.referencedViews.contains(view)
185+
_ * jobManagement.requirePlugin('sectioned-view')
180186
}
181187

182188
def 'nested view deprecated variant'() {
@@ -203,6 +209,7 @@ class JobParentSpec extends Specification {
203209
view instanceof NestedView
204210
parent.referencedViews.contains(view)
205211
view.node.description[0].text() == 'foo'
212+
_ * jobManagement.requirePlugin('nested-view')
206213
}
207214

208215
def 'nested view without closure'() {
@@ -213,6 +220,7 @@ class JobParentSpec extends Specification {
213220
view.name == 'test'
214221
view instanceof NestedView
215222
parent.referencedViews.contains(view)
223+
_ * jobManagement.requirePlugin('nested-view')
216224
}
217225

218226
def 'delivery pipeline view deprecated variant'() {
@@ -239,6 +247,7 @@ class JobParentSpec extends Specification {
239247
view instanceof DeliveryPipelineView
240248
parent.referencedViews.contains(view)
241249
view.node.description[0].text() == 'foo'
250+
_ * jobManagement.requirePlugin('delivery-pipeline-plugin')
242251
}
243252

244253
def 'delivery pipeline view without closure'() {
@@ -249,6 +258,7 @@ class JobParentSpec extends Specification {
249258
view.name == 'test'
250259
view instanceof DeliveryPipelineView
251260
parent.referencedViews.contains(view)
261+
_ * jobManagement.requirePlugin('delivery-pipeline-plugin')
252262
}
253263

254264
def 'folder deprecated variant'() {
@@ -261,6 +271,7 @@ class JobParentSpec extends Specification {
261271
folder.name == 'test'
262272
parent.referencedJobs.contains(folder)
263273
2 * jobManagement.logDeprecationWarning()
274+
_ * jobManagement.requirePlugin('cloudbees-folder')
264275
}
265276

266277
def 'folder'() {
@@ -273,6 +284,7 @@ class JobParentSpec extends Specification {
273284
folder.name == 'test'
274285
parent.referencedJobs.contains(folder)
275286
folder.node.displayName[0].text() == 'foo'
287+
_ * jobManagement.requirePlugin('cloudbees-folder')
276288
}
277289

278290
def 'folder without closure'() {
@@ -282,6 +294,7 @@ class JobParentSpec extends Specification {
282294
then:
283295
folder.name == 'test'
284296
parent.referencedJobs.contains(folder)
297+
_ * jobManagement.requirePlugin('cloudbees-folder')
285298
}
286299

287300
def 'default config file deprecated variant'() {
@@ -295,6 +308,7 @@ class JobParentSpec extends Specification {
295308
configFile.type == ConfigFileType.Custom
296309
parent.referencedConfigFiles.contains(configFile)
297310
2 * jobManagement.logDeprecationWarning()
311+
_ * jobManagement.requirePlugin('config-file-provider')
298312
}
299313

300314
def 'custom config file deprecated variant'() {
@@ -308,6 +322,7 @@ class JobParentSpec extends Specification {
308322
configFile.type == ConfigFileType.Custom
309323
parent.referencedConfigFiles.contains(configFile)
310324
2 * jobManagement.logDeprecationWarning()
325+
_ * jobManagement.requirePlugin('config-file-provider')
311326
}
312327

313328
def 'custom config file'() {
@@ -321,6 +336,7 @@ class JobParentSpec extends Specification {
321336
configFile.type == ConfigFileType.Custom
322337
configFile.comment == 'foo'
323338
parent.referencedConfigFiles.contains(configFile)
339+
_ * jobManagement.requirePlugin('config-file-provider')
324340
}
325341

326342
def 'custom config file without closure'() {
@@ -331,6 +347,7 @@ class JobParentSpec extends Specification {
331347
configFile.name == 'test'
332348
configFile.type == ConfigFileType.Custom
333349
parent.referencedConfigFiles.contains(configFile)
350+
_ * jobManagement.requirePlugin('config-file-provider')
334351
}
335352

336353
def 'Maven settings config file deprecated variant'() {
@@ -344,6 +361,7 @@ class JobParentSpec extends Specification {
344361
configFile.type == ConfigFileType.MavenSettings
345362
parent.referencedConfigFiles.contains(configFile)
346363
2 * jobManagement.logDeprecationWarning()
364+
_ * jobManagement.requirePlugin('config-file-provider')
347365
}
348366

349367
def 'Maven settings config file'() {
@@ -357,6 +375,7 @@ class JobParentSpec extends Specification {
357375
configFile.type == ConfigFileType.MavenSettings
358376
configFile.comment == 'foo'
359377
parent.referencedConfigFiles.contains(configFile)
378+
_ * jobManagement.requirePlugin('config-file-provider')
360379
}
361380

362381
def 'Maven settings config file without closure'() {
@@ -367,6 +386,7 @@ class JobParentSpec extends Specification {
367386
configFile.name == 'test'
368387
configFile.type == ConfigFileType.MavenSettings
369388
parent.referencedConfigFiles.contains(configFile)
389+
_ * jobManagement.requirePlugin('config-file-provider')
370390
}
371391

372392
def 'readFileInWorkspace from seed job'() {
@@ -500,6 +520,7 @@ class JobParentSpec extends Specification {
500520
then:
501521
job.name == 'test'
502522
parent.referencedJobs.contains(job)
523+
_ * jobManagement.requirePlugin('build-flow-plugin')
503524
}
504525

505526
def 'matrixJob deprecated variant'() {
@@ -544,6 +565,7 @@ class JobParentSpec extends Specification {
544565
then:
545566
job.name == 'test'
546567
parent.referencedJobs.contains(job)
568+
_ * jobManagement.requirePlugin('maven-plugin')
547569
}
548570

549571
def 'multiJob deprecated variant'() {
@@ -566,6 +588,7 @@ class JobParentSpec extends Specification {
566588
then:
567589
job.name == 'test'
568590
parent.referencedJobs.contains(job)
591+
_ * jobManagement.requirePlugin('jenkins-multijob-plugin')
569592
}
570593

571594
def 'workflow deprecated variant'() {
@@ -588,5 +611,6 @@ class JobParentSpec extends Specification {
588611
then:
589612
job.name == 'test'
590613
parent.referencedJobs.contains(job)
614+
_ * jobManagement.requirePlugin('workflow-aggregator')
591615
}
592616
}

job-dsl-plugin/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ buildscript {
44
maven {
55
url 'http://repo.jenkins-ci.org/releases/'
66
}
7+
mavenLocal()
78
}
89
dependencies {
9-
classpath 'org.jenkins-ci.tools:gradle-jpi-plugin:0.10.1'
10+
classpath 'org.jenkins-ci.tools:gradle-jpi-plugin:0.10.2-SNAPSHOT'
1011
}
1112
}
1213

0 commit comments

Comments
 (0)