From ec35b6f76489d6a3af1337926457c0ff61b22f42 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@keylocation.sg>
Date: Mon, 26 Jun 2017 09:43:24 +0200
Subject: [PATCH] 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
---
 lib/helpers/github-app.js                     | 12 ++++++-
 .../__snapshots__/github-app.spec.js.snap     | 13 ++++++++
 test/helpers/github-app.spec.js               | 31 +++++++++++++++++++
 3 files changed, 55 insertions(+), 1 deletion(-)

diff --git a/lib/helpers/github-app.js b/lib/helpers/github-app.js
index 8e115e54a0..a03a83518d 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 e20a00364b..6a84905b25 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 007e1cb748..ee26ffaa01 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();
+    });
   });
 });
-- 
GitLab