Skip to content

Commit 33845c8

Browse files
Geethik07simonmartyGeethik Rachaputi
authored
Fix edge case having a value with numeric start and also a prefix (#242)
* Update action.yml * Fix edge case having a value with numeric start and also a prefix * Revert "Update action.yml" This reverts commit dc17d6a. * Fix edge case having a value with numeric start and also a prefix * Update utils.test.ts Updated 1Password with 7Value to make it a unique test case. * Fix and test at injectSecret level * Add a new unit test case with valid tempEnvName --------- Co-authored-by: Simon Marty <[email protected]> Co-authored-by: Geethik Rachaputi <[email protected]>
1 parent fbd65ea commit 33845c8

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

__tests__/utils.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,41 @@ describe('Test secret parsing and handling', () => {
325325
expect(core.exportVariable).toHaveBeenCalledWith('TEST_SECRET_CONFIG_OPTIONS_C', '100');
326326
});
327327

328+
test('Maintains single underscore between prefix and numeric properties', () => {
329+
const secretName = 'DB';
330+
const secretValue = JSON.stringify({
331+
"7Value": "test-value"
332+
});
333+
334+
const secretsToCleanup = injectSecret(
335+
secretName,
336+
secretValue,
337+
true,
338+
undefined
339+
);
340+
341+
expect(secretsToCleanup).toHaveLength(1);
342+
expect(secretsToCleanup[0]).toBe('DB_7VALUE');
343+
});
344+
345+
test('Maintains single underscore between prefix and numeric properties with a EnvName', () => {
346+
const secretName = 'DB';
347+
const secretValue = JSON.stringify({
348+
"7Value": "test-value"
349+
});
350+
351+
const secretsToCleanup = injectSecret(
352+
secretName,
353+
secretValue,
354+
true,
355+
undefined,
356+
TEST_ENV_NAME
357+
);
358+
359+
expect(secretsToCleanup).toHaveLength(1);
360+
expect(secretsToCleanup[0]).toBe('TEST_SECRET_7VALUE');
361+
});
362+
328363
/*
329364
* Test: parseAliasFromId()
330365
*/
@@ -369,6 +404,7 @@ describe('Test secret parsing and handling', () => {
369404
expect(transformToValidEnvName('0Admin')).toBe('_0ADMIN')
370405
});
371406

407+
372408
test('Transformation function is applied', () => {
373409
expect(transformToValidEnvName('secret3', (x) => x.toUpperCase())).toBe('SECRET3')
374410
});
@@ -418,4 +454,4 @@ describe('Test secret parsing and handling', () => {
418454
test.each([ 'something', '' ])('NameTransformation parsing of string %s should fail.', (input) => {
419455
expect(() => parseTransformationFunction(input)).toThrow();
420456
});
421-
});
457+
});

src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ export function injectSecret(
149149

150150
// Append the current key to the name of the env variable and check to avoid prepending an underscore
151151
const newEnvName = [
152-
tempEnvName || transformToValidEnvName(secretName, nameTransformation),
153-
transformToValidEnvName(k, nameTransformation)
152+
tempEnvName || transformToValidEnvName(secretName, nameTransformation, false),
153+
transformToValidEnvName(k, nameTransformation, true)
154154
]
155155
.filter(elem => elem) // Uses truthy-ness of elem to determine if it remains
156156
.join("_"); // Join the remaining elements with an underscore
@@ -195,9 +195,9 @@ export function isJSONString(secretValue: string): boolean {
195195
* Transforms the secret name into a valid environmental variable name
196196
* It should consist of only upper case letters, digits, and underscores and cannot begin with a number
197197
*/
198-
export function transformToValidEnvName(secretName: string, nameTransformation?: TransformationFunc): string {
198+
export function transformToValidEnvName(secretName: string, nameTransformation?: TransformationFunc, hasPrefix : boolean = false): string {
199199
// Leading digits are invalid
200-
if (secretName.match(/^[0-9]/)){
200+
if (!hasPrefix && secretName.match(/^[0-9]/)) {
201201
secretName = '_'.concat(secretName);
202202
}
203203

0 commit comments

Comments
 (0)