jenkins_pipeline_builder 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +28 -43
  4. data/{spec/unit_tests/spec_helper.rb → .simplecov} +2 -15
  5. data/README.md +66 -11
  6. data/Rakefile +1 -25
  7. data/bin/generate +0 -1
  8. data/jenkins_pipeline_builder.gemspec +6 -6
  9. data/lib/jenkins_pipeline_builder.rb +14 -8
  10. data/lib/jenkins_pipeline_builder/builders.rb +207 -155
  11. data/lib/jenkins_pipeline_builder/cli/base.rb +1 -1
  12. data/lib/jenkins_pipeline_builder/cli/describe.rb +12 -8
  13. data/lib/jenkins_pipeline_builder/cli/list.rb +7 -7
  14. data/lib/jenkins_pipeline_builder/compiler.rb +62 -49
  15. data/lib/jenkins_pipeline_builder/extensions.rb +108 -0
  16. data/lib/jenkins_pipeline_builder/generator.rb +209 -160
  17. data/lib/jenkins_pipeline_builder/job_builder.rb +174 -105
  18. data/lib/jenkins_pipeline_builder/module_registry.rb +43 -59
  19. data/lib/jenkins_pipeline_builder/publishers.rb +166 -93
  20. data/lib/jenkins_pipeline_builder/pull_request.rb +7 -7
  21. data/lib/jenkins_pipeline_builder/triggers.rb +63 -40
  22. data/lib/jenkins_pipeline_builder/utils.rb +10 -10
  23. data/lib/jenkins_pipeline_builder/version.rb +1 -1
  24. data/lib/jenkins_pipeline_builder/view.rb +94 -123
  25. data/lib/jenkins_pipeline_builder/wrappers.rb +163 -96
  26. data/spec/lib/jenkins_pipeline_builder/compiler_spec.rb +31 -0
  27. data/spec/lib/jenkins_pipeline_builder/extensions_spec.rb +145 -0
  28. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/pullrequest_pipeline/PullRequest-10-SampleJob.yaml +2 -0
  29. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/pullrequest_pipeline/PullRequest-40-PullRequestGenerator.yaml +12 -0
  30. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/pullrequest_pipeline/project.yaml +12 -0
  31. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/sample_pipeline/SamplePipeline-10-Commit.yaml +3 -0
  32. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/sample_pipeline/SamplePipeline-20-Acceptance.yaml +3 -0
  33. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/sample_pipeline/SamplePipeline-30-Release.yaml +3 -0
  34. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/sample_pipeline/project.yaml +12 -0
  35. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/sample_pipeline/view.yaml +5 -0
  36. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/template_pipeline/jobs.tar.gz +0 -0
  37. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/template_pipeline/project.yaml +15 -0
  38. data/spec/lib/jenkins_pipeline_builder/fixtures/generator_tests/test_job.xml +48 -0
  39. data/spec/lib/jenkins_pipeline_builder/fixtures/view_test/duplicate_view.yaml +4 -0
  40. data/spec/lib/jenkins_pipeline_builder/fixtures/view_test/parent_view.yaml +9 -0
  41. data/spec/lib/jenkins_pipeline_builder/fixtures/view_test/regular_view.yaml +5 -0
  42. data/spec/lib/jenkins_pipeline_builder/generator_spec.rb +165 -0
  43. data/spec/lib/jenkins_pipeline_builder/module_registry_spec.rb +147 -0
  44. data/spec/lib/jenkins_pipeline_builder/pull_request_spec.rb +47 -0
  45. data/spec/lib/jenkins_pipeline_builder/spec_helper.rb +25 -0
  46. data/spec/lib/jenkins_pipeline_builder/utils_spec.rb +22 -0
  47. data/spec/lib/jenkins_pipeline_builder/view_spec.rb +54 -0
  48. metadata +54 -136
  49. data/lib/jenkins_pipeline_builder/extendable.rb +0 -42
  50. data/lib/jenkins_pipeline_builder/xml_helper.rb +0 -40
  51. data/spec/func_tests/spec_helper.rb +0 -18
  52. data/spec/func_tests/view_spec.rb +0 -90
  53. data/spec/unit_tests/compiler_spec.rb +0 -20
  54. data/spec/unit_tests/fixtures/files/Job-Build-Flow.xml +0 -57
  55. data/spec/unit_tests/fixtures/files/Job-Build-Flow.yaml +0 -22
  56. data/spec/unit_tests/fixtures/files/Job-Build-Maven.xml +0 -129
  57. data/spec/unit_tests/fixtures/files/Job-Build-Maven.yaml +0 -31
  58. data/spec/unit_tests/fixtures/files/Job-DSL.yaml +0 -14
  59. data/spec/unit_tests/fixtures/files/Job-DSL1.xml +0 -27
  60. data/spec/unit_tests/fixtures/files/Job-DSL2.xml +0 -25
  61. data/spec/unit_tests/fixtures/files/Job-Gem-Build.xml +0 -142
  62. data/spec/unit_tests/fixtures/files/Job-Gem-Build.yaml +0 -43
  63. data/spec/unit_tests/fixtures/files/Job-Generate-From-Template.xml +0 -32
  64. data/spec/unit_tests/fixtures/files/Job-Generate-From-Template.yaml +0 -8
  65. data/spec/unit_tests/fixtures/files/Job-Multi-Project.xml +0 -134
  66. data/spec/unit_tests/fixtures/files/Job-Multi-Project.yaml +0 -49
  67. data/spec/unit_tests/fixtures/files/archive_artifact.xml +0 -23
  68. data/spec/unit_tests/fixtures/files/archive_artifact.yaml +0 -5
  69. data/spec/unit_tests/fixtures/files/choice_parameter.xml +0 -39
  70. data/spec/unit_tests/fixtures/files/choice_parameter.yaml +0 -14
  71. data/spec/unit_tests/fixtures/files/concurrent_build.xml +0 -17
  72. data/spec/unit_tests/fixtures/files/concurrent_build.yaml +0 -4
  73. data/spec/unit_tests/fixtures/files/copy_artifact.xml +0 -30
  74. data/spec/unit_tests/fixtures/files/copy_artifact.yaml +0 -14
  75. data/spec/unit_tests/fixtures/files/discard_old.xml +0 -17
  76. data/spec/unit_tests/fixtures/files/discard_old.yaml +0 -8
  77. data/spec/unit_tests/fixtures/files/downstream.xml +0 -36
  78. data/spec/unit_tests/fixtures/files/downstream.yaml +0 -13
  79. data/spec/unit_tests/fixtures/files/downstream_blocking.xml +0 -19
  80. data/spec/unit_tests/fixtures/files/downstream_blocking.yaml +0 -15
  81. data/spec/unit_tests/fixtures/files/git_include_exclude.xml +0 -53
  82. data/spec/unit_tests/fixtures/files/git_include_exclude.yaml +0 -8
  83. data/spec/unit_tests/fixtures/files/groovy_postbuild.xml +0 -29
  84. data/spec/unit_tests/fixtures/files/groovy_postbuild.yaml +0 -9
  85. data/spec/unit_tests/fixtures/files/periodic_build.xml +0 -21
  86. data/spec/unit_tests/fixtures/files/periodic_build.yaml +0 -5
  87. data/spec/unit_tests/fixtures/files/post_build_script.xml +0 -28
  88. data/spec/unit_tests/fixtures/files/post_build_script.yaml +0 -11
  89. data/spec/unit_tests/fixtures/files/prepare_environment.xml +0 -17
  90. data/spec/unit_tests/fixtures/files/prepare_environment.yaml +0 -7
  91. data/spec/unit_tests/fixtures/files/project.yaml +0 -15
  92. data/spec/unit_tests/fixtures/files/properties_file.xml +0 -27
  93. data/spec/unit_tests/fixtures/files/properties_file.yaml +0 -8
  94. data/spec/unit_tests/fixtures/files/remote_job.xml +0 -18
  95. data/spec/unit_tests/fixtures/files/remote_job.yaml +0 -19
  96. data/spec/unit_tests/fixtures/files/rvm05.xml +0 -26
  97. data/spec/unit_tests/fixtures/files/rvm05.yaml +0 -5
  98. data/spec/unit_tests/fixtures/files/specific_priority.xml +0 -22
  99. data/spec/unit_tests/fixtures/files/specific_priority.yaml +0 -6
  100. data/spec/unit_tests/fixtures/files/throttle.xml +0 -17
  101. data/spec/unit_tests/fixtures/files/throttle.yaml +0 -8
  102. data/spec/unit_tests/fixtures/files/upstream.xml +0 -17
  103. data/spec/unit_tests/fixtures/files/upstream.yaml +0 -7
  104. data/spec/unit_tests/fixtures/pull_request/00.yaml +0 -14
  105. data/spec/unit_tests/fixtures/pull_request/10.yaml +0 -7
  106. data/spec/unit_tests/fixtures/pull_request/11.yaml +0 -4
  107. data/spec/unit_tests/fixtures/pull_request/project.yaml +0 -11
  108. data/spec/unit_tests/fixtures/templates/external_job.yaml +0 -4
  109. data/spec/unit_tests/fixtures/templates/project_with_jobs.yaml +0 -25
  110. data/spec/unit_tests/generator_spec.rb +0 -94
  111. data/spec/unit_tests/module_registry_spec.rb +0 -16
  112. data/spec/unit_tests/resolve_dependencies_spec.rb +0 -266
@@ -20,137 +20,206 @@
20
20
  # THE SOFTWARE.
21
21
  #
22
22
 
23
- module JenkinsPipelineBuilder
24
- class JobBuilder < Extendable
25
- register :description do |description, n_xml|
26
- desc = n_xml.xpath('//description').first
27
- desc.content = "#{description}"
28
- end
23
+ job_attribute do
24
+ name :description
25
+ plugin_id 123
26
+ min_version 0
27
+ announced false
29
28
 
30
- register :scm_params do |params, n_xml|
31
- XmlHelper.update_node_text(n_xml, '//scm/localBranch', params[:local_branch]) if params[:local_branch]
32
- XmlHelper.update_node_text(n_xml, '//scm/recursiveSubmodules', params[:recursive_update]) if params[:recursive_update]
33
- XmlHelper.update_node_text(n_xml, '//scm/wipeOutWorkspace', params[:wipe_workspace]) if params[:wipe_workspace]
34
- XmlHelper.update_node_text(n_xml, '//scm/excludedUsers', params[:excluded_users]) if params[:excluded_users]
35
- XmlHelper.update_node_text(n_xml, '//scm/userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/name', params[:remote_name]) if params[:remote_name]
36
- XmlHelper.update_node_text(n_xml, '//scm/skipTag', params[:skip_tag]) if params[:skip_tag]
37
- XmlHelper.update_node_text(n_xml, '//scm/userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/refspec', params[:refspec]) if params[:refspec]
38
- XmlHelper.update_node_text(n_xml, '//scm/excludedRegions', params[:excluded_regions]) if params[:excluded_regions]
39
- XmlHelper.update_node_text(n_xml, '//scm/includedRegions', params[:included_regions]) if params[:included_regions]
40
- end
29
+ before do
30
+ xpath('//project/description').remove
31
+ end
32
+
33
+ xml('//project') do |description|
34
+ description "#{description}"
35
+ end
36
+ end
41
37
 
42
- register :hipchat do |params, n_xml|
43
- fail 'No HipChat room specified' unless params[:room]
38
+ job_attribute do
39
+ name :scm_params
40
+ plugin_id 123
41
+ min_version 0
42
+ announced false
44
43
 
45
- properties = n_xml.xpath('//properties').first
46
- Nokogiri::XML::Builder.with(properties) do |xml|
47
- xml.send('jenkins.plugins.hipchat.HipChatNotifier_-HipChatJobProperty') do
48
- xml.room params[:room]
49
- xml.startNotification params[:'start-notify'] || false
44
+ # XML preprocessing
45
+ # TODO: Actually figure out how to merge using the builder DSL
46
+ # This delete the things we are going to add later is pretty crappy
47
+ # Alternately don't use/tweak the xml the api client generates
48
+ # (which is where I assume this is coming from)
49
+ before do |params|
50
+ xpath('//scm/localBranch').remove if params[:local_branch]
51
+ xpath('//scm/recursiveSubmodules').remove if params[:recursive_update]
52
+ xpath('//scm/wipeOutWorkspace').remove if params[:wipe_workspace]
53
+ xpath('//scm/excludedUsers').remove if params[:excluded_users]
54
+ xpath('//scm/userRemoteConfigs').remove if params[:remote_name] || params[:refspec]
55
+ xpath('//scm/skipTag').remove if params[:skip_tag]
56
+ xpath('//scm/excludedRegions').remove if params[:excluded_regions]
57
+ xpath('//scm/includedRegions').remove if params[:included_regions]
58
+
59
+ end
60
+
61
+ xml '//scm' do |params|
62
+ localBranch params[:local_branch] if params[:local_branch]
63
+ recursiveSubmodules params[:recursive_update] if params[:recursive_update]
64
+ wipeOutWorkspace params[:wipe_workspace] if params[:wipe_workspace]
65
+ excludedUsers params[:excluded_users] if params[:excluded_users]
66
+ if params[:remote_name]
67
+ userRemoteConfigs do
68
+ send('hudson.plugins.git.UserRemoteConfig') do
69
+ name params[:remote_name]
50
70
  end
51
71
  end
52
72
  end
53
-
54
- register :priority do |params, n_xml|
55
- n_builders = n_xml.xpath('//properties').first
56
- Nokogiri::XML::Builder.with(n_builders) do |xml|
57
- xml.send('jenkins.advancedqueue.AdvancedQueueSorterJobProperty', 'plugin' => 'PrioritySorter') do
58
- xml.useJobPriority params[:use_priority]
59
- xml.priority params[:job_priority] || -1
73
+ skipTag params[:skip_tag] if params[:skip_tag]
74
+ if params[:refspec]
75
+ userRemoteConfigs do
76
+ send 'hudson.plugins.git.UserRemoteConfig' do
77
+ refspec params[:refspec]
60
78
  end
61
79
  end
62
80
  end
81
+ excludedRegions params[:excluded_regions] if params[:excluded_regions]
82
+ includedRegions params[:included_regions] if params[:included_regions]
83
+ end
84
+ end
85
+
86
+ job_attribute do
87
+ name :hipchat
88
+ plugin_id 123
89
+ min_version 0
90
+ announced false
91
+
92
+ xml '//properties' do |params|
93
+ fail 'No HipChat room specified' unless params[:room]
94
+
95
+ send('jenkins.plugins.hipchat.HipChatNotifier_-HipChatJobProperty') do
96
+ room params[:room]
97
+ startNotification params[:'start-notify'] || false
98
+ end
99
+ end
100
+ end
101
+
102
+ job_attribute do
103
+ name :priority
104
+ plugin_id 123
105
+ min_version 0
106
+ announced false
107
+
108
+ xml '//properties' do |params|
109
+ send('jenkins.advancedqueue.AdvancedQueueSorterJobProperty', 'plugin' => 'PrioritySorter') do
110
+ useJobPriority params[:use_priority]
111
+ priority params[:job_priority] || -1
112
+ end
113
+ end
114
+ end
115
+
116
+ job_attribute do
117
+ name :parameters
118
+ plugin_id 123
119
+ min_version 0
120
+ announced false
63
121
 
64
- register :parameters do |params, n_xml|
65
- n_builders = n_xml.xpath('//properties').first
66
- Nokogiri::XML::Builder.with(n_builders) do |xml|
67
- xml.send('hudson.model.ParametersDefinitionProperty') do
68
- xml.parameterDefinitions do
69
- param_proc = lambda do |xml, params, type|
70
- xml.send(type) do
71
- xml.name params[:name]
72
- xml.description params[:description]
73
- xml.defaultValue params[:default]
74
- if params[:type] == 'choice'
75
- xml.choices('class' => 'java.util.Arrays$ArrayList') do
76
- xml.a('class' => 'string-array') do
77
- params[:values].each do |value|
78
- xml.string value
79
- end
80
- end
122
+ xml '//properties' do |params|
123
+ send('hudson.model.ParametersDefinitionProperty') do
124
+ parameterDefinitions do
125
+ params.each do |param|
126
+ case param[:type]
127
+ when 'string'
128
+ paramType = 'hudson.model.StringParameterDefinition'
129
+ when 'bool'
130
+ paramType = 'hudson.model.BooleanParameterDefinition'
131
+ when 'text'
132
+ paramType = 'hudson.model.TextParameterDefinition'
133
+ when 'password'
134
+ paramType = 'hudson.model.PasswordParameterDefinition'
135
+ when 'choice'
136
+ paramType = 'hudson.model.ChoiceParameterDefinition'
137
+ else
138
+ paramType = 'hudson.model.StringParameterDefinition'
139
+ end
140
+
141
+ send(paramType) do
142
+ name param[:name]
143
+ description param[:description]
144
+ defaultValue param[:default]
145
+ if param[:type] == 'choice'
146
+ choices('class' => 'java.util.Arrays$ArrayList') do
147
+ a('class' => 'string-array') do
148
+ param[:values].each do |value|
149
+ string value
81
150
  end
82
151
  end
83
152
  end
84
153
  end
85
- params.each do |param|
86
- case param[:type]
87
- when 'string'
88
- paramType = 'hudson.model.StringParameterDefinition'
89
- when 'bool'
90
- paramType = 'hudson.model.BooleanParameterDefinition'
91
- when 'text'
92
- paramType = 'hudson.model.TextParameterDefinition'
93
- when 'password'
94
- paramType = 'hudson.model.PasswordParameterDefinition'
95
- when 'choice'
96
- paramType = 'hudson.model.ChoiceParameterDefinition'
97
- else
98
- paramType = 'hudson.model.StringParameterDefinition'
99
- end
100
-
101
- param_proc.call xml, param, paramType
102
- end
103
154
  end
104
155
  end
105
156
  end
106
157
  end
158
+ end
159
+ end
107
160
 
108
- register :discard_old do |params, n_xml|
109
- properties = n_xml.child
110
- Nokogiri::XML::Builder.with(properties) do |xml|
111
- xml.send('logRotator', 'class' => 'hudson.tasks.LogRotator') do
112
- xml.daysToKeep params[:days] if params[:days]
113
- xml.numToKeep params[:number] || -1
114
- xml.artifactDaysToKeep params[:artifact_days] || -1
115
- xml.artifactNumToKeep params[:artifact_number] || -1
116
- end
117
- end
161
+ job_attribute do
162
+ name :discard_old
163
+ plugin_id 123
164
+ min_version 0
165
+ announced false
166
+
167
+ xml '//project' do |params|
168
+ send('logRotator', 'class' => 'hudson.tasks.LogRotator') do
169
+ daysToKeep params[:days] if params[:days]
170
+ numToKeep params[:number] || -1
171
+ artifactDaysToKeep params[:artifact_days] || -1
172
+ artifactNumToKeep params[:artifact_number] || -1
118
173
  end
174
+ end
175
+ end
119
176
 
120
- register :throttle do |params, n_xml|
121
- properties = n_xml.xpath('//properties').first
122
- cat_set = params[:option] == 'category'
123
- Nokogiri::XML::Builder.with(properties) do |xml|
124
- xml.send('hudson.plugins.throttleconcurrents.ThrottleJobProperty', 'plugin' => 'throttle-concurrents') do
125
- xml.maxConcurrentPerNode params[:max_per_node] || 0
126
- xml.maxConcurrentTotal params[:max_total] || 0
127
- xml.throttleEnabled true
128
- xml.throttleOption params[:option] || 'alone'
129
- xml.categories do
130
- xml.string params[:category] if cat_set
131
- end
132
- end
177
+ job_attribute do
178
+ name :throttle
179
+ plugin_id 100
180
+ min_version 0
181
+ announced false
182
+
183
+ xml '//properties' do |params|
184
+ cat_set = params[:option] == 'category'
185
+ send('hudson.plugins.throttleconcurrents.ThrottleJobProperty', 'plugin' => 'throttle-concurrents') do
186
+ maxConcurrentPerNode params[:max_per_node] || 0
187
+ maxConcurrentTotal params[:max_total] || 0
188
+ throttleEnabled true
189
+ throttleOption params[:option] || 'alone'
190
+ categories do
191
+ string params[:category] if cat_set
133
192
  end
134
193
  end
194
+ end
195
+ end
135
196
 
136
- register :prepare_environment do |params, n_xml|
137
- properties = n_xml.xpath('//properties').first
138
- Nokogiri::XML::Builder.with(properties) do |xml|
139
- xml.send('EnvInjectJobProperty') do
140
- xml.info do
141
- xml.propertiesContent params[:properties_content] if params[:properties_content]
142
- xml.loadFilesFromMaster params[:load_from_master] if params[:load_from_master]
143
- end
144
- xml.on true
145
- xml.keepJenkinsSystemVariables params[:keep_environment] if params[:keep_environment]
146
- xml.keepBuildVariables params[:keep_build] if params[:keep_build]
147
- end
197
+ job_attribute do
198
+ name :prepare_environment
199
+ plugin_id 123
200
+ min_version 0
201
+ announced false
202
+
203
+ xml '//properties' do |params|
204
+ send('EnvInjectJobProperty') do
205
+ info do
206
+ propertiesContent params[:properties_content] if params[:properties_content]
207
+ loadFilesFromMaster params[:load_from_master] if params[:load_from_master]
148
208
  end
209
+ on true
210
+ keepJenkinsSystemVariables params[:keep_environment] if params[:keep_environment]
211
+ keepBuildVariables params[:keep_build] if params[:keep_build]
149
212
  end
213
+ end
214
+ end
150
215
 
151
- register :concurrent_build do |params, n_xml|
152
- concurrentBuild = n_xml.xpath('//concurrentBuild').first
153
- concurrentBuild.content = (params == true) ? 'true' : 'false'
154
- end
216
+ job_attribute do
217
+ name :concurrent_build
218
+ plugin_id 123
219
+ min_version 0
220
+ announced false
221
+
222
+ xml '//concurrentBuild' do |params|
223
+ (params == true) ? 'true' : 'false'
155
224
  end
156
225
  end
@@ -23,49 +23,32 @@
23
23
  module JenkinsPipelineBuilder
24
24
  class ModuleRegistry
25
25
  attr_accessor :registry, :registered_modules
26
- ENTRIES = {
27
- builders: '//builders',
28
- publishers: '//publishers',
29
- wrappers: '//buildWrappers',
30
- triggers: '//triggers'
31
- }
32
-
33
- # creates register_triggers and so on
34
- # we declare job attribues below since it doesn't follow the pattern
35
- ENTRIES.keys.each do |key|
36
- # TODO: Too lazy to figure out a better way to do this
37
- singular_key = key.to_s.singularize.to_sym
38
- define_method "register_#{singular_key}" do |name, jenkins_name, description, &block|
39
- @registered_modules[key][name] = {
40
- jenkins_name: jenkins_name,
41
- description: description
42
- }
43
- @registry[:job][key][:registry][name] = block
44
- end
45
- end
46
-
47
26
  def initialize
48
27
  @registry = { job: {} }
49
- @registered_modules = { job_attributes: {} }
28
+ end
50
29
 
51
- entries.each do |key, value|
52
- @registered_modules[key] = {}
53
- @registry[:job][key] = {}
54
- @registry[:job][key][:registry] = {}
55
- @registry[:job][key][:method] = ->(registry, params, n_xml) { run_registry_on_path(value, registry, params, n_xml) }
56
- end
30
+ def logger
31
+ JenkinsPipelineBuilder.logger
57
32
  end
58
33
 
34
+ # Ideally refactor this out to be derived from the registry,
35
+ # but I'm lazy for now
59
36
  def entries
60
- ENTRIES
37
+ {
38
+ builders: '//builders',
39
+ publishers: '//publishers',
40
+ wrappers: '//buildWrappers',
41
+ triggers: '//triggers'
42
+ }
61
43
  end
62
44
 
63
- def register_job_attribute(name, jenkins_name, description, &block)
64
- @registered_modules[:job_attributes][name] = {
65
- jenkins_name: jenkins_name,
66
- description: description
67
- }
68
- @registry[:job][name] = block
45
+ def register(prefix, extension)
46
+ name = prefix.pop
47
+ root = prefix.inject(@registry, :[])
48
+ root[name] = {} unless root[name]
49
+
50
+ root[name][extension.name] = [] unless root[name][extension.name]
51
+ root[name][extension.name] << extension
69
52
  end
70
53
 
71
54
  def get(path)
@@ -85,36 +68,37 @@ module JenkinsPipelineBuilder
85
68
  traverse_registry(registry, params, n_xml)
86
69
  end
87
70
 
88
- def traverse_registry(registry, params, n_xml)
71
+ def traverse_registry(registry, params, n_xml, strict = false)
89
72
  params.each do |key, value|
90
- execute_registry_item(registry, key, value, n_xml)
91
- end
92
- end
93
-
94
- def traverse_registry_param_array(registry, params, n_xml)
95
- params.each do |item|
96
- key = item.keys.first
97
- next if key.nil?
98
- execute_registry_item(registry, key, item[key], n_xml)
99
- end
100
- end
101
-
102
- def execute_registry_item(registry, key, value, n_xml)
103
- registry_item = registry[key]
104
- if registry_item.kind_of?(Hash)
105
- sub_registry = registry_item[:registry]
106
- method = registry_item[:method]
107
- method.call(sub_registry, value, n_xml)
108
- elsif registry_item.kind_of?(Method) || registry_item.kind_of?(Proc)
109
- registry_item.call(value, n_xml) unless registry_item.nil?
73
+ next unless registry.is_a? Hash
74
+ unless registry.key? key
75
+ fail "!!!! could not find key #{key} !!!!" if strict
76
+ next
77
+ end
78
+ reg_value = registry[key]
79
+ if reg_value.is_a?(Array) && reg_value.first.is_a?(Extension)
80
+ next unless reg_value.select { |x| x.class == Extension }.size == reg_value.size
81
+ # TODO: Actually compare versions installed on box here
82
+ reg = reg_value.sort { |x, y| x.min_version <=> y.min_version }.last
83
+ logger.debug "Using #{reg.type} #{reg.name} version #{reg.min_version}"
84
+ execute_extension(reg, value, n_xml)
85
+ elsif value.is_a? Hash
86
+ traverse_registry reg_value, value, n_xml, true
87
+ elsif value.is_a? Array
88
+ value.each do |v|
89
+ traverse_registry reg_value, v, n_xml, true
90
+ end
91
+ end
110
92
  end
111
93
  end
112
94
 
113
- def run_registry_on_path(path, registry, params, n_xml)
114
- n_builders = n_xml.xpath(path).first
95
+ def execute_extension(extension, value, n_xml)
96
+ n_builders = n_xml.xpath(extension.path).first
97
+ n_builders.instance_exec(value, &extension.before) if extension.before
115
98
  Nokogiri::XML::Builder.with(n_builders) do |xml|
116
- traverse_registry_param_array(registry, params, xml)
99
+ xml.instance_exec value, &extension.xml
117
100
  end
101
+ n_builders.instance_exec(value, &extension.after) if extension.after
118
102
  end
119
103
  end
120
104
  end
@@ -18,135 +18,208 @@
18
18
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
19
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
  # THE SOFTWARE.
21
- #
22
21
 
23
- module JenkinsPipelineBuilder
24
- class Publishers < Extendable
25
- register :description_setter do |params, xml|
26
- xml.send('hudson.plugins.descriptionsetter.DescriptionSetterPublisher') do
27
- xml.regexp params[:regexp]
28
- xml.regexpForFailed params[:regexp]
29
- xml.description params[:description]
30
- xml.descriptionForFailed params[:description]
31
- xml.setForMatrix false
32
- end
22
+ publisher do
23
+ name :description_setter
24
+ plugin_id 123
25
+ min_version 0
26
+ announced false
27
+
28
+ xml do |params|
29
+ send('hudson.plugins.descriptionsetter.DescriptionSetterPublisher') do
30
+ regexp params[:regexp]
31
+ regexpForFailed params[:regexp]
32
+ description params[:description]
33
+ descriptionForFailed params[:description]
34
+ setForMatrix false
33
35
  end
36
+ end
37
+ end
34
38
 
35
- register :downstream do |params, xml|
36
- xml.send('hudson.plugins.parameterizedtrigger.BuildTrigger') do
37
- xml.configs do
38
- xml.send('hudson.plugins.parameterizedtrigger.BuildTriggerConfig') do
39
- xml.configs do
40
- params[:data] = [{ params: '' }] unless params[:data]
41
- params[:data].each do |config|
42
- if config[:params]
43
- xml.send('hudson.plugins.parameterizedtrigger.PredefinedBuildParameters') do
44
- xml.properties config[:params]
45
- end
39
+ publisher do
40
+ name :downstream
41
+ plugin_id 123
42
+ min_version 0
43
+ announced false
44
+
45
+ xml do |params|
46
+ send('hudson.plugins.parameterizedtrigger.BuildTrigger') do
47
+ configs do
48
+ send('hudson.plugins.parameterizedtrigger.BuildTriggerConfig') do
49
+ configs do
50
+ params[:data] = [{ params: '' }] unless params[:data]
51
+ params[:data].each do |config|
52
+ if config[:params]
53
+ send('hudson.plugins.parameterizedtrigger.PredefinedBuildParameters') do
54
+ properties config[:params]
46
55
  end
47
- if config[:file]
48
- xml.send('hudson.plugins.parameterizedtrigger.FileBuildParameters') do
49
- xml.propertiesFile config[:file]
50
- xml.failTriggerOnMissing false
51
- end
56
+ end
57
+ if config[:file]
58
+ send('hudson.plugins.parameterizedtrigger.FileBuildParameters') do
59
+ propertiesFile config[:file]
60
+ failTriggerOnMissing false
52
61
  end
53
62
  end
54
63
  end
55
- xml.projects params[:project]
56
- xml.condition params[:condition] || 'SUCCESS'
57
- xml.triggerWithNoParameters params[:trigger_with_no_parameters] || false
58
64
  end
65
+ projects params[:project]
66
+ condition params[:condition] || 'SUCCESS'
67
+ triggerWithNoParameters params[:trigger_with_no_parameters] || false
59
68
  end
60
69
  end
61
70
  end
71
+ end
72
+ end
62
73
 
63
- register :hipchat do |params, xml|
64
- params = {} if params == true
65
- xml.send('jenkins.plugins.hipchat.HipChatNotifier') do
66
- xml.jenkinsUrl params[:jenkinsUrl] || ''
67
- xml.authToken params[:authToken] || ''
68
- xml.room params[:room] || ''
69
- end
74
+ publisher do
75
+ name :hipchat
76
+ plugin_id 101
77
+ min_version 0
78
+ announced false
79
+
80
+ xml do |params|
81
+ params = {} if params == true
82
+ send('jenkins.plugins.hipchat.HipChatNotifier') do
83
+ jenkinsUrl params[:jenkinsUrl] || ''
84
+ authToken params[:authToken] || ''
85
+ room params[:room] || ''
70
86
  end
87
+ end
88
+ end
71
89
 
72
- register :git do |params, xml|
73
- xml.send('hudson.plugins.git.GitPublisher') do
74
- xml.configVersion params[:configVersion] || 2
75
- xml.pushMerge params[:'push-merge'] || false
76
- xml.pushOnlyIfSuccess params[:'push-only-if-success'] || false
77
- xml.branchesToPush do
78
- xml.send('hudson.plugins.git.GitPublisher_-BranchToPush') do
79
- xml.targetRepoName params[:targetRepoName] || 'origin'
80
- xml.branchName params[:branchName] || 'master'
81
- end
90
+ publisher do
91
+ name :git
92
+ plugin_id 123
93
+ min_version 0
94
+ announced false
95
+
96
+ xml do |params|
97
+ send('hudson.plugins.git.GitPublisher') do
98
+ configVersion params[:configVersion] || 2
99
+ pushMerge params[:'push-merge'] || false
100
+ pushOnlyIfSuccess params[:'push-only-if-success'] || false
101
+ branchesToPush do
102
+ send('hudson.plugins.git.GitPublisher_-BranchToPush') do
103
+ targetRepoName params[:targetRepoName] || 'origin'
104
+ branchName params[:branchName] || 'master'
82
105
  end
83
106
  end
84
107
  end
108
+ end
109
+ end
85
110
 
86
- register :junit_result do |params, xml|
87
- xml.send('hudson.tasks.junit.JUnitResultArchiver') do
88
- xml.testResults params[:test_results] || ''
89
- xml.keepLongStdio false
90
- xml.testDataPublishers
91
- end
111
+ publisher do
112
+ name :junit_result
113
+ plugin_id 123
114
+ min_version 0
115
+ announced false
116
+
117
+ xml do |params|
118
+ send('hudson.tasks.junit.JUnitResultArchiver') do
119
+ testResults params[:test_results] || ''
120
+ keepLongStdio false
121
+ testDataPublishers
92
122
  end
123
+ end
124
+ end
125
+
126
+ publisher do
127
+ name :coverage_result
128
+ plugin_id 123
129
+ min_version 0
130
+ announced false
93
131
 
94
- register :coverage_result do |params, xml|
95
- xml.send('hudson.plugins.rubyMetrics.rcov.RcovPublisher') do
96
- xml.reportDir params[:report_dir]
97
- xml.targets do
98
- coverage_metric('TOTAL_COVERAGE', params[:total], xml)
99
- coverage_metric('CODE_COVERAGE', params[:code], xml)
132
+ xml do |params|
133
+ send('hudson.plugins.rubyMetrics.rcov.RcovPublisher') do
134
+ reportDir params[:report_dir]
135
+ targets do
136
+ { 'TOTAL_COVERAGE' => params[:total], 'CODE_COVERAGE' => params[:code] }.each do |key, inner_params|
137
+ send('hudson.plugins.rubyMetrics.rcov.model.MetricTarget') do
138
+ metric key
139
+ healthy inner_params[:healthy]
140
+ unhealthy inner_params[:unhealthy]
141
+ unstable inner_params[:unstable]
142
+ end
100
143
  end
101
144
  end
102
145
  end
146
+ end
147
+ end
103
148
 
104
- register :post_build_script do |params, xml|
105
- xml.send('org.jenkinsci.plugins.postbuildscript.PostBuildScript') do
106
- xml.buildSteps do
107
- if params[:shell_command]
108
- xml.send('hudson.tasks.Shell') do
109
- xml.command params[:shell_command]
110
- end
149
+ publisher do
150
+ name :post_build_script
151
+ plugin_id 123
152
+ min_version 0
153
+ announced false
154
+
155
+ xml do |params|
156
+ send('org.jenkinsci.plugins.postbuildscript.PostBuildScript') do
157
+ buildSteps do
158
+ if params[:shell_command]
159
+ send('hudson.tasks.Shell') do
160
+ command params[:shell_command]
111
161
  end
112
162
  end
113
- xml.scriptOnlyIfSuccess params[:on_success]
114
- xml.scriptOnlyIfFailure params[:on_failure]
115
- xml.executeOn params[:execute_on] || 'BOTH'
116
163
  end
164
+ scriptOnlyIfSuccess params[:on_success]
165
+ scriptOnlyIfFailure params[:on_failure]
166
+ executeOn params[:execute_on] || 'BOTH'
117
167
  end
168
+ end
169
+ end
118
170
 
119
- register :groovy_postbuild do |params, xml|
120
- xml.send('org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder', 'plugin' => 'groovy-postbuild') do
121
- xml.groovyScript params[:groovy_script]
122
- xml.behavior params[:behavior] || '0'
123
- xml.runFormMatrixParent 'false'
124
- if params[:additional_classpaths]
125
- params[:additional_classpaths].each do |path|
126
- xml.send('org.jvnet.hudson.plugins.groovypostbuild.GroovyScriptPath') do
127
- xml.path path[:path] || '/'
128
- end
171
+ publisher do
172
+ name :groovy_postbuild
173
+ plugin_id 123
174
+ min_version 0
175
+ announced false
176
+
177
+ xml do |params|
178
+ send('org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder', 'plugin' => 'groovy-postbuild') do
179
+ groovyScript params[:groovy_script]
180
+ behavior params[:behavior] || '0'
181
+ runFormMatrixParent 'false'
182
+ if params[:additional_classpaths]
183
+ params[:additional_classpaths].each do |path|
184
+ send('org.jvnet.hudson.plugins.groovypostbuild.GroovyScriptPath') do
185
+ path path[:path] || '/'
129
186
  end
130
187
  end
131
188
  end
132
189
  end
190
+ end
191
+ end
192
+
193
+ publisher do
194
+ name :archive_artifact
195
+ plugin_id 123
196
+ min_version 0
197
+ announced false
198
+
199
+ xml do |params|
200
+ send('hudson.tasks.ArtifactArchiver') do
201
+ artifacts params[:artifacts]
202
+ excludes params[:excludes] if params[:excludes]
203
+ latestOnly params[:latest_only] || false
204
+ allowEmptyArchive params[:allow_empty] || false
133
205
 
134
- register :archive_artifact do |params, xml|
135
- xml.send('hudson.tasks.ArtifactArchiver') do
136
- xml.artifacts params[:artifacts]
137
- xml.excludes params[:excludes] if params[:excludes]
138
- xml.latestOnly params[:latest_only] || false
139
- xml.allowEmptyArchive params[:allow_empty] || false
140
- end
141
206
  end
207
+ end
208
+ end
142
209
 
143
- def self.coverage_metric(name, params, xml)
144
- xml.send('hudson.plugins.rubyMetrics.rcov.model.MetricTarget') do
145
- xml.metric name
146
- xml.healthy params[:healthy]
147
- xml.unhealthy params[:unhealthy]
148
- xml.unstable params[:unstable]
149
- end
210
+ publisher do
211
+ name :email_notifications
212
+ plugin_id 123
213
+ min_version 0
214
+ announced false
215
+
216
+ xml do |params|
217
+ send 'hudson.tasks.Mailer', 'plugin' => 'mailer' do
218
+ recipients params[:recipients] || ''
219
+ send_unstable = false
220
+ send_unstable = true if params[:send_if_unstable] == false
221
+ dontNotifyEveryUnstableBuild send_unstable
222
+ sendToIndividuals params[:send_to_individuals] if params[:send_to_individuals]
150
223
  end
151
224
  end
152
225
  end