-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgithub.php
More file actions
87 lines (69 loc) · 2.96 KB
/
github.php
File metadata and controls
87 lines (69 loc) · 2.96 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
80
81
82
83
84
85
86
87
<?php
require 'vendor/autoload.php';
use Guzzle\Http\Client;
$githubToken = '';
$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 = isset($_POST['payload']) ? $_POST['payload'] : '{}';
$json = json_decode($request);
$repo = $json->repository->name;
$branch = $json->ref;
$api = '/repos/' . $json->repository->owner->name . '/' . $json->repository->name . '/commits';
$client = new Client('https://api.github.com');
$i = 0;
foreach ($json->commits as $commit) {
// skip non-distinct commit, propably merged
if (!$commit->distinct) {
continue;
}
// prepare subject
$subject = $commit->message . ' [' . $repo . ']';
// prepare commit variables
$id = $commit->id;
$url = $commit->url;
// get commit diff from GitHub
$response = $client->get($api . '/' . $id . '?access_token=' . $githubToken)->send();
$body = (string) $response->getBody();
$data = json_decode($body);
$message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
$message .= '<p>Commit: <a href="' . $url . '" class="commit">' . $id . '</a><br/>Branch: ' . $branch . '</p>';
foreach ($data->files as $file) {
$lines = explode("\n", $file->patch);
$message .= '<pre style="margin: 0; border: 1px solid #d2e6ed;">';
$message .= '<div style="background: #EAF2F5; color: #000; font-size: 11px; font-family: Monaco, monospace; padding: 7px 4px; border-bottom: 1px solid #e9f2f5;">' . $file->filename . '</div>';
foreach ($lines as $line) {
switch (substr($line, 0, 1)) {
case '+':
$message .= '<div style="background: #DDFFDD; color: #000; font-size: 11px; font-family: Monaco, monospace;">'; // green
break;
case '-':
$message .= '<div style="background: #FFDDDD; color: #000; font-size: 11px; font-family: Monaco, monospace;">'; // red
break;
case '@':
$message .= '<div style="background: #f7fafb; color:#999999; font-size: 11px; font-family: Monaco, monospace;">'; // blue
break;
default:
$message .= '<div style="background: #FFF; color: #000; font-size: 11px; font-family: Monaco, monospace;">'; // white, normal
}
$message .= htmlentities($line, ENT_QUOTES, "UTF-8");
$message .= '</div>';
}
$message .= '</pre><br/>';
}
// 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);
}