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

Commit 5948d80

Browse files
authored
[FEATURE] add exception chain to hld add variable group command (#470)
1 parent ec536f8 commit 5948d80

File tree

5 files changed

+122
-49
lines changed

5 files changed

+122
-49
lines changed

src/commands/hld/append-variable-group.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
33
import { RENDER_HLD_PIPELINE_FILENAME } from "../../lib/constants";
44
import { appendVariableGroupToPipelineYaml } from "../../lib/fileutils";
55
import { logger } from "../../logger";
6+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
7+
import { errorStatusCode } from "../../lib/errorStatusCode";
8+
import { hasValue } from "../../lib/validator";
69
import decorator from "./append-variable-group.decorator.json";
710

811
/**
@@ -18,22 +21,28 @@ export const execute = async (
1821
variableGroupName: string,
1922
exitFn: (status: number) => Promise<void>
2023
): Promise<void> => {
21-
if (!variableGroupName) {
22-
logger.error("Variable group name is missing.");
23-
await exitFn(1);
24-
return;
25-
}
26-
2724
try {
25+
if (!hasValue(variableGroupName)) {
26+
throw buildError(
27+
errorStatusCode.VALIDATION_ERR,
28+
"hld-append-var-group-name-missing"
29+
);
30+
}
31+
2832
appendVariableGroupToPipelineYaml(
2933
hldRepoPath,
3034
RENDER_HLD_PIPELINE_FILENAME,
3135
variableGroupName
3236
);
3337
await exitFn(0);
3438
} catch (err) {
35-
logger.error(`Error occurred while appending variable group.`);
36-
logger.error(err);
39+
logError(
40+
buildError(
41+
errorStatusCode.CMD_EXE_ERR,
42+
"hld-append-var-group-cmd-failed",
43+
err
44+
)
45+
);
3746
await exitFn(1);
3847
}
3948
};

src/lib/errorStatusCode.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export enum errorStatusCode {
66
VALIDATION_ERR = 1001,
77
EXE_FLOW_ERR = 1002,
88
ENV_SETTING_ERR = 1010,
9+
FILE_IO_ERR = 1011,
910
GIT_OPS_ERR = 1100,
1011
}

src/lib/fileutils.test.ts

+80-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
21
////////////////////////////////////////////////////////////////////////////////
32
// !!NOTE!!
43
// This test suite uses mock-fs to mock out the the file system
@@ -34,6 +33,7 @@ import {
3433
createTestServiceBuildAndUpdatePipelineYaml,
3534
} from "../test/mockFactory";
3635
import { AccessYaml, AzurePipelinesYaml, MaintainersFile } from "../types";
36+
import { errorStatusCode } from "./errorStatusCode";
3737
import {
3838
addNewServiceToMaintainersFile,
3939
appendVariableGroupToPipelineYaml,
@@ -461,6 +461,17 @@ describe("appendVariableGroupToHldAzurePipelinesYaml", () => {
461461
createTestHldAzurePipelinesYamlWithVariableGroup()
462462
);
463463
});
464+
it("negative test", () => {
465+
try {
466+
appendVariableGroupToPipelineYaml(
467+
uuid(),
468+
RENDER_HLD_PIPELINE_FILENAME,
469+
"my-vg"
470+
);
471+
} catch (err) {
472+
expect(err.errorCode).toBe(errorStatusCode.FILE_IO_ERR);
473+
}
474+
});
464475
});
465476

466477
describe("generateDefaultHldComponentYaml", () => {
@@ -667,26 +678,48 @@ describe("serviceBuildUpdatePipeline", () => {
667678
expect(deserializedYaml).toStrictEqual(buildPipelineYaml);
668679

669680
// variables should include all groups
670-
for (const group of variableGroups) {
671-
expect(buildPipelineYaml.variables!.includes({ group }));
681+
expect(buildPipelineYaml.variables).toBeDefined();
682+
if (buildPipelineYaml.variables) {
683+
expect(buildPipelineYaml.variables.length).toBe(variableGroups.length);
684+
685+
for (const group of variableGroups) {
686+
expect(buildPipelineYaml.variables.includes({ group }));
687+
}
672688
}
673-
expect(buildPipelineYaml.variables!.length).toBe(variableGroups.length);
674689

675690
// trigger branches should include all ring branches
676-
for (const branch of ringBranches) {
677-
expect(buildPipelineYaml.trigger!.branches!.include!.includes(branch));
691+
expect(buildPipelineYaml.trigger).toBeDefined();
692+
if (buildPipelineYaml.trigger) {
693+
expect(buildPipelineYaml.trigger.branches).toBeDefined();
694+
695+
if (buildPipelineYaml.trigger.branches) {
696+
expect(buildPipelineYaml.trigger.branches.include).toBeDefined();
697+
698+
if (buildPipelineYaml.trigger.branches.include) {
699+
expect(buildPipelineYaml.trigger.branches.include.length).toBe(
700+
ringBranches.length
701+
);
702+
for (const branch of ringBranches) {
703+
expect(buildPipelineYaml.trigger.branches.include.includes(branch));
704+
}
705+
}
706+
}
678707
}
679-
expect(buildPipelineYaml.trigger!.branches!.include!.length).toBe(
680-
ringBranches.length
681-
);
682708

683709
// verify components of stages
684-
expect(buildPipelineYaml.stages && buildPipelineYaml.stages.length === 2);
685-
for (const stage of buildPipelineYaml.stages!) {
686-
for (const job of stage.jobs) {
687-
// pool.vmImage should be 'gentoo'
688-
expect(job.pool!.vmImage).toBe(VM_IMAGE);
689-
expect(job.steps);
710+
expect(buildPipelineYaml.stages).toBeDefined();
711+
if (buildPipelineYaml.stages) {
712+
expect(buildPipelineYaml.stages && buildPipelineYaml.stages.length === 2);
713+
for (const stage of buildPipelineYaml.stages) {
714+
for (const job of stage.jobs) {
715+
expect(job.pool).toBeDefined();
716+
717+
if (job.pool) {
718+
// pool.vmImage should be 'gentoo'
719+
expect(job.pool.vmImage).toBe(VM_IMAGE);
720+
expect(job.steps);
721+
}
722+
}
690723
}
691724
}
692725
});
@@ -723,28 +756,44 @@ describe("serviceBuildUpdatePipeline", () => {
723756
"utf8"
724757
)
725758
);
726-
const hasCorrectIncludes = azureYaml.trigger!.paths!.include!.includes(
727-
path.relative(randomDirPath, serviceReference.servicePath)
728-
);
729-
expect(hasCorrectIncludes).toBe(true);
730759

731-
let hasCorrectVariableGroup1 = false;
732-
let hasCorrectVariableGroup2 = false;
733-
for (const value of Object.values(azureYaml.variables!)) {
734-
const item = value as { group: string };
760+
expect(azureYaml.trigger).toBeDefined();
735761

736-
if (item.group === variableGroups[0]) {
737-
hasCorrectVariableGroup1 = true;
738-
}
762+
if (azureYaml.trigger) {
763+
expect(azureYaml.trigger.paths).toBeDefined();
764+
765+
if (azureYaml.trigger.paths) {
766+
expect(azureYaml.trigger.paths.include).toBeDefined();
739767

740-
if (item.group === variableGroups[1]) {
741-
hasCorrectVariableGroup2 = true;
768+
if (azureYaml.trigger.paths.include) {
769+
const hasCorrectIncludes = azureYaml.trigger.paths.include.includes(
770+
path.relative(randomDirPath, serviceReference.servicePath)
771+
);
772+
expect(hasCorrectIncludes).toBe(true);
773+
}
742774
}
743775
}
744776

745-
expect(hasCorrectIncludes).toBe(true);
746-
expect(hasCorrectVariableGroup1).toBe(true);
747-
expect(hasCorrectVariableGroup2).toBe(true);
777+
expect(azureYaml.variables).toBeDefined();
778+
779+
if (azureYaml.variables) {
780+
let hasCorrectVariableGroup1 = false;
781+
let hasCorrectVariableGroup2 = false;
782+
for (const value of Object.values(azureYaml.variables)) {
783+
const item = value as { group: string };
784+
785+
if (item.group === variableGroups[0]) {
786+
hasCorrectVariableGroup1 = true;
787+
}
788+
789+
if (item.group === variableGroups[1]) {
790+
hasCorrectVariableGroup2 = true;
791+
}
792+
}
793+
794+
expect(hasCorrectVariableGroup1).toBe(true);
795+
expect(hasCorrectVariableGroup2).toBe(true);
796+
}
748797
}
749798
});
750799
});

src/lib/fileutils.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
User,
2121
} from "../types";
2222
import { readYaml, write } from "../config";
23+
import { build as buildError } from "../lib/errorBuilder";
24+
import { errorStatusCode } from "../lib/errorStatusCode";
2325

2426
/**
2527
* Read given pipeline file as json object.
@@ -482,16 +484,23 @@ export const appendVariableGroupToPipelineYaml = (
482484
fileName: string,
483485
variableGroupName: string
484486
): void => {
485-
const pipelineFile = readYaml(path.join(dir, fileName)) as AzurePipelinesYaml;
486-
487-
if (!pipelineFile.variables) {
488-
pipelineFile.variables = [];
487+
try {
488+
const pipelineFile = readYaml(
489+
path.join(dir, fileName)
490+
) as AzurePipelinesYaml;
491+
pipelineFile.variables = pipelineFile.variables || [];
492+
493+
pipelineFile.variables.push({ group: variableGroupName });
494+
495+
logger.info(`Updating '${dir}/${fileName}'.`);
496+
write(pipelineFile, dir, fileName);
497+
} catch (err) {
498+
throw buildError(
499+
errorStatusCode.FILE_IO_ERR,
500+
"fileutils-append-variable-group-to-pipeline-yaml",
501+
err
502+
);
489503
}
490-
491-
pipelineFile.variables.push({ group: variableGroupName });
492-
493-
logger.info(`Updating '${dir}/${fileName}'.`);
494-
write(pipelineFile, dir, fileName);
495504
};
496505

497506
/**

src/lib/i18n.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
"infra-err-tf-path-not-found": "Provided Terraform {0} path is invalid or cannot be found: {1}",
2626
"infra-err-create-scaffold": "Could not create scaffold",
2727
"infra-err-git-clone-failed": "Could not clone the source remote repository. The remote repo might not exist or you did not have the rights to access it",
28-
"infra-git-source-no-exist": "Source path, {0} did not exist."
28+
"infra-git-source-no-exist": "Source path, {0} did not exist.",
29+
30+
"hld-append-var-group-cmd-failed": "HLD Append Variable Group Command was not successfully executed.",
31+
"hld-append-var-group-name-missing": "Variable group name was not provided. Provide variable group.",
32+
33+
"fileutils-append-variable-group-to-pipeline-yaml": "Could not append variable group name to manifest-generation.yaml file in HLD repo. Check this is file exist and if it is YAML format."
2934
}
3035
}

0 commit comments

Comments
 (0)