From 45739cddfde88bd933cfac3d1eb825ec5e2aab87 Mon Sep 17 00:00:00 2001
From: Emanuel Bennici <eb@fabmation.de>
Date: Mon, 19 Apr 2021 16:18:06 +0200
Subject: [PATCH] feat: Allow multiple autodiscover filter (#9453)

---
 docs/usage/self-hosted-configuration.md |  2 +-
 lib/config/definitions.ts               |  4 +++-
 lib/config/types.ts                     |  2 +-
 lib/workers/global/autodiscover.spec.ts | 24 +++++++++++++++++++++---
 lib/workers/global/autodiscover.ts      |  9 ++++++++-
 5 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/docs/usage/self-hosted-configuration.md b/docs/usage/self-hosted-configuration.md
index b6673d42fc..3f243012c1 100644
--- a/docs/usage/self-hosted-configuration.md
+++ b/docs/usage/self-hosted-configuration.md
@@ -81,7 +81,7 @@ e.g.
 
 ```json
 {
-  "autodiscoverFilter": "project/*"
+  "autodiscoverFilter": ["project/*"]
 }
 ```
 
diff --git a/lib/config/definitions.ts b/lib/config/definitions.ts
index ecc1d455c5..9d9d2c57f8 100644
--- a/lib/config/definitions.ts
+++ b/lib/config/definitions.ts
@@ -579,7 +579,9 @@ const options: RenovateOptions[] = [
     name: 'autodiscoverFilter',
     description: 'Filter the list of autodiscovered repositories.',
     stage: 'global',
-    type: 'string',
+    type: 'array',
+    subType: 'string',
+    allowString: true,
     default: null,
   },
   {
diff --git a/lib/config/types.ts b/lib/config/types.ts
index ba94103c94..cadba956bc 100644
--- a/lib/config/types.ts
+++ b/lib/config/types.ts
@@ -68,7 +68,7 @@ export interface RenovateSharedConfig {
 // The below should contain config options where stage=global
 export interface GlobalOnlyConfig {
   autodiscover?: boolean;
-  autodiscoverFilter?: string;
+  autodiscoverFilter?: string[];
   baseDir?: string;
   forceCli?: boolean;
   gitPrivateKey?: string;
diff --git a/lib/workers/global/autodiscover.spec.ts b/lib/workers/global/autodiscover.spec.ts
index 7779fcd41f..bec82c983a 100644
--- a/lib/workers/global/autodiscover.spec.ts
+++ b/lib/workers/global/autodiscover.spec.ts
@@ -49,18 +49,36 @@ describe(getName(__filename), () => {
   });
   it('filters autodiscovered github repos', async () => {
     config.autodiscover = true;
-    config.autodiscoverFilter = 'project/re*';
+    config.autodiscoverFilter = ['project/re*', 'new/prj*'];
     config.platform = PLATFORM_TYPE_GITHUB;
     hostRules.find = jest.fn(() => ({
       token: 'abc',
     }));
     ghApi.getRepos = jest.fn(() =>
-      Promise.resolve(['project/repo', 'project/another-repo'])
+      Promise.resolve([
+        'project/repo',
+        'project/another-repo',
+        'new/prj-test',
+        'new/not-matched',
+      ])
     );
     const res = await autodiscoverRepositories(config);
-    expect(res.repositories).toEqual(['project/repo']);
+    expect(res.repositories).toEqual(['project/repo', 'new/prj-test']);
   });
   it('filters autodiscovered github repos but nothing matches', async () => {
+    config.autodiscover = true;
+    config.autodiscoverFilter = ['project/re*'];
+    config.platform = 'github';
+    hostRules.find = jest.fn(() => ({
+      token: 'abc',
+    }));
+    ghApi.getRepos = jest.fn(() =>
+      Promise.resolve(['another-project/repo', 'another-project/another-repo'])
+    );
+    const res = await autodiscoverRepositories(config);
+    expect(res).toEqual(config);
+  });
+  it('filters autodiscovered github repos with string variable', async () => {
     config.autodiscover = true;
     config.autodiscoverFilter = 'project/re*';
     config.platform = 'github';
diff --git a/lib/workers/global/autodiscover.ts b/lib/workers/global/autodiscover.ts
index 396f32b9e2..bfc3b666d2 100644
--- a/lib/workers/global/autodiscover.ts
+++ b/lib/workers/global/autodiscover.ts
@@ -30,7 +30,14 @@ export async function autodiscoverRepositories(
     return config;
   }
   if (config.autodiscoverFilter) {
-    discovered = discovered.filter(minimatch.filter(config.autodiscoverFilter));
+    const matched = new Set<string>();
+
+    for (const filter of config.autodiscoverFilter) {
+      const res = minimatch.match(discovered, filter);
+      res.forEach((e) => matched.add(e));
+    }
+
+    discovered = [...matched];
     if (!discovered.length) {
       // Soft fail (no error thrown) if no accessible repositories match the filter
       logger.debug('None of the discovered repositories matched the filter');
-- 
GitLab