Last active
July 18, 2020 08:11
-
-
Save mmollick/7a667f0bfd41f8ba3ca15fa24df0e79d to your computer and use it in GitHub Desktop.
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
{ | |
"name": "mmollick/github-team-repos", | |
"authors": [ | |
{ | |
"name": "Mike Mollick", | |
"email": "[email protected]" | |
} | |
], | |
"require": { | |
"guzzlehttp/guzzle": "^7.0" | |
} | |
} |
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 | |
require_once __DIR__ . '/vendor/autoload.php'; | |
// Create personal access token and place here | |
$access_token = ''; | |
// Set organization name as it appears in the Github URLs | |
$org = ''; | |
// Set team name as it appears in the Github URLs | |
$teamName = ''; | |
$client = new \GuzzleHttp\Client([ | |
'base_uri' => 'https://api.github.com/resource' | |
]); | |
function request(string $method, string $url, array $params = []) : \Psr\Http\Message\ResponseInterface { | |
global $access_token, $client; | |
return $client->request($method, $url, [ | |
'query' => array_merge($params, [ | |
'access_token' => $access_token, | |
]) | |
]); | |
} | |
// Get list of repositories from Github organization | |
$response = request('GET', "/orgs/${org}/repos", ['type' => 'all']); | |
$hasNextPage = str_contains($response->getHeader('Link')[0], 'next'); | |
$repos = json_decode($response->getBody()); | |
$page = 2; | |
while ($hasNextPage) { | |
$response = request('GET', "/orgs/${org}/repos", ['page' => $page, 'type' => 'all']); | |
$hasNextPage = str_contains($response->getHeader('Link')[0], 'next'); | |
$repos = array_merge($repos, json_decode($response->getBody())); | |
$page++; | |
} | |
// Set access for each repo for the team. "permission" denotes what kind of access type they have, "pull" is read only | |
foreach ($repos as $repo) { | |
fwrite(STDOUT, $repo->full_name . PHP_EOL); | |
request('PUT', "/orgs/${org}/teams/${teamName}/repos/" . strtolower($repo->full_name), ['permission' => 'pull']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment