jenkins-builder 0.2.1 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4d6b880d68ab2944b3b897fc2e0a4211abaf0649ebb81184f33891ed96f6af3
4
- data.tar.gz: 62622ae525c0c80d90e9385c5748126a5764e64a8ee93e6ea3ab7c05bb895b03
3
+ metadata.gz: 8887c90149d2c3ad5d81be60be732cde64ca6c37634cb68d3f2863220187d409
4
+ data.tar.gz: aae2638d1db504ba89c087ee6a914d06c6b707ef8720db9bcb88fed3ccbdf418
5
5
  SHA512:
6
- metadata.gz: 6b9a70fb62f8d6ad0c27b337af6a0a878506851be9a2206efc32c247ca57b2e97b9e7f8f21a9c5e0c95182c005dab522301aab5f1975e151f163d122d2703fa6
7
- data.tar.gz: 240e6787b99afeb2b8f2d29dd47508a5bd791d23c0273a85230198aaeb3fb6b8dbaaa7d3e7fe2d59836eb5a3c8456d5153ad155294fb73d6fa028a786340c4a1
6
+ metadata.gz: 332a95d45a60d3bdebfaa79f92de9965906d2a813f672d09a5026a9b21bc6a856dccb6cf938b5bafb2128749c767ea7f2707acd234e6b7fcfa88258f1d217e95
7
+ data.tar.gz: 7d4579797431ca988a1b785088e8912c1071988892c35d968429ea28985c015b10a416139bc0ccf8da7df94d32d7a1de4eb9c99c675062b5ede3a9dfe71a1a0f
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jenkins-builder (0.2.1)
4
+ jenkins-builder (0.2.6)
5
+ ferrum (~> 0.9)
5
6
  jenkins_api_client (~> 1.5.3)
6
7
  pastel (~> 0.7.2)
7
8
  security (~> 0.1.3)
@@ -11,10 +12,20 @@ PATH
11
12
  GEM
12
13
  remote: https://gems.ruby-china.com/
13
14
  specs:
15
+ addressable (2.7.0)
16
+ public_suffix (>= 2.0.2, < 5.0)
14
17
  byebug (10.0.2)
18
+ chef-utils (16.3.38)
19
+ cliver (0.3.2)
15
20
  coderay (1.1.2)
21
+ concurrent-ruby (1.1.6)
16
22
  diff-lcs (1.3)
17
- equatable (0.5.0)
23
+ equatable (0.6.1)
24
+ ferrum (0.9)
25
+ addressable (~> 2.5)
26
+ cliver (~> 0.3)
27
+ concurrent-ruby (~> 1.1)
28
+ websocket-driver (>= 0.6, < 0.8)
18
29
  jenkins_api_client (1.5.3)
19
30
  json (>= 1.0)
20
31
  mixlib-shellout (>= 1.1.0)
@@ -22,15 +33,16 @@ GEM
22
33
  socksify (>= 1.7.0)
23
34
  terminal-table (>= 1.4.0)
24
35
  thor (>= 0.16.0)
25
- json (2.3.0)
36
+ json (2.3.1)
26
37
  method_source (0.9.1)
27
38
  mini_portile2 (2.4.0)
28
- mixlib-shellout (2.4.0)
29
- nokogiri (1.10.4)
39
+ mixlib-shellout (3.1.2)
40
+ chef-utils
41
+ nokogiri (1.10.10)
30
42
  mini_portile2 (~> 2.4.0)
31
- pastel (0.7.2)
32
- equatable (~> 0.5.0)
33
- tty-color (~> 0.4.0)
43
+ pastel (0.7.4)
44
+ equatable (~> 0.6)
45
+ tty-color (~> 0.5)
34
46
  pry (0.11.3)
35
47
  coderay (~> 1.1.0)
36
48
  method_source (~> 0.9.0)
@@ -40,6 +52,7 @@ GEM
40
52
  pry-doc (0.13.4)
41
53
  pry (~> 0.11)
42
54
  yard (~> 0.9.11)
55
+ public_suffix (4.0.5)
43
56
  rake (10.5.0)
44
57
  rspec (3.8.0)
45
58
  rspec-core (~> 3.8.0)
@@ -59,12 +72,15 @@ GEM
59
72
  terminal-table (1.8.0)
60
73
  unicode-display_width (~> 1.1, >= 1.1.1)
61
74
  thor (0.20.3)
62
- tty-color (0.4.3)
63
- tty-cursor (0.6.0)
75
+ tty-color (0.5.1)
76
+ tty-cursor (0.7.1)
64
77
  tty-spinner (0.8.0)
65
78
  tty-cursor (>= 0.5.0)
66
- unicode-display_width (1.4.0)
67
- yard (0.9.16)
79
+ unicode-display_width (1.7.0)
80
+ websocket-driver (0.7.3)
81
+ websocket-extensions (>= 0.1.0)
82
+ websocket-extensions (0.1.5)
83
+ yard (0.9.24)
68
84
 
69
85
  PLATFORMS
70
86
  ruby
data/exe/jk CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  require 'jenkins/builder'
4
4
 
5
- Jenkins::Builder::App.new.main(ARGV)
5
+ Jenkins::Builder::CLI.main(ARGV)
@@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
35
35
  spec.add_dependency 'security', '~> 0.1.3'
36
36
  spec.add_dependency 'pastel', '~> 0.7.2'
37
37
  spec.add_dependency 'tty-spinner', '~> 0.8.0'
38
+ spec.add_dependency 'ferrum', '~> 0.9'
38
39
 
39
40
  spec.add_development_dependency "bundler", "~> 2.1.2"
40
41
  spec.add_development_dependency "rake", "~> 10.0"
@@ -1,11 +1,11 @@
1
1
  require 'jenkins/builder/cli'
2
2
  require 'jenkins/builder/config'
3
- require 'jenkins/builder/secret'
4
3
  require 'jenkins_api_client'
5
4
  require 'pastel'
6
5
  require 'tty-spinner'
7
6
  require 'time'
8
7
  require 'cgi'
8
+ require 'ferrum'
9
9
 
10
10
 
11
11
  module JenkinsApi
@@ -24,47 +24,33 @@ module JenkinsApi
24
24
  end
25
25
  end
26
26
 
27
- $is_mac = `uname`.chomp == 'Darwin'
28
-
29
27
  module Jenkins
30
28
  module Builder
31
29
  class App
32
30
 
33
- attr_accessor :config, :secret, :client, :options
31
+ attr_accessor :config, :client, :options
34
32
 
35
- def initialize(options={})
33
+ def initialize(service, options={})
36
34
  @options = options
37
- @config = Jenkins::Builder::Config.new
38
- @secret = ($is_mac ? Jenkins::Builder::Secret.new : @config)
35
+ @service = service
36
+ @config = Jenkins::Builder::Config.new(@service)
39
37
 
40
- if @config.url && @config.username && @secret.password
38
+ if @config.url && @config.username && @config.password
41
39
  @client = JenkinsApi::Client.new(server_url: @config.url,
42
40
  username: @config.username,
43
- password: @secret.password)
41
+ password: @config.password)
44
42
  end
45
43
  end
46
44
 
47
- def main(args)
48
- # validate_os!
49
- validate_fzf!
50
- Jenkins::Builder::CLI.create_alias_commands(@config.aliases || [])
51
- Jenkins::Builder::CLI.start(args)
52
- rescue => e
53
- STDERR.puts(e.message)
54
- end
55
-
56
45
  def setup(options)
57
46
  validate_credentials!(options)
58
47
 
59
48
  config.url = options[:url]
60
49
  config.username = options[:username]
61
50
  config.branches = options[:branches]
51
+ config.password = options[:password]
62
52
  config.save!
63
53
 
64
- secret.username = options[:username]
65
- secret.password = options[:password]
66
- secret.save!
67
-
68
54
  puts 'Credentials setup successfully.'
69
55
  end
70
56
 
@@ -74,7 +60,7 @@ module Jenkins
74
60
  Username: #{@config.username}
75
61
  INFO
76
62
 
77
- puts "Password: #{@secret.password}" if options[:password]
63
+ puts "Password: #{@config.password}" if options[:password]
78
64
  end
79
65
 
80
66
  def create_alias(name, command)
@@ -100,9 +86,19 @@ module Jenkins
100
86
 
101
87
  def build_each(jobs)
102
88
  if @options[:failfast]
103
- jobs.find { |job| build(job).nil? }
89
+ failed_job = jobs.find { |job| build(job).nil? }
90
+ if failed_job
91
+ exit 1
92
+ else
93
+ exit 0
94
+ end
104
95
  else
105
- jobs.each { |job| build(job) }
96
+ results = jobs.map { |job| build(job) }
97
+ if results.any? { |r| r.nil? }
98
+ exit 1
99
+ else
100
+ exit 0
101
+ end
106
102
  end
107
103
  end
108
104
 
@@ -115,11 +111,11 @@ module Jenkins
115
111
 
116
112
  def fetch_all_jobs
117
113
  refresh_jobs_cache unless validate_jobs_cache
118
- @config['jobs-cache']['jobs']
114
+ @config['services'][@service]['jobs-cache']['jobs']
119
115
  end
120
116
 
121
117
  def refresh_jobs_cache
122
- @config['jobs-cache'] = {
118
+ @config['services'][@service]['jobs-cache'] = {
123
119
  'expire' => (Time.now + 86400*30).strftime('%F %T'),
124
120
  'jobs' => all_jobs
125
121
  }
@@ -127,8 +123,8 @@ module Jenkins
127
123
  end
128
124
 
129
125
  def validate_jobs_cache
130
- @config['jobs-cache'] && !@config['jobs-cache'].empty? && \
131
- Time.parse(@config['jobs-cache']['expire']) > Time.now
126
+ @config['services'][@service]['jobs-cache'] && !@config['services'][@service]['jobs-cache'].empty? && \
127
+ Time.parse(@config['services'][@service]['jobs-cache']['expire']) > Time.now
132
128
  end
133
129
 
134
130
  def all_jobs
@@ -152,14 +148,42 @@ module Jenkins
152
148
  msg = "#{job_name} with branch #{branch}"
153
149
  mbranch_param = {name: 'mbranch', value: branch}
154
150
  params = mbranch_param.merge(json: {parameter: mbranch_param}.to_json)
155
- @client.api_post_request("/job/#{job_name}/build?delay=0sec", params, true)
151
+ begin
152
+ @client.api_post_request("/job/#{job_name}/build?delay=0sec", params, true)
153
+ rescue JenkinsApi::Exceptions::ForbiddenWithCrumb => e
154
+ start_build_use_ferrum(job_name, branch)
155
+ end
156
156
  else
157
157
  msg = job_name
158
- @client.api_post_request("/job/#{job_name}/build?delay=0sec")
158
+ begin
159
+ @client.api_post_request("/job/#{job_name}/build?delay=0sec")
160
+ rescue JenkinsApi::Exceptions::ForbiddenWithCrumb => e
161
+ start_build_use_ferrum(job_name, nil)
162
+ end
159
163
  end
160
164
  puts Pastel.new.cyan.bold("\n%s%s %s %s%s\n" % [' '*30, '★ '*5, msg, '★ '*5, ' '*30])
161
165
  end
162
166
 
167
+ def start_build_use_ferrum(job_name, branch)
168
+ browser = Ferrum::Browser.new
169
+ browser.goto("#{config.url}/login")
170
+ username_input = browser.at_css('input[name=j_username]')
171
+ password_input = browser.at_css('input[name=j_password]')
172
+ username_input.focus.type(config.username)
173
+ password_input.focus.type(config.password)
174
+ browser.at_css('input[name=Submit]').click
175
+ if branch
176
+ browser.goto("#{config.url}/job/#{job_name}/build?delay=0sec")
177
+ sleep(2)
178
+ browser.evaluate("document.querySelector('#gitParameterSelect').value = '#{branch}'")
179
+ browser.at_css('#yui-gen1-button').click
180
+ else
181
+ browser.goto("#{config.url}/job/#{job_name}/")
182
+ browser.at_xpath('#tasks a.task-link').click
183
+ end
184
+ browser.quit
185
+ end
186
+
163
187
  def check_and_show_result(job_name, latest_build_no)
164
188
  while (build_no = @client.job.get_current_build_number(job_name)) <= latest_build_no
165
189
  sleep 1
@@ -173,7 +197,6 @@ module Jenkins
173
197
  all_console_output = ''
174
198
 
175
199
  loop do
176
- # require 'pry'; binding.pry;
177
200
  console_output = @client.job.get_console_output(job_name, build_no, 0, 'text')
178
201
  all_console_output = console_output['output']
179
202
  print console_output['output'][printed_size..-1] unless @options[:silent]
@@ -213,16 +236,6 @@ module Jenkins
213
236
 
214
237
  private
215
238
 
216
- def validate_os!
217
- raise 'Darwin is the only supported OS now.' unless `uname`.chomp == 'Darwin'
218
- end
219
-
220
- def validate_fzf!
221
- `fzf --version`
222
- rescue Errno::ENOENT
223
- raise 'Required command fzf is not installed.'
224
- end
225
-
226
239
  def validate_credentials!(options)
227
240
  @client = JenkinsApi::Client.new(server_url: options[:url],
228
241
  username: options[:username],
@@ -7,6 +7,12 @@ module Jenkins
7
7
  class CLI < ::Thor
8
8
 
9
9
  class << self
10
+ def main(args)
11
+ validate_fzf!
12
+ @config = Jenkins::Builder::Config.new
13
+ create_alias_commands(@config.aliases || [])
14
+ start(args)
15
+ end
10
16
  def create_alias_commands(aliases)
11
17
  aliases.each do |name, command|
12
18
  desc "#{name}", "Alias for: `#{command}'"
@@ -15,8 +21,17 @@ module Jenkins
15
21
  end
16
22
  end
17
23
  end
24
+
25
+ def validate_fzf!
26
+ `fzf --version`
27
+ rescue Errno::ENOENT
28
+ raise 'Required command fzf is not installed.'
29
+ end
30
+
18
31
  end
19
32
 
33
+ class_option :service, type: :string, aliases: ['-s'], desc: 'Specify service name'
34
+
20
35
  desc 'setup [-e]', 'Setup URL, username and password, or open config file in an editor when -e specified.'
21
36
  option :edit, type: :boolean, aliases: ['-e'], desc: 'open config file in an editor'
22
37
  def setup
@@ -39,8 +54,8 @@ module Jenkins
39
54
  Jenkins::Builder::App.new.print_info(options)
40
55
  end
41
56
 
42
- desc 'build [-s] [-f] <JOB_IDENTIFIERS>', 'Build jobs'
43
- option :silent, type: :boolean, aliases: ['-s'], desc: 'suppress console output.'
57
+ desc 'build [-q] [-f] <JOB_IDENTIFIERS>', 'Build jobs'
58
+ option :quiet, type: :boolean, aliases: ['-q'], desc: 'suppress console output.'
44
59
  option :failfast, type: :boolean, aliases: ['-f'], desc: 'stop immediately when building fails.'
45
60
  option :version, type: :boolean, aliases: ['-v'], desc: 'Show version.'
46
61
  def build(*jobs)
@@ -48,7 +63,13 @@ module Jenkins
48
63
  puts Jenkins::Builder::VERSION
49
64
  exit
50
65
  end
51
- app = Jenkins::Builder::App.new(options)
66
+ if options.service.nil?
67
+ service = fzf(Config.new().services).first
68
+ exit if service.nil?
69
+ else
70
+ service = options.service
71
+ end
72
+ app = Jenkins::Builder::App.new(service, options)
52
73
  if jobs.empty?
53
74
  jobs = fzf(app.fetch_all_jobs)
54
75
  exit if jobs.empty?
@@ -4,10 +4,11 @@ module Jenkins
4
4
  module Builder
5
5
  class Config
6
6
 
7
- attr_accessor :file, :config
7
+ attr_accessor :file, :config, :service
8
8
 
9
- def initialize
9
+ def initialize(service = nil)
10
10
 
11
+ @service = service
11
12
  @file = File.expand_path('~/.jenkins-builder.yaml')
12
13
 
13
14
  if File.exist?(@file)
@@ -25,20 +26,24 @@ module Jenkins
25
26
  @config[k] = v
26
27
  end
27
28
 
29
+ def services
30
+ @config['services'].keys
31
+ end
32
+
28
33
  def username
29
- @config['username']
34
+ @config['services'][@service]['username']
30
35
  end
31
36
 
32
37
  def username=(name)
33
- @config['username'] = name
38
+ @config['services'][@service]['username'] = name
34
39
  end
35
40
 
36
41
  def password
37
- @config['password']
42
+ @config['services'][@service]['password']
38
43
  end
39
44
 
40
45
  def password=(passwd)
41
- @config['password'] = passwd
46
+ @config['services'][@service]['password'] = passwd
42
47
  end
43
48
 
44
49
  def aliases
@@ -50,11 +55,11 @@ module Jenkins
50
55
  end
51
56
 
52
57
  def url
53
- @config['url']
58
+ @config['services'][@service]['url']
54
59
  end
55
60
 
56
61
  def url=(url)
57
- @config['url'] = url
62
+ @config['services'][@service]['url'] = url
58
63
  end
59
64
 
60
65
  def branches
@@ -66,7 +71,7 @@ module Jenkins
66
71
  end
67
72
 
68
73
  def hooks_of(job)
69
- hooks = @config['hooks'] && @config['hooks'][job]
74
+ hooks = @config['services'][@service]['hooks'] && @config['services'][@service]['hooks'][job]
70
75
  hooks = [hooks] if hooks.is_a?(String)
71
76
  hooks
72
77
  end
@@ -1,5 +1,5 @@
1
1
  module Jenkins
2
2
  module Builder
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.6"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jenkins-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-26 00:00:00.000000000 Z
11
+ date: 2020-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 0.8.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: ferrum
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.9'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: bundler
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -196,7 +210,7 @@ licenses:
196
210
  - MIT
197
211
  metadata:
198
212
  allowed_push_host: https://rubygems.org
199
- post_install_message:
213
+ post_install_message:
200
214
  rdoc_options: []
201
215
  require_paths:
202
216
  - lib
@@ -212,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
226
  version: '0'
213
227
  requirements: []
214
228
  rubygems_version: 3.1.2
215
- signing_key:
229
+ signing_key:
216
230
  specification_version: 4
217
231
  summary: Build Jenkins Jobs
218
232
  test_files: []