-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitbucket2mail.php
90 lines (73 loc) · 4.05 KB
/
gitbucket2mail.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
<?php
function str_lreplace($search, $replace, $subject) {
$pos = strrpos($subject, $search);
if ($pos !== false) {
$subject = substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}
$to = isset($_GET['to']) ? $_GET['to'] : '{}';
$request = isset($_POST['payload']) ? $_POST['payload'] : '{}';
$json = json_decode($request);
$repoName = $json->repository->name;
$branch = $json->ref;
$message = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
$message .= '<div style="margin-top: 10px;font-size: 30px; font-family: Arial, monospace;">Tirasa - GitBucket</div>';
$message .= '<hr/>';
$from = '';
$mailMessage = '';
if ($json->issue) {
$mailMessage = 'Issue #' . $json->issue->number . ' ' . $json->issue->title . ': ' . ($json->comment ? 'commented' : $json->action);
$from = $json->sender->login . ' <' . $json->sender->email . '>';
$issueRef = $json->repository->html_url . '/issues/' . $json->issue->number;
if ($json->comment) {
$issueRef .= '#comment-' . $json->comment->id;
$message .= '<b>' . $json->comment->user->login . '</b> commented:<br/>';
$message .= '<pre>' . $json->comment->body . '</pre>';
}
$message .= '<a href="' . $issueRef . '">' . $issueRef . '</a>';
} else {
$firstCommitMessage = $json->commits[0]->message;
foreach ($json->commits as $commit) {
// prepare commit variables
$commiterName = $json->pusher->name;
$commiterEmail = $json->pusher->email;
$commitId = $commit->id;
$commitMessage = $commit->message;
$commitTimestamp = $commit->timestamp;
// temporary(?) fix
$commitUrl = str_replace("commits", "commit", str_replace("api/v3/", "", $commit->url));
$commitAdded = $commit->added;
$commitModiefied = $commit->modified;
$commitRemoved = $commit->removed;
// general Info
$message .= '<div><strong>Branch: </strong>' . $branch . '</div>';
$message .= '<div><strong>Commit: </strong><a href="' . $commitUrl . '" class="commit">' . $commitId . '</a></div>';
$message .= '<div><strong>Date: </strong>' . $commitTimestamp . '</div>';
$message .= '<div><strong>Message</strong><br/>' . nl2br(str_lreplace('\n', '', $commitMessage)) . '</div>';
$message .= '</div>';
$message .= '<div style="margin-top: 10px;font-size: 15px;font-weight:bold; font-family: Arial, monospace;">Changed paths:</div>';
$message .= '<div style="margin-top: 10px; border: 1px solid #d2e6ed;">';
foreach ($commit->added as $commitAdded) {
$message .= '<div style="background: #EAF2F5; color: #000; font-size: 12px; font-family: Arial, monospace; padding: 1px 4px; border-bottom: 1px solid #d2e6ed;">A ' . $commitAdded . '</div>';
}
foreach ($commit->modified as $commitModified) {
$message .= '<div style="background: #EAF2F5; color: #000; font-size: 12px; font-family: Arial, monospace; padding: 1px 4px; border-bottom: 1px solid #d2e6ed;">M ' . $commitModified . '</div>';
}
foreach ($commit->removed as $commitRemoved) {
$message .= '<div style="background: #EAF2F5; color: #000; font-size: 12px; font-family: Arial, monospace; padding: 1px 4px; border-bottom: 1px solid #d2e6ed;">D ' . $commitRemoved . '</div>';
}
$message .= '</div>';
}
$mailMessage = str_replace('\n', ' ', str_lreplace('\n', ' ', $firstCommitMessage));
$from = $commiterName . ' <' . $commiterEmail . '>';
}
$message .= '<div style="margin-top: 10px;font-size: 12px; font-family: Arial, monospace;">--<br/>You received this message because you are subscribed to Tirasa ' . $repoName . ' project.</div>';
$message .= '</body></html>';
$subject = '[' . $repoName . '] ' . $mailMessage;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $to . "\r\n";
mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $headers);
?>