From 77a5c15daaa102a5aad026f69b94212bbecd7f7c Mon Sep 17 00:00:00 2001
From: RahulGautamSingh <rahultesnik@gmail.com>
Date: Sun, 2 Feb 2025 20:46:42 +0530
Subject: [PATCH] fix(manager/jsonata): populate extract result with manager
 config fields (#33992)

---
 .../manager/custom/jsonata/index.spec.ts      | 35 +++++++++++++++++++
 lib/modules/manager/custom/jsonata/index.ts   | 18 ++++++++--
 lib/modules/manager/types.ts                  |  1 +
 3 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/lib/modules/manager/custom/jsonata/index.spec.ts b/lib/modules/manager/custom/jsonata/index.spec.ts
index 76f964a5c5..8c958ff2ea 100644
--- a/lib/modules/manager/custom/jsonata/index.spec.ts
+++ b/lib/modules/manager/custom/jsonata/index.spec.ts
@@ -67,6 +67,7 @@ describe('modules/manager/custom/jsonata/index', () => {
     const res = await extractPackageFile(json, 'unused', config);
 
     expect(res).toMatchObject({
+      ...config,
       deps: [
         {
           depName: 'foo',
@@ -119,6 +120,7 @@ describe('modules/manager/custom/jsonata/index', () => {
     const res = await extractPackageFile(json, 'unused', config);
 
     expect(res).toMatchObject({
+      ...config,
       deps: [
         {
           depName: 'foo',
@@ -190,6 +192,7 @@ describe('modules/manager/custom/jsonata/index', () => {
     const res = await extractPackageFile(json, 'unused', config);
 
     expect(res).toMatchObject({
+      ...config,
       deps: [
         {
           depName: 'foo',
@@ -313,6 +316,7 @@ describe('modules/manager/custom/jsonata/index', () => {
     };
     const res = await extractPackageFile('{}', 'unused', config);
     expect(res).toMatchObject({
+      ...config,
       deps: [
         {
           depName: 'foo',
@@ -327,4 +331,35 @@ describe('modules/manager/custom/jsonata/index', () => {
       ],
     });
   });
+
+  it('populates manager config and jsonata manager template fields in extract result', async () => {
+    const config = {
+      fileFormat: 'json',
+      matchStrings: [`{"depName": "foo"}`, `{"depName": "bar"}`],
+      currentValueTemplate: '1.0.0',
+      datasourceTemplate: 'npm',
+      // should be included present extract result as it is not valid jsonata manager template
+      // adding here for testing
+      autoReplaceStringTemplate: `{{{depName}}}:{{{newValue}}}`,
+    };
+    const res = await extractPackageFile('{}', 'unused', config);
+    expect(res).toMatchObject({
+      deps: [
+        {
+          depName: 'foo',
+          currentValue: '1.0.0',
+          datasource: 'npm',
+        },
+        {
+          depName: 'bar',
+          currentValue: '1.0.0',
+          datasource: 'npm',
+        },
+      ],
+      fileFormat: 'json',
+      matchStrings: [`{"depName": "foo"}`, `{"depName": "bar"}`],
+      currentValueTemplate: '1.0.0',
+      datasourceTemplate: 'npm',
+    });
+  });
 });
diff --git a/lib/modules/manager/custom/jsonata/index.ts b/lib/modules/manager/custom/jsonata/index.ts
index 8324c0efc7..5c212da17b 100644
--- a/lib/modules/manager/custom/jsonata/index.ts
+++ b/lib/modules/manager/custom/jsonata/index.ts
@@ -4,7 +4,8 @@ import { logger } from '../../../../logger';
 import { parseJson } from '../../../../util/common';
 import { parseYaml } from '../../../../util/yaml';
 import type { PackageFileContent } from '../../types';
-import type { JsonataExtractConfig } from './types';
+import { validMatchFields } from '../utils';
+import type { JSONataManagerTemplates, JsonataExtractConfig } from './types';
 import { handleMatching } from './utils';
 
 export const categories: Category[] = ['custom'];
@@ -47,7 +48,20 @@ export async function extractPackageFile(
     return null;
   }
 
-  return {
+  const res: PackageFileContent & JSONataManagerTemplates = {
     deps,
+    matchStrings: config.matchStrings,
+    fileFormat: config.fileFormat,
   };
+
+  // copy over templates for autoreplace
+  for (const field of validMatchFields.map(
+    (f) => `${f}Template` as keyof JSONataManagerTemplates,
+  )) {
+    if (config[field]) {
+      res[field] = config[field];
+    }
+  }
+
+  return res;
 }
diff --git a/lib/modules/manager/types.ts b/lib/modules/manager/types.ts
index 0937191789..28ede22bda 100644
--- a/lib/modules/manager/types.ts
+++ b/lib/modules/manager/types.ts
@@ -71,6 +71,7 @@ export interface PackageFileContent<T = Record<string, any>>
   skipInstalls?: boolean | null;
   matchStrings?: string[];
   matchStringsStrategy?: MatchStringsStrategy;
+  fileFormat?: string;
 }
 
 export interface PackageFile<T = Record<string, any>>
-- 
GitLab