Created
June 7, 2013 02:16
-
-
Save mechamogera/5726649 to your computer and use it in GitHub Desktop.
Jenkinsで勤務時間外に失敗したジョブを調べるrubyスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source "https://rubygems.org" | |
gem "holidays" | |
gem "trollop" | |
gem "rest-client" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'holidays' | |
require 'trollop' | |
require 'rest-client' | |
require 'json' | |
require 'pp' | |
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 | |
private | |
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 = Time.local(start_date.year, start_date.mon, start_date.mday, 17, 45) | |
end_time = Time.local(end_date.year, end_date.mon, end_date.mday, 9) | |
return :is_over if time < start_time | |
return start_time < time && time < end_time | |
end | |
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 | |
end | |
opts = Trollop::options do | |
opt :jenkins_job_url, 'jenkins job url', :type => :string | |
opt :date, 'target date', :type => :date, :default => Date.today | |
opt :no_proxy, 'do not use proxy', :default => false | |
end | |
Trollop::die :jenkins_job_url, "must exist" unless opts[:jenkins_job_url] | |
JenkinsJobChecker.new(opts).check(opts[:jenkins_job_url], opts[:date]) do |data| | |
puts "#{data[:result]},#{data[:url]},#{data[:timestamp]}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment