jenkins-builder 0.2.0 → 0.2.5

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: b745c7d55f5fb2257c81035e59a6c92fd10cbb608b2e273bf5f155de4ffca550
4
- data.tar.gz: 69aae1ed249d03a1ea767258746d76df5f8b7c4b0ef2daf31254603732eead11
3
+ metadata.gz: b3d8b25fd6d3fd82e3fb04b58038f3d536d907510def01ae42ecb6d09ad8bb9b
4
+ data.tar.gz: 9e623633692b17797b3ac2fc3ee425f7b6da70ca1e7ae9acd119d00cfb2e34ce
5
5
  SHA512:
6
- metadata.gz: 04e55dfc6976959a3970c53098b7b24744023bd335ecf69cd7d5740a7ea3583e4eae02926950d2f83ebe2236cee701b27d83dc7e263522b9ef6af556f4cfb9a9
7
- data.tar.gz: ed860533a48ceb4d54a0f226a143e76094240e1b4c658c5a285ef6854c5e571f09fb7b976a99d9a7c415171a144b2b1e259d57eb7e02490a21ab9a57094135f2
6
+ metadata.gz: 562efc35d87d9e2e3786ee18f994e2445c5927d9349da8c47edbe53a902010345756e2b986d98e03ed287fbdb8b9a6dafe77292ee3fb2b6dce17c0785876db1a
7
+ data.tar.gz: 17350f8058207c60c647767ac91392777a756111cf401b6d9ad4ab8e91fc02374771c469c05057e18cdc3eea47ff6a52f82971ee97fe484fc68cf88baccd6eae
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jenkins-builder (0.1.6)
4
+ jenkins-builder (0.2.5)
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.1.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.3)
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,18 +72,21 @@ 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
71
87
 
72
88
  DEPENDENCIES
73
- bundler (~> 1.16)
89
+ bundler (~> 2.1.2)
74
90
  jenkins-builder!
75
91
  pry (~> 0.11.3)
76
92
  pry-byebug (~> 3.6.0)
@@ -79,4 +95,4 @@ DEPENDENCIES
79
95
  rspec (~> 3.0)
80
96
 
81
97
  BUNDLED WITH
82
- 1.17.2
98
+ 2.1.2
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,8 +35,9 @@ 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
- spec.add_development_dependency "bundler", "~> 1.16"
40
+ spec.add_development_dependency "bundler", "~> 2.1.2"
40
41
  spec.add_development_dependency "rake", "~> 10.0"
41
42
  spec.add_development_dependency "rspec", "~> 3.0"
42
43
  spec.add_development_dependency "pry", "~> 0.11.3"
@@ -1,52 +1,56 @@
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'
7
+ require 'cgi'
8
+ require 'ferrum'
8
9
 
9
- $is_mac = `uname`.chomp == 'Darwin'
10
+
11
+ module JenkinsApi
12
+ module UriHelper
13
+ # Encode a string for using in the query part of an URL
14
+ #
15
+ def form_encode(string)
16
+ URI.encode_www_form_component string.encode(Encoding::UTF_8)
17
+ end
18
+
19
+ # Encode a string for use in the hiearchical part of an URL
20
+ #
21
+ def path_encode(path)
22
+ CGI.escape(path.encode(Encoding::UTF_8))
23
+ end
24
+ end
25
+ end
10
26
 
11
27
  module Jenkins
12
28
  module Builder
13
29
  class App
14
30
 
15
- attr_accessor :config, :secret, :client, :options
31
+ attr_accessor :config, :client, :options
16
32
 
17
- def initialize(options={})
33
+ def initialize(service, options={})
18
34
  @options = options
19
- @config = Jenkins::Builder::Config.new
20
- @secret = ($is_mac ? Jenkins::Builder::Secret.new : @config)
35
+ @service = service
36
+ @config = Jenkins::Builder::Config.new(@service)
21
37
 
22
- if @config.url && @config.username && @secret.password
38
+ if @config.url && @config.username && @config.password
23
39
  @client = JenkinsApi::Client.new(server_url: @config.url,
24
40
  username: @config.username,
25
- password: @secret.password)
41
+ password: @config.password)
26
42
  end
27
43
  end
28
44
 
29
- def main(args)
30
- # validate_os!
31
- validate_fzf!
32
- Jenkins::Builder::CLI.create_alias_commands(@config.aliases || [])
33
- Jenkins::Builder::CLI.start(args)
34
- rescue => e
35
- STDERR.puts(e.message)
36
- end
37
-
38
45
  def setup(options)
39
46
  validate_credentials!(options)
40
47
 
41
48
  config.url = options[:url]
42
49
  config.username = options[:username]
43
50
  config.branches = options[:branches]
51
+ config.password = options[:password]
44
52
  config.save!
45
53
 
46
- secret.username = options[:username]
47
- secret.password = options[:password]
48
- secret.save!
49
-
50
54
  puts 'Credentials setup successfully.'
51
55
  end
52
56
 
@@ -56,7 +60,7 @@ module Jenkins
56
60
  Username: #{@config.username}
57
61
  INFO
58
62
 
59
- puts "Password: #{@secret.password}" if options[:password]
63
+ puts "Password: #{@config.password}" if options[:password]
60
64
  end
61
65
 
62
66
  def create_alias(name, command)
@@ -82,9 +86,19 @@ module Jenkins
82
86
 
83
87
  def build_each(jobs)
84
88
  if @options[:failfast]
85
- 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
86
95
  else
87
- 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
88
102
  end
89
103
  end
90
104
 
@@ -97,11 +111,11 @@ module Jenkins
97
111
 
98
112
  def fetch_all_jobs
99
113
  refresh_jobs_cache unless validate_jobs_cache
100
- @config['jobs-cache']['jobs']
114
+ @config['services'][@service]['jobs-cache']['jobs']
101
115
  end
102
116
 
103
117
  def refresh_jobs_cache
104
- @config['jobs-cache'] = {
118
+ @config['services'][@service]['jobs-cache'] = {
105
119
  'expire' => (Time.now + 86400*30).strftime('%F %T'),
106
120
  'jobs' => all_jobs
107
121
  }
@@ -109,8 +123,8 @@ module Jenkins
109
123
  end
110
124
 
111
125
  def validate_jobs_cache
112
- @config['jobs-cache'] && !@config['jobs-cache'].empty? && \
113
- 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
114
128
  end
115
129
 
116
130
  def all_jobs
@@ -134,14 +148,42 @@ module Jenkins
134
148
  msg = "#{job_name} with branch #{branch}"
135
149
  mbranch_param = {name: 'mbranch', value: branch}
136
150
  params = mbranch_param.merge(json: {parameter: mbranch_param}.to_json)
137
- @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
138
156
  else
139
157
  msg = job_name
140
- @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
141
163
  end
142
164
  puts Pastel.new.cyan.bold("\n%s%s %s %s%s\n" % [' '*30, '★ '*5, msg, '★ '*5, ' '*30])
143
165
  end
144
166
 
167
+ def start_build_use_ferrum(job_name, branch)
168
+ browser = Ferrum::Browser.new(headless: false)
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
+
145
187
  def check_and_show_result(job_name, latest_build_no)
146
188
  while (build_no = @client.job.get_current_build_number(job_name)) <= latest_build_no
147
189
  sleep 1
@@ -155,7 +197,6 @@ module Jenkins
155
197
  all_console_output = ''
156
198
 
157
199
  loop do
158
- # require 'pry'; binding.pry;
159
200
  console_output = @client.job.get_console_output(job_name, build_no, 0, 'text')
160
201
  all_console_output = console_output['output']
161
202
  print console_output['output'][printed_size..-1] unless @options[:silent]
@@ -195,16 +236,6 @@ module Jenkins
195
236
 
196
237
  private
197
238
 
198
- def validate_os!
199
- raise 'Darwin is the only supported OS now.' unless `uname`.chomp == 'Darwin'
200
- end
201
-
202
- def validate_fzf!
203
- `fzf --version`
204
- rescue Errno::ENOENT
205
- raise 'Required command fzf is not installed.'
206
- end
207
-
208
239
  def validate_credentials!(options)
209
240
  @client = JenkinsApi::Client.new(server_url: options[:url],
210
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.0"
3
+ VERSION = "0.2.5"
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.0
4
+ version: 0.2.5
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: 2019-06-13 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,20 +80,34 @@ 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
86
100
  requirements:
87
101
  - - "~>"
88
102
  - !ruby/object:Gem::Version
89
- version: '1.16'
103
+ version: 2.1.2
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
- version: '1.16'
110
+ version: 2.1.2
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rake
99
113
  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
@@ -211,8 +225,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
225
  - !ruby/object:Gem::Version
212
226
  version: '0'
213
227
  requirements: []
214
- rubygems_version: 3.0.3
215
- signing_key:
228
+ rubygems_version: 3.1.2
229
+ signing_key:
216
230
  specification_version: 4
217
231
  summary: Build Jenkins Jobs
218
232
  test_files: []