jenkins_pipeline_builder 0.14.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDBiZGQ1ODhjMWU2ZTE0Mjk0MDM5ZjIzMmUyYzMwOTcyMDc2ODJkMA==
4
+ MjAwOGQ5NDM1MmMyYTkzNjExNzViMWYyMzRlMzFiYTYwMTc0ZTU3MA==
5
5
  data.tar.gz: !binary |-
6
- OTExNDQ5YjYyYTlhYzUxNjgxY2QzMDI4ODkyZDM5NjQ4MDI1NDg1Mw==
6
+ MzlmOGIxNmRhYWFiYjYyMDUxNmIzNzZjOGFhZTIxNmUxNmE2NjhmMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZmRkMmE4OTA3MDA3YjZkM2Q2NDJkZGFhMTA1Njc0MGQ1N2E2NTBhYzgzYWM1
10
- Mjc5ZjMzNjVhMDJjODI3ZjQ4ZDc4MTdjN2VlNjI0NjlmN2M1NDEyNWY2OWI4
11
- YmMzMDJjNTZmYmIzNTk4NzNkMjc5MWRlMGE2ZDFlNTkxOWVjMGQ=
9
+ YzMwNWQzZjliNGFkZTY5ZjY5NzgzMzY1MTdmZThjZjI5MjhkNDAwNzVhNzE3
10
+ YTI2ZGY5ZWVhNWRhMDliOWVmNGE1NGIwNDdhYjEwNTdiNjM5MGVmYzExOTY5
11
+ YmU1NmJmZGYxZjlhMTJmZWRlNTU2OWY0ZGQ1MWVjOWY5YjI1OTE=
12
12
  data.tar.gz: !binary |-
13
- ZjEwYjc1ODg1MzliMzNkYzkwM2M2ZDU4MGJhNjc4M2EwMTlmMWEzYTNlMmRi
14
- Zjk3ZTM3YTlkYzBmOThjOWI1ODJhNzgyMzFhNGI2OWM3YTk5NWY3NWY5OThh
15
- Y2M4ZWRmOWQ3YTA2MWJjMjk3OWZhZDkwZGI0YmJlZTgxYTQyOWY=
13
+ ZGMwNjliZDJiNWQ4YWJlZTlmOWQxZjVlMTQxNjQwOTgwYjc5YTUyMDA2NWU1
14
+ NDdlZDgzMjg5YmJhY2U1NTY2NWM3YWI1NzhhZmU5OGQ1Y2M3ZTBjOTQ1NjU0
15
+ MmY5N2E5Zjc5NzNjMTExYmIwYjNkOWM4NDcwZGIzOTU2NGFlNjE=
data/.simplecov CHANGED
@@ -3,10 +3,10 @@ SimpleCov.profiles.define 'spec' do
3
3
  add_group 'jenkins_pipeline_builder', '/lib/'
4
4
  add_filter 'spec'
5
5
  coverage_dir 'out/coverage'
6
- formatter SimpleCov::Formatter::MultiFormatter[
6
+ formatter SimpleCov::Formatter::MultiFormatter.new([
7
7
  SimpleCov::Formatter::Console,
8
8
  SimpleCov::Formatter::RcovFormatter,
9
- ]
9
+ ])
10
10
  end
11
11
 
12
12
  class SimpleCov::Formatter::Console
data/circle.yml ADDED
@@ -0,0 +1,8 @@
1
+ machine:
2
+ timezone:
3
+ America/New_York
4
+
5
+ test:
6
+ override:
7
+ - bundle exec rake
8
+
@@ -34,6 +34,12 @@ module JenkinsPipelineBuilder
34
34
  # This is the helper class that sets up the credentials from the command
35
35
  # line parameters given and initializes the Jenkins Pipeline Builder.
36
36
  class Helper
37
+ class << self
38
+ attr_accessor :jenkins_api_creds
39
+ end
40
+
41
+ DEFAULT_FILE_FORMATS = %w(rb json yaml).freeze
42
+
37
43
  # Sets up the credentials and initializes the Jenkins Pipeline Builder
38
44
  #
39
45
  # @param [Hash] options Options obtained from the command line
@@ -41,23 +47,24 @@ module JenkinsPipelineBuilder
41
47
  # @return [JenkinsPipelineBuilder::Generator] A new Client object
42
48
  #
43
49
  def self.setup(options)
44
- creds = process_creds options
50
+ process_creds options
45
51
 
46
- JenkinsPipelineBuilder.credentials = creds
52
+ JenkinsPipelineBuilder.credentials = jenkins_api_creds
47
53
  generator = JenkinsPipelineBuilder.generator
48
54
  JenkinsPipelineBuilder.debug! if options[:debug]
49
55
  generator
50
56
  end
51
57
 
52
58
  def self.process_creds(options)
53
- if valid_cli_creds? options
59
+ default_file = find_default_file
60
+ if options[:debug]
61
+ self.jenkins_api_creds = { username: :foo, password: :bar, server_ip: :baz }
62
+ elsif valid_cli_creds? options
54
63
  process_cli_creds(options)
55
64
  elsif options[:creds_file]
56
65
  process_creds_file options[:creds_file]
57
- elsif File.exist?("#{ENV['HOME']}/.jenkins_api_client/login.yml")
58
- YAML.load_file(File.expand_path("#{ENV['HOME']}/.jenkins_api_client/login.yml", __FILE__))
59
- elsif options[:debug]
60
- { username: :foo, password: :bar, server_ip: :baz }
66
+ elsif default_file
67
+ process_creds_file default_file
61
68
  else
62
69
  msg = 'Credentials are not set. Please pass them as parameters or'
63
70
  msg << ' set them in the default credentials file'
@@ -71,23 +78,45 @@ module JenkinsPipelineBuilder
71
78
  end
72
79
 
73
80
  def self.process_creds_file(file)
74
- return JSON.parse(IO.read(File.expand_path(file))) if file.end_with? 'json'
75
- YAML.load_file(File.expand_path(file))
81
+ return load File.expand_path(file) if file.end_with? 'rb'
82
+ return self.jenkins_api_creds = JSON.parse(IO.read(File.expand_path(file))) if file.end_with? 'json'
83
+ self.jenkins_api_creds = YAML.load_file(File.expand_path(file))
76
84
  end
77
85
 
78
86
  def self.process_cli_creds(options)
79
- creds = {}.with_indifferent_access.merge options
80
- if creds[:server] =~ Resolv::AddressRegex
81
- creds[:server_ip] = creds.delete :server
82
- elsif creds[:server] =~ URI.regexp
83
- creds[:server_url] = creds.delete :server
87
+ self.jenkins_api_creds = {}.with_indifferent_access.merge options
88
+ if jenkins_api_creds[:server] =~ Resolv::AddressRegex
89
+ jenkins_api_creds[:server_ip] = jenkins_api_creds.delete :server
90
+ elsif jenkins_api_creds[:server] =~ URI.regexp
91
+ jenkins_api_creds[:server_url] = jenkins_api_creds.delete :server
84
92
  else
85
- msg = "server given (#{creds[:server]}) is neither a URL nor an IP."
93
+ msg = "server given (#{jenkins_api_creds[:server]}) is neither a URL nor an IP."
86
94
  msg << ' Please pass either a valid IP address or valid URI'
87
95
  $stderr.puts msg
88
96
  exit 1
89
97
  end
90
- creds
98
+ end
99
+
100
+ private_class_method
101
+
102
+ def self.find_default_file
103
+ default_file_name = "#{ENV['HOME']}/.jenkins_api_client/login"
104
+
105
+ found_suffix = nil
106
+ DEFAULT_FILE_FORMATS.each do |suffix|
107
+ next unless File.exist?("#{default_file_name}.#{suffix}")
108
+ if !found_suffix
109
+ found_suffix = suffix
110
+ else
111
+ logger.warn "Multiple default files found! Using '#{default_file_name}.#{found_suffix}' but \
112
+ '#{default_file_name}.#{suffix}' found."
113
+ end
114
+ end
115
+ "#{ENV['HOME']}/.jenkins_api_client/login.#{found_suffix}" if found_suffix
116
+ end
117
+
118
+ def self.logger
119
+ JenkinsPipelineBuilder.logger
91
120
  end
92
121
  end
93
122
  end
@@ -21,5 +21,5 @@
21
21
  #
22
22
 
23
23
  module JenkinsPipelineBuilder
24
- VERSION = '0.14.0'.freeze
24
+ VERSION = '0.15.0'.freeze
25
25
  end
@@ -2,6 +2,8 @@ require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
3
  describe JenkinsPipelineBuilder::CLI::Helper do
4
4
  context '#setup' do
5
+ let(:creds_file_base) { 'spec/lib/jenkins_pipeline_builder/fixtures/sample_creds' }
6
+
5
7
  context 'username and password given' do
6
8
  let(:options) do
7
9
  {
@@ -46,8 +48,6 @@ describe JenkinsPipelineBuilder::CLI::Helper do
46
48
  end
47
49
 
48
50
  context 'credential file given' do
49
- let(:creds_file_base) { 'spec/lib/jenkins_pipeline_builder/fixtures/sample_creds' }
50
-
51
51
  let(:expected_options) do
52
52
  {
53
53
  'username' => 'username',
@@ -61,6 +61,7 @@ describe JenkinsPipelineBuilder::CLI::Helper do
61
61
  options = {
62
62
  creds_file: "#{creds_file_base}.yaml"
63
63
  }
64
+ expect(YAML).to receive(:load_file).and_call_original
64
65
  expect(JenkinsPipelineBuilder).to receive(:credentials=).with(expected_options)
65
66
  described_class.setup(options)
66
67
  end
@@ -69,6 +70,16 @@ describe JenkinsPipelineBuilder::CLI::Helper do
69
70
  options = {
70
71
  creds_file: "#{creds_file_base}.json"
71
72
  }
73
+ expect(JSON).to receive(:parse).and_call_original
74
+ expect(JenkinsPipelineBuilder).to receive(:credentials=).with(expected_options)
75
+ described_class.setup(options)
76
+ end
77
+
78
+ it 'should handle credentials passed as a ruby file' do
79
+ options = {
80
+ creds_file: "#{creds_file_base}.rb"
81
+ }
82
+ expect(File).to receive(:expand_path).with(options[:creds_file]).and_call_original
72
83
  expect(JenkinsPipelineBuilder).to receive(:credentials=).with(expected_options)
73
84
  described_class.setup(options)
74
85
  end
@@ -86,10 +97,79 @@ describe JenkinsPipelineBuilder::CLI::Helper do
86
97
  end
87
98
 
88
99
  it 'should puts and error to stdout and exit if no credentials are passed' do
89
- expect(File).to receive(:exist?).and_return(false)
100
+ allow(File).to receive(:exist?).and_return(false)
90
101
  expect($stderr).to receive(:puts).with(/Credentials are not set/)
91
102
  expect { described_class.setup({}) }.to raise_error(SystemExit, 'exit')
92
103
  end
93
104
  end
105
+
106
+ context 'default credential files' do
107
+ let(:default_creds_base) { '/foo/.jenkins_api_client/login' }
108
+
109
+ before(:each) do
110
+ allow(ENV).to receive(:[]).with('HOME').and_return '/foo'
111
+ end
112
+
113
+ it 'checks for all 3 supported formats in order' do
114
+ expect(File).to receive(:exist?).with("#{default_creds_base}.rb")
115
+ expect(File).to receive(:exist?).with("#{default_creds_base}.json")
116
+ expect(File).to receive(:exist?).with("#{default_creds_base}.yaml")
117
+ expect { described_class.setup({}) }.to raise_error(SystemExit, 'exit')
118
+ end
119
+
120
+ it 'loads the default ruby file' do
121
+ expect(File).to receive(:exist?).with("#{default_creds_base}.rb").and_return true
122
+ expect(File).to receive(:exist?).with("#{default_creds_base}.json").and_return false
123
+ expect(File).to receive(:exist?).with("#{default_creds_base}.yaml").and_return false
124
+ expect(File).to receive(:expand_path).with("#{default_creds_base}.rb").and_return "#{creds_file_base}.rb"
125
+ described_class.setup({})
126
+ end
127
+
128
+ it 'loads the default json file' do
129
+ expect(File).to receive(:exist?).with("#{default_creds_base}.rb").and_return false
130
+ expect(File).to receive(:exist?).with("#{default_creds_base}.json").and_return true
131
+ expect(File).to receive(:exist?).with("#{default_creds_base}.yaml").and_return false
132
+ expect(File).to receive(:expand_path).with("#{default_creds_base}.json").and_return "#{creds_file_base}.json"
133
+ expect(JSON).to receive(:parse).and_call_original
134
+ described_class.setup({})
135
+ end
136
+
137
+ it 'loads the default yaml file' do
138
+ expect(File).to receive(:exist?).with("#{default_creds_base}.rb").and_return false
139
+ expect(File).to receive(:exist?).with("#{default_creds_base}.json").and_return false
140
+ expect(File).to receive(:exist?).with("#{default_creds_base}.yaml").and_return true
141
+ expect(File).to receive(:expand_path).with("#{default_creds_base}.yaml").and_return "#{creds_file_base}.yaml"
142
+ expect(YAML).to receive(:load_file).and_call_original
143
+ described_class.setup({})
144
+ end
145
+
146
+ it 'warns if both ruby and yaml' do
147
+ used_suffix = 'rb'
148
+ found_suffix = 'yaml'
149
+ expect(File).to receive(:exist?).with("#{default_creds_base}.rb").and_return true
150
+ expect(File).to receive(:exist?).with("#{default_creds_base}.json").and_return false
151
+ expect(File).to receive(:exist?).with("#{default_creds_base}.yaml").and_return true
152
+ expect(JenkinsPipelineBuilder.logger).to receive(:warn)
153
+ .with(/'#{default_creds_base}\.#{used_suffix}' but '#{default_creds_base}\.#{found_suffix}' found\./)
154
+ .and_return true
155
+ expect(File).to receive(:expand_path)
156
+ .with("#{default_creds_base}.#{used_suffix}").and_return "#{creds_file_base}.#{used_suffix}"
157
+ described_class.setup({})
158
+ end
159
+
160
+ it 'warns if both ruby and json' do
161
+ used_suffix = 'rb'
162
+ found_suffix = 'json'
163
+ expect(File).to receive(:exist?).with("#{default_creds_base}.rb").and_return true
164
+ expect(File).to receive(:exist?).with("#{default_creds_base}.json").and_return true
165
+ expect(File).to receive(:exist?).with("#{default_creds_base}.yaml").and_return false
166
+ expect(JenkinsPipelineBuilder.logger).to receive(:warn)
167
+ .with(/'#{default_creds_base}\.#{used_suffix}' but '#{default_creds_base}\.#{found_suffix}' found\./)
168
+ .and_return true
169
+ expect(File).to receive(:expand_path)
170
+ .with("#{default_creds_base}.#{used_suffix}").and_return "#{creds_file_base}.#{used_suffix}"
171
+ described_class.setup({})
172
+ end
173
+ end
94
174
  end
95
175
  end
@@ -0,0 +1,6 @@
1
+ JenkinsPipelineBuilder::CLI::Helper.jenkins_api_creds = {
2
+ 'username' => 'username',
3
+ 'password' => 'password',
4
+ 'server_url' => 'https://localhost.localdomain',
5
+ 'server_port' => 8080
6
+ }
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: 0.14.0
4
+ version: 0.15.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: 2016-02-05 00:00:00.000000000 Z
12
+ date: 2016-04-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -331,6 +331,7 @@ files:
331
331
  - README.md
332
332
  - Rakefile
333
333
  - bin/generate
334
+ - circle.yml
334
335
  - commit_build.sh
335
336
  - config/login.yml
336
337
  - example/pipeline/.ruby-version
@@ -426,6 +427,7 @@ files:
426
427
  - spec/lib/jenkins_pipeline_builder/fixtures/job_collection/extensions/extension.rb
427
428
  - spec/lib/jenkins_pipeline_builder/fixtures/job_collection/extensions/helpers/my_test_thing_helper.rb
428
429
  - spec/lib/jenkins_pipeline_builder/fixtures/sample_creds.json
430
+ - spec/lib/jenkins_pipeline_builder/fixtures/sample_creds.rb
429
431
  - spec/lib/jenkins_pipeline_builder/fixtures/sample_creds.yaml
430
432
  - spec/lib/jenkins_pipeline_builder/fixtures/view_test/duplicate_view.yaml
431
433
  - spec/lib/jenkins_pipeline_builder/fixtures/view_test/parent_view.yaml
@@ -512,6 +514,7 @@ test_files:
512
514
  - spec/lib/jenkins_pipeline_builder/fixtures/job_collection/extensions/extension.rb
513
515
  - spec/lib/jenkins_pipeline_builder/fixtures/job_collection/extensions/helpers/my_test_thing_helper.rb
514
516
  - spec/lib/jenkins_pipeline_builder/fixtures/sample_creds.json
517
+ - spec/lib/jenkins_pipeline_builder/fixtures/sample_creds.rb
515
518
  - spec/lib/jenkins_pipeline_builder/fixtures/sample_creds.yaml
516
519
  - spec/lib/jenkins_pipeline_builder/fixtures/view_test/duplicate_view.yaml
517
520
  - spec/lib/jenkins_pipeline_builder/fixtures/view_test/parent_view.yaml