Repository navigation
fix: handle non-string variables in setup-vars and update eslint n rules (#4174) - #4517
reginaldalfret wants to merge 1 commit into
Conversation
…les (GoogleCloudPlatform#4174) Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
There was a problem hiding this comment.
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])); |
There was a problem hiding this comment.
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.
| value = value.replaceAll(re, String(env[key])); | |
| value = value.replaceAll(re, env[key] == null ? '' : String(env[key])); |
Description
Fixes #4174
This PR addresses two maintenance and correctness issues in test configuration and workflow scripts:
Update ESLint rules for
eslint-plugin-n(eslintrc rules may be invalid #4174):package.jsoninstallseslint-plugin-n, but.eslintrc.jsondefined rules using the legacynode/*prefix (node/no-missing-import, etc.).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 usingeslint-plugin-n.Handle non-string variables and guard ID token in
.github/scripts/setup-vars.js:substituteVarscrashed withTypeError: value.replaceAll is not a functionwhen 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.idTokenhandling: only callcore.exportVariable('ID_TOKEN', idToken)andcore.setSecret(idToken)whenidTokenis provided, preventing uninitialized tokens from exporting"undefined"and masking it across GitHub Actions workflow logs..github/scripts/setup-vars.test.jscovering non-string environment variables and conditionalidTokensecret masking.Checklist
npm testin.github/scriptspasses (21/21 passing)eslint --print-config