From 62959c4f4e034ac09a80e3e9bbdec41686661483 Mon Sep 17 00:00:00 2001 From: Eddy Filip Date: Fri, 7 Jul 2023 18:55:12 +0100 Subject: [PATCH] Improve loadSecrets function Return early if no env vars with valid secret references are found --- dist/index.js | 3 +++ src/utils.test.ts | 8 ++++++++ src/utils.ts | 5 +++++ 3 files changed, 16 insertions(+) diff --git a/dist/index.js b/dist/index.js index 029f4e4..388e576 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4179,6 +4179,9 @@ const loadSecrets = async (shouldExportEnv) => { // Iterate over them to find 1Password references, extract the secret values, // and make them available in the next steps either as step outputs or as environment variables. const res = await exec.getExecOutput(`sh -c "op env ls"`); + if (res.stdout === "") { + return; + } const envs = res.stdout.replace(/\n+$/g, "").split(/\r?\n/); for (const envName of envs) { extractSecret(envName, shouldExportEnv); diff --git a/src/utils.test.ts b/src/utils.test.ts index d31b73a..ec003a6 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -140,6 +140,14 @@ describe("loadSecrets", () => { ); }); + it("return early if no env vars with secrets found", async () => { + (exec.getExecOutput as jest.Mock).mockReturnValueOnce({ stdout: "" }); + await loadSecrets(true); + + expect(exec.getExecOutput).toHaveBeenCalledWith('sh -c "op env ls"'); + expect(core.exportVariable).not.toHaveBeenCalled(); + }); + describe("core.exportVariable", () => { it("is called when shouldExportEnv is true", async () => { await loadSecrets(true); diff --git a/src/utils.ts b/src/utils.ts index a276408..02cd8cb 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -77,6 +77,11 @@ export const loadSecrets = async (shouldExportEnv: boolean): Promise => { // Iterate over them to find 1Password references, extract the secret values, // and make them available in the next steps either as step outputs or as environment variables. const res = await exec.getExecOutput(`sh -c "op env ls"`); + + if (res.stdout === "") { + return; + } + const envs = res.stdout.replace(/\n+$/g, "").split(/\r?\n/); for (const envName of envs) { extractSecret(envName, shouldExportEnv);