Skip to content
Snippets Groups Projects
Commit ec35b6f7 authored by Rhys Arkins's avatar Rhys Arkins Committed by GitHub
Browse files

Enable CLI override of app repositories list (#358)

* Filter GitHub App repositories list if configured via CLI

Closes #354

* Fix existing tests

* Add new test
parent 49703e90
No related branches found
No related tags found
No related merge requests found
...@@ -39,6 +39,10 @@ async function getUserRepositories(appToken, installationId) { ...@@ -39,6 +39,10 @@ async function getUserRepositories(appToken, installationId) {
async function getRepositories(config) { async function getRepositories(config) {
logger.debug(`githubAppHelper.getRepositories`); logger.debug(`githubAppHelper.getRepositories`);
const configuredRepositories = config.repositories.map(
repository =>
typeof repository === 'string' ? repository : repository.repository
);
let installedRepos = []; let installedRepos = [];
try { try {
const appToken = module.exports.generateJwt( const appToken = module.exports.generateJwt(
...@@ -49,11 +53,17 @@ async function getRepositories(config) { ...@@ -49,11 +53,17 @@ async function getRepositories(config) {
logger.info(`Found installations for ${installations.length} users`); logger.info(`Found installations for ${installations.length} users`);
for (const installation of installations) { for (const installation of installations) {
logger.debug(JSON.stringify(installation)); logger.debug(JSON.stringify(installation));
const installationRepos = await module.exports.getUserRepositories( let installationRepos = await module.exports.getUserRepositories(
appToken, appToken,
installation.id installation.id
); );
logger.debug(`installationRepos=${JSON.stringify(installationRepos)}`); logger.debug(`installationRepos=${JSON.stringify(installationRepos)}`);
if (configuredRepositories.length) {
installationRepos = installationRepos.filter(
repository =>
configuredRepositories.indexOf(repository.repository) !== -1
);
}
installedRepos = installedRepos.concat(installationRepos); installedRepos = installedRepos.concat(installationRepos);
} }
} catch (err) { } catch (err) {
......
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`helpers/github-app getRepositories returns filtered list of repos 1`] = `
Array [
Object {
"repository": "a/b",
"token": "token_a",
},
Object {
"repository": "d/f",
"token": "token_d",
},
]
`;
exports[`helpers/github-app getRepositories returns list of repos 1`] = ` exports[`helpers/github-app getRepositories returns list of repos 1`] = `
Array [ Array [
Object { Object {
......
...@@ -38,6 +38,7 @@ describe('helpers/github-app', () => { ...@@ -38,6 +38,7 @@ describe('helpers/github-app', () => {
const config = { const config = {
githubAppId: 123, githubAppId: 123,
githubAppKey: 'some_key', githubAppKey: 'some_key',
repositories: [],
}; };
beforeEach(() => { beforeEach(() => {
githubAppHelper.generateJwt = jest.fn(); githubAppHelper.generateJwt = jest.fn();
...@@ -90,5 +91,35 @@ describe('helpers/github-app', () => { ...@@ -90,5 +91,35 @@ describe('helpers/github-app', () => {
const results = await githubAppHelper.getRepositories(config); const results = await githubAppHelper.getRepositories(config);
expect(results).toMatchSnapshot(); expect(results).toMatchSnapshot();
}); });
it('returns filtered list of repos', async () => {
ghApi.getInstallations.mockImplementationOnce(() => [
{ id: 567 },
{ id: 568 },
]);
githubAppHelper.getUserRepositories.mockImplementationOnce(() => [
{
repository: 'a/b',
token: 'token_a',
},
{
repository: 'a/c',
token: 'token_a',
},
]);
githubAppHelper.getUserRepositories.mockImplementationOnce(() => [
{
repository: 'd/e',
token: 'token_d',
},
{
repository: 'd/f',
token: 'token_d',
},
]);
config.repositories = ['a/b', 'd/f', 'x/y'];
const results = await githubAppHelper.getRepositories(config);
expect(results.length).toBe(2);
expect(results).toMatchSnapshot();
});
}); });
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment