Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use "and" logic for positive and negative autodiscoverFilter values #33774

Open
rarkins opened this issue Jan 22, 2025 · 0 comments
Open

Use "and" logic for positive and negative autodiscoverFilter values #33774

rarkins opened this issue Jan 22, 2025 · 0 comments
Labels
breaking Breaking change, requires major version bump core:config Related to config capabilities and presets priority-3-medium Default priority, "should be done" but isn't prioritised ahead of others type:feature Feature (new functionality)

Comments

@rarkins
Copy link
Collaborator

rarkins commented Jan 22, 2025

Describe the proposed change(s).

Instead of this currently documented limitation:

Only use a single negation and don't mix with other filters because all filters are combined with or.

We should use our more general "matchRegexOrGlob" logic so that negative filters are anded together.

Breaking change so needs a major release. This is potentially all that's required:

diff --git a/lib/workers/global/autodiscover.spec.ts b/lib/workers/global/autodiscover.spec.ts
index 8af94614d..5aa73c12e 100644
--- a/lib/workers/global/autodiscover.spec.ts
+++ b/lib/workers/global/autodiscover.spec.ts
@@ -147,19 +147,6 @@ describe('workers/global/autodiscover', () => {
     expect(res.repositories).toEqual(['project/another-repo']);
   });
 
-  it('fail if regex pattern is not valid', async () => {
-    config.autodiscover = true;
-    config.autodiscoverFilter = ['/project/re**./'];
-    config.platform = 'github';
-    hostRules.find = jest.fn(() => ({
-      token: 'abc',
-    }));
-    ghApi.getRepos = jest.fn(() =>
-      Promise.resolve(['project/repo', 'project/another-repo']),
-    );
-    await expect(autodiscoverRepositories(config)).rejects.toThrow();
-  });
-
   it('filters autodiscovered github repos with multiple values', async () => {
     config.autodiscover = true;
     config.autodiscoverFilter = ['another-project/re*', 'department/dev/*'];
@@ -196,4 +183,23 @@ describe('workers/global/autodiscover', () => {
     const res = await autodiscoverRepositories(config);
     expect(res.repositories).toEqual(['project/repo', 'PROJECT/repo2']);
   });
+
+  it('ands together positive and negative matches', async () => {
+    config.autodiscover = true;
+    config.autodiscoverFilter = ['asdf/**', '!asdf/config/**'];
+    config.platform = 'github';
+    hostRules.find = jest.fn(() => ({
+      token: 'abc',
+    }));
+    ghApi.getRepos = jest.fn(() =>
+      Promise.resolve([
+        'asdf/foo',
+        'asdf/config/bar',
+        'asdf/baz/repo',
+        'apple/orange',
+      ]),
+    );
+    const res = await autodiscoverRepositories(config);
+    expect(res.repositories).toEqual(['asdf/foo', 'asdf/baz/repo']);
+  });
 });
diff --git a/lib/workers/global/autodiscover.ts b/lib/workers/global/autodiscover.ts
index 6f45baf90..f5915125b 100644
--- a/lib/workers/global/autodiscover.ts
+++ b/lib/workers/global/autodiscover.ts
@@ -3,7 +3,12 @@ import type { AllConfig } from '../../config/types';
 import { logger } from '../../logger';
 import { platform } from '../../modules/platform';
 import { minimatchFilter } from '../../util/minimatch';
-import { getRegexPredicate, isRegexMatch } from '../../util/string-match';
+import {
+  getRegexPredicate,
+  isRegexMatch,
+  matchRegexOrGlobList,
+} from '../../util/string-match';
+import { match } from 'assert';
 
 // istanbul ignore next
 function repoName(value: string | { repository: string }): string {
@@ -103,22 +108,7 @@ export async function autodiscoverRepositories(
 }
 
 export function applyFilters(repos: string[], filters: string[]): string[] {
-  const matched = new Set<string>();
-
-  for (const filter of filters) {
-    let res: string[];
-    if (isRegexMatch(filter)) {
-      const autodiscoveryPred = getRegexPredicate(filter);
-      if (!autodiscoveryPred) {
-        throw new Error(`Failed to parse regex pattern "${filter}"`);
-      }
-      res = repos.filter(autodiscoveryPred);
-    } else {
-      res = repos.filter(minimatchFilter(filter, { dot: true, nocase: true }));
-    }
-    for (const repository of res) {
-      matched.add(repository);
-    }
-  }
-  return repos.filter((repository) => matched.has(repository));
+  return repos.filter((repository) =>
+    matchRegexOrGlobList(repository, filters),
+  );
 }
@rarkins rarkins added breaking Breaking change, requires major version bump core:config Related to config capabilities and presets priority-3-medium Default priority, "should be done" but isn't prioritised ahead of others type:feature Feature (new functionality) labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Breaking change, requires major version bump core:config Related to config capabilities and presets priority-3-medium Default priority, "should be done" but isn't prioritised ahead of others type:feature Feature (new functionality)
Projects
None yet
Development

No branches or pull requests

1 participant