From 5658777d7a5de1e1ea9e432597e8c66d7fa00f53 Mon Sep 17 00:00:00 2001
From: Johannes Feichtner <343448+Churro@users.noreply.github.com>
Date: Wed, 12 Mar 2025 11:15:08 +0100
Subject: [PATCH] refactor(gradle): unify handling of custom and predefined
 registry URLs (#34714)

---
 lib/modules/manager/gradle/parser/handlers.ts | 35 +++++--------------
 .../manager/gradle/parser/registry-urls.ts    | 25 ++++++++-----
 2 files changed, 25 insertions(+), 35 deletions(-)

diff --git a/lib/modules/manager/gradle/parser/handlers.ts b/lib/modules/manager/gradle/parser/handlers.ts
index ca32f64a6e..5676deb402 100644
--- a/lib/modules/manager/gradle/parser/handlers.ts
+++ b/lib/modules/manager/gradle/parser/handlers.ts
@@ -1,8 +1,8 @@
-import URL from 'node:url';
 import upath from 'upath';
 import { logger } from '../../../../logger';
 import { getSiblingFileName } from '../../../../util/fs';
 import { regEx } from '../../../../util/regex';
+import { parseUrl } from '../../../../util/url';
 import type { PackageDependency } from '../../types';
 import type { parseGradle as parseGradleCallback } from '../parser';
 import type {
@@ -15,7 +15,6 @@ import { isDependencyString, parseDependencyString } from '../utils';
 import {
   GRADLE_PLUGINS,
   GRADLE_TEST_SUITES,
-  REGISTRY_URLS,
   findVariable,
   interpolateString,
   loadFromTokenMap,
@@ -337,19 +336,7 @@ function isPluginRegistry(ctx: Ctx): boolean {
   return false;
 }
 
-export function handlePredefinedRegistryUrl(ctx: Ctx): Ctx {
-  const registryName = loadFromTokenMap(ctx, 'registryUrl')[0].value;
-
-  ctx.registryUrls.push({
-    registryUrl: REGISTRY_URLS[registryName as keyof typeof REGISTRY_URLS],
-    scope: isPluginRegistry(ctx) ? 'plugin' : 'dep',
-    content: ctx.tmpRegistryContent,
-  });
-
-  return ctx;
-}
-
-export function handleCustomRegistryUrl(ctx: Ctx): Ctx {
+export function handleRegistryUrl(ctx: Ctx): Ctx {
   let localVariables = ctx.globalVars;
 
   if (ctx.tokenMap.name) {
@@ -373,17 +360,13 @@ export function handleCustomRegistryUrl(ctx: Ctx): Ctx {
   );
   if (registryUrl) {
     registryUrl = registryUrl.replace(regEx(/\\/g), '');
-    try {
-      const { host, protocol } = URL.parse(registryUrl);
-      if (host && protocol) {
-        ctx.registryUrls.push({
-          registryUrl,
-          scope: isPluginRegistry(ctx) ? 'plugin' : 'dep',
-          content: ctx.tmpRegistryContent,
-        });
-      }
-    } catch {
-      // no-op
+    const url = parseUrl(registryUrl);
+    if (url?.host && url.protocol) {
+      ctx.registryUrls.push({
+        registryUrl,
+        scope: isPluginRegistry(ctx) ? 'plugin' : 'dep',
+        content: ctx.tmpRegistryContent,
+      });
     }
   }
 
diff --git a/lib/modules/manager/gradle/parser/registry-urls.ts b/lib/modules/manager/gradle/parser/registry-urls.ts
index 1d602b5c8f..3b2077c2e3 100644
--- a/lib/modules/manager/gradle/parser/registry-urls.ts
+++ b/lib/modules/manager/gradle/parser/registry-urls.ts
@@ -1,4 +1,4 @@
-import type { parser } from 'good-enough-parser';
+import type { lexer, parser } from 'good-enough-parser';
 import { query as q } from 'good-enough-parser';
 import { regEx } from '../../../../util/regex';
 import type { Ctx } from '../types';
@@ -14,11 +14,7 @@ import {
   storeInTokenMap,
   storeVarToken,
 } from './common';
-import {
-  handleCustomRegistryUrl,
-  handlePredefinedRegistryUrl,
-  handleRegistryContent,
-} from './handlers';
+import { handleRegistryContent, handleRegistryUrl } from './handlers';
 import { qPlugins } from './plugins';
 
 const cleanupTmpContentSpec = (ctx: Ctx): Ctx => {
@@ -96,7 +92,18 @@ const qUri = q
 // mavenCentral()
 // mavenCentral { ... }
 const qPredefinedRegistries = q
-  .sym(regEx(`^(?:${Object.keys(REGISTRY_URLS).join('|')})$`), storeVarToken)
+  .sym(
+    regEx(`^(?:${Object.keys(REGISTRY_URLS).join('|')})$`),
+    (ctx: Ctx, node: lexer.Token) => {
+      const nodeTransformed: lexer.Token = {
+        ...node,
+        type: 'string-value',
+        value: REGISTRY_URLS[node.value as keyof typeof REGISTRY_URLS],
+      };
+      storeVarToken(ctx, nodeTransformed);
+      return ctx;
+    },
+  )
   .handler((ctx) => storeInTokenMap(ctx, 'registryUrl'))
   .alt(
     q
@@ -114,7 +121,7 @@ const qPredefinedRegistries = q
       search: q.opt(qRegistryContent),
     }),
   )
-  .handler(handlePredefinedRegistryUrl)
+  .handler(handleRegistryUrl)
   .handler(cleanupTmpContentSpec)
   .handler(cleanupTempVars);
 
@@ -158,7 +165,7 @@ const qCustomRegistryUrl = q
       .opt(qMavenArtifactRegistry),
     qMavenArtifactRegistry,
   )
-  .handler(handleCustomRegistryUrl)
+  .handler(handleRegistryUrl)
   .handler(cleanupTmpContentSpec)
   .handler(cleanupTempVars);
 
-- 
GitLab