Skip to content

Commit

Permalink
v6: Fix normalized name not usable as token (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
qetza authored Mar 22, 2024
1 parent 4d825ee commit b21cc8e
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 160 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,5 @@ jobs:
npm --prefix tasks/ReplaceTokensV5 ci
npm --prefix tasks/ReplaceTokensV6 ci
- name: Build
run: npm run build

- name: Test
run: npm run test
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Changelog
## 5.0.6
Task 6.0.5
- Fix normalized variable names not supported as token name ([#15](https://github.com/qetza/replacetokens-task/issues/15)) ([#20](https://github.com/qetza/replacetokens-task/issues/20)).

## 5.0.5
Task 5.3.3
- Fix telemetry account hash.
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Please refer to the [release page](https://github.com/qetza/replacetokens-task/r
## Breaking changes in v6
The task was completely rewritten to use the npm package [@qetza/replacetokens](https://www.npmjs.com/package/@qetza/replacetokens) and be more similar with the new [ReplaceTokens GitHub Actions](https://github.com/marketplace/actions/replacetokens):
- support only node 16 (mininum agent version 2.206.1)
- tokens **must** match real variable names and not normalized names done by the Azure Pipelines agent (token `BUILD_DEFINITIONNAME` will **not** match build variable `Build.DefinitionName`)
- renamed input _targetFiles_ to _sources_
- migrated to [fast-glob](https://github.com/mrmlnc/fast-glob) for glob patterns causing syntax changes (must use forward slash (`/`) for directory separator whatever the OS)
- removed support for comma-separated paths in _targetFiles_
Expand Down
3 changes: 3 additions & 0 deletions tasks/ReplaceTokensV6/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## 6.0.5
- Fix normalized variable names not supported as token name ([#15](https://github.com/qetza/replacetokens-task/issues/15)) ([#20](https://github.com/qetza/replacetokens-task/issues/20)).

## 6.0.4
- Update @qetza/replacetokens to 1.4.0.
- Change telemetry provider.
Expand Down
1 change: 0 additions & 1 deletion tasks/ReplaceTokensV6/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Please refer to the [release page](https://github.com/qetza/replacetokens-task/r
## Breaking changes in v6
The task was completely rewritten to use the npm package [@qetza/replacetokens](https://www.npmjs.com/package/@qetza/replacetokens) and be more similar with the new [ReplaceTokens GitHub Actions](https://github.com/marketplace/actions/replacetokens):
- support only node 16 (mininum agent version 2.206.1)
- tokens **must** match real variable names and not normalized names done by the Azure Pipelines agent (token `BUILD_DEFINITIONNAME` will **not** match build variable `Build.DefinitionName`)
- renamed input _targetFiles_ to _sources_
- migrated to [fast-glob](https://github.com/mrmlnc/fast-glob) for glob patterns causing syntax changes (must use forward slash (`/`) for directory separator whatever the OS)
- removed support for comma-separated paths in _targetFiles_
Expand Down
50 changes: 22 additions & 28 deletions tasks/ReplaceTokensV6/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ async function run() {
},
recursive: tl.getBoolInput('enableRecursion'),
root: tl.getPathInput('rootDirectory', false, true),
separator: tl.getInput('variableSeparator') || rt.Defaults.Separator,
token: {
pattern:
getChoiceInput('tokenPattern', [
Expand Down Expand Up @@ -111,19 +110,9 @@ async function run() {
console.info('##[endgroup]');
};

// load variables
const variables = await rt.parseVariables(
[
JSON.stringify(
tl.getVariables().reduce((result, current) => {
result[current.name] = current.value;
return result;
}, {})
),
...getAdditionalVariables()
],
{ normalizeWin32: true, root: options.root, separator: options.separator }
);
// load additional variables
const separator = tl.getInput('variableSeparator') || rt.Defaults.Separator;
const additionalVariables = await getAdditionalVariables(options.root, separator);

// set telemetry attributes
telemetryEvent.setAttributes({
Expand All @@ -139,7 +128,7 @@ async function run() {
'missing-var-default': options.missing.default,
'missing-var-log': options.missing.log,
recusrive: options.recursive,
separator: options.separator,
separator: separator,
'token-pattern': options.token.pattern,
'token-prefix': options.token.prefix,
'token-suffix': options.token.suffix,
Expand All @@ -152,7 +141,7 @@ async function run() {
});

// replace tokens
const result = await rt.replaceTokens(sources, variables, options);
const result = await rt.replaceTokens(sources, (name: string) => (name in additionalVariables ? additionalVariables[name] : tl.getVariable(name)), options);

if (result.files === 0) {
(msg => {
Expand Down Expand Up @@ -228,22 +217,27 @@ var getChoiceInput = function (name: string, choices: string[], alias?: string):
var variableFilesCount = 0;
var variablesEnvCount = 0;
var inlineVariablesCount = 0;
var getAdditionalVariables = function (): string[] {
var getAdditionalVariables = async function (root?: string, separator?: string): Promise<{ [key: string]: string }> {
const input = tl.getInput('additionalVariables') || '';
if (!input) return [];
if (!input) return {};

switch (input[0]) {
case '@': // single string referencing a file
++variableFilesCount;
return [input];
return await rt.loadVariables(
(() => {
switch (input[0]) {
case '@': // single string referencing a file
++variableFilesCount;
return [input];

case '$': // single string referencing environment variable
++variablesEnvCount;
return [input];
case '$': // single string referencing environment variable
++variablesEnvCount;
return [input];

default: // yaml format
return getAdditionalVariablesFromYaml(input);
}
default: // yaml format
return getAdditionalVariablesFromYaml(input);
}
})(),
{ normalizeWin32: true, root: root, separator: separator }
);
};

var getAdditionalVariablesFromYaml = function (input: string): string[] {
Expand Down
14 changes: 7 additions & 7 deletions tasks/ReplaceTokensV6/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tasks/ReplaceTokensV6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"@opentelemetry/api": "^1.8.0",
"@opentelemetry/sdk-trace-base": "^1.22.0",
"@opentelemetry/semantic-conventions": "^1.22.0",
"@qetza/replacetokens": "^1.4.0",
"@qetza/replacetokens": "^1.5.0",
"axios": "^1.6.7",
"azure-pipelines-task-lib": "^4.1.0",
"js-yaml": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion tasks/ReplaceTokensV6/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 6,
"Minor": 0,
"Patch": 4
"Patch": 5
},
"releaseNotes": "breaking changes, see [changelog](https://github.com/qetza/replacetokens-task/blob/master/tasks/ReplaceTokensV6/CHANGELOG.md)",
"instanceNameFormat": "Replace tokens",
Expand Down
1 change: 1 addition & 0 deletions tasks/ReplaceTokensV6/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_tmp/
Loading

0 comments on commit b21cc8e

Please sign in to comment.