-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworker.php
71 lines (62 loc) · 2.91 KB
/
worker.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
<?php
// Heroku Scheduler Command
// php -c $HOME/conf/etc.d/04_mongo.ini $HOME/worker.php
$appPath = __DIR__;
$prCheckPath = "${appPath}/bin/prCheck";
$githubCommentPath = "${appPath}/bin/githubComment";
$githubToken = getenv('GITHUB_API_TOKEN');
$guthubApiUrl = getenv('GITHUB_API_URL') ?: 'https://api.github.com';
$mongoUrl = parse_url(getenv('MONGOHQ_URL'));
$dbName = str_replace('/', '', $mongoUrl['path']);
$m = new Mongo(getenv('MONGOHQ_URL'));
$db = $m->$dbName;
$pulls = $db->pulls;
$pullResults = $db->pullResults;
foreach ($pulls->find() as $pull) {
echo "Handling PR #{$pull['number']} for {$pull['repository']}\n";
$pulls->remove($pull);
$curl = curl_init("{$guthubApiUrl}/repos/{$pull['repository']}/pulls/{$pull['number']}?access_token={$githubToken}");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('User-Agent: CodeStandardChecker/1.0'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($curl), true);
if (is_array($result) && array_key_exists('state', $result) && $result['state'] !== 'open') {
echo "Pull Request is closed.\n";
continue;
}
$repositoryPath = "{$appPath}/data/{$pull['repository']}";
$command = "{$prCheckPath} -r origin -p {$pull['number']} -g {$githubToken} -R {$pull['repository']} -s {$repositoryPath}";
echo "Executing {$command}\n";
$exitStatus = null;
$output = null;
exec($command, $output, $exitStatus);
$fields = explode(' ', implode(' ', $output));
if ($exitStatus !== 0 || count($fields) !== 5) {
echo "Unexpected number of fields in prCheck result.\n";
$pulls->insert($pull);
continue;
}
$pullQuery = array('repository' => $pull['repository'], 'number' => $pull['number']);
$results = array();
list($results['filesAdded'], $results['linesAdded'], $results['probFilesAdded'], $results['errorsAdded'], $results['warnAdded']) = $fields;
$previousResults = $pullResults->findOne($pullQuery, array('_id' => false, 'number' => false, 'repository' => false));
if ($previousResults == $results) {
echo "Results were identical to last run.\n";
continue;
}
$resultMessage = function($type, $i) {
$addedOrDeleted = $i >= 0 ? 'Added' : 'Deleted';
return "{$type} {$addedOrDeleted}: " . abs($i);
};
$message = implode(
"\n",
array(
$resultMessage('Files', $results['filesAdded']),
$resultMessage('Lines', $results['linesAdded']),
$resultMessage('Problem Files', $results['probFilesAdded']),
$resultMessage('PHPCS Errors', $results['errorsAdded']),
$resultMessage('PHPCS Warnings', $results['warnAdded']),
)
);
passthru("{$githubCommentPath} -R {$pull['repository']} -p {$pull['number']} -g {$githubToken} -m " . escapeshellarg($message));
$pullResults->update($pullQuery, array('$set' => $results), array('upsert' => true));
}