/
scheduler.php
80 lines (65 loc) · 1.89 KB
/
scheduler.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
<?php
/**
* @file
* Ping handler for post-commit hook to schedule a develop pull job.
*/
namespace AKlump\PostCommit;
global $conf;
require_once dirname(__FILE__) . '/bootstrap.php';
// Make a note that we got hit.
$log = new Logger($conf['logs_dir'] . '/orders.txt');
// Access check
$access = !empty($_GET['key']) && $_GET['key'] === $conf['secret'];
// If access then we'll schedule the job.
if ($access) {
$contents = file_get_contents("php://input");
// Dump this for troubleshooting purposes.
file_put_contents($conf['logs_dir'] . '/last.json', $contents);
$translators = array();
$translators[] = new GitHub($contents);
$translators[] = new GitLab($contents);
foreach ($translators as $data) {
if ($data->isUnderstood()) {
break;
}
}
// Allow testing in the url via ?ref=refs/heads/master.
if (isset($_GET['branch'])) {
$data->setBranch($_GET['branch']);
}
elseif (isset($_GET['ref'])) {
$data->setBranch($_GET['ref']);
}
if (isset($_GET['repo'])) {
$data->setRepoName($_GET['repo']);
}
$config = new Config($conf);
$config
->setBranch($data->getBranch())
->setName($data->getRepoName());
$log->append('origin user: ' . $data->getUsername());
$log->append('repo: ' . $data->getRepoName());
$log->append('branch: ' . $data->getBranch());
$total_added = 0;
if (!($jobs = $conf['jobber']->getJobsFileHandle('a'))) {
$log->append('Invalid log configuration.');
}
elseif ($jobs && ($commands = $config->getJobs())) {
foreach ($commands as $cmd) {
fwrite($jobs, $cmd . PHP_EOL);
$log->header();
++$total_added;
}
}
fclose($jobs);
if ($total_added) {
$log->append('jobs added: ' . $total_added);
}
}
else {
header("HTTP/1.0 401 Access Denied");
$log->append('401: Access Denied');
}
$log->close();
header("Content-Type: text/html");
print '<p>' . $log->view("</p>\n</p>") . '</p>' . PHP_EOL;