forked from PrestaShop/traces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraces
executable file
·97 lines (80 loc) · 4.2 KB
/
traces
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
#!/usr/bin/env php
<?php
function includeIfExists($file)
{
if (file_exists($file)) {
return include $file;
}
}
if ((!$loader = includeIfExists(__DIR__.'/vendor/autoload.php')) && (!$loader = includeIfExists(__DIR__.'/../../autoload.php'))) {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
'curl -sS https://getcomposer.org/installer | php'.PHP_EOL.
'php composer.phar install'.PHP_EOL);
}
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Yaml\Yaml;
const GITHUB_CONTRIBUTORS_POINT = 'https://api.github.com/repos/{repository}/contributors';
const GITHUB_USERS_POINT = 'https://api.github.com/users/{login}';
const REGEX = '/=([[:digit:]]+)>; rel="last"/';
/**
* @author Mickaël Andrieu <[email protected]>
*
* A simple CLI tool to generate information about GitHub contributors of a repository.
*/
(new Application('traces', '1.0.0'))
->register('traces')
->setDescription('A simple CLI tool to generate information about GitHub contributors of a repository.')
->addArgument('repository', InputArgument::REQUIRED, 'GitHub repository, for instance PrestaShop/PrestaShop')
->addArgument('user', InputArgument::REQUIRED, 'GitHub username, for instance `johndoe`')
->addArgument('password', InputArgument::REQUIRED, 'GitHub password, for instance `secr3tSt0rY`')
->addOption('config', null, InputOption::VALUE_REQUIRED, 'Configuration file, for exclusions')
->setCode(function(InputInterface $input, OutputInterface $output) {
$io = new SymfonyStyle($input, $output);
$repository = $input->getArgument('repository');
$user = $input->getArgument('user');
$password = $input->getArgument('password');
$config = $input->getOption('config');
$authHeaders = ['auth_basic' => [$user, $password]];
$requestUri = str_replace('{repository}', $repository, GITHUB_CONTRIBUTORS_POINT);
$client = HttpClient::create();
$response = $client->request('GET', $requestUri, ['timeout' => 2.0]);
$headers = $response->getHeaders();
if ('application/json; charset=utf-8' === $headers['content-type'][0]) {
if(array_key_exists('link', $headers)){
$headerValue = $headers['link'][0];
preg_match(REGEX, $headerValue, $matches);
$nbPages = $matches[1];
$allContributors = [];
for ($i = 1; $i <= $nbPages; $i++) {
$response = $client->request('GET', $requestUri . '?page=' . $i, $authHeaders);
$contributors = json_decode($response->getContent(), true);
$allContributors = array_merge($allContributors, $contributors);
}
$users = [];
$excludes = $config ? Yaml::parse(file_get_contents($config))['config']['exclusions'] : [];
foreach($allContributors as $contributor) {
if (in_array($contributor['login'], $excludes)) {
continue;
}
$requestUri = str_replace('{login}', $contributor['login'], GITHUB_USERS_POINT);
$response = $client->request('GET', $requestUri, ['headers' => $authHeaders]);
$user = json_decode($response->getContent(), true);
$user['contributions'] = $contributor['contributions'];
$users[] = $user;
}
$fs = new Filesystem();
$fs->dumpFile('contributors.js', json_encode($users, JSON_PRETTY_PRINT));
}
}
$io->success(sprintf('%s contributors found for the repository "%s".', count($users), $repository));
})
->getApplication()
->setDefaultCommand('traces', true)
->run();