Skip to content

Instantly share code, notes, and snippets.

@Jelle-S
Created June 17, 2025 12:20
Show Gist options
  • Save Jelle-S/72e4bd84c69ff8f8d2b4c405a1d99a65 to your computer and use it in GitHub Desktop.
Save Jelle-S/72e4bd84c69ff8f8d2b4c405a1d99a65 to your computer and use it in GitHub Desktop.
Get all repos a Github app has access to within an organisation, given the app id, installation id and private key.
<?php
/**
* composer require knplabs/github-api:^3.16 lcobucci/jwt:^4.1
*/
require 'vendor/autoload.php';
use Github\Client;
use Github\HttpClient\Builder;
use Github\AuthMethod;
use Github\ResultPager;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Signer\Key\LocalFileReference;
use Lcobucci\JWT\Signer\Rsa\Sha256;
$appId = '';
$installationId = '';
$orgName = '';
$privateKeyPath = '';
$builder = new Builder();
$github = new Client($builder, 'machine-man-preview');
$config = Configuration::forAsymmetricSigner(
new Sha256(),
LocalFileReference::file($privateKeyPath),
LocalFileReference::file($privateKeyPath)
);
$now = new \DateTimeImmutable();
$jwt = $config->builder(ChainedFormatter::withUnixTimestampDates())
->issuedBy($appId)
->issuedAt($now)
->expiresAt($now->modify('+1 minute'))
->getToken($config->signer(), $config->signingKey())
;
$github->authenticate($jwt->toString(), null, AuthMethod::JWT);
$tokenData = $github->apps()->createInstallationToken($installationId);
$installationToken = $tokenData['token'];
$github->authenticate($installationToken, null, AuthMethod::ACCESS_TOKEN);
$pager = new ResultPager($github);
$repositories = $pager->fetchAll(
$github->organizations(),
'repositories',
[$orgName]
);
foreach ($repositories as $repo) {
echo $repo['full_name'] . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment