Repository: elastic/synthetics
Pull request #963 (2024-10-01 12:08:57)

feat: add fields via cli
fixes https://github.com/elastic/synthetics/issues/962

example 

` npm run push --fields '{"env":"dev"}'`


Patch:
diff --git a/src/cli.ts b/src/cli.ts
index e462408..23d6bec 100644
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -65,6 +65,7 @@ const {
   configOpt,
   tags,
   match,
+  fields,
 } = getCommonCommandOpts();
 
 program
@@ -194,6 +195,7 @@ program
 
   .addOption(pattern)
   .addOption(tags)
+  .addOption(fields)
   .addOption(match)
   .addOption(params)
   .addOption(playwrightOpts)
diff --git a/src/common_types.ts b/src/common_types.ts
index 0799c78..b6d94df 100644
--- a/src/common_types.ts
+++ b/src/common_types.ts
@@ -223,6 +223,7 @@ type BaseArgs = {
   schedule?: MonitorConfig['schedule'];
   locations?: MonitorConfig['locations'];
   privateLocations?: MonitorConfig['privateLocations'];
+  fields?: MonitorConfig['fields'];
 };
 
 export type CliArgs = BaseArgs & {
diff --git a/src/options.ts b/src/options.ts
index 78ed784..355b549 100644
--- a/src/options.ts
+++ b/src/options.ts
@@ -70,6 +70,9 @@ export async function normalizeOptions(
       : {};
 
   options.params = Object.freeze(merge(config.params, cliArgs.params || {}));
+  options.fields = Object.freeze(
+    merge(config.monitor?.fields ?? {}, cliArgs?.fields || {})
+  );
 
   /**
    * Merge playwright options from CLI and Synthetics config
@@ -233,6 +236,24 @@ export function getCommonCommandOpts() {
     '--match <name>',
     'run/push tests with a name or tags that matches a pattern'
   );
+  const fields = createOption(
+    '--fields <jsonstring>',
+    'add fields to the monitor(s) in the format { "key": "value"}'
+  ).argParser((fieldsStr: string) => {
+    const fields = JSON.parse(fieldsStr);
+    if (typeof fields !== 'object') {
+      throw new Error('Invalid fields format');
+    }
+    // make sure all values are strings
+    return Object.entries(fields).reduce((acc, [key, value]) => {
+      if (typeof value !== 'string') {
+        acc[key] = JSON.stringify(value);
+      } else {
+        acc[key] = value;
+      }
+      return acc;
+    }, {});
+  });
 
   return {
     auth,
@@ -243,6 +264,7 @@ export function getCommonCommandOpts() {
     configOpt,
     tags,
     match,
+    fields,
   };
 }
 


Test patch:
diff --git a/__tests__/cli.test.ts b/__tests__/cli.test.ts
index 3a6af40..ddb69fc 100644
--- a/__tests__/cli.test.ts
+++ b/__tests__/cli.test.ts
@@ -332,7 +332,7 @@ describe('CLI', () => {
     }
   });
 
-  it('run expect assetions with type check', async () => {
+  it('run expect assertions with type check', async () => {
     // flag turns on type checking
     process.env['TS_NODE_TYPE_CHECK'] = 'true';
     const cli = new CLIMock()
diff --git a/__tests__/fixtures/synthetics.config.ts b/__tests__/fixtures/synthetics.config.ts
index d202443..748360d 100644
--- a/__tests__/fixtures/synthetics.config.ts
+++ b/__tests__/fixtures/synthetics.config.ts
@@ -48,6 +48,9 @@ module.exports = env => {
           enabled: false,
         },
       },
+      fields: {
+        area: 'website',
+      },
     },
   };
   if (env !== 'development' && config.params) {
diff --git a/__tests__/options.test.ts b/__tests__/options.test.ts
index 0d68140..fbc0a92 100644
--- a/__tests__/options.test.ts
+++ b/__tests__/options.test.ts
@@ -91,6 +91,9 @@ describe('options', () => {
           enabled: false,
         },
       },
+      fields: {
+        area: 'website',
+      },
     });
 
     expect(
@@ -101,6 +104,9 @@ describe('options', () => {
           screenshots: 'only-on-failure',
           locations: ['australia_east'],
           privateLocations: ['test'],
+          fields: {
+            env: 'dev',
+          },
         },
         'push'
       )
@@ -118,6 +124,10 @@ describe('options', () => {
           enabled: false,
         },
       },
+      fields: {
+        env: 'dev',
+        area: 'website',
+      },
     });
   });