-
Notifications
You must be signed in to change notification settings - Fork 7
/
github.php
204 lines (148 loc) · 4.18 KB
/
github.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
ob_start();
/**
* GitHub Service Hook (Post-Receive URL)
*
* This script, once properly configured, will execute a 'git pull'
* every time someone commits a change to the documentation in a
* GitHub repository, therefore always keeping your docs up to date.
*
* For this to work, your web server must have Git installed, and
* you must have a pre-existing Git clone repository for your project.
*/
/**
* SETTINGS
*/
$config = new stdClass();
/**
* The directory where the docs are stored in the repository.
* This is used to verify that the commit actually changed a
* document file, rather than some other non-doc related file.
*
* If you want the script to run on every commit, set the path
* to blank (''), otherwise, enter your doc path.
*/
$config->repo_path = '';
/**
* The is the local path on your server where you have the Git
* clone of your project (w/docs).
*
* Note:
* This folder must be a pre-existing Git clone repository.
*
* Due to the limitations of Git, your entire repository will
* be stored in this directory. If you don't want this, you
* may want to create a separate repository just for your docs.
*/
$config->path = './';
/**
* Update Key
*
* Prevents unauthorized updates. Be sure to include this when
* entering the Post-Receive URL in GitHub.
*
* @example http://example.com/github.php?key=myKEYgoesHERE
*/
$config->key = 'CHANGE_ME';
/**
* Git Pull Shell Command
*
* You may need to set the full path to your Git executable depending
* on how Git is setup on your server.
*
* Adding " 2>&1" to the end of the command will allow you to see any
* errors that occurred during the git update in the log file.
*/
$config->command = 'git pull 2>&1';
/**
* GitHub Error Log
*
* Filename & path where you want to store the GitHub error log.
* If you don't with to keep a log, simply set the string to ''.
*/
$config->log = 'github.log';
/**
* Verify that an authorized client is submitting the request...
*/
if (!isset($_REQUEST['key']) || $_REQUEST['key'] != $config->key)
_die('ERROR: Incorrect Key');
/**
* Check to see if we have a payload to process...
*/
if (!isset($_REQUEST['payload']))
_die('ERROR: No payload received');
/**
* Analyze the JSON data, if possible, use the built in PHP function,
* if that doesn't exist, fall back to the slower alternate method.
*/
if (!function_exists('json_decode')) {
require_once 'assets/json.php';
$json = new Services_JSON();
$paylaod = $json->decode($_REQUEST['payload']);
}else {
$paylaod = json_decode($_REQUEST['payload']);
}
/**
* Verify that we have something to process...
*/
if (!isset($paylaod->commits))
_die('ERROR: No commits to process');
/**
* Create a list of files added, removed, or modified from the commit,
* and store them in as an array in $files.
*/
$files = array();
foreach ($paylaod->commits as $commit) {
if (isset($commit->added))
$files = array_merge($files,$commit->added);
if (isset($commit->removed))
$files = array_merge($files,$commit->removed);
if (isset($commit->modified))
$files = array_merge($files,$commit->modified);
}
$update = false;
if (empty($config->repo_path)){
/**
* If the path is empty, we need to do an update, every time.
*/
$update = true;
}elseif(!empty($config->repo_path)) {
/**
* Otherwise, run through the changed files, and see if any files
* have been changed in the docs directory of the repository
*/
foreach ($files as $file) {
if (strpos($file,$config->repo_path) !== false) {
$update = true;
break;
}
}
}
/**
* No updates exist...
*/
if (!$update)
_die('No updates necessary');
/**
* It's time to update...
*/
// Move to the path
chdir($config->path);
// Do the pull...
$last_line = system($config->command,$exit_code);
if ($exit_code !== 0 || strpos($last_line,'fatal:') !== false)
_die("ERROR: Couldn't update local repository");
_die('Success!');
function _die($message='')
{
global $config;
if (empty($config->log))
die($message);
$log = "##################################################\n";
$log .= date('r')."\n";
$log .= "$message\n";
$log .= ob_get_contents()."\n";
file_put_contents($config->log,$log,FILE_APPEND);
die();
}
?>