/
github.php
322 lines (262 loc) · 8.02 KB
/
github.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* GitHub operations layer.
*/
if (! defined('PMAHOOKS')) {
fail('Invalid invocation!');
}
require_once './config.php';
$curlBaseOpts = [
CURLOPT_USERAGENT => 'phpMyAdmin-bot',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: token ' . GITHUB_TOKEN,
],
];
/**
* Builds the body of the email to send
*
* @see https://developer.github.com/v3/activity/events/types/#pushevent
*
* @param stdClass $inputData The JSON payload
*
* @return stdClass {
* "repoName": "org/repo",
* "compare": "URL TO COMPARE PAGE",
* "emailBody": "THE RAW BODY",
* "headCommitTitle": "commit title",
* "headCommitShortHash": "abcde",
* "authorName": "William Desportes",
* "authorEmail": "williamdes@wdes.fr"
* }
*/
function github_webhook_push(stdClass $inputData): stdClass
{
$msg = '';
$msg .= 'Branch: ' . $inputData->ref . PHP_EOL;
$msg .= 'Home: ' . $inputData->repository->html_url . PHP_EOL;
foreach ($inputData->commits as $commit) {
$msg .= 'Commit: ' . $commit->id . PHP_EOL;
$msg .= $commit->url . PHP_EOL;
$msg .= 'Author: ' . $commit->author->name . ' <' . $commit->author->email . '>' . PHP_EOL;
$date = new DateTime($commit->timestamp);
$msg .= 'Date: ' . $date->format('Y-m-d (D, m F Y) P') . PHP_EOL . PHP_EOL;
$msg .= 'Changed paths: ' . PHP_EOL;
foreach ($commit->added as $file) {
$msg .= 'A ' . $file . PHP_EOL;
}
foreach ($commit->modified as $file) {
$msg .= 'M ' . $file . PHP_EOL;
}
foreach ($commit->removed as $file) {
$msg .= 'D ' . $file . PHP_EOL;
}
$msg .= PHP_EOL . 'Log Message:' . PHP_EOL . '-----------' . PHP_EOL;
$msg .= $commit->message . PHP_EOL . PHP_EOL;
}
$data = new stdClass();
$data->repoName = $inputData->repository->full_name;
$data->compare = $inputData->compare;
$data->emailBody = $msg;
// Commits can be empty and head_commit filled in case of a tag
$firstCommit = $inputData->commits[0] ?? $inputData->head_commit;
$data->headCommitTitle = explode("\n", $firstCommit->message)[0];
$data->headCommitShortHash = substr($firstCommit->id, 0, 6);
$data->authorName = $firstCommit->author->name;
$data->authorEmail = $firstCommit->author->email;
return $data;
}
/**
* Verifies signature from GitHub
*/
function github_verify_post()
{
if (empty(GITHUB_HOOK_SECRET)) {
fail('Missing hook secret configuration!');
}
if (! isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
fail("HTTP header 'X-Hub-Signature' is missing.");
} elseif (! extension_loaded('hash')) {
fail("Missing 'hash' extension to check the secret code validity.");
}
[$algo, $hash] = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2) + ['', ''];
if (! in_array($algo, ['sha1', 'sha256', 'sha512'], true)) {
fail("Hash algorithm '$algo' is not allowed.");
}
if (! in_array($algo, hash_algos(), true)) {
fail("Hash algorithm '$algo' is not supported.");
}
$rawPost = file_get_contents('php://input');
$newhash = hash_hmac($algo, $rawPost, GITHUB_HOOK_SECRET);
if (hash_equals($hash, $newhash)) {
return;
}
fail('Hook secret does not match.');
}
/**
* Creates a GitHub release
*/
function github_make_release($repo, $tag, $version, $description)
{
global $curlBaseOpts;
$ch = curl_init();
$result = [
'release' => [
'project' => $repo,
'tag' => $tag,
'version' => $version,
'description' => $description,
],
];
//set the url, number of POST vars, POST data
curl_setopt_array($ch, $curlBaseOpts);
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/' . $repo . '/releases');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['tag_name' => $tag, 'name' => $version, 'body' => $description]));
//execute post
$response = curl_exec($ch);
//close connection
curl_close($ch);
$result['response'] = json_decode($response);
return $result;
}
/**
* Posts a comment on GitHub pull request
*/
function github_comment_pull($repo, $pullid, $comment)
{
global $curlBaseOpts;
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt_array($ch, $curlBaseOpts);
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/' . $repo . '/issues/' . $pullid . '/comments');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['body' => $comment]));
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_decode($result, true);
}
/**
* Posts a comment on GitHub commit request
*/
function github_comment_commit($repo, $sha, $comment)
{
global $curlBaseOpts;
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt_array($ch, $curlBaseOpts);
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/' . $repo . '/commits/' . $sha . '/comments');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['body' => $comment]));
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_decode($result, true);
}
/**
* Returns list of pull request commits.
*/
function github_pull_commits($repo, $pullid)
{
global $curlBaseOpts;
$ch = curl_init();
curl_setopt_array($ch, $curlBaseOpts);
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/' . $repo . '/pulls/' . $pullid . '/commits');
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_decode($result, true);
}
/**
* Returns diff of pull request commit detail.
*/
function github_commit_detail($repo, $commit)
{
global $curlBaseOpts;
$ch = curl_init();
curl_setopt_array($ch, $curlBaseOpts);
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/' . $repo . '/commits/' . $commit);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_decode($result, true);
}
/**
* Returns diff of pull request commit detail.
*/
function github_commit_comments($repo, $sha)
{
global $curlBaseOpts;
$ch = curl_init();
curl_setopt_array($ch, $curlBaseOpts);
curl_setopt($ch, CURLOPT_URL, 'https://api.github.com/repos/' . $repo . '/commits/' . $sha . '/comments');
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_decode($result, true);
}
/**
* Returns diff of pull request commits.
*/
function github_pull_diff($repo, $pullid)
{
global $curlBaseOpts;
$ch = curl_init();
curl_setopt_array($ch, $curlBaseOpts);
curl_setopt($ch, CURLOPT_URL, 'https://github.com/' . $repo . '/pull/' . $pullid . '.patch');
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
/**
* Trigger website rendering.
*/
function trigger_website_render()
{
$file = fopen(WEBSITE_HOOK, 'w');
fclose($file);
}
/**
* Trigger website rendering.
*/
function trigger_docs_render()
{
$file = fopen(DOCS_HOOK, 'w');
fclose($file);
}
/**
* Issues JSON response
*
* @param mixed $data JSON data to print
*/
function json_response($data, $status = 'success', $message = null)
{
header('Content-Type: application/json; charset=UTF-8');
header('X-Content-Type-Options: nosniff');
$response = ['status' => $status];
if (isset($data)) {
$response['data'] = $data;
}
if (isset($message)) {
$response['message'] = $message;
}
echo json_encode($response, JSON_PRETTY_PRINT);
}
/**
* Terminates request with error
*/
function fail($message, $code = 500)
{
http_response_code($code);
json_response(null, 'error', $message);
die;
}