Skip to content

fix: handle non-string variables in setup-vars and update eslint n rules (#4174) - #4517

Open
reginaldalfret wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
reginaldalfret:fix/setup-vars-and-eslint-rules
Open

reginaldalfret wants to merge 1 commit into
GoogleCloudPlatform:mainfrom
reginaldalfret:fix/setup-vars-and-eslint-rules

Conversation

@reginaldalfret

Copy link
Copy Markdown

Description

Fixes #4174

This PR addresses two maintenance and correctness issues in test configuration and workflow scripts:

  1. Update ESLint rules for eslint-plugin-n (eslintrc rules may be invalid #4174):

    • Root package.json installs eslint-plugin-n, but .eslintrc.json defined rules using the legacy node/* prefix (node/no-missing-import, etc.).
    • Added corresponding n/* rule definitions (n/no-missing-import, n/no-missing-require, n/no-unpublished-import, n/no-unpublished-require, n/no-unsupported-features/es-syntax) so ESLint correctly applies rule overrides when using eslint-plugin-n.
  2. Handle non-string variables and guard ID token in .github/scripts/setup-vars.js:

    • substituteVars crashed with TypeError: value.replaceAll is not a function when environment variables were numbers or booleans (e.g. {PORT: 8080}). Added a type check to return non-string inputs safely, and ensured replacement values are converted to string.
    • Guarded idToken handling: only call core.exportVariable('ID_TOKEN', idToken) and core.setSecret(idToken) when idToken is provided, preventing uninitialized tokens from exporting "undefined" and masking it across GitHub Actions workflow logs.
    • Added unit tests in .github/scripts/setup-vars.test.js covering non-string environment variables and conditional idToken secret masking.

Checklist

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test in .github/scripts passes (21/21 passing)
  • Lint pass: configuration verified with eslint --print-config
  • Please merge this PR for me once it is approved

…les (GoogleCloudPlatform#4174)

Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
@reginaldalfret
reginaldalfret requested review from a team as code owners October 26, 2026 10:55
@product-auto-label product-auto-label Bot added the samples Issues that are directly related to samples. label Oct 26, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the ESLint configuration to include 'n/*' rules, conditionally exports and sets the 'ID_TOKEN' secret only when provided, and hardens the 'substituteVars' function to handle non-string inputs and variables safely. Additionally, comprehensive unit tests are added to cover these new behaviors. A review comment suggests improving 'substituteVars' by replacing 'null' or 'undefined' environment variables with an empty string instead of their literal string representations to avoid unexpected values.

for (const key in env) {
let re = new RegExp(`\\$(${key}\\b|\\{\\s*${key}\\s*\\})`, 'g');
value = value.replaceAll(re, env[key]);
value = value.replaceAll(re, String(env[key]));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If an environment variable in env is undefined or null, calling String(env[key]) will substitute the literal string "undefined" or "null" into the value. This can lead to unexpected and hard-to-debug values (e.g., "http://localhost:undefined"). It is safer to substitute an empty string "" instead.

Suggested change
value = value.replaceAll(re, String(env[key]));
value = value.replaceAll(re, env[key] == null ? '' : String(env[key]));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eslintrc rules may be invalid

1 participant