Skip to content

Commit

Permalink
[steps] [ENG-14373] Fix multiline slack messages with env vars (#478)
Browse files Browse the repository at this point in the history
* [steps] Use quotes on env var value

Added quotes around the env var value when saving it into the file.
The reason for it is that if you set the env value to something like "line 1
line 2
line 3"
without the quotes it would remove the implicit new-line characters and save it as 1 line

See: https://linear.app/expo/issue/ENG-14373/fix-multiline-slack-message-from-env-vars

* [steps] Add util function

Added an util function to replace the doubly backslashed escape character in the raw env value with actual escape characters

See: https://linear.app/expo/issue/ENG-14373/fix-multiline-slack-message-from-env-vars

* [steps] Fix escape chars

Used the previously added function to fix the escape characters when reading the raw env value from the file and updating the context

See: https://linear.app/expo/issue/ENG-14373/fix-multiline-slack-message-from-env-vars
  • Loading branch information
radoslawkrzemien authored Dec 13, 2024
1 parent 9ff91c8 commit 75a9f3d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/steps/bin/set-env
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ if [[ "$NAME" == *"="* ]]; then
exit 1
fi

echo -n $VALUE > $__EXPO_STEPS_ENVS_DIR/$NAME
echo -n "$VALUE" > $__EXPO_STEPS_ENVS_DIR/$NAME
4 changes: 3 additions & 1 deletion packages/steps/src/BuildStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { BuildStepEnv } from './BuildStepEnv.js';
import { BuildRuntimePlatform } from './BuildRuntimePlatform.js';
import { jsepEval } from './utils/jsepEval.js';
import { interpolateJobContext } from './interpolation.js';
import { fixEscapeCharactersInRawEnvValue } from './utils/envUtils.js';

export enum BuildStepStatus {
NEW = 'new',
Expand Down Expand Up @@ -484,7 +485,8 @@ export class BuildStep extends BuildStepOutputAccessor {
const entries = await Promise.all(
filenames.map(async (basename) => {
const rawContents = await fs.readFile(path.join(envsDir, basename), 'utf-8');
return [basename, rawContents];
const updatedContents = fixEscapeCharactersInRawEnvValue(rawContents);
return [basename, updatedContents];
})
);
this.ctx.global.updateEnv({
Expand Down
17 changes: 17 additions & 0 deletions packages/steps/src/utils/envUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function fixEscapeCharactersInRawEnvValue(rawValue: string): string {
const escapeCharsMatches = rawValue.match(/\\(\w)/g);
let updatedValue = rawValue;
for (const match of escapeCharsMatches ?? []) {
if (match[1] === 'n') {
updatedValue = updatedValue.replace(`${match[0]}${match[1]}`, '\n');
}
if (match[1] === 't') {
updatedValue = updatedValue.replace(`${match[0]}${match[1]}`, '\t');
}
if (match[1] === 'r') {
updatedValue = updatedValue.replace(`${match[0]}${match[1]}`, '\r');
}
}

return updatedValue;
}

0 comments on commit 75a9f3d

Please sign in to comment.