From 198e34e0ef6a65b35dc8867850651120cf40ba4b Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Sat, 3 Feb 2024 03:22:19 -0300
Subject: [PATCH] refactor: Tidy arguments for `matchRegexOrMinimatch` (#27026)

---
 lib/config/validation.ts               |  4 ++--
 lib/util/http/host-rules.ts            |  4 ++--
 lib/util/package-rules/match.ts        | 15 ++++-----------
 lib/util/package-rules/repositories.ts | 21 +++++++++++++++++++--
 4 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/lib/config/validation.ts b/lib/config/validation.ts
index bb648fcdc5..812d33e64d 100644
--- a/lib/config/validation.ts
+++ b/lib/config/validation.ts
@@ -706,7 +706,7 @@ export async function validateConfig(
     }
 
     if (key === 'hostRules' && is.array(val)) {
-      const allowedHeaders = GlobalConfig.get('allowedHeaders');
+      const allowedHeaders = GlobalConfig.get('allowedHeaders', []);
       for (const rule of val as HostRule[]) {
         if (!rule.headers) {
           continue;
@@ -718,7 +718,7 @@ export async function validateConfig(
               message: `Invalid hostRules headers value configuration: header must be a string.`,
             });
           }
-          if (!anyMatchRegexOrMinimatch(allowedHeaders, header)) {
+          if (!anyMatchRegexOrMinimatch(header, allowedHeaders)) {
             errors.push({
               topic: 'Configuration Error',
               message: `hostRules header \`${header}\` is not allowed by this bot's \`allowedHeaders\`.`,
diff --git a/lib/util/http/host-rules.ts b/lib/util/http/host-rules.ts
index 75919272ca..4eab58e539 100644
--- a/lib/util/http/host-rules.ts
+++ b/lib/util/http/host-rules.ts
@@ -165,11 +165,11 @@ export function applyHostRule<GotOptions extends HostRulesGotOptions>(
   }
 
   if (hostRule.headers) {
-    const allowedHeaders = GlobalConfig.get('allowedHeaders');
+    const allowedHeaders = GlobalConfig.get('allowedHeaders', []);
     const filteredHeaders: Record<string, string> = {};
 
     for (const [header, value] of Object.entries(hostRule.headers)) {
-      if (anyMatchRegexOrMinimatch(allowedHeaders, header)) {
+      if (anyMatchRegexOrMinimatch(header, allowedHeaders)) {
         filteredHeaders[header] = value;
       } else {
         logger.once.error(
diff --git a/lib/util/package-rules/match.ts b/lib/util/package-rules/match.ts
index c66f19284d..1d14d19e58 100644
--- a/lib/util/package-rules/match.ts
+++ b/lib/util/package-rules/match.ts
@@ -1,9 +1,8 @@
-import is from '@sindresorhus/is';
 import { logger } from '../../logger';
 import { minimatch } from '../minimatch';
 import { regEx } from '../regex';
 
-export function matchRegexOrMinimatch(pattern: string, input: string): boolean {
+export function matchRegexOrMinimatch(input: string, pattern: string): boolean {
   if (pattern.length > 2 && pattern.startsWith('/') && pattern.endsWith('/')) {
     try {
       const regex = regEx(pattern.slice(1, -1));
@@ -18,14 +17,8 @@ export function matchRegexOrMinimatch(pattern: string, input: string): boolean {
 }
 
 export function anyMatchRegexOrMinimatch(
-  patterns: string[] | undefined,
-  input: string | undefined,
+  input: string,
+  patterns: string[],
 ): boolean | null {
-  if (is.undefined(patterns)) {
-    return null;
-  }
-  if (is.undefined(input)) {
-    return false;
-  }
-  return patterns.some((pattern) => matchRegexOrMinimatch(pattern, input));
+  return patterns.some((pattern) => matchRegexOrMinimatch(input, pattern));
 }
diff --git a/lib/util/package-rules/repositories.ts b/lib/util/package-rules/repositories.ts
index 01c958d461..824df9164e 100644
--- a/lib/util/package-rules/repositories.ts
+++ b/lib/util/package-rules/repositories.ts
@@ -1,3 +1,4 @@
+import is from '@sindresorhus/is';
 import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
 import { Matcher } from './base';
 import { anyMatchRegexOrMinimatch } from './match';
@@ -7,13 +8,29 @@ export class RepositoriesMatcher extends Matcher {
     { repository }: PackageRuleInputConfig,
     { matchRepositories }: PackageRule,
   ): boolean | null {
-    return anyMatchRegexOrMinimatch(matchRepositories, repository);
+    if (is.undefined(matchRepositories)) {
+      return null;
+    }
+
+    if (is.undefined(repository)) {
+      return false;
+    }
+
+    return anyMatchRegexOrMinimatch(repository, matchRepositories);
   }
 
   override excludes(
     { repository }: PackageRuleInputConfig,
     { excludeRepositories }: PackageRule,
   ): boolean | null {
-    return anyMatchRegexOrMinimatch(excludeRepositories, repository);
+    if (is.undefined(excludeRepositories)) {
+      return null;
+    }
+
+    if (is.undefined(repository)) {
+      return false;
+    }
+
+    return anyMatchRegexOrMinimatch(repository, excludeRepositories);
   }
 }
-- 
GitLab