From 1c06046643f00f70ac01432266c2ba47e36e7036 Mon Sep 17 00:00:00 2001 From: Vio Date: Thu, 1 Feb 2024 19:57:56 +0100 Subject: [PATCH 1/2] refactor: Update debug entries --- src/agent.js | 12 ++++++------ src/utils.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/agent.js b/src/agent.js index adda98e0..438cec03 100644 --- a/src/agent.js +++ b/src/agent.js @@ -36,9 +36,9 @@ export const agent = (artifactsData, config, args = {}, logger = console) => { const envVars = getEnvVars(); - // Normalized env vars - merge provided args with env vars + // Normalized params - merge provided args with env vars // @type {EnvVars} - const normalizedEnvVars = { + const normalizedParams = { slug: args.slug || envVars.slug, branch: args.branch || envVars.branch, pr: args.pr || envVars.pr, @@ -54,23 +54,23 @@ export const agent = (artifactsData, config, args = {}, logger = console) => { endpoint: envVars.endpoint, }; - debug('normalized env vars ', maskObjectProperties(normalizedEnvVars, ['key'])); + debug('normalized parameters - agent configuration with environmental variables fallback', maskObjectProperties(normalizedParams, ['key'])); const { includeCommitMessage } = config; const params = { agentVersion: packageInfo.version, - ...normalizedEnvVars, + ...normalizedParams, // Get commit message using git if includeCommitMessage is set and // there is no --commit-message argument or RELATIVE_CI_COMMIT_MESSAGE - ...includeCommitMessage && !normalizedEnvVars.commitMessage && { + ...includeCommitMessage && !normalizedParams.commitMessage && { commitMessage: getCommitMessage(), }, }; - debug('Job parameters', maskObjectProperties(params, ['key'])); + debug('job parameters', maskObjectProperties(params, ['key'])); // Validate parameters if (!params.key) { diff --git a/src/utils.js b/src/utils.js index dacd61ec..dd360b1d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -125,7 +125,7 @@ export function getEnvVars() { commit: process.env.RELATIVE_CI_COMMIT, commitMessage: process.env.RELATIVE_CI_COMMIT_MESSAGE, }; - debug('custom environment variables', maskObjectProperties(customEnvVars, ['key'])); + debug('RELATIVE_CI environment variables', maskObjectProperties(customEnvVars, ['key'])); const resolvedEnvVars = { key: customEnvVars.key, @@ -140,7 +140,7 @@ export function getEnvVars() { commit: customEnvVars.commit || envCIvars.commit, commitMessage: customEnvVars.commitMessage, }; - debug('resolved environment variables', maskObjectProperties(envCIvars, ['key'])); + debug('resolved environment variables', maskObjectProperties(resolvedEnvVars, ['key'])); return resolvedEnvVars; } From 748768090a9bb8a858c524017a19fc67cbe50110 Mon Sep 17 00:00:00 2001 From: Vio Date: Thu, 1 Feb 2024 20:09:16 +0100 Subject: [PATCH 2/2] fix: Prevent boolean values for empty env vars --- src/utils.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/utils.js b/src/utils.js index dd360b1d..b61efddf 100644 --- a/src/utils.js +++ b/src/utils.js @@ -103,6 +103,19 @@ function resolveSlug(envVars) { return 'slug' in envVars ? envVars.slug : ''; } +/** + * @param {import('env-ci').CiEnv} data + * @param {string} key + * @returns {string | undefined} + */ +function getEnvCIVar(data, key) { + if (!data[key]) { + return undefined; + } + + return data[key]; +} + /** * Extract CI environment variables using env-ci and custom fallback env vars * @returns {EnvVars} @@ -131,12 +144,12 @@ export function getEnvVars() { key: customEnvVars.key, endpoint: customEnvVars.endpoint, isCi: envCIvars.isCi, // process.env.CI - service: customEnvVars.service || ('service' in envCIvars && envCIvars.service), + service: customEnvVars.service || getEnvCIVar(envCIvars, 'service'), slug: customEnvVars.slug || resolveSlug(envCIvars), - branch: customEnvVars.branch || ('prBranch' in envCIvars && envCIvars.prBranch) || ('branch' in envCIvars && envCIvars.branch), - pr: customEnvVars.pr || ('pr' in envCIvars && envCIvars.pr), - build: customEnvVars.build || ('build' in envCIvars && envCIvars.build), - buildUrl: customEnvVars.buildUrl || ('buildUrl' in envCIvars && envCIvars.buildUrl), + branch: customEnvVars.branch || getEnvCIVar(envCIvars, 'prBranch') || getEnvCIVar(envCIvars, 'branch'), + pr: customEnvVars.pr || getEnvCIVar(envCIvars, 'pr'), + build: customEnvVars.build || getEnvCIVar(envCIvars, 'build'), + buildUrl: customEnvVars.buildUrl || getEnvCIVar(envCIvars, 'buildUrl'), commit: customEnvVars.commit || envCIvars.commit, commitMessage: customEnvVars.commitMessage, };