forked from cloudfoundry-attic/cf-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_travis.rb
executable file
·89 lines (69 loc) · 2.45 KB
/
check_travis.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env ruby
require 'net/http'
require 'json'
require 'optparse'
class String
COLORS = { yellow: "\e[0;33m", green: "\e[0;32m", red: "\e[0;31m", blue: "\e[0;34m", reset: "\e[0m" }.freeze
def self.color=(color); @color = color; end
def self.color?; !!@color; end
def color(color)
String.color? ? "#{COLORS[color]}#{self}#{COLORS[:reset]}" : self
end
end
class TravisBuild
TRAVIS_URL = "https://api.travis-ci.org/repos/%s/builds".freeze
def initialize(shas, color = true)
@shas = shas
remote = `git remote -v`.match(/github.com[\/:]([^\s]+?).git/)
@repo = remote && remote[1]
String.color = color
end
def log_with_travis_status
output = [travis? ? "https://travis-ci.org/#{@repo}".color(:yellow) : "No Travis".color(:blue)]
`git log #{@shas} --oneline`.split("\n").each do |commit|
sha, message = commit.split(" ", 2)
output << "\n#{sha.color(:yellow)} #{message}#{travis_status(sha) if travis?}"
end
puts output.join
end
def log_sorted_by_author_with_travis_status
output = ["Bump #{@repo}:\n".color(:blue)]
commits_by_author = Hash.new("")
`git log #{@shas} --pretty=format:'%h %an: %s'`.split("\n").each do |commit|
sha, message = commit.split(" ", 2)
author, message = message.split(":", 2)
commits_by_author[author] += "\n #{message}#{travis_status(sha) if travis?}"
end
commits_by_author.each { |author, commit| output << " #{author.color(:yellow)}:#{commit}\n" }
puts output.join
end
private
def travis_json
@travis_json ||= begin
uri = URI(TRAVIS_URL % @repo)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
JSON.parse(http.request(Net::HTTP::Get.new(uri.path)).body)
end
end
def travis?
@travis ||= @repo && travis_json && travis_json.any?
end
def travis_status(sha)
build = travis_json.find { |build| build["commit"].start_with?(sha) }
message = case build && build["result"]
when 0; "Travis Success: #{build_url(build)}".color(:green)
when 1; "Travis Failed: #{build_url(build)}".color(:red)
else; "Travis #{build ? "#{build["state"]}: #{build_url(build)}" : "Unknown"}".color(:blue)
end
" ( #{message} )"
end
def build_url(build)
"https://travis-ci.org/#{@repo}/builds/#{build["id"]}"
end
end
if __FILE__ == $PROGRAM_NAME
travis = TravisBuild.new(ARGV[0], true)
travis.log_with_travis_status
end