forked from zirkeldesign/satis-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.php
138 lines (114 loc) · 3.99 KB
/
webhook.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
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;
if (!file_exists(__DIR__.'/config.yml')) {
echo "Please, define your satis configuration in a config.yml file.\nYou can use the config.yml.dist as a template.";
exit(-1);
}
$defaults = array(
'bin' => 'bin/satis',
'json' => 'satis.json',
'webroot' => 'web/',
'user' => null,
'secret' => null
);
$config = Yaml::parse(__DIR__.'/config.yml');
$config = array_merge($defaults, $config);
$errors = array();
if (!file_exists($config['bin'])) {
$errors[] = 'The Satis bin could not be found.';
}
if (!file_exists($config['json'])) {
$errors[] = 'The satis.json file could not be found.';
}
if (!file_exists($config['webroot'])) {
$errors[] = 'The webroot directory could not be found.';
}
if (!empty($errors)) {
echo 'The build cannot be run due to some errors. Please, review them and check your config.yml:'."\n";
foreach ($errors as $error) {
echo '- '.$error."\n";
}
exit(-1);
}
// Determine GIT provider
if (isset($_SERVER['HTTP_X_GITHUB_EVENT'])) {
$gitProvider = 'github';
}
elseif (isset($_SERVER['HTTP_X_GITLAB_EVENT'])) {
$gitProvider = 'gitlab';
}
elseif (isset($_SERVER['User-Agent']) && $_SERVER['User-Agent'] === 'Bitbucket-Webhooks/2.0') {
$gitProvider = 'bitbucket';
}
else {
$gitProvider = 'general';
}
switch ($gitProvider) {
case 'github':
// Read the JSON data from GitHub
$rawPost = file_get_contents('php://input');
$githubData = json_decode($rawPost);
// Check the secret
$header = $_SERVER['HTTP_X_HUB_SIGNATURE'];
list($algo, $hash) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE'], 2) + array('', '');
try {
if ($hash !== hash_hmac($algo, $rawPost, $config['secret'])) {
throw new \Exception('Hook secret does not match.');
}
}
catch (Exception $e) {
header('HTTP/1.1 403 Forbidden');
echo $e->getMessage();die;
}
// Get the repo URL's
$cloneUrl = $githubData->repository->clone_url;
$sshUrl = $githubData->repository->ssh_url;
break;
case 'gitlab':
// Read the JSON data from GitLab
$rawPost = file_get_contents('php://input');
$data = json_decode($rawPost);
// Get the repo URL's
$cloneUrl = 'https://bitbucket.org/' . $data->repository->full_name . ".git";
$sshUrl = 'git@bitbucket.org:' . $data->repository->full_name . ".git";
break;
case 'bitbucket':
$rawPost = file_get_contents('php://input');
$gitlabData = json_decode($rawPost);
// Get the repo URL's
$cloneUrl = $gitlabData->project->http_url;
$sshUrl = $gitlabData->project->ssh_url;
break;
default:
}
// Read the satis JSON
$satisData = json_decode(file_get_contents('satis.json'));
$command = null;
if ($gitProvider === 'github' || $gitProvider === 'gitlab') {
// Update a specific repository if the webhook call came from GitHub or GitLab
foreach($satisData->repositories as $repository) {
if ($repository->url === $cloneUrl || $repository->url === $sshUrl) {
$repositoryUrl = $repository->url;
}
}
$command = sprintf('%s build --repository-url %s %s %s', $config['bin'], $repositoryUrl, $config['json'], $config['webroot']);
}
else {
// Update all repositories if the webhook didn't come from GitHub or GitLab
$command = sprintf('%s build %s %s', $config['bin'], $config['json'], $config['webroot']);
}
if (null !== $config['user'] && !is_null($command)) {
$command = sprintf('sudo -u %s -i %s', $config['user'], $command);
}
$process = new Process($command);
$exitCode = $process->run(function ($type, $buffer) {
if ('err' === $type) {
echo 'E';
error_log($buffer);
} else {
echo '.';
}
});
echo "\n\n" . ($exitCode === 0 ? 'Successful rebuild!' : 'Oops! An error occured!') . "\n";