Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mechamogera
Last active December 20, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mechamogera/6152728 to your computer and use it in GitHub Desktop.
Save mechamogera/6152728 to your computer and use it in GitHub Desktop.
Jenkinsの休み中のジョブ結果確認ツール

Usage

  • Example
$ bundle exec ruby jenkins_job_check.rb --no-proxy -j http://example.com/job/hoge
FAILURE , http://example.com/job/hoge/1373/ , 2013-08-02 20:10:45 +0900
# A sample Gemfile
source "https://rubygems.org"
gem "holidays"
gem "trollop"
gem "rest-client"
require 'holidays'
require 'trollop'
require 'rest-client'
require 'json'
class JenkinsJobChecker
def initialize(opts = {})
RestClient.proxy = ENV['HTTP_PROXY'] || ENV['http_proxy'] unless opts[:no_proxy]
end
def check(job_url, date, &block)
start_date = calc_last_working_date(date)
result = jenkins_request(job_url)
result["builds"].each do |build|
build_data = parse_build(jenkins_request(build["url"]))
res = check_time(start_date, date, build_data[:timestamp])
break if res == :is_over
block.call build_data if res && build_data[:result] != "SUCCESS"
end
end
def check_term(date)
start_date = calc_last_working_date(date)
return { :start_time => start_time(start_date), :end_time => end_time(date) }
end
private
def calc_last_working_date(date)
last_date = date - 1
while last_date.holiday?(:jp) || last_date.cwday == 0 || last_date.cwday == 6 || last_date.cwday == 7
last_date = last_date - 1
end
return last_date
end
def start_time(start_date)
return Time.local(start_date.year, start_date.mon, start_date.mday, 17, 45)
end
def end_time(end_date)
return Time.local(end_date.year, end_date.mon, end_date.mday, 9)
end
def jenkins_request(url)
JSON.parse(RestClient.get("#{url}/api/json"))
end
def parse_build(data)
{
:timestamp => Time.at(data["timestamp"].to_i / 1000),
:result => data["result"],
:url => data["url"]
}
end
def check_time(start_date, end_date, time)
start_time = start_time(start_date)
end_time = end_time(end_date)
return :is_over if time < start_time
return start_time < time && time < end_time
end
end
opts = Trollop::options do
opt :jenkins_job_url, 'jenkins job url', :type => :string, :required => true
opt :date, 'target date', :type => :date, :default => Date.today
opt :no_proxy, 'do not use proxy', :default => false
opt :output_check_term, 'display check term', :default => false
end
if opts[:output_check_term]
term = JenkinsJobChecker.new(opts).check_term(opts[:date])
puts "#{term[:start_time]} - #{term[:end_time]}"
exit
end
return_code = 0
JenkinsJobChecker.new(opts).check(opts[:jenkins_job_url], opts[:date]) do |data|
puts "#{data[:result]},#{data[:url]},#{data[:timestamp]}"
return_code = 1
end
exit(return_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment