Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 9e4b663

Browse files
edaenaEdaena Salinassamiyaakhtardennisseah
authored
Append variable group to manifest-generation.yaml (#453)
* Add variable group to existing manifest-generation.yaml * Update integration tests * Adding files for append variable group * Add unit test file * Update test mock function * Update unit test * updates Co-authored-by: Edaena Salinas <[email protected]> Co-authored-by: Samiya Akhtar <[email protected]> Co-authored-by: Dennis Seah <[email protected]>
1 parent b6c4c22 commit 9e4b663

9 files changed

+161
-2
lines changed

docs/commands/data.json

+5
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@
226226
}
227227
]
228228
},
229+
"hld append-variable-group": {
230+
"command": "append-variable-group <variable-group-name>",
231+
"alias": "avg",
232+
"description": "Appends the name of an existing variable group to the current manifest-generation.yaml file."
233+
},
229234
"hld init": {
230235
"command": "init",
231236
"alias": "i",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"command": "append-variable-group <variable-group-name>",
3+
"alias": "avg",
4+
"description": "Appends the name of an existing variable group to the current manifest-generation.yaml file."
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { execute } from "./append-variable-group";
2+
import * as fileutils from "../../lib/fileutils";
3+
4+
describe("Test execute function", () => {
5+
it("missing variable group name", async () => {
6+
const exitFn = jest.fn();
7+
await execute("my-path", "", exitFn);
8+
expect(exitFn).toBeCalledTimes(1);
9+
expect(exitFn.mock.calls).toEqual([[1]]);
10+
});
11+
it("appends variable group", async () => {
12+
const exitFn = jest.fn();
13+
spyOn(fileutils, "appendVariableGroupToPipelineYaml");
14+
await execute("my-path", "my-vg", exitFn);
15+
expect(exitFn).toBeCalledTimes(1);
16+
expect(exitFn.mock.calls).toEqual([[0]]);
17+
});
18+
});
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import commander from "commander";
2+
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
3+
import { RENDER_HLD_PIPELINE_FILENAME } from "../../lib/constants";
4+
import { appendVariableGroupToPipelineYaml } from "../../lib/fileutils";
5+
import { logger } from "../../logger";
6+
import decorator from "./append-variable-group.decorator.json";
7+
8+
/**
9+
* Executes the command, can call exit function with 0 or 1
10+
* when command completed successfully or failed respectively.
11+
*
12+
* @param hldRepoPath The hld repository path
13+
* @param variableGroupName The variable group name
14+
* @param exitFn exit function
15+
*/
16+
export const execute = async (
17+
hldRepoPath: string,
18+
variableGroupName: string,
19+
exitFn: (status: number) => Promise<void>
20+
): Promise<void> => {
21+
if (!variableGroupName) {
22+
logger.error("Variable group name is missing.");
23+
await exitFn(1);
24+
return;
25+
}
26+
27+
try {
28+
appendVariableGroupToPipelineYaml(
29+
hldRepoPath,
30+
RENDER_HLD_PIPELINE_FILENAME,
31+
variableGroupName
32+
);
33+
await exitFn(0);
34+
} catch (err) {
35+
logger.error(`Error occurred while appending variable group.`);
36+
logger.error(err);
37+
await exitFn(1);
38+
}
39+
};
40+
41+
/**
42+
* Adds the init command to the commander command object
43+
* @param command Commander command object to decorate
44+
*/
45+
export const commandDecorator = (command: commander.Command): void => {
46+
buildCmd(command, decorator).action(async (variableGroupName: string) => {
47+
const hldRepoPath = process.cwd();
48+
await execute(hldRepoPath, variableGroupName, async (status: number) => {
49+
await exitCmd(logger, process.exit, status);
50+
});
51+
});
52+
};

src/commands/hld/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Command } from "../command";
22

3-
const subfolders = ["init", "pipeline", "reconcile"];
3+
const subfolders = ["init", "pipeline", "reconcile", "append-variable-group"];
44

55
export const commandDecorator = Command(
66
"hld",

src/lib/fileutils.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ import { disableVerboseLogging, enableVerboseLogging } from "../logger";
2828
import {
2929
createTestComponentYaml,
3030
createTestHldAzurePipelinesYaml,
31+
createTestHldAzurePipelinesYamlWithVariableGroup,
3132
createTestHldLifecyclePipelineYaml,
3233
createTestMaintainersYaml,
3334
createTestServiceBuildAndUpdatePipelineYaml,
3435
} from "../test/mockFactory";
3536
import { AccessYaml, AzurePipelinesYaml, MaintainersFile } from "../types";
3637
import {
3738
addNewServiceToMaintainersFile,
39+
appendVariableGroupToPipelineYaml,
3840
generateAccessYaml,
3941
generateDefaultHldComponentYaml,
4042
generateDockerfile,
@@ -423,6 +425,44 @@ describe("generateHldAzurePipelinesYaml", () => {
423425
});
424426
});
425427

428+
describe("appendVariableGroupToHldAzurePipelinesYaml", () => {
429+
const targetDirectory = "hld-repository";
430+
const writeSpy = jest.spyOn(fs, "writeFileSync");
431+
const appendSpy = jest.spyOn(fs, "appendFileSync");
432+
beforeEach(() => {
433+
mockFs({
434+
"hld-repository": {},
435+
});
436+
});
437+
afterEach(() => {
438+
mockFs.restore();
439+
});
440+
it("should append a variable group", () => {
441+
const absTargetPath = path.resolve(targetDirectory);
442+
const expectedFilePath = `${absTargetPath}/${RENDER_HLD_PIPELINE_FILENAME}`;
443+
444+
const mockFsOptions = {
445+
[`${targetDirectory}/${RENDER_HLD_PIPELINE_FILENAME}`]: createTestHldAzurePipelinesYaml() as string,
446+
};
447+
mockFs(mockFsOptions);
448+
449+
appendVariableGroupToPipelineYaml(
450+
absTargetPath,
451+
RENDER_HLD_PIPELINE_FILENAME,
452+
"my-vg"
453+
);
454+
expect(writeSpy).toBeCalledWith(
455+
expectedFilePath,
456+
`${getVersionMessage()}\n`,
457+
"utf8"
458+
);
459+
expect(appendSpy).toBeCalledWith(
460+
expectedFilePath,
461+
createTestHldAzurePipelinesYamlWithVariableGroup()
462+
);
463+
});
464+
});
465+
426466
describe("generateDefaultHldComponentYaml", () => {
427467
const targetDirectory = "hld-repository";
428468
const writeSpy = jest.spyOn(fs, "writeFileSync");

src/lib/fileutils.ts

+25
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
MaintainersFile,
2020
User,
2121
} from "../types";
22+
import { readYaml, write } from "../config";
2223

2324
/**
2425
* Read given pipeline file as json object.
@@ -470,6 +471,29 @@ export const updateTriggerBranchesForServiceBuildAndUpdatePipeline = (
470471
);
471472
};
472473

474+
/**
475+
* Appends a variable group an Azure pipeline yaml
476+
* @param dir The directory where the pipeline yaml file is
477+
* @param pipelineFile The name of the pipeline yaml file
478+
* @param variableGroupName The name of the variable group to be added
479+
*/
480+
export const appendVariableGroupToPipelineYaml = (
481+
dir: string,
482+
fileName: string,
483+
variableGroupName: string
484+
): void => {
485+
const pipelineFile = readYaml(path.join(dir, fileName)) as AzurePipelinesYaml;
486+
487+
if (!pipelineFile.variables) {
488+
pipelineFile.variables = [];
489+
}
490+
491+
pipelineFile.variables.push({ group: variableGroupName });
492+
493+
logger.info(`Updating '${dir}/${fileName}'.`);
494+
write(pipelineFile, dir, fileName);
495+
};
496+
473497
/**
474498
* Returns a the Manifest Generation Pipeline as defined here: https://github.com/microsoft/bedrock/blob/master/gitops/azure-devops/ManifestGeneration.md#add-azure-pipelines-build-yaml
475499
*/
@@ -481,6 +505,7 @@ const manifestGenerationPipelineYaml = (): string => {
481505
include: ["master"],
482506
},
483507
},
508+
variables: [],
484509
pool: {
485510
vmImage: VM_IMAGE,
486511
},

src/test/mockFactory.ts

+14
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ export const createTestHldAzurePipelinesYaml = (
413413
include: ["master"],
414414
},
415415
},
416+
variables: [],
416417
pool: {
417418
vmImage: VM_IMAGE,
418419
},
@@ -509,6 +510,19 @@ export const createTestHldAzurePipelinesYaml = (
509510
: data;
510511
};
511512

513+
export const createTestHldAzurePipelinesYamlWithVariableGroup = (
514+
asString = true
515+
): AzurePipelinesYaml | string => {
516+
const data: AzurePipelinesYaml = createTestHldAzurePipelinesYaml(
517+
false
518+
) as AzurePipelinesYaml;
519+
data.variables = [{ group: "my-vg" }];
520+
521+
return asString
522+
? yaml.safeDump(data, { lineWidth: Number.MAX_SAFE_INTEGER })
523+
: data;
524+
};
525+
512526
export const createTestComponentYaml = (
513527
asString = true
514528
): ComponentYaml | string => {

tests/validations.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ spk hld install-manifest-pipeline --org-name $AZDO_ORG -d $AZDO_PROJECT --person
153153
# Will no longer be needed once install-manifest-pipeline supports adding a VG
154154
##################################
155155
cd $hld_dir
156-
printf "variables:\n - group: $vg_name\n" | cat - manifest-generation.yaml > temp && mv temp manifest-generation.yaml
156+
spk hld append-variable-group $vg_name
157157
git add .
158158
git commit -m "Adding variable group $vg_name to pipeline"
159159
git push origin master

0 commit comments

Comments
 (0)