-
Notifications
You must be signed in to change notification settings - Fork 16
/
GitHubUpdater.php
executable file
·293 lines (268 loc) · 10.2 KB
/
GitHubUpdater.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* GitHub Updater script
* Fetch changes from GitHub and pull them into a local database
*
* Copyright (c) The BBQTeam 2012
* Version 5.0
* This new version changed completely the logic behind the updater.
* We now fetch the SHA from GitHub, verify the latest we have in DB
* Then fetch commit until we get the latest SHA in DB.
* All info are still loaded from DB into an array before starting to
* fetch on Github. Reverted to use MySQL again.
*/
// In the newer php version, we need to set up the time zone.
date_default_timezone_set("Europe/Paris");
if (php_sapi_name() != "cli") {
die("Changelog updater must be run from CLI!");
}
// Configs
set_time_limit(0);
require_once("/var/www/changelog/config.php");
require_once("/var/www/changelog/Thread.php");
/**
* Escape the string to be given to MySQL
*/
function esc($txt) {
return mysql_real_escape_string($txt);
}
/**
* Parses a GitHub date
*/
function githubDate($date) {
$a = new DateTime($date);
$a->setTimezone(new DateTimeZone('UTC'));
return $a->format("Y-m-d H:i:s");
}
// We output everything to a file, get it easy to debug the log.
function ob_file_callback($buffer)
{
global $ob_file;
fwrite($ob_file,$buffer);
}
$ob_file = fopen('log.txt', 'w');
ob_start('ob_file_callback');
// Connect to MySQL
mysql_connect($CFG['SQL']['Host'], $CFG['SQL']['User'], $CFG['SQL']['Pass'], TRUE) or die(mysql_error());
mysql_select_db($CFG['SQL']['DB']) or die(mysql_error());
mysql_query("START TRANSACTION;") or die(mysql_error());
$startTime = microtime(true);
$repoCounter = 0;
// Get all the information from all XML file.
// Improved version, we also fetch the latest SHA from DB here. This avois us to
// use a thread to do it cause thread doesn't handle mysql connection well.
$files = scandir("/var/www/changelog/projects/");
$repoID = array();
$repoOwner = array();
$repoName = array();
$repoBranch = array();
$versionNo = array();
$lastCommitDBSHA = array();
$att = 'name';
$att2 = 'version';
$xml = simplexml_load_file("/var/www/changelog/projects/cm.xml");//.$project);
$reposgeneric = $xml->xpath("//genericlist");
$reposspecific = $xml->xpath("//specificlist");
$owner = (string)$xml->owner;
foreach($reposgeneric as $value)
{
foreach($value->children() as $git)
{
$reponame = (string)$git->attributes()->$att;
foreach($git->children() as $branch)
{
$branchname = (string)$branch->attributes()->$att;
$versionNo[$repoCounter] = $branch->attributes()->$att2;
$repoID[$repoCounter] = $repoCounter;
$repoOwner[$repoCounter] = $owner;
$repoName[$repoCounter] = $reponame;
$repoBranch[$repoCounter] = $branchname;
$lastCommitDB = mysql_query("SELECT SHA FROM commits WHERE Repository = '".$repoName[$repoCounter]."' AND GitUsername = '".$repoOwner[$repoCounter]."' AND Branch = '".$repoBranch[$repoCounter]."' ORDER BY CommitDate DESC LIMIT 1") or die(mysql_error());
$fetch = mysql_fetch_assoc($lastCommitDB);
$lastCommitDBSHA[$repoCounter] = $fetch['SHA'];
$repoCounter++;
}
}
}
foreach($reposspecific as $value)
{
foreach($value->children() as $git)
{
$reponame = (string)$git->attributes()->$att;
foreach($git->children() as $branch)
{
$branchname = (string)$branch->attributes()->$att;
$versionNo[$repoCounter] = $branch->attributes()->$att2;
$repoID[$repoCounter] = $repoCounter;
$repoOwner[$repoCounter] = $owner;
$repoName[$repoCounter] = $reponame;
$repoBranch[$repoCounter] = $branchname;
$lastCommitDB = mysql_query("SELECT SHA FROM commits WHERE Repository = '".$repoName[$repoCounter]."' AND GitUsername = '".$repoOwner[$repoCounter]."' AND Branch = '".$repoBranch[$repoCounter]."' ORDER BY CommitDate DESC LIMIT 1") or die(mysql_error());
$fetch = mysql_fetch_assoc($lastCommitDB);
$lastCommitDBSHA[$repoCounter] = $fetch['SHA'];
$repoCounter++;
}
}
}
function fetchLastCommitSHA($pRepoId, $pRepoOwner, $pRepoName, $pRepoBranch, $pCFG, $pContext)
{
global $lastCommitDBSHA;
global $distinctRepo;
global $context;
global $versionNo;
$branches_github = "";
$i = 0;
$exit = false;
do
{
$branches_github = file_get_contents("https://api.github.com/repos/".$pRepoOwner."/".$pRepoName."/branches/".$pRepoBranch, false, $pContext );
if(empty($branches_github))
{
$i++;
sleep(2);
echo "\nSomething went wrong with: ".$pRepoName.". Will try again in 2 sec.";
}
else
{
$exit = true;
}
}while(($i < 6) and (!$exit));
if($i == 5)
{
echo "Error loading branches for ".$pRepoOwner."/".$pRepoName."\n";
echo "\nThe thread was killed for your safety\n";
posix_kill(getmypid(), 9);
}
else
{
$branches_json = json_decode($branches_github, true);
if(strlen($lastCommitDBSHA[$pRepoId]) != 40)
{
$branches_github = file_get_contents("https://api.github.com/repos/".$pRepoOwner."/".$pRepoName."/commits?per_page=100&sha=".$branches_json['commit']['sha'], false, $pContext);
$branches_json = json_decode($branches_github, true);
$nbCommits = 0;
foreach($branches_json as $gitCommits)
{
mysql_query("INSERT INTO commits(SHA,GitUsername,Repository,Branch,Author,Message,CommitDate,VersionNo) VALUES('" . esc($gitCommits['sha']) ."', '".esc($pRepoOwner)."' ,'".esc($pRepoName)."', '".esc($pRepoBranch)."', '".esc($gitCommits['commit']['committer']['name'])."', '".esc($gitCommits['commit']['message'])."', '".esc(githubDate($gitCommits['commit']['committer']['date']))."','".$versionNo[$pRepoId]."');") or die(mysql_error());
$nbCommits++;
}
echo "\n----------------------------------------------------------------------------------------------------";
echo "\nRepo ".$pRepoName. " at branch ".$pRepoBranch." is fresh and added ".$nbCommits." commits.\n";
echo "\n----------------------------------------------------------------------------------------------------";
posix_kill(getmypid(), 9);
}
elseif($branches_json['commit']['sha'] != $lastCommitDBSHA[$pRepoId] && $branches_json['commit']['sha'] != "")
{
$branches_github = file_get_contents("https://api.github.com/repos/".$pRepoOwner."/".$pRepoName."/commits?per_page=100&sha=".$branches_json['commit']['sha'], false, $pContext);
$branches_json = json_decode($branches_github, true);
$doneFlag = false;
$nbCommits = 0;
foreach($branches_json as $gitCommits)
{
if(($lastCommitDBSHA[$pRepoId] != $gitCommits['sha']) and !$doneFlag)
{
mysql_query("INSERT INTO commits(SHA,GitUsername,Repository,Branch,Author,Message,CommitDate,VersionNo) VALUES('" . esc($gitCommits['sha']) ."', '".esc($pRepoOwner)."' ,'".esc($pRepoName)."', '".esc($pRepoBranch)."', '".esc($gitCommits['commit']['committer']['name'])."', '".esc($gitCommits['commit']['message'])."', '".esc(githubDate($gitCommits['commit']['committer']['date']))."','".$versionNo[$pRepoId]."');") or die(mysql_error());
$nbCommits++;
}
else
{
$doneFlag = true;
}
}
echo "\n----------------------------------------------------------------------------------------------------";
echo "\nAdded ".$nbCommits." commits into ".$pRepoName."\n";
echo "\n----------------------------------------------------------------------------------------------------";
posix_kill(getmypid(), 9);
}
else
{
//This is used to know if the Repo were up to date. Create too much overhead
// in the output so it's disabled.
//echo "\n".$branches_json['commit']['sha']." ".$lastCommitDBSHA[$pRepoId];
echo "\n----------------------------------------------------------------------------------------------------";
echo "Repo ".$pRepoName. " at branch ".$pRepoBranch." was up to date.\n";
echo "\n----------------------------------------------------------------------------------------------------";
posix_kill(getmypid(), 9);
}
}
}
// Here we start the import for the commits
$distinctRepo = 0;
$Threads = array();
for ($i = 0; $i < 12; $i++) {
$Threads[$i] = new Thread("fetchLastCommitSHA");
}
foreach($repoID as $repoNo)
{
$threadFound = false;
while (!$threadFound)
{
usleep(50);
$i=1;
foreach($Threads as $thread)
{
if (!$thread->isAlive())
{
if($i%2)
{
$threadFound = true;
$thread->start($repoNo, $repoOwner[$repoNo], $repoName[$repoNo], $repoBranch[$repoNo], $CFG, $CFG['context'][0]);
break;
}
else
{
$threadFound = true;
$thread->start($repoNo, $repoOwner[$repoNo], $repoName[$repoNo], $repoBranch[$repoNo], $CFG, $CFG['context'][1]);
break;
}
}
$i++;
}
}
}
echo "\n=================================";
echo "\nOperations done! Waiting on threads to finish...";
echo "\n=================================";
$threadsDone = true;
$selfkill = false;
$overtime = 0;
do
{
$threadsDone = true;
foreach ($Threads as $index => $thread)
{
if ($thread->isAlive())
{
echo "\nWaiting thread $index...";
$threadsDone = false;
break;
}
}
sleep(1);
$overtime++;
if ($overtime >= 10) {
echo "\nWarning: Timed out waiting on threads to finish. I'll kill them ALL WITH FIRE!";
foreach($Threads as $thread)
{
$thread->kill();
}
break;
}
} while ($threadsDone == false);
mysql_query("COMMIT;") or die(mysql_error());
if (isset($params["c"])) {
// We keep only the commits of the last 4 months
echo "Cleaning 4+months old commits\n";
mysql_error("DELETE FROM commits WHERE CommitDate < '".date("Y-m-d H:i:s", time()-3600*24*31*4). "'") or die(mysql_error());
echo "Cleaned " . mysql_affected_rows() . " commits\n";
}
//echo "\nGITHUB REQUESTS: " . $nbGitHubRequests . "\n";
//echo "\nFor ".$distinctRepo." distincts repos.";
//echo "\n";
$duration = floatval(microtime(true) - $startTime);
echo "\n\nTook ".$duration." seconds to verify and update ALL THE REPOS\n";
$nbrequest = file_get_contents("https://api.github.com/rate_limit", false, $CFG['context'][1]);
$nbrequest_decoded = json_decode($nbrequest,true);
echo "\nWe have : ".$nbrequest_decoded['rate']['remaining']." GitHub requests left \n";
ob_end_flush();
?>