-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
github-ci.1m.php
executable file
·275 lines (239 loc) · 9.27 KB
/
github-ci.1m.php
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env php
<?php
/**
* <xbar.title>Github CI Status</xbar.title>
* <xbar.version>v1.0</xbar.version>
* <xbar.author>Jordan Andree</xbar.author>
* <xbar.author.github>jordanandree</xbar.author.github>
* <xbar.desc>Displays Github Pull Request CI Check statuses</xbar.desc>
* <xbar.dependencies>php</xbar.dependencies>
* <xbar.abouturl>https://github.com/jordanandree/bitbar-github-ci</xbar.abouturl>
*
* Icon sourced from feather icons: https://feathericons.com/
*
* To setup, create or edit your ~/.bitbarrc file with a new section:
*
* [github_ci]
* access_token=xxx # Personal Access Token
* username=jordanandree
* repos[]=jordanandree/bitbar-github-ci
* repos[]=jordanandree/dotfiles
*/
class GithubCIStatus
{
/**
* Default config values
*
* @var array
*/
protected $default_config = [
"hostname" => "github.com",
];
/**
* Config options sourced from ~/.bitbarrc
*
* @var stdClass
*/
protected $config;
/**
* Status line template for each check
*
* @var string
*/
protected $status_line = "%s %s | href=%s";
/**
* State of CI checks
*
* @var string
*/
protected $state = "success";
/**
* Lock param for mutating state
*
* @var bool
*/
protected $state_lock = false;
/**
* Menu bar icon
*
* @var string
*/
protected $icon = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAAlwSFlzAAAWJQAAFiUBSVIk8AAABDtpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIgogICAgICAgICAgICB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIKICAgICAgICAgICAgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiCiAgICAgICAgICAgIHhtbG5zOnRpZmY9Imh0dHA6Ly9ucy5hZG9iZS5jb20vdGlmZi8xLjAvIj4KICAgICAgICAgPHhtcDpDcmVhdG9yVG9vbD5BZG9iZSBQaG90b3Nob3AgQ0MgMjAxNyAoTWFjaW50b3NoKTwveG1wOkNyZWF0b3JUb29sPgogICAgICAgICA8eG1wTU06RG9jdW1lbnRJRD54bXAuZGlkOjVEMzE5OTBGREQzRTExRTdCNjU1Q0M4MUYwMENBMTNDPC94bXBNTTpEb2N1bWVudElEPgogICAgICAgICA8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xoBL9I8jT7DWI+7EaJ0OAgqbHgsrw3w71P1uN9CbUUTIpeFtZYBHa7hId25ykB6BbyyR9jZq/HO0nmKuTHsBLV32Ssytl2u5/bQXTA3hiHegVzq6EqbtgXCj01AqmB3DZOtBrnF0Jc60r4p/j3D3mdZSd1/X/9JbmmEDam1D3F7oWotJAjw1A34TV0cj+deg43wGreaw/aYdcghIvgL7jyfHBzie6QXX96ivqf09ojw2DgqK9cQRs1KZ1k90FzWIyeFHOXrgGXWA5pnW1T4FM2YxXH4t7sNxFrMNuByvitQboZRkN32+2BrMP7EeOz0ns01iWoFlqH5hoZlp+f00rdqMFBK1fUp1gdbQS22A4FJR5RFwFS36HPTOVtdD1a09kySacVqM1KyCfTxvnrCvwDHuWS9jg+rY6vzc1WxvAGd8Rs6tdRzf2FtDS1UMdPIZLoDMsn4leU5bofZtoQiWJrstHYDPJ0o15Klv8uTwxSZc2WFo02yWwB16kOyvdzhqAnvEVmkHnVue9Fpqg4uL3QKy4/quR6FquuMRWIOtBfoN5Ox37Mzi0igWlmJ3aQbUauAlHQacmSxT3DVrgbVbAf+X7C2311IYwO5eYAAAAAElFTkSuQmCC";
/**
* Output the Pull Request checks
*
* @return string
*/
public function run()
{
$lines = [];
try {
$pull_requests = $this->searchPullRequests();
if (empty($pull_requests)) {
$this->sendOutput("No Pull Requests. Get to work!");
exit;
}
foreach ($pull_requests as $pr) {
$repo_name = substr($pr->repository_url, strlen($this->getConfig()->base_uri . "repos/"));
$pr_info = $this->getPullRequest($repo_name, $pr->number);
$status = $this->getCommitStatus($repo_name, $pr_info->head->sha);
$lines[] = $this->formatLine($status->state, $pr_info->title, $pr_info->html_url);
if ($status->state !== $this->state && !$this->state_lock) {
$this->state = $status->state;
$this->state_lock = true;
}
foreach ($status->statuses as $check) {
$lines[] = "--" . $this->formatLine($check->state, $check->context, $check->target_url);
}
}
} catch (RuntimeException $e) {
$this->state = "failure";
$this->sendOutput($e->getMessage());
exit;
}
$this->sendOutput($lines);
}
/**
* echo back output to bitbar
*
* @param string[]|string
*
* @return void
*/
public function sendOutput($lines)
{
echo $this->statusIcon($this->state) . " | templateImage=" . $this->icon;
echo "\n---\n";
if (is_array($lines)) {
echo implode($lines, "\n");
} else {
echo $lines . "\n";
}
}
/**
* Format a line for output
*
* @param mixed ...$text
*
* @return string
*/
public function formatLine(...$args)
{
$icon = $this->statusIcon($args[0]);
$args[0] = $icon;
return sprintf($this->status_line, ...$args);
}
/**
* Get the memoized configuration struct or set it
*
* @return stdClass
*
* @throws RuntimeException
*/
protected function getConfig()
{
if (is_null($this->config)) {
$bitbarrc = getenv('HOME') . "/.bitbarrc";
if (!file_exists($bitbarrc)) {
throw new RuntimeException("~/.bitbarrc is missing");
}
$config = parse_ini_file($bitbarrc, true);
if (!array_key_exists("github_ci", $config)) {
throw new RuntimeException("[github_ci] section is missing in ~/.bitbarrc");
}
$config = array_merge($this->default_config, $config["github_ci"]);
$config["base_uri"] = "https://" . $config["hostname"] . "/api/v3/";
$this->config = (object) $config;
}
return $this->config;
}
/**
* Send a Request to the Github API
*
* @param string $endpoint
* @param array $options
*
* @return stdClass
*
* @throws RuntimeException
*/
protected function sendRequest($endpoint, $params = [])
{
$url = $this->getConfig()->base_uri . $endpoint;
$params["access_token"] = $this->getConfig()->access_token;
$url .= "?" . http_build_query($params);
$headers = null;
$body = null;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
if (!empty($response)) {
list($headers, $body) = explode("\r\n\r\n", $response, 2);
}
curl_close($ch);
if (is_null($body)) {
throw new RuntimeException("Error making request to the Github API. Check your configuration.");
} else {
return json_decode($body);
}
}
/**
* Get a single Pull Request
*
* @param string $repo
* @param int $id
*
* @return stdClass
*/
protected function getPullRequest($repo, $id)
{
return $this->sendRequest("repos/$repo/pulls/$id");
}
/**
* Get the status of a single Commit
*
* @param string $repo
* @param string $sha
*
* @return stdClass
*/
protected function getCommitStatus($repo, $sha)
{
return $this->sendRequest("repos/$repo/commits/$sha/status");
}
/**
* Perform search for open issues by the author in the repos
*
* @return stdClass
*/
protected function searchPullRequests()
{
$q = "state:open author:" . $this->getConfig()->username;
foreach ($this->getConfig()->repos as $repo) {
$q .= " repo:$repo";
}
return $this->sendRequest("search/issues", [
"q" => $q,
])->items;
}
/**
* Get the icon for the status
*
* @param string $status
*
* @return string
*/
protected function statusIcon($status)
{
$map = [
"success" => "\033[0;32m●\033[0m",
"failure" => "\033[0;31m●\033[0m",
"pending" => "\033[0;34m●\033[0m",
"warning" => "\033[0;33m●\033[0m",
];
return $map[$status];
}
}
echo (new GithubCIStatus())->run();