Skip to content

Instantly share code, notes, and snippets.

@mmollick
Last active July 18, 2020 08:11
Show Gist options
  • Save mmollick/7a667f0bfd41f8ba3ca15fa24df0e79d to your computer and use it in GitHub Desktop.
Save mmollick/7a667f0bfd41f8ba3ca15fa24df0e79d to your computer and use it in GitHub Desktop.
{
"name": "mmollick/github-team-repos",
"authors": [
{
"name": "Mike Mollick",
"email": "[email protected]"
}
],
"require": {
"guzzlehttp/guzzle": "^7.0"
}
}
<?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