From e87af9231900098183498212a3a560a80d6ae90c Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Fri, 17 Feb 2023 11:54:35 +0300
Subject: [PATCH] feat(bazel): Support for `maven.artifact` positional args
 (#20471)

---
 lib/modules/manager/bazel/parser.spec.ts      | 14 ++++++++++++-
 lib/modules/manager/bazel/parser.ts           | 21 +++++++++++++------
 lib/modules/manager/bazel/rules/index.spec.ts | 20 ++++++++++++++++--
 lib/modules/manager/bazel/rules/maven.ts      | 19 ++++++++++++-----
 4 files changed, 60 insertions(+), 14 deletions(-)

diff --git a/lib/modules/manager/bazel/parser.spec.ts b/lib/modules/manager/bazel/parser.spec.ts
index 3f7075bda6..7bdccab916 100644
--- a/lib/modules/manager/bazel/parser.spec.ts
+++ b/lib/modules/manager/bazel/parser.spec.ts
@@ -228,6 +228,12 @@ describe('modules/manager/bazel/parser', () => {
             group = "com.example2",
             artifact = "bar",
             version = "2.2.2",
+          ),
+          maven.artifact(
+            "com.example3",
+            "baz",
+            "3.3.3",
+            neverlink = True
           )
         ],
         repositories = [
@@ -246,10 +252,16 @@ describe('modules/manager/bazel/parser', () => {
           'com.example1:foo:1.1.1',
           {
             _function: 'maven.artifact',
-            artifact: 'bar',
             group: 'com.example2',
+            artifact: 'bar',
             version: '2.2.2',
           },
+          {
+            _function: 'maven.artifact',
+            '0': 'com.example3',
+            '1': 'baz',
+            '2': '3.3.3',
+          },
         ],
         repositories: [
           'https://example1.com/maven2',
diff --git a/lib/modules/manager/bazel/parser.ts b/lib/modules/manager/bazel/parser.ts
index c6a8f77828..36e5f45714 100644
--- a/lib/modules/manager/bazel/parser.ts
+++ b/lib/modules/manager/bazel/parser.ts
@@ -11,6 +11,7 @@ interface Ctx {
   stack: NestedFragment[];
   recordKey?: string;
   subRecordKey?: string;
+  argIndex?: number;
 }
 
 function emptyCtx(source: string): Ctx {
@@ -140,13 +141,18 @@ const kwParams = q
             startsWith: '(',
             endsWith: ')',
             search: q
-              .sym<Ctx>((ctx, { value: subRecordKey }) => ({
-                ...ctx,
-                subRecordKey,
-              }))
-              .op('=')
+              .opt(
+                q
+                  .sym<Ctx>((ctx, { value: subRecordKey }) => ({
+                    ...ctx,
+                    subRecordKey,
+                  }))
+                  .op('=')
+              )
               .str((ctx, { value: subRecordValue, offset }) => {
-                const subRecordKey = ctx.subRecordKey!;
+                const argIndex = ctx.argIndex ?? 0;
+
+                const subRecordKey = ctx.subRecordKey! ?? argIndex.toString();
                 const ruleFragment = currentFragment(ctx);
                 if (ruleFragment.type === 'record') {
                   ruleFragment.children[subRecordKey] = {
@@ -156,9 +162,12 @@ const kwParams = q
                   };
                 }
                 delete ctx.subRecordKey;
+                ctx.argIndex = argIndex + 1;
                 return ctx;
               }),
             postHandler: (ctx, tree) => {
+              delete ctx.argIndex;
+
               const callFrag = currentFragment(ctx);
               ctx.stack.pop();
               if (callFrag.type === 'record' && tree.type === 'wrapped-tree') {
diff --git a/lib/modules/manager/bazel/rules/index.spec.ts b/lib/modules/manager/bazel/rules/index.spec.ts
index 8396398151..a1b9a95536 100644
--- a/lib/modules/manager/bazel/rules/index.spec.ts
+++ b/lib/modules/manager/bazel/rules/index.spec.ts
@@ -357,11 +357,17 @@ describe('modules/manager/bazel/rules/index', () => {
           artifacts: [
             'com.example1:foo:1.1.1',
             {
-              artifact: 'bar',
-              function: 'maven.artifact',
+              _function: 'maven.artifact',
               group: 'com.example2',
+              artifact: 'bar',
               version: '2.2.2',
             },
+            {
+              _function: 'maven.artifact',
+              '0': 'com.example3',
+              '1': 'baz',
+              '2': '3.3.3',
+            },
           ],
           repositories: [
             'https://example1.com/maven2',
@@ -389,6 +395,16 @@ describe('modules/manager/bazel/rules/index', () => {
             'https://example2.com/maven2',
           ],
         },
+        {
+          currentValue: '3.3.3',
+          datasource: 'maven',
+          depType: 'maven_install',
+          depName: 'com.example3:baz',
+          registryUrls: [
+            'https://example1.com/maven2',
+            'https://example2.com/maven2',
+          ],
+        },
       ]);
     });
   });
diff --git a/lib/modules/manager/bazel/rules/maven.ts b/lib/modules/manager/bazel/rules/maven.ts
index 82fcdd3934..448185e371 100644
--- a/lib/modules/manager/bazel/rules/maven.ts
+++ b/lib/modules/manager/bazel/rules/maven.ts
@@ -5,11 +5,20 @@ import type { PackageDependency } from '../../types';
 
 export const mavenRules = ['maven_install'] as const;
 
-const ArtifactSpec = z.object({
-  group: z.string(),
-  artifact: z.string(),
-  version: z.string(),
-});
+const ArtifactSpec = z.union([
+  z.object({
+    group: z.string(),
+    artifact: z.string(),
+    version: z.string(),
+  }),
+  z
+    .object({
+      '0': z.string(),
+      '1': z.string(),
+      '2': z.string(),
+    })
+    .transform((x) => ({ group: x[0], artifact: x[1], version: x[2] })),
+]);
 type ArtifactSpec = z.infer<typeof ArtifactSpec>;
 
 export const MavenTarget = z
-- 
GitLab