forked from BBQDroid/BBQLog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GitHubUpdater.php
executable file
·177 lines (140 loc) · 5.86 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
<?php
/**
* GitHub Updater script
* Fetch changes from GitHub and pull them into a local database
*
* Copyright (c) The BBQTeam 2012
*
*/
if (php_sapi_name() != "cli") {
die("Changelog updater must be run from CLI!");
}
// Configs
set_time_limit(0);
require_once("config.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");
}
// Flush after every echo
ob_implicit_flush(TRUE);
$nbGitHubRequests = 0;
// Connect to MySQL
mysql_connect($CFG['SQL']['Host'], $CFG['SQL']['User'], $CFG['SQL']['Pass']) 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);
// Start import
echo "Starting repositories processing...\n";
$params = getopt("c", array("repo::"));
if (!empty($params["repo"])) {
echo "Processing ".$params["repo"]."\n";
$repo = $params["repo"];
}
// If a specific repo is passed in GET, update only this repository
if (!empty($params["repo"])) {
$repositories = mysql_query("SELECT * FROM repositories WHERE Repository='".esc($params["repo"])."' ");
} else {
$repositories = mysql_query("SELECT DISTINCT * FROM repositories GROUP BY GitUsername,Repository;") or die(mysql_error());
}
while ($repo = mysql_fetch_assoc($repositories)) {
echo "\n".str_repeat("-", 100)."\n";
echo "Processing ".$repo['GitUsername']."/".$repo['Repository']."\n";
// load last commit of the branch
$i = 0;
while (!@$branches_github = file_get_contents("https://api.github.com/repos/".$repo['GitUsername']."/".$repo['Repository']."/branches")) {
$i++;
if ($i > 5) {
echo "Error loading branches for ".$repo['GitUsername']."/".$repo['Repository']."\n";
continue 2;
}
sleep(3);
}
$nbGitHubRequests++;
$branches_json = json_decode($branches_github, true);
$branches_sql = mysql_query("SELECT Branch FROM repositories WHERE GitUsername='".esc($repo['GitUsername'])."' AND Repository='".esc($repo['Repository'])."'");
while ($branch = mysql_fetch_assoc($branches_sql)) {
// get the latest commit in db
$query = mysql_query("SELECT SHA FROM commits WHERE GitUsername='".esc($repo['GitUsername'])."' AND Repository='".esc($repo['Repository'])."' AND Branch='".esc($branch['Branch'])."' ORDER BY CommitDate DESC LIMIT 1");
$fetch = mysql_fetch_assoc($query);
$lastCommitDB = $fetch['SHA'];
// get the latest commit on GitHub
$lastCommitSHA = "";
foreach($branches_json as $gitBranch) {
if ($gitBranch['name'] == $branch['Branch'] && ($lastCommitDB == "" || $lastCommitSHA != $lastCommitDB)) {
$lastCommitSHA = $gitBranch['commit']['sha'];
echo "Found branch " . $branch['Branch'] . " (last commit: $lastCommitSHA)\n";
break;
}
}
if (empty($lastCommitSHA)) {
// the last commit in DB is already the latest one of the branch, skipping
continue;
}
echo "Fetching commits from ".$repo['GitUsername']."/".$repo['Repository']." from branch ".$branch['Branch']."\n";
$commitSHA = $lastCommitSHA;
$nbFetched = 0;
$previousFetchedCommit = "";
$inserts = 0;
// we limit max 20 requests per branch (thats 2000 commits)
while ($nbFetched < 20 && $previousFetchedCommit != $commitSHA) {
$previousFetchedCommit = $commitSHA;
$i = 0;
while (!@$commits_github = file_get_contents("https://api.github.com/repos/".$repo['GitUsername']."/".$repo['Repository']."/commits?per_page=100&sha=$commitSHA")) {
$i++;
if ($i > 5) {
echo "Error loading changes for ".$repo['GitUsername']."/".$repo['Repository'].":".$commitSHA;
continue 4;
}
sleep(3);
}
$commits_json = json_decode($commits_github,true);
$nbGitHubRequests++;
$lastReached = false;
foreach($commits_json as $commit) {
if ($commit['sha'] == $lastCommitDB || strtotime(githubDate($commit['commit']['committer']['date'])) < time()-3600*24*31*4) { // max 4 months
$lastReached = true;
break;
}
// Merge commits
//if ($commit['committer'] == null) {
// mysql_query("UPDATE commits SET CommitDate='".githubDate($commit['commit']['committer']['date'])."' WHERE SHA='".$commit['parents'][1]["sha"]."'");
//} else {
mysql_query("INSERT IGNORE INTO commits(SHA,GitUsername,Repository,Branch,Author,Message,CommitDate) VALUES('" . esc($commit['sha']) ."', '".esc($repo['GitUsername'])."' ,'".esc($repo['Repository'])."', '".esc($branch['Branch'])."', '".esc($commit['committer']['login'])."', '".esc($commit['commit']['message'])."', '".esc(githubDate($commit['commit']['committer']['date']))."');") or die(mysql_error());
//}
$inserts++;
$commitSHA = $commit['sha'];
}
if ($lastReached) {
break;
}
echo "Commits batch imported\n";
$nbFetched++;
}
echo "Done fetching branch " . $branch['Branch'] . " of repo " . $repo['Repository'] . " ($inserts inserts)...\n";
}
echo "\n";
}
mysql_query("COMMIT;") or die(mysql_error());
echo mysql_num_rows($repositories)." repositories updated\n";
if (isset($params["c"])) {
// We keep only the commits of the last 4 months
echo "Cleaning 4+months old commits\n";
mysql_query("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 "GITHUB REQUESTS: " . $nbGitHubRequests . "\n";
echo "\n";
$duration = floatval(microtime(true) - $startTime);
echo $duration."s elapsed\n";
?>