From 6ac7e108090ac7cb2a318b8a8ce3d681dad0a383 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Mon, 6 Apr 2020 11:04:12 -0700 Subject: [PATCH 1/2] Add markup file for hld command --- docs/commands/data.json | 3 +- src/commands/hld/append-variable-group.md | 43 +++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/commands/hld/append-variable-group.md diff --git a/docs/commands/data.json b/docs/commands/data.json index 1e3c26ee6..af15e99b5 100644 --- a/docs/commands/data.json +++ b/docs/commands/data.json @@ -236,7 +236,8 @@ "hld append-variable-group": { "command": "append-variable-group ", "alias": "avg", - "description": "Appends the name of an existing variable group to the current manifest-generation.yaml file." + "description": "Appends the name of an existing variable group to the current manifest-generation.yaml file.", + "markdown": "## Description\n\nAppend a variable group name to the current `manifest-generation.yaml` of an\ninitialized hld repository.\n\n## Example\n\nWhen an HLD repository is first initialized with `spk hld init`, the top portion\nof the `manifest-generation.yaml` looks like this:\n\n```yaml\n# GENERATED WITH SPK VERSION 0.5.8\ntrigger:\n branches:\n include:\n - master\nvariables: []\npool:\n vmImage: ubuntu-latest\nsteps:\n.\n.\n.\n```\n\nrunning `spk hld append-variable-group my-vg` with a variable group name, in\nthis case `my-vg`, will add it under the `variables` section:\n\n```yaml\n# GENERATED WITH SPK VERSION 0.5.8\ntrigger:\n branches:\n include:\n - master\nvariables:\n - group: my-variable-group\npool:\n vmImage: ubuntu-latest\nsteps:\n.\n.\n.\n```\n" }, "hld init": { "command": "init", diff --git a/src/commands/hld/append-variable-group.md b/src/commands/hld/append-variable-group.md new file mode 100644 index 000000000..c6fbdbacd --- /dev/null +++ b/src/commands/hld/append-variable-group.md @@ -0,0 +1,43 @@ +## Description + +Append a variable group name to the current `manifest-generation.yaml` of an +initialized hld repository. + +## Example + +When an HLD repository is first initialized with `spk hld init`, the top portion +of the `manifest-generation.yaml` looks like this: + +```yaml +# GENERATED WITH SPK VERSION 0.5.8 +trigger: + branches: + include: + - master +variables: [] +pool: + vmImage: ubuntu-latest +steps: +. +. +. +``` + +running `spk hld append-variable-group my-vg` with a variable group name, in +this case `my-vg`, will add it under the `variables` section: + +```yaml +# GENERATED WITH SPK VERSION 0.5.8 +trigger: + branches: + include: + - master +variables: + - group: my-variable-group +pool: + vmImage: ubuntu-latest +steps: +. +. +. +``` From f8c3232651fe936283ac0249b85ede34dd23475e Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Mon, 6 Apr 2020 16:19:08 -0700 Subject: [PATCH 2/2] Handle duplicate entries --- docs/commands/data.json | 2 +- src/commands/hld/append-variable-group.md | 3 ++- src/lib/fileutils.ts | 22 +++++++++++++++++----- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/docs/commands/data.json b/docs/commands/data.json index af15e99b5..02a6e1f7a 100644 --- a/docs/commands/data.json +++ b/docs/commands/data.json @@ -237,7 +237,7 @@ "command": "append-variable-group ", "alias": "avg", "description": "Appends the name of an existing variable group to the current manifest-generation.yaml file.", - "markdown": "## Description\n\nAppend a variable group name to the current `manifest-generation.yaml` of an\ninitialized hld repository.\n\n## Example\n\nWhen an HLD repository is first initialized with `spk hld init`, the top portion\nof the `manifest-generation.yaml` looks like this:\n\n```yaml\n# GENERATED WITH SPK VERSION 0.5.8\ntrigger:\n branches:\n include:\n - master\nvariables: []\npool:\n vmImage: ubuntu-latest\nsteps:\n.\n.\n.\n```\n\nrunning `spk hld append-variable-group my-vg` with a variable group name, in\nthis case `my-vg`, will add it under the `variables` section:\n\n```yaml\n# GENERATED WITH SPK VERSION 0.5.8\ntrigger:\n branches:\n include:\n - master\nvariables:\n - group: my-variable-group\npool:\n vmImage: ubuntu-latest\nsteps:\n.\n.\n.\n```\n" + "markdown": "## Description\n\nAppend a variable group name to the current `manifest-generation.yaml` of an\ninitialized hld repository.\n\n## Example\n\nWhen an HLD repository is first initialized with `spk hld init`, the top portion\nof the `manifest-generation.yaml` looks like this:\n\n```yaml\n# GENERATED WITH SPK VERSION 0.5.8\ntrigger:\n branches:\n include:\n - master\nvariables: []\npool:\n vmImage: ubuntu-latest\nsteps:\n.\n.\n.\n```\n\nrunning `spk hld append-variable-group my-vg` with a variable group name, in\nthis case `my-vg`, will add it under the `variables` section if it does not\nalready exist:\n\n```yaml\n# GENERATED WITH SPK VERSION 0.5.8\ntrigger:\n branches:\n include:\n - master\nvariables:\n - group: my-variable-group\npool:\n vmImage: ubuntu-latest\nsteps:\n.\n.\n.\n```\n" }, "hld init": { "command": "init", diff --git a/src/commands/hld/append-variable-group.md b/src/commands/hld/append-variable-group.md index c6fbdbacd..45fcaa09c 100644 --- a/src/commands/hld/append-variable-group.md +++ b/src/commands/hld/append-variable-group.md @@ -24,7 +24,8 @@ steps: ``` running `spk hld append-variable-group my-vg` with a variable group name, in -this case `my-vg`, will add it under the `variables` section: +this case `my-vg`, will add it under the `variables` section if it does not +already exist: ```yaml # GENERATED WITH SPK VERSION 0.5.8 diff --git a/src/lib/fileutils.ts b/src/lib/fileutils.ts index c8f76b683..6c913c915 100644 --- a/src/lib/fileutils.ts +++ b/src/lib/fileutils.ts @@ -495,11 +495,23 @@ export const appendVariableGroupToPipelineYaml = ( path.join(dir, fileName) ) as AzurePipelinesYaml; pipelineFile.variables = pipelineFile.variables || []; - - pipelineFile.variables.push({ group: variableGroupName }); - - logger.info(`Updating '${dir}/${fileName}'.`); - write(pipelineFile, dir, fileName); + let variableGroupExists = false; + + pipelineFile.variables.forEach((variable) => { + if ("group" in variable && variable.group === variableGroupName) { + variableGroupExists = true; + logger.info( + `Variable group '${variableGroupName}' already exits in '${dir}/${fileName}'.` + ); + } + }); + + if (!variableGroupExists) { + pipelineFile.variables.push({ group: variableGroupName }); + + logger.info(`Updating '${dir}/${fileName}'.`); + write(pipelineFile, dir, fileName); + } } catch (err) { throw buildError( errorStatusCode.FILE_IO_ERR,