This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
forked from dound/github-post-receive-php-handler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub_post_receive.php
204 lines (177 loc) · 6.91 KB
/
github_post_receive.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* github POST processor.
*
* @author David Underhill
* @version 0.1 (updated 31-May-2009 @ 06:01 PDT)
*/
define('SEND_HTML_EMAIL', false); // HTML or plaintext?
define('SEND_DIFF', true); // Add a diff of the changes?
define('SHOW_AGGREGATE', false); // Aggregate the files changed list at the end of the email
// (not recommended if send_diff set)
define('USE_LOCAL', false); // Use a local git repo instead of the github API
define('EMAIL_FROM', 'noreply@example.com');
// some constants for HTML tags
define('HTML_HEADER', SEND_HTML_EMAIL ? '<html><body>' : '');
define('HTML_BR', SEND_HTML_EMAIL ? '<br/>' : "\n");
define('HTML_P', SEND_HTML_EMAIL ? '<p>' : "\n");
define('HTML_P_END', SEND_HTML_EMAIL ? '</p>' : "\n");
define('HTML_BLOCKQUOTE', SEND_HTML_EMAIL ? '<blockquote>' : "\n");
define('HTML_BLOCKQUOTE_END', SEND_HTML_EMAIL ? '</blockquote>' : '');
define('HTML_FOOTER', SEND_HTML_EMAIL ? '</body></html>' : '');
/** Generates a URL based on SEND_HTML_EMAIL. */
function make_url($url, $text, $is_mail_to) {
if(SEND_HTML_EMAIL)
return '<a href="' . ($is_mail_to ? 'mailto:' : '') . $url .'">' . $text . '</a>';
elseif($is_mail_to)
return $url;
else
return "$text ($url)";
}
/**
* Emails information about a push specified by github's JSON format.
*
* @param to email address(es)
* @param subj_header text to prefix the header with
* @param github_json string which contains github's JSON post-receive data
*/
function mail_github_post_receive($to, $subj_header, $github_json) {
$obj = json_decode($github_json);
if(!$obj) {
error_log("bad JSON: $github_json");
exit(0);
}
$num_commits = count($obj->{'commits'});
if($num_commits == 0) {
error_log("no commits in JSON: $github_json");
exit(0);
}
// create the subject line
$branch = str_replace('refs/heads/', '', $obj->{'ref'});
$last_commit = $obj->{'after'};
$subj = "$subj_header $branch -> $last_commit";
// repo details for diff
$repo_owner = $obj->{'repository'}->{'owner'}->{'name'};
$repo = $obj->{'repository'}->{'name'};
// extract information about each commit
$commits = '';
$added = array();
$deleted = array();
$modified = array();
foreach($obj->{'commits'} as $commit) {
$id = $commit->{'id'};
$url = $commit->{'url'};
$author = $commit->{'author'};
$author_name = $author->{'name'};
$author_email = $author->{'email'};
$msg = $commit->{'message'};
$date = $commit->{'timestamp'};
if(SHOW_AGGREGATE) {
if(isset($commit->{'added'})) {
$added = array_merge($added, $commit->{'added'});
}
if(isset($commit->{'deleted'})) {
$deleted = array_merge($deleted, $commit->{'deleted'});
}
if(isset($commit->{'modified'})) {
$modified = array_merge($modified, $commit->{'modified'});
}
} else {
$paths = "Changed paths:\n";
if (isset($commit->{'added'}) && count($commit->{'added'}) > 0) {
foreach($commit->{'added'} as $add)
$paths .= " A $add\n";
}
if (isset($commit->{'removed'}) && count($commit->{'removed'}) > 0) {
foreach($commit->{'removed'} as $rem)
$paths .= " R $rem\n";
}
if (isset($commit->{'modified'}) && count($commit->{'modified'}) > 0) {
foreach($commit->{'modified'} as $mod)
$paths .= " $mod\n";
}
}
if(SEND_DIFF)
$msg = "Commit Message:\n$msg";
if(!SEND_HTML_EMAIL)
$msg = "\n$msg";
if(!SHOW_AGGREGATE)
$msg = "$msg\n\n$paths";
$commits .=
HTML_P . 'Commit: ' . $id .
HTML_BR . " $url" .
HTML_BR . "Author: $author_name (" . make_url($author_email, $author_email, true) . ')' .
HTML_BR . "Date: $date" .
HTML_BLOCKQUOTE .
str_replace("\n", HTML_BR, $msg . "\n\n" .
(SEND_HTML_EMAIL ?
htmlentities(github_get_diff($repo_owner, $repo, $id)) :
github_get_diff($repo_owner, $repo, $id))) . HTML_BLOCKQUOTE_END . HTML_P_END;
}
// create a list of aggregate additions/deletions/modifications
$changes_txt = '';
if(SHOW_AGGREGATE) {
$changes = array("Additions"=>$added, "Deletions"=>$deleted, "Modifications"=>$modified);
foreach($changes as $what => $what_list) {
if(count($what_list) > 0) {
$changes_txt .= HTML_BR . "$what:" . HTML_BR;
$items = array_unique($what_list);
sort($items);
foreach($items as $item) {
$changes_txt .= " -- $item" . HTML_BR;
}
}
}
}
// create the body of the mail
$repo = $obj->{'repository'};
$name = $repo->{'name'};
$url = $repo->{'url'};
$commits_noun = ($num_commits == 1) ? 'commit' : 'commits';
$body = HTML_HEADER .
"This automated email contains information about $num_commits new $commits_noun which have been\n" .
"pushed to the '$name' repo located at $url.\n" .
"\n" .
$commits .
$changes_txt .
HTML_FOOTER;
// build the mail headers
$headers = "From: " . EMAIL_FROM . " (GIT diff mailer)\r\n";
if(SEND_HTML_EMAIL)
$headers .= "MIME-Version: 1.0\r\n" .
"Content-type: text/html\r\n";
// send the mail
if(!mail($to, $subj, $body, $headers))
error_log("failed to email github info to '$to' ($subj, $body)");
else {
$body = str_replace("\n", "<br/>", $body);
echo "$to<br/>$subj<br/>$body<br/>";
}
}
function github_get_diff($repo_owner, $repo, $commit)
{
if (SEND_DIFF == false)
return '';
$ret = '';
if (!USE_LOCAL) {
$json = file_get_contents("http://github.com/api/v2/json/commits/show/$repo_owner/$repo/$commit");
$json = json_decode($json);
if ($json == null)
return '*bad json when retrieving commit diff*';
if (isset($json->{'commit'}->{'modified'}) && count($json->{'commit'}->{'modified'}) > 0) {
foreach($json->{'commit'}->{'modified'} as $mod) {
$ret .= "Modified: " . $mod->{'filename'} . "\n" .
"===================================================================\n" . $mod->{'diff'} . "\n\n";
}
}
} else {
if (preg_match('/^[0-9a-f]+$/', $commit)) {
ob_start();
passthru('./diff_local.sh ' . escapeshellarg($commit));
$ret = ob_get_contents();
ob_end_clean();
}
}
return $ret;
}
?>