Skip to content

Instantly share code, notes, and snippets.

@harikt
Last active June 10, 2016 16:01
Show Gist options
  • Save harikt/4edac0007c715d75a9ed2ded1c84cf32 to your computer and use it in GitHub Desktop.
Save harikt/4edac0007c715d75a9ed2ded1c84cf32 to your computer and use it in GitHub Desktop.
Get all the contributors from the repos of an organization
{
"require": {
"guzzlehttp/guzzle": "4.*"
}
}
<?php
$username = 'username';
$password = 'api-key';
$organization = 'auraphp';
$file = __DIR__ . '/contributors.json';
require __DIR__ . '/vendor/autoload.php';
if (file_exists($file)) {
$json_string = file_get_contents($file);
$contributors = json_decode($json_string, true);
} else {
$contributors = [];
}
$client = new GuzzleHttp\Client();
$last_page = $page = 1;
do {
$org_url = "https://api.github.com/orgs/{$organization}/repos?per_page=30&page={$page}";
$request = $client->get($org_url, ['auth' => [$username, $password]]);
$repos = $request->json();
if ($page == 1 && $request->getHeader('link')) {
preg_match('/page=(\d+)>; rel="last"$/', $request->getHeader('link'), $matches);
$last_page = $matches[1];
}
foreach ($repos as $repo) {
$contrib_last_page = $contrib_page = 1;
do {
$repo_url = "https://api.github.com/repos/{$organization}/{$repo['name']}/contributors?per_page=30&page={$contrib_page}";
try {
$request_repo = $client->get($repo_url, ['auth' => [$username, $password]]);
$repo_contributors = $request_repo->json();
if ($contrib_page == 1 && $request_repo->getHeader('link')) {
preg_match('/page=(\d+)>; rel="last"$/', $request_repo->getHeader('link'), $matches);
$contrib_last_page = $matches[1];
}
foreach ($repo_contributors as $contributor) {
if (! isset($contributors[$contributor['login']])) {
$login = $contributor['login'];
try {
$request = $client->get("https://api.github.com/users/$login", ['auth' => [$username, $password]]);
$result = $request->json();
} catch (Exception $e) {
}
$name = !empty($result['name']) ? $result['name'] : $contributor['login'];
$contributors[$contributor['login']] = array(
'html_url' => $contributor['html_url'],
'avatar_url' => $contributor['avatar_url'],
'name' => $name
);
}
}
$contrib_page++;
} catch (Exception $e) {
echo $e->getMessage();
}
} while($contrib_page <= $contrib_last_page);
}
$page++;
} while($page <= $last_page);
file_put_contents($file, json_encode($contributors, JSON_PRETTY_PRINT));
echo "Total contributors : " . count($contributors) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment