Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.
Merged
2 changes: 1 addition & 1 deletion src/commands/project/pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe("installLifecyclePipeline and execute tests", () => {
expect(e).toBeDefined();
const builtDefnString = JSON.stringify({ fakeProperty: "temp" });
expect(e.message).toBe(
`Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}`
`project-pipeline-err-invalid-build-definition: Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}`
);
}
});
Expand Down
98 changes: 62 additions & 36 deletions src/commands/project/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import {
} from "../../lib/commandBuilder";
import {
BUILD_SCRIPT_URL,
PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE,
PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE,
PROJECT_PIPELINE_FILENAME,
} from "../../lib/constants";
import { AzureDevOpsOpts } from "../../lib/git";
Expand All @@ -39,6 +37,8 @@ import {
import { logger } from "../../logger";
import { BedrockFileInfo, ConfigYaml } from "../../types";
import decorator from "./pipeline.decorator.json";
import { build as buildError, log as logError } from "../../lib/errorBuilder";
import { errorStatusCode } from "../../lib/errorStatusCode";

export interface CommandOptions {
orgName: string | undefined;
Expand All @@ -54,9 +54,15 @@ export interface CommandOptions {
export const checkDependencies = (projectPath: string): void => {
const file: BedrockFileInfo = bedrockFileInfo(projectPath);
if (file.exist === false) {
throw new Error(PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE);
throw buildError(
errorStatusCode.VALIDATION_ERR,
"project-pipeline-err-init-dependency"
);
} else if (file.hasVariableGroups === false) {
throw new Error(PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE);
throw buildError(
errorStatusCode.VALIDATION_ERR,
"project-pipeline-err-cvg"
);
}
};

Expand All @@ -75,11 +81,17 @@ export const fetchValidateValues = (
spkConfig: ConfigYaml | undefined
): CommandOptions => {
if (!spkConfig) {
throw new Error("SPK Config is missing");
throw buildError(
errorStatusCode.VALIDATION_ERR,
"project-pipeline-err-spk-config-missing"
);
}
const azureDevops = spkConfig?.azure_devops;
if (!opts.repoUrl) {
throw Error(`Repo url not defined`);
throw buildError(
errorStatusCode.VALIDATION_ERR,
"project-pipeline-err-repo-url-undefined"
);
}

const values: CommandOptions = {
Expand Down Expand Up @@ -109,7 +121,10 @@ export const fetchValidateValues = (
const error = validateForRequiredValues(decorator, map);

if (error.length > 0) {
throw Error("invalid option values");
throw buildError(
errorStatusCode.VALIDATION_ERR,
"project-pipeline-err-invalid-options"
);
}

validateProjectNameThrowable(values.devopsProject!);
Expand Down Expand Up @@ -162,10 +177,16 @@ const createPipeline = async (
definition
);
} catch (err) {
logger.error(
`Error occurred during pipeline creation for ${values.pipelineName}`
const errorInfo = buildError(
errorStatusCode.EXE_FLOW_ERR,
{
errorKey: "project-pipeline-err-pipeline-create",
values: [values.pipelineName],
},
err
);
throw err; // catch by other catch block
logError(errorInfo);
throw errorInfo;
}
};

Expand All @@ -191,9 +212,10 @@ export const installLifecyclePipeline = async (
);
if (typeof pipeline.id === "undefined") {
const builtDefnString = JSON.stringify(pipeline);
throw Error(
`Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}`
);
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "project-pipeline-err-invalid-build-definition",
values: [builtDefnString],
});
}
logger.info(`Created pipeline for ${values.pipelineName}`);
logger.info(`Pipeline ID: ${pipeline.id}`);
Expand All @@ -213,28 +235,29 @@ export const execute = async (
projectPath: string,
exitFn: (status: number) => Promise<void>
): Promise<void> => {
if (!opts.repoUrl || !opts.pipelineName) {
logger.error(`Values for repo url and/or pipeline name are missing`);
await exitFn(1);
return;
}
const gitUrlType = await isGitHubUrl(opts.repoUrl);
if (gitUrlType) {
logger.error(
`GitHub repos are not supported. Repo url: ${opts.repoUrl} is invalid`
);
await exitFn(1);
return;
}
if (!projectPath) {
logger.error("Project Path is missing");
await exitFn(1);
return;
}
try {
if (!opts.repoUrl || !opts.pipelineName) {
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "project-pipeline-err-missing-values",
values: ["repo url and/or pipeline name"],
});
}
const gitUrlType = await isGitHubUrl(opts.repoUrl);
if (gitUrlType) {
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "project-pipeline-err-github-repo",
values: [opts.repoUrl],
});
}
if (!projectPath) {
throw buildError(errorStatusCode.VALIDATION_ERR, {
errorKey: "project-pipeline-err-missing-values",
values: ["project path"],
});
}

logger.verbose(`project path: ${projectPath}`);
logger.verbose(`project path: ${projectPath}`);

try {
checkDependencies(projectPath);
const gitOriginUrl = await getOriginUrl();
const values = fetchValidateValues(opts, gitOriginUrl, Config());
Expand All @@ -254,10 +277,13 @@ export const execute = async (
await installLifecyclePipeline(values);
await exitFn(0);
} catch (err) {
logger.error(
`Error occurred installing pipeline for project hld lifecycle.`
logError(
buildError(
errorStatusCode.CMD_EXE_ERR,
"project-pipeline-cmd-failed",
err
)
);
logger.error(err);
await exitFn(1);
}
};
Expand Down
11 changes: 9 additions & 2 deletions src/lib/gitutils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@ export const getRepositoryName = (originUrl: string): string => {
logger.debug("github repo found.");
return name;
} else {
throw Error(
"Could not determine origin repository, or it is not a supported type."
if (!resource.startsWith("http")) {
throw buildError(
errorStatusCode.VALIDATION_ERR,
"git-err-invalid-repository-url"
);
}
throw buildError(
errorStatusCode.VALIDATION_ERR,
"git-err-validating-remote-git"
);
}
};
Expand Down
14 changes: 14 additions & 0 deletions src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@
"hld-append-var-group-cmd-failed": "HLD Append Variable Group command was not successfully executed.",
"hld-append-var-group-name-missing": "Variable group name was not provided. Provide variable group.",

"project-pipeline-cmd-failed": "Project install lifecycle pipeline was not successfully executed.",
"project-pipeline-err-github-repo": "GitHub repos are not supported. Repo url: {0} is invalid",
"project-pipeline-err-missing-values": "Value for {0} is missing.",
"project-pipeline-err-init-dependency": "Please run `spk project init` and `spk project cvg` commands before running this command to initialize the project.",
"project-pipeline-err-cvg": "Please run `spk project cvg` command before running this command to create a variable group.",
"project-pipeline-err-repo-url-undefined": "Repo url not defined",
"project-pipeline-err-spk-config-missing": "SPK Config is missing",
"project-pipeline-err-invalid-options": "Invalid option values",
"project-pipeline-err-pipeline-create": "Error occurred during pipeline creation for {0}",
"project-pipeline-err-invalid-build-definition": "Invalid BuildDefinition created, parameter 'id' is missing from {0}",

"git-err-validating-remote-git": "Could not determine origin repository, or it is not a supported type.",
"git-err-invalid-repository-url": "Repositiry URL does not begin with http(s).",

"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.",
"fileutils-generate-hld-pipeline-yaml": "Could not generate HLD Azure pipeline YAML.",
"fileutils-generate-default-hld-component-yaml": "Could not generate default HLD component YAML.",
Expand Down