-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgitlab.php
More file actions
79 lines (62 loc) · 2.09 KB
/
gitlab.php
File metadata and controls
79 lines (62 loc) · 2.09 KB
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
<?php
require 'vendor/autoload.php';
use Goutte\Client;
use TijsVerkoyen\CssToInlineStyles\CssToInlineStyles;
$gitlabToken = '';
$from = '';
$to = '';
$secret = '';
$config = __DIR__.'/config.php';
if (stream_resolve_include_path($config)) {
include $config;
}
// check secret
if ($secret !== @$_GET['secret']) {
exit;
}
// read hook request
$request = file_get_contents('php://input');
$json = json_decode($request);
$branch = $json->repository->name . ':' . $json->ref;
$many = count($json->commits) > 1;
$i = 0;
foreach ($json->commits as $commit) {
// prepare subject
$subject = $commit->message . ' [' . $branch . ']';
if ($many) {
$subject .= '[' . $i++ . ']';
}
// prepare commit variables
$id = $commit->id;
$url = str_replace('commits', 'commit', $commit->url);
// crawl commit diff from GitLab
$client = new Client();
if ($gitlabDisableSecure) {
$guzzle = $client->getClient();
$guzzle->setConfig(
array(
'curl.CURLOPT_SSL_VERIFYHOST' => false,
'curl.CURLOPT_SSL_VERIFYPEER' => false,
)
);
$client->setClient($guzzle);
}
$crawler = $client->request('GET', $url . '?private_token=' . $gitlabToken);
// remove GitLab layout
$html = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
$html .= '<p>Commit: <a href="' . $url . '" class="commit">' . $id . '</a></p>';
foreach ($crawler->filter('.diff_file') as $node) {
$html .= $node->ownerDocument->saveHTML($node);
}
$html .= '</body></html>';
$css = file_get_contents('style.css');
// convert CSS to inline styles for GMail
$inline = new CssToInlineStyles($html, $css);
$message = $inline->convert();
// send email
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $commit->author->name . ' <' . $from . '>' . "\r\n";
$headers .= 'Reply-To: ' . $commit->author->email . "\r\n";
mail($to, $subject, $message, $headers);
}