Skip to content

Instantly share code, notes, and snippets.

@tibdex
Created June 27, 2019 20:36
Show Gist options
  • Save tibdex/d6b63cc82356f8270d03ba56d85c7235 to your computer and use it in GitHub Desktop.
Save tibdex/d6b63cc82356f8270d03ba56d85c7235 to your computer and use it in GitHub Desktop.
List all repositories that a GitHub App can access.
const { App } = require("@octokit/app"); // ^3.0.0
const Octokit = require("@octokit/rest"); // ^16.25.0
const APP_ID = 1337;
const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
...
your private key content
...
-----END RSA PRIVATE KEY-----`;
const listAccessibleRepos = async ({ appId, privateKey }) => {
const app = new App({ id: appId, privateKey });
const jwt = await app.getSignedJsonWebToken();
const octokit = new Octokit({ auth: `Bearer ${jwt}` });
const options = octokit.apps.listInstallations.endpoint.merge();
const data = await octokit.paginate(options);
const installationIds = data.map(({ id }) => id);
const allUrls = await Promise.all(
installationIds.map(async installationId => {
const installationAccessToken = await app.getInstallationAccessToken({
installationId,
});
const installationOctokit = new Octokit({
auth: `token ${installationAccessToken}`,
previews: ["machine-man-preview"],
});
const {
data: { repositories },
} = await installationOctokit.apps.listRepos();
const urls = repositories.map(({ html_url }) => html_url);
return urls;
}),
);
console.log(allUrls);
};
listAccessibleRepos({ appId: APP_ID, privateKey: PRIVATE_KEY });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment