jenkins_pipeline_builder 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4d433fdb4421e7ff066fd49444c45abe2d2b29df62228b95b9d0933b0dce7f6
4
- data.tar.gz: 2586bd749811dd2848490e146b926e42c7332526282c53ff43c5f706ad49a8fc
3
+ metadata.gz: b2b8dbf5bb5aecf737f7de86141b559cf74704d23ceaa754f4d9c19803401a37
4
+ data.tar.gz: a412dce1a03b09be9298c5d6381a0fe7c85f4b1f9d7bf2229292de1b845cad86
5
5
  SHA512:
6
- metadata.gz: 0969f829a1b73b76c1954ea323352f66d346b90b307705016d28d8b1a74987580f4baaea75dc2ba8f5c59b4e7c41039307c2a4b96019cc0a30c8ab51db861940
7
- data.tar.gz: 6a02902dc545bdd9e2086503818b0cf9095edbe0964a290140bbe1bd718bb3c0748de2763d5b290f7af05927efafa9299418d1145e843fe1f81f78c9cb5f3302
6
+ metadata.gz: d4c23fe8ad05dd27dd99a55c771a5b8a423f8b7af3e8069d70f9f6d2f4aa3d72c4a0348b5ffd487a98452190d6ff8c5fd8aad9b5d6d393f1d9ab9cd853cde4c5
7
+ data.tar.gz: d40e0eb1e3a388975bf324822c334dcc085bf0dc58657b14cef9cbf744e393b383cd882c01fdee438c7e17934a4ca5f5a7563a595912d2a4a239a24baaa7761a
@@ -31,7 +31,7 @@ module JenkinsPipelineBuilder
31
31
  Helper.setup(parent_options).dump(job_name)
32
32
  end
33
33
 
34
- desc 'bootstrap Path', 'Generates pipeline from folder or a file'
34
+ desc 'bootstrap Path [ProjectName]', 'Generates pipeline from folder or a file'
35
35
  def bootstrap(path, project_name = nil)
36
36
  failed = Helper.setup(parent_options).bootstrap(path, project_name)
37
37
  exit(0) if failed.empty? # weird ordering, but rubocop decrees
@@ -39,12 +39,13 @@ module JenkinsPipelineBuilder
39
39
  exit(1)
40
40
  end
41
41
 
42
- desc 'pull_request Path', 'Generates jenkins jobs based on a git pull request.'
42
+ option :base_branch_only, type: :boolean
43
+ desc 'pull_request Path [ProjectName] [--base_branch_only]', 'Generates jenkins jobs based on a git pull request.'
43
44
  def pull_request(path, project_name = nil)
44
- Helper.setup(parent_options).pull_request(path, project_name)
45
+ Helper.setup(parent_options).pull_request(path, project_name, options[:base_branch_only])
45
46
  end
46
47
 
47
- desc 'file Path', 'Does the same thing as bootstrap but doesn\'t actually create jobs on the server'
48
+ desc 'file Path [ProjectName]', 'Does the same thing as bootstrap but doesn\'t actually create jobs on the server'
48
49
  def file(path, project_name = nil)
49
50
  Helper.setup(parent_options).file(path, project_name)
50
51
  end
@@ -54,7 +54,7 @@ build_step do
54
54
  end
55
55
  end
56
56
  else
57
- configs(class: 'empty-list'){}
57
+ configs(class: 'empty-list') {}
58
58
  end
59
59
 
60
60
  projects state[:name]
@@ -56,7 +56,8 @@ module JenkinsPipelineBuilder
56
56
  publish(project_name || job_collection.projects.first[:name])
57
57
  end
58
58
 
59
- def pull_request(path, project_name)
59
+ # rubocop:disable Metrics/AbcSize
60
+ def pull_request(path, project_name, base_branch_only = false)
60
61
  logger.info "Pull Request Generator Running from path #{path}"
61
62
  load_job_collection path unless job_collection.loaded?
62
63
  defaults = job_collection.defaults[:value]
@@ -64,12 +65,14 @@ module JenkinsPipelineBuilder
64
65
  pr_generator.delete_closed_prs
65
66
  errors = []
66
67
  pr_generator.open_prs.each do |pr|
67
- pr_generator.convert! job_collection, pr
68
+ next if base_branch_only && defaults[:git_branch] != pr[:base]
69
+ pr_generator.convert! job_collection, pr[:number]
68
70
  error = publish(project_name)
69
71
  errors << error unless error.empty?
70
72
  end
71
73
  errors.empty?
72
74
  end
75
+ # rubocop:enable Metrics/AbcSize
73
76
 
74
77
  def file(path, project_name)
75
78
  logger.info "Generating files from path #{path}"
@@ -29,32 +29,32 @@ module JenkinsPipelineBuilder
29
29
  @open_prs = active_prs defaults[:github_site], defaults[:git_org], defaults[:git_repo_name]
30
30
  end
31
31
 
32
- def convert!(job_collection, pr)
33
- job_collection.defaults[:value][:application_name] = "#{application_name}-PR#{pr}"
34
- job_collection.defaults[:value][:pull_request_number] = pr.to_s
35
- job_collection.jobs.each { |j| override j[:value], pr }
32
+ def convert!(job_collection, pr_number)
33
+ job_collection.defaults[:value][:application_name] = "#{application_name}-PR#{pr_number}"
34
+ job_collection.defaults[:value][:pull_request_number] = pr_number.to_s
35
+ job_collection.jobs.each { |j| override j[:value], pr_number }
36
36
  end
37
37
 
38
38
  def delete_closed_prs
39
39
  return if JenkinsPipelineBuilder.debug
40
40
  jobs_to_delete = JenkinsPipelineBuilder.client.job.list "^#{application_name}-PR(\\d+)-(.*)$"
41
- open_prs.each do |n|
42
- jobs_to_delete.reject! { |j| j.start_with? "#{application_name}-PR#{n}" }
41
+ open_prs.each do |pr|
42
+ jobs_to_delete.reject! { |j| j.start_with? "#{application_name}-PR#{pr[:number]}" }
43
43
  end
44
44
  jobs_to_delete.each { |j| JenkinsPipelineBuilder.client.job.delete j }
45
45
  end
46
46
 
47
47
  private
48
48
 
49
- def override(job, pr)
49
+ def override(job, pr_number)
50
50
  git_version = JenkinsPipelineBuilder.registry.registry[:job][:scm_params].installed_version
51
- job[:scm_branch] = "origin/pr/#{pr}/head"
51
+ job[:scm_branch] = "origin/pr/#{pr_number}/head"
52
52
  job[:scm_params] ||= {}
53
- job[:scm_params][:refspec] = "refs/pull/#{pr}/head:refs/remotes/origin/pr/#{pr}/head"
53
+ job[:scm_params][:refspec] = "refs/pull/#{pr_number}/head:refs/remotes/origin/pr/#{pr_number}/head"
54
54
  job[:scm_params][:changelog_to_branch] ||= {}
55
55
  if Gem::Version.new(2.0) < git_version
56
56
  job[:scm_params][:changelog_to_branch]
57
- .merge!(remote: 'origin', branch: "pr/#{pr}/head")
57
+ .merge!(remote: 'origin', branch: "pr/#{pr_number}/head")
58
58
  end
59
59
  end
60
60
 
@@ -66,7 +66,7 @@ module JenkinsPipelineBuilder
66
66
  begin
67
67
  resp = Net::HTTP.get_response(URI.parse(url))
68
68
  pulls = JSON.parse(resp.body)
69
- pulls.map { |p| p['number'] }
69
+ pulls.map { |p| { number: p['number'], base: p['base']['ref'] } }
70
70
  rescue StandardError
71
71
  raise 'Failed connecting to github!'
72
72
  end
@@ -21,5 +21,5 @@
21
21
  #
22
22
 
23
23
  module JenkinsPipelineBuilder
24
- VERSION = '1.6.0'.freeze
24
+ VERSION = '1.7.0'.freeze
25
25
  end
@@ -1,7 +1,7 @@
1
1
  require File.expand_path('../spec_helper', __FILE__)
2
2
 
3
3
  describe JenkinsPipelineBuilder::ExtensionSet do
4
- subject(:set) { JenkinsPipelineBuilder::ExtensionSet.new('foo'){} }
4
+ subject(:set) { JenkinsPipelineBuilder::ExtensionSet.new('foo') {} }
5
5
 
6
6
  before :each do
7
7
  set.name 'example'
@@ -5,6 +5,7 @@
5
5
  github_site: 'https://github.com'
6
6
  git_org: 'testorg'
7
7
  git_repo_name: 'generator_tests'
8
+ git_branch: 'master'
8
9
 
9
10
  - project:
10
11
  name: PullRequest
@@ -133,6 +133,9 @@ describe JenkinsPipelineBuilder::Generator do
133
133
  JenkinsPipelineBuilder.registry.registry[:job][:scm_params].clear_installed_version
134
134
  end
135
135
 
136
+ let(:pr_master) { { number: 1, base: 'master' } }
137
+ let(:pr_not_master) { { number: 2, base: 'not-master' } }
138
+ let(:open_prs) { [pr_master, pr_not_master] }
136
139
  let(:path) { File.expand_path('../fixtures/generator_tests/pullrequest_pipeline', __FILE__) }
137
140
  it 'produces no errors while creating pipeline PullRequest' do
138
141
  job_name = 'PullRequest'
@@ -145,7 +148,7 @@ describe JenkinsPipelineBuilder::Generator do
145
148
  .and_return(pr_generator)
146
149
  expect(pr_generator).to receive(:delete_closed_prs)
147
150
  expect(pr_generator).to receive(:convert!)
148
- expect(pr_generator).to receive(:open_prs).and_return [1]
151
+ expect(pr_generator).to receive(:open_prs).and_return [pr_master]
149
152
  success = @generator.pull_request(path, job_name)
150
153
  expect(success).to be_truthy
151
154
  end
@@ -161,7 +164,7 @@ describe JenkinsPipelineBuilder::Generator do
161
164
  .and_return(pr_generator)
162
165
  expect(pr_generator).to receive(:delete_closed_prs)
163
166
  expect(pr_generator).to receive(:convert!).twice
164
- expect(pr_generator).to receive(:open_prs).and_return [1, 2]
167
+ expect(pr_generator).to receive(:open_prs).and_return open_prs
165
168
  expect(@generator.pull_request(path, job_name)).to be_truthy
166
169
  end
167
170
 
@@ -175,13 +178,33 @@ describe JenkinsPipelineBuilder::Generator do
175
178
  git_repo_name: 'generator_tests'))
176
179
  .and_return(pr_generator)
177
180
  expect(pr_generator).to receive(:delete_closed_prs)
178
- allow(pr_generator).to receive(:convert!) do |job_collection, pr|
179
- job_collection.defaults[:value][:application_name] = "testapp-PR#{pr}"
181
+ allow(pr_generator).to receive(:convert!) do |job_collection, pr_number|
182
+ job_collection.defaults[:value][:application_name] = "testapp-PR#{pr_number}"
180
183
  end
181
- expect(pr_generator).to receive(:open_prs).and_return [1, 2]
184
+ expect(pr_generator).to receive(:open_prs).and_return open_prs
182
185
  expect(@generator.pull_request(path, job_name)).to be_truthy
183
186
  expect(@generator.job_collection.projects.first[:settings][:application_name]).to eq 'testapp-PR2'
184
187
  end
188
+
189
+ it 'correctly creates jobs only for the base branch' do
190
+ job_name = 'PullRequest'
191
+ pr_generator = double('pr_generator')
192
+ expect(JenkinsPipelineBuilder::PullRequestGenerator).to receive(:new)
193
+ .with(hash_including(
194
+ application_name: 'testapp',
195
+ github_site: 'https://github.com',
196
+ git_org: 'testorg',
197
+ git_repo_name: 'generator_tests'
198
+ )).and_return(pr_generator)
199
+
200
+ expect(pr_generator).to receive(:open_prs).and_return open_prs
201
+ expect(pr_generator).to receive(:delete_closed_prs)
202
+ expect(pr_generator).to receive(:convert!)
203
+ .with(instance_of(JenkinsPipelineBuilder::JobCollection), pr_master[:number])
204
+ .once
205
+
206
+ expect(@generator.pull_request(path, job_name, true)).to be_truthy
207
+ end
185
208
  # Things to check for
186
209
  # Fail - no PR job type found
187
210
  # Encounters failure during build process
@@ -8,7 +8,7 @@ describe JenkinsPipelineBuilder::PullRequestGenerator do
8
8
  let(:git_repo_name) { 'git_repo' }
9
9
  let(:prs) { (1..10).map { |n| "#{application_name}-PR#{n}" } }
10
10
  let(:closed_prs) { (1..6).map { |n| "#{application_name}-PR#{n}" } }
11
- let(:open_prs_json) { (7..10).map { |n| { number: n } }.to_json }
11
+ let(:open_prs_json) { (7..10).map { |n| { number: n, base: { ref: 'master' } } }.to_json }
12
12
  let(:url) { "#{github_site}/api/v3/repos/#{git_org}/#{git_repo_name}/pulls" }
13
13
  let(:params) do
14
14
  {
@@ -91,41 +91,41 @@ describe JenkinsPipelineBuilder::PullRequestGenerator do
91
91
  .to_return(status: 200, body: open_prs_json, headers: {})
92
92
  end
93
93
 
94
+ let(:pr_number) { 8 }
95
+
94
96
  it 'converts the job application name' do
95
97
  collection = job_collection.clone
96
- subject.convert! collection, 8
97
- expect(collection.defaults[:value][:application_name]).to eq "#{application_name}-PR8"
98
+ subject.convert! collection, pr_number
99
+ expect(collection.defaults[:value][:application_name]).to eq "#{application_name}-PR#{pr_number}"
98
100
  end
99
101
 
100
102
  it 'provides the PR number to the job settings' do
101
103
  collection = job_collection.clone
102
- subject.convert! collection, 8
103
- expect(collection.defaults[:value][:pull_request_number]).to eq '8'
104
+ subject.convert! collection, pr_number
105
+ expect(collection.defaults[:value][:pull_request_number]).to eq pr_number.to_s
104
106
  end
105
107
 
106
108
  it 'overrides the git params' do
107
- pr = 8
108
109
  collection = job_collection.clone
109
- subject.convert! collection, pr
110
+ subject.convert! collection, pr_number
110
111
  expect(collection.jobs.first[:value]).to eq(
111
- scm_branch: "origin/pr/#{pr}/head",
112
+ scm_branch: "origin/pr/#{pr_number}/head",
112
113
  scm_params: {
113
- refspec: "refs/pull/#{pr}/head:refs/remotes/origin/pr/#{pr}/head",
114
- changelog_to_branch: { remote: 'origin', branch: "pr/#{pr}/head" },
114
+ refspec: "refs/pull/#{pr_number}/head:refs/remotes/origin/pr/#{pr_number}/head",
115
+ changelog_to_branch: { remote: 'origin', branch: "pr/#{pr_number}/head" },
115
116
  random: 'foo'
116
117
  }
117
118
  )
118
119
  end
119
120
 
120
121
  it 'does not override extra params' do
121
- pr = 8
122
122
  collection = job_collection.clone
123
- subject.convert! collection, pr
123
+ subject.convert! collection, pr_number
124
124
  expect(collection.jobs.first[:value]).to eq(
125
- scm_branch: "origin/pr/#{pr}/head",
125
+ scm_branch: "origin/pr/#{pr_number}/head",
126
126
  scm_params: {
127
- refspec: "refs/pull/#{pr}/head:refs/remotes/origin/pr/#{pr}/head",
128
- changelog_to_branch: { remote: 'origin', branch: "pr/#{pr}/head" },
127
+ refspec: "refs/pull/#{pr_number}/head:refs/remotes/origin/pr/#{pr_number}/head",
128
+ changelog_to_branch: { remote: 'origin', branch: "pr/#{pr_number}/head" },
129
129
  random: 'foo'
130
130
  }
131
131
  )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jenkins_pipeline_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.0
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Moochnick
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-12-21 00:00:00.000000000 Z
12
+ date: 2018-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -485,7 +485,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
485
485
  version: '0'
486
486
  requirements: []
487
487
  rubyforge_project:
488
- rubygems_version: 2.7.3
488
+ rubygems_version: 2.7.4
489
489
  signing_key:
490
490
  specification_version: 4
491
491
  summary: This gem is will boostrap your Jenkins pipelines