-
-
Save samsonasik/cf631762de1a39bf88a7be5c9b6a5801 to your computer and use it in GitHub Desktop.
Get all the contributors from the repos of an organization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"guzzlehttp/guzzle": "4.*" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
$request = $client->get("https://api.github.com/orgs/{$organization}/repos?per_page=100", ['auth' => [$username, $password]]); | |
$repos = $request->json(); | |
foreach ($repos as $repo) { | |
$repo_url = "https://api.github.com/repos/{$organization}/{$repo['name']}/contributors"; | |
try { | |
$request = $client->get($repo_url, ['auth' => [$username, $password]]); | |
$repo_contributors = $request->json(); | |
foreach ($repo_contributors as $contributor) { | |
if (isset($contributors[$contributor['login']])) { | |
} else { | |
$login = $contributor['login']; | |
try { | |
// if you don't want the name of the user as Hari KT, can remove this fetch | |
$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 | |
); | |
} | |
} | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
} | |
} | |
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