-
Notifications
You must be signed in to change notification settings - Fork 2
/
circleci.1m.rb
executable file
·145 lines (118 loc) · 6.48 KB
/
circleci.1m.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/ruby
#
# <bitbar.title>CircleCI monitor</bitbar.title>
# <bitbar.version>v0.1</bitbar.version>
# <bitbar.author>Raphael H. Doehring</bitbar.author>
# <bitbar.author.github>wrtsprt</bitbar.author.github>
# <bitbar.desc>Plugin monitoring CircleCI latest builds</bitbar.desc>
# <bitbar.dependencies>ruby</bitbar.dependencies>
# <bitbar.abouturl>https://github.com/wrtsprt/circle-ci-bitbar/blob/master/README.md</bitbar.abouturl>
require 'net/http'
require 'json'
require 'open-uri'
CIRCLE_CI_LOGO_GREEN = 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAAt9JREFUOBGVVU1MU0EQntltsYohUhDCRduCxsSD8lMaNUYPxkTl4FUhGpNi24uoFw960MQYE6MJB6EP0agRTbhw8eKBxIMcHhY4GBMi5SdB4CIoKlFo366zwL6+Fiq6yWb+vpmdNzO7D2GdVZswtswJ0SglnkSEGimhDBB+E3QKAfoYQE8yGHmDiDLXneyZJSmCPxG/CALvAEhvxrKWI8cBxqBlNBjrc1rtgCpY4L3xnGiTE/A3npzTlGXLWH20TeMo+5UVSBit/xNMedH3uoSUD/2mEV4NQ5WhFTDjVwXI+1rppJTBPMkjtMvowB1Om+YJk+LoCiW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XUDgaD0uKSAgM6wKEPY7HYvXQ67VZ+OnigdCXBRe0pK6dTQrTIQKwuIH/ndrunV1ZWmpAHGo3GBnhT5RBftQ3lHCL1Q/dS2XbBNJtNydVjVqqbk8bb7fY2y/r906QCmbBaaqIfPR7PdKFQqEmAaqZEKJBIOqmZlL3Q6XTWtP2FIRzVgNLW60AgsH95efmz8ne73YtKV5Lqq2zzjXIMpMHlPkomk3vEzmQyOZ7BmXw+31A4DpxDP6ZsJanwlZFKpXa2Wq1PGFtVYCC/c9hVv9//FLKW+OQQKrsGNufAyhUs8XQctO6ASV4AdMcJGtgdwOvo42CsgQzB2X89lwRrtVopEokEUZNDwIIZY/uGxCwXncxR3aIY9pQ43aDSJ/hOSeBfFpX/8fqyn0OCfS7/NHKW/WMzQjDygk1Tmf0ulBy7Qp3gL58AeQbfs0d+An4BFEsa0Ka0vXAAAAAASUVORK5CYII='
CIRCLE_CI_BASE_URL = 'https://circleci.com/api/v1.1'
COLORS = {
'running' => '#61D3E5',
'success' => '#39C988',
'fixed' => '#39C988',
'failed' =>'#EF5B58',
'timedout' => '#F3BA61',
'canceled' => '#898989',
'scheduled' =>'#AC7DD3',
'no_tests' => 'black',
}
$all_green = true
$any_running = false
def i_can_haz_internet?
begin
true if open('https://circleci.com')
rescue
false
end
end
def api_token
output = `security find-generic-password -l circle-ci-alfred-token -w`
if output =~ /The specified item could not be found in the keychain/
raise "api token not setup in keychain"
else
output.chomp
end
end
# https://circleci.com/docs/api/v1-reference/
def recent_builds
url = CIRCLE_CI_BASE_URL + '/recent-builds' + '?circle-token=' + api_token
uri = URI(url)
res = Net::HTTP.get_response(uri)
raise "unexpected response code: #{res.code}" unless res.code == '200'
# puts res.code # => '200'
res.body
end
def extract_recent_builds_info(recent_builds)
recent_builds.map do |build|
[build['branch'], build['status']]
end
end
def filtered_builds(builds)
after_exclude = builds.reject do |build|
build['status'] == 'canceled'
end
after_exclude.select do |build|
build['branch'] =~ /^raphael-/ || build['branch'] =~ /^master$/
end
end
def collect_by_branch(buildz)
by_branch = {}
buildz.each do |build|
branch = build['branch']
unless by_branch.has_key?(branch)
by_branch[branch] = {'builds' => []}
by_branch[branch]['status'] = build['status']
$all_green = false unless %w(success fixed).include? build['status']
$any_running = true if %w{running queued}.include? build['status']
end
by_branch[branch]['builds'] << build
end
by_branch
end
def format_for_bitbar(buildz)
# "Show Graphs | color=#123def href=http://example.com/graph?foo=bar"
buildz.each do |build|
puts "#{build['branch']} (#{build['status']}) | color=#{COLORS[build['status']]} href=#{build['build_url']}"
end
end
def format_by_branch(buildz_by_branch)
buildz_by_branch.each do |branch, branch_info|
builds = branch_info['builds']
puts "---"
puts "#{branch} | size=14 color=black"
# "Show Graphs | color=#123def href=http://example.com/graph?foo=bar"
builds.each do |build|
puts " - #{build['branch']} (#{build['status']}) | size=12 color=#{COLORS[build['status']]} href=#{build['build_url']}"
end
end
end
def display_recent_builds
the_recent_builds = recent_builds
recent_builds_hash = JSON.parse(the_recent_builds)
filtered_builds = filtered_builds recent_builds_hash
builds_by_branch = collect_by_branch filtered_builds
# extract_recent_builds_info recent_builds_hash
if $any_running
puts "| image=#{CIRCLE_CI_LOGO_PLAY}"
elsif $all_green
puts "| image=#{CIRCLE_CI_LOGO_GREEN}"
else
puts "| image=#{CIRCLE_CI_LOGO_RED}"
end
puts '---'
# format_for_bitbar filtered_builds
format_by_branch builds_by_branch
end
if i_can_haz_internet?
display_recent_builds
else
puts "⏳"
end