diff --git a/lib/helpers/github-app.js b/lib/helpers/github-app.js index 8e115e54a098a8fa6304d4cc510407d6d2569593..a03a83518d9c7ececbf9e18c5be6fcf2247c42bb 100644 --- a/lib/helpers/github-app.js +++ b/lib/helpers/github-app.js @@ -39,6 +39,10 @@ async function getUserRepositories(appToken, installationId) { async function getRepositories(config) { logger.debug(`githubAppHelper.getRepositories`); + const configuredRepositories = config.repositories.map( + repository => + typeof repository === 'string' ? repository : repository.repository + ); let installedRepos = []; try { const appToken = module.exports.generateJwt( @@ -49,11 +53,17 @@ async function getRepositories(config) { logger.info(`Found installations for ${installations.length} users`); for (const installation of installations) { logger.debug(JSON.stringify(installation)); - const installationRepos = await module.exports.getUserRepositories( + let installationRepos = await module.exports.getUserRepositories( appToken, installation.id ); logger.debug(`installationRepos=${JSON.stringify(installationRepos)}`); + if (configuredRepositories.length) { + installationRepos = installationRepos.filter( + repository => + configuredRepositories.indexOf(repository.repository) !== -1 + ); + } installedRepos = installedRepos.concat(installationRepos); } } catch (err) { diff --git a/test/helpers/__snapshots__/github-app.spec.js.snap b/test/helpers/__snapshots__/github-app.spec.js.snap index e20a00364bea12ccc0d2236877f3dcdde60af40b..6a84905b256786d9e2a9a8ebadf75bf6528baab8 100644 --- a/test/helpers/__snapshots__/github-app.spec.js.snap +++ b/test/helpers/__snapshots__/github-app.spec.js.snap @@ -1,5 +1,18 @@ // 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`] = ` Array [ Object { diff --git a/test/helpers/github-app.spec.js b/test/helpers/github-app.spec.js index 007e1cb748d2ebc086086e3c1cbb01fb67eb67c9..ee26ffaa01ccae4f4a5ea9f625ba52e6a37af9a4 100644 --- a/test/helpers/github-app.spec.js +++ b/test/helpers/github-app.spec.js @@ -38,6 +38,7 @@ describe('helpers/github-app', () => { const config = { githubAppId: 123, githubAppKey: 'some_key', + repositories: [], }; beforeEach(() => { githubAppHelper.generateJwt = jest.fn(); @@ -90,5 +91,35 @@ describe('helpers/github-app', () => { const results = await githubAppHelper.getRepositories(config); 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(); + }); }); });