-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub.php
330 lines (277 loc) · 11.5 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
323
324
325
326
327
328
329
330
<?php
/*
GitHub integration for Sausage Machine
Copyright (C) 2015 Gottfried Haider for PublishingLab
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@require_once('config.inc.php');
require_once('git.inc.php');
require_once('hybrid.inc.php');
require_once('makefile.inc.php');
require_once('router.inc.php');
require_once('util.inc.php');
require_once('vendor/OAuth2/Client.php');
require_once('vendor/OAuth2/GrantType/IGrantType.php');
require_once('vendor/OAuth2/GrantType/AuthorizationCode.php');
/**
* Get the URL to redirect a client to in order to get a GitHub authentication token
* @return string
*/
function github_get_auth($param = array()) {
$redirect_uri = base_url();
// add explicit route since GitHub prepends it with their own keys and values
$redirect_uri .= 'github.php?auth_callback';
unset($param[0]);
$redirect_uri .= '&' . http_build_query($param);
$client = new OAuth2\Client(config('github_client_id'), config('github_client_secret'));
$auth_url = $client->getAuthenticationUrl('https://github.com/login/oauth/authorize', $redirect_uri, array('scope' => 'public_repo'));
return $auth_url;
}
/**
* Called by GitHub - set the access token as cookie and redirect to URL specified by client
*/
function github_get_auth_callback($param = array()) {
if (empty($param['code'])) {
// Required parameter code missing or empty
// see https://developer.github.com/v3/oauth/#web-application-flow
setcookie('github_access_token', NULL, -1);
@header('Location: ' . (!empty($param['target']) ? $param['target'] : base_url()));
die();
}
// ask GitHub for access token
$client = new OAuth2\Client(config('github_client_id'), config('github_client_secret'));
$response = $client->getAccessToken('https://github.com/login/oauth/access_token', 'authorization_code', array('code' => $param['code'], 'redirect_uri' => ''));
// parse response
parse_str($response['result'], $oauth_param);
if (empty($oauth_param['access_token'])) {
// Expected parameter access_token missing or empty
// see print_r($response, true)
setcookie('github_access_token', NULL, -1);
} else {
setcookie('github_access_token', $oauth_param['access_token']);
}
@header('Location: ' . (!empty($param['target']) ? $param['target'] : base_url()));
die();
}
/**
* Create a repository on GitHub and push a local (temporary) repository to it
*/
function github_post_repo($param = array()) {
if (empty($param['github_access_token'])) {
router_error_400('Required parameter github_access_token missing or empty');
}
if (empty($param['github_repo_name'])) {
router_error_400('Required parameter github_repo_name missing or empty');
}
if (empty($param['temp'])) {
router_error_400('Required parameter temp missing or empty');
}
$github_repo = github_create_repo($param['github_access_token'], $param['github_repo_name']);
if ($github_repo === false) {
router_error_500('Error creating GitHub repository ' . $param['github_repo_name'] . '. Make sure there is no existing repository with the same name.');
}
$ret = github_add_collaborator($param['github_access_token'], $github_repo, config('github_push_as'));
if ($ret === false) {
router_error_500('Error adding ' . config('github_push_as') . ' as a collaborator to ' . $github_repo);
}
$ret = github_add_webhook($param['github_access_token'], $github_repo);
if ($ret === false) {
router_error_500('Error adding webhook to ' . $github_repo);
}
$modified = repo_get_modified_files($param['temp']);
$ret = repo_stage_files($param['temp'], $modified);
if ($ret === false) {
router_error_500('Error staging files ' . implode(', ', $modified) . ' to ' . $param['temp']);
}
$ret = repo_commit($param['temp'], 'Initial commit');
if ($ret === false) {
router_error_500('Error committing ' . $param['temp']);
}
$ret = repo_push($param['temp'], 'ssh://[email protected]/' . $github_repo . '.git');
if ($ret === false) {
router_error_500('Error pushing to ' . $github_repo . '. Make sure all checks in setup.php pass.');
}
// add to projects.json
// XXX (later): create helper functions, make atomic
$s = @file_get_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json');
$projects = @json_decode($s, true);
if (!@is_array($projects)) {
$projects = array();
}
$projects[] = array('created' => time(), 'updated' => time(), 'github_repo' => $github_repo, 'parent' => repo_get_url($param['temp']));
$old_umask = @umask(0000);
@file_put_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json', json_encode($projects));
@umask($old_umask);
return $github_repo;
}
function github_post_push($param = array()) {
$commit_msg_prefix = 'Regenerate output files';
$payload = json_decode($param['payload'], true);
// prevent error on "ping" notifications
if (!isset($payload['head_commit']['message'])) {
return 'Not a commit';
}
// prevent recursions
if (substr($payload['head_commit']['message'], 0, strlen($commit_msg_prefix)) === $commit_msg_prefix) {
return 'Not acting on my own changes';
}
// ref is like "refs/heads/master"
$branch = @array_pop(explode('/', $payload['ref']));
$tmp_key = get_repo($payload['repository']['clone_url'], $branch, true);
if ($tmp_key === false) {
router_error_500('Error getting branch ' . $branch . ' of ' . $payload['repository']['clone_url']);
}
$ret_val = make_run(tmp_dir($tmp_key), 'all', $out);
// run "make clean" to remove temporary files (but not output files, that's the idea)
make_run(tmp_dir($tmp_key), 'clean');
$modified = repo_get_modified_files($tmp_key);
if (empty($modified) && $ret_val === 0) {
// nothing to commit, no error
return 'No changes';
}
$ret = repo_stage_files($tmp_key, $modified);
if ($ret === false) {
router_error_500('Error staging files ' . implode(', ', $modified) . ' to ' . $tmp_key);
}
// setup commit message
$msg = $commit_msg_prefix; // this needs to come first
if ($ret_val !== 0) {
$msg .= ' (error)' . "\n\n";
$msg .= 'The process returned error code ' . $ret_val . '. ';
} else {
$msg .= "\n\n";
}
$msg .= 'The version of Pandoc used is ' . get_pandoc_version() . '.' . "\n\n";
$msg .= 'Output:' . "\n";
$msg .= implode("\n", $out);
$ret = repo_commit($tmp_key, $msg);
if ($ret === false) {
router_error_500('Error committing ' . $tmp_key);
}
$ret = repo_push($tmp_key, $payload['repository']['ssh_url']);
if ($ret === false) {
router_error_500('Error pushing to ' . $payload['repository']['ssh_url']);
}
// count the number of collaborators
$seen = array();
foreach ($payload['commits'] as $commit) {
if (!in_array($commit['author']['email'], $seen)) {
$seen[] = $commit['author']['email'];
}
}
if (1 < $seen) {
// subtract the sausage machine
$seen--;
}
// XXX (later): create helper functions, make atomic
$s = @file_get_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json');
$projects = @json_decode($s, true);
if (!@is_array($projects)) {
$projects = array();
}
foreach ($projects as &$p) {
if ($payload['repository']['full_name'] === $p['github_repo']) {
$p['updated'] = time();
$p['collaborators'] = count($seen);
}
}
$old_umask = @umask(0000);
@file_put_contents(rtrim(config('content_dir', 'content'), '/') . '/projects.json', json_encode($projects));
@umask($old_umask);
return 'Success';
}
/**
* Create a repository on GitHub
* @param String $github_access_token see route_get_github_auth & route_get_github_auth_callback
* @param String $github_repo_name name of repository to create
* @return String GitHub username, followed by a slash, followed by the name of the respository
*/
function github_create_repo($github_access_token, $github_repo_name) {
$client = new OAuth2\Client(config('github_client_id'), config('github_client_secret'));
$client->setAccessToken($github_access_token);
$client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_TOKEN);
$client->setCurlOption(CURLOPT_USERAGENT, config('github_useragent'));
$response = $client->fetch('https://api.github.com/user/repos', json_encode(array('name' => $github_repo_name, 'description' => 'Another hybrid book project managed by Sausage Machine')), 'POST');
if (!@is_string($response['result']['full_name'])) {
return false;
} else {
return $response['result']['full_name'];
}
}
/**
* Add a collaborator to a repository on GitHub
* @param String $github_access_token see route_get_github_auth & route_get_github_auth_callback
* @param String $github_repo GitHub username, followed by a slash, followed by the name of the respository
* @param String $collaborator GitHub username to add
* @return true if sucessful, false if not
*/
function github_add_collaborator($github_access_token, $github_repo, $collaborator) {
$client = new OAuth2\Client(config('github_client_id'), config('github_client_secret'));
$client->setAccessToken($github_access_token);
$client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_TOKEN);
$client->setCurlOption(CURLOPT_USERAGENT, config('github_useragent'));
// special case: repo owner and collaborator are the same
$pos = strpos($github_repo, '/');
if ($pos !== false && substr($github_repo, 0, $pos) === $collaborator) {
return true;
}
$response = $client->fetch('https://api.github.com/repos/' . $github_repo . '/collaborators/' . $collaborator, '', 'PUT');
if (!isset($response['code']) || $response['code'] !== 204) {
return false;
} else {
return true;
}
}
/**
* Add the necessary webhooks for the Sausage Machine to function
* @param String $github_access_token see route_get_github_auth & route_get_github_auth_callback
* @param String $github_repo GitHub username, followed by a slash, followed by the name of the respository
* @return true if sucessful, false if not
*/
function github_add_webhook($github_access_token, $github_repo) {
$client = new OAuth2\Client(config('github_client_id'), config('github_client_secret'));
$client->setAccessToken($github_access_token);
$client->setAccessTokenType(OAuth2\Client::ACCESS_TOKEN_TOKEN);
$client->setCurlOption(CURLOPT_USERAGENT, config('github_useragent'));
$param = array(
'name' => 'web',
'active' => true,
'events' => array(
'push'
),
'config' => array(
'url' => base_url() . 'github.php?push',
'content_type' => 'form'
)
);
$response = $client->fetch('https://api.github.com/repos/' . $github_repo . '/hooks', json_encode($param), 'POST');
if (!isset($response['code']) || $response['code'] !== 201) {
return false;
} else {
return true;
}
}
register_route('GET' , 'auth', 'github_get_auth');
/* invoked by GitHub */
register_route('GET' , 'auth_callback=?', 'github_get_auth_callback');
register_route('POST', 'repo', 'github_post_repo');
/* invoked by GitHub webhook */
register_route('POST', 'push=?', 'github_post_push');
$query = $_SERVER['QUERY_STRING'];
// use the first URL argument for the router
$pos = strpos($query, '&');
if ($pos !== false) {
$query = substr($query, 0, $pos);
}
// return JSON by default
@header('Content-type: application/json; charset=utf-8');
echo json_encode(route($_SERVER['REQUEST_METHOD'], $query));