diff --git a/docs/commands/data.json b/docs/commands/data.json index a13dd3f6a..0b25ba6a0 100644 --- a/docs/commands/data.json +++ b/docs/commands/data.json @@ -461,12 +461,14 @@ { "arg": "-a, --personal-access-token ", "description": "Personal Access Token", - "required": true + "required": true, + "inherit": "azureDevops.access_token" }, { "arg": "-o, --org-name ", "description": "Organization Name for Azure DevOps", - "required": true + "required": true, + "inherit": "azureDevops.org" }, { "arg": "-u, --repo-url ", @@ -476,7 +478,8 @@ { "arg": "-d, --devops-project ", "description": "Azure DevOps Project name", - "required": true + "required": true, + "inherit": "azureDevops.project" }, { "arg": "-b, --build-script-url ", diff --git a/src/commands/hld/pipeline.test.ts b/src/commands/hld/pipeline.test.ts index 38949eb53..e46ed8dd0 100644 --- a/src/commands/hld/pipeline.test.ts +++ b/src/commands/hld/pipeline.test.ts @@ -93,8 +93,8 @@ const orgNameTest = (hasVal: boolean): void => { ); } else { expect(() => populateValues(data)).toThrow( - `The following arguments are required: - -o, --org-name ` + `validation-err-missing-vals: These mandatory options were missing: + -o, --org-name . Provide them.` ); } }; @@ -119,8 +119,8 @@ const projectNameTest = (hasVal: boolean): void => { ); } else { expect(() => populateValues(data)).toThrow( - `The following arguments are required: - -d, --devops-project ` + `validation-err-missing-vals: These mandatory options were missing: + -d, --devops-project . Provide them.` ); } }; diff --git a/src/commands/project/pipeline.decorator.json b/src/commands/project/pipeline.decorator.json index 4f8ac8229..a70e1f971 100644 --- a/src/commands/project/pipeline.decorator.json +++ b/src/commands/project/pipeline.decorator.json @@ -11,12 +11,14 @@ { "arg": "-a, --personal-access-token ", "description": "Personal Access Token", - "required": true + "required": true, + "inherit": "azureDevops.access_token" }, { "arg": "-o, --org-name ", "description": "Organization Name for Azure DevOps", - "required": true + "required": true, + "inherit": "azureDevops.org" }, { "arg": "-u, --repo-url ", @@ -26,7 +28,8 @@ { "arg": "-d, --devops-project ", "description": "Azure DevOps Project name", - "required": true + "required": true, + "inherit": "azureDevops.project" }, { "arg": "-b, --build-script-url ", diff --git a/src/commands/project/pipeline.test.ts b/src/commands/project/pipeline.test.ts index a7300da9a..68ea2305e 100644 --- a/src/commands/project/pipeline.test.ts +++ b/src/commands/project/pipeline.test.ts @@ -97,8 +97,13 @@ describe("test fetchValidateValues function", () => { fetchValidateValues(mockMissingValues, gitUrl, { azure_devops: {}, }); - }).toThrow(`project-pipeline-err-invalid-options: Invalid option values`); + }) + .toThrow(`validation-err-missing-vals: These mandatory options were missing: + -a, --personal-access-token + -o, --org-name + -d, --devops-project . Provide them.`); }); + it("SPK Config's azure_devops do not have value and command line does not have values", () => { expect(() => { fetchValidateValues(nullValues, gitUrl, { diff --git a/src/commands/project/pipeline.ts b/src/commands/project/pipeline.ts index 65f1e82cd..6b74513a8 100644 --- a/src/commands/project/pipeline.ts +++ b/src/commands/project/pipeline.ts @@ -10,6 +10,7 @@ import { fileInfo as bedrockFileInfo } from "../../lib/bedrockYaml"; import { build as buildCmd, exit as exitCmd, + populateInheritValueFromConfig, validateForRequiredValues, } from "../../lib/commandBuilder"; import { @@ -97,51 +98,34 @@ export const fetchValidateValues = ( "project-pipeline-err-spk-config-missing" ); } - const azureDevops = spkConfig?.azure_devops; + const repoUrl = validateRepoUrl(opts, gitOriginUrl); - const values: CommandOptions = { - buildScriptUrl: opts.buildScriptUrl || BUILD_SCRIPT_URL, - devopsProject: opts.devopsProject || azureDevops?.project, - orgName: opts.orgName || azureDevops?.org, - personalAccessToken: opts.personalAccessToken || azureDevops?.access_token, - pipelineName: - opts.pipelineName || getRepositoryName(gitOriginUrl) + "-lifecycle", - repoName: getRepositoryName(repoUrl) || getRepositoryName(gitOriginUrl), - repoUrl: opts.repoUrl || getRepositoryUrl(gitOriginUrl), - yamlFileBranch: opts.yamlFileBranch, - }; - const map: { [key: string]: string | undefined } = {}; - (Object.keys(values) as Array).forEach((key) => { - const val = values[key]; - if (key === "personalAccessToken") { - logger.debug(`${key}: XXXXXXXXXXXXXXXXX`); - } else { - logger.debug(`${key}: ${val}`); - } - map[key] = val; - }); + (opts.pipelineName = + opts.pipelineName || getRepositoryName(gitOriginUrl) + "-lifecycle"), + (opts.repoName = + getRepositoryName(repoUrl) || getRepositoryName(gitOriginUrl)), + (opts.repoUrl = opts.repoUrl || getRepositoryUrl(gitOriginUrl)); + opts.yamlFileBranch = opts.yamlFileBranch || "master"; + opts.buildScriptUrl = opts.buildScriptUrl || BUILD_SCRIPT_URL; - const error = validateForRequiredValues(decorator, map); - if (error.length > 0) { - throw buildError( - errorStatusCode.VALIDATION_ERR, - "project-pipeline-err-invalid-options" - ); - } + populateInheritValueFromConfig(decorator, Config(), opts); + + // error will be thrown if validation fails + validateForRequiredValues(decorator, opts, true); // validateForRequiredValues has validated the following // values are validate, adding || "" is just to // satisfy the no-non-null-assertion eslint rule const configVals: ConfigValues = { - orgName: values.orgName || "", - buildScriptUrl: values.buildScriptUrl || BUILD_SCRIPT_URL, - devopsProject: values.devopsProject || "", - repoName: values.repoName, - personalAccessToken: values.personalAccessToken || "", - pipelineName: values.pipelineName || "", - repoUrl: values.repoUrl || "", - yamlFileBranch: values.yamlFileBranch, + orgName: opts.orgName || "", + buildScriptUrl: opts.buildScriptUrl || BUILD_SCRIPT_URL, + devopsProject: opts.devopsProject || "", + repoName: opts.repoName, + personalAccessToken: opts.personalAccessToken || "", + pipelineName: opts.pipelineName || "", + repoUrl: opts.repoUrl || "", + yamlFileBranch: opts.yamlFileBranch, }; validateProjectNameThrowable(configVals.devopsProject); diff --git a/src/lib/commandBuilder.ts b/src/lib/commandBuilder.ts index e6e3d1f17..ab392c984 100644 --- a/src/lib/commandBuilder.ts +++ b/src/lib/commandBuilder.ts @@ -125,7 +125,10 @@ export const validateForRequiredValues = ( const errors = missingItems.map((item) => item.opt.arg); if (toThrow && errors.length !== 0) { - throw `The following arguments are required:\n ${errors.join("\n ")}`; + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "validation-err-missing-vals", + values: [errors.join("\n ")], + }); } if (errors.length !== 0) { diff --git a/src/lib/i18n.json b/src/lib/i18n.json index c5e1bef4f..549f6872a 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -69,7 +69,6 @@ "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-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}", @@ -224,6 +223,7 @@ "variable-group-create-cmd-err-org-missing": "Provide a value for {0}.", "variable-group-create-cmd-err-file-missing": "Provide a file with the variable group manifest.", + "validation-err-missing-vals": "These mandatory options were missing:\n {0}. Provide them.", "validation-err-org-name-missing": "Organization name was missing. Provide it.", "validation-err-org-name": "Organization name must start with a letter or number, followed by letters, numbers or hyphens, and must end with a letter or number.", "validation-err-password-missing": "Password was missing. Provide it.",