-
Notifications
You must be signed in to change notification settings - Fork 5
/
minicli
executable file
·72 lines (57 loc) · 2.18 KB
/
minicli
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
#!/usr/bin/php
<?php
if (php_sapi_name() !== 'cli') {
exit;
}
require __DIR__ . '/vendor/autoload.php';
use Minicli\App;
use Minicli\Curly\Client;
use Minicli\Stencil;
$ignoreUsers = getenv('CONTRIB_IGNORE') ?: 'github-actions[bot],renovate-bot,dependabot';
$app = new App([
'app_path' => __DIR__ . '/app/Command',
'repository' => getenv('CONTRIB_REPOSITORY') ?: null,
'output_file' => getenv('CONTRIB_OUTPUT_FILE') ?: 'CONTRIBUTORS.md',
'stencil_dir' => getenv('CONTRIB_STENCIL_DIR') ?: __DIR__ . '/.stencil',
'template' => getenv('CONTRIB_TEMPLATE') ?: 'contributors',
'ignore_users' => explode(',', $ignoreUsers)
]);
$app->registerCommand('update-contributors', function () use ($app) {
if (!$app->config->has('repository')) {
$app->getPrinter()->error("Missing CONTRIB_REPOSITORY environment variable.");
return 1;
}
$app->getPrinter()->info('Fetching top contributors...');
$client = new Client();
$response = $client->get(
"https://api.github.com/repos/" . $app->config->repository . "/contributors",
['Accept: application/vnd.github.v3+json', 'User-Agent: Curly']
);
if ($response['code'] != 200) {
$app->getPrinter()->error("an error occurred: " . $response['code']);
return 1;
}
$stencil = new Stencil($app->config->stencil_dir);
$content = "";
foreach (json_decode($response['body']) as $item) {
if (!in_array($item->login, $app->config->ignore_users)) {
$content .= "- [$item->login]($item->html_url)\n";
}
}
$output = $stencil->applyTemplate($app->config->template, [
'repo' => $app->config->repository,
'content' => $content,
'updated' => (new DateTimeImmutable())->format(DateTimeInterface::RFC822)
]);
try {
$contrib_file = fopen($app->config->output_file, 'w+');
fwrite($contrib_file, $output);
fclose($contrib_file);
} catch (Exception $exception) {
$app->getPrinter()->error("An error occurred while trying to save the contrib file.");
return 1;
}
$app->getPrinter()->success("Finished updating contrib file.");
return 0;
});
$app->runCommand($argv);