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

Commit a3d83fe

Browse files
authored
[FEATURE] Simplify inheritance of opt vals for hld install-manifest-pipeline cmd (#503)
1 parent ae7b9ef commit a3d83fe

File tree

8 files changed

+60
-70
lines changed

8 files changed

+60
-70
lines changed

docs/commands/data.json

+23-9
Original file line numberDiff line numberDiff line change
@@ -283,27 +283,37 @@
283283
{
284284
"arg": "-a, --personal-access-token <personal-access-token>",
285285
"description": "Personal Access Token",
286-
"defaultValue": ""
286+
"defaultValue": "",
287+
"inherit": "azure_devops.access_token",
288+
"required": true
287289
},
288290
{
289291
"arg": "-o, --org-name <organization-name>",
290292
"description": "Organization Name for Azure DevOps",
291-
"defaultValue": ""
293+
"defaultValue": "",
294+
"inherit": "azure_devops.org",
295+
"required": true
292296
},
293297
{
294298
"arg": "-u, --hld-url <hld-url>",
295299
"description": "HLD Repository URL",
296-
"defaultValue": ""
300+
"defaultValue": "",
301+
"inherit": "azure_devops.hld_repository",
302+
"required": true
297303
},
298304
{
299305
"arg": "-m, --manifest-url <manifest-url>",
300306
"description": "Manifest Repository URL",
301-
"defaultValue": ""
307+
"defaultValue": "",
308+
"inherit": "azure_devops.manifest_repository",
309+
"required": true
302310
},
303311
{
304312
"arg": "-d, --devops-project <devops-project>",
305313
"description": "Azure DevOps Project",
306-
"defaultValue": ""
314+
"defaultValue": "",
315+
"inherit": "azure_devops.project",
316+
"required": true
307317
},
308318
{
309319
"arg": "-b, --build-script-url <build-script-url>",
@@ -389,7 +399,8 @@
389399
{
390400
"arg": "-U, --hld-repo-url <hld-repo-url>",
391401
"description": "The high level definition (HLD) git repo url; falls back to azure_devops.org in spk config.",
392-
"required": true
402+
"required": true,
403+
"inherit": "azure_devops.hld_repository"
393404
},
394405
{
395406
"arg": "-u, --service-principal-id <service-principal-id>",
@@ -409,17 +420,20 @@
409420
{
410421
"arg": "-o, --org-name <organization-name>",
411422
"description": "Azure DevOps organization name; falls back to azure_devops.org in spk config.",
412-
"required": true
423+
"required": true,
424+
"inherit": "azure_devops.org"
413425
},
414426
{
415427
"arg": "-d, --devops-project <project>",
416428
"description": "Azure DevOps project name; falls back to azure_devops.project in spk config.",
417-
"required": true
429+
"required": true,
430+
"inherit": "azure_devops.project"
418431
},
419432
{
420433
"arg": "-a, --personal-access-token <personal-access-token>",
421434
"description": "Azure DevOps Personal access token; falls back to azure_devops.access_token in spk config.",
422-
"required": true
435+
"required": true,
436+
"inherit": "azure_devops.access_token"
423437
}
424438
],
425439
"markdown": "## Description\n\nCreate new variable group in Azure DevOps project\n\n## Command Prerequisites\n\nIn addition to an existing\n[Azure DevOps project](https://azure.microsoft.com/en-us/services/devops/), to\nlink secrets from an Azure key vault as variables in Variable Group, you will\nneed an existing key vault containing your secrets and the Service Principal for\nauthorization with Azure Key Vault.\n\n1. Use existng or\n [create a service principal either in Azure Portal](https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal)\n or\n [with Azure CLI](https://docs.microsoft.com/en-us/cli/azure/create-an-azure-service-principal-azure-cli?view=azure-cli-latest).\n2. Use existing or\n [create a Azure Container Registry in Azure Portal](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal)\n or\n [with Azure CLI](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-azure-cli).\n"

src/commands/deployment/dashboard.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import { isPortNumberString, validatePrereqs } from "../../lib/validator";
99
import { logger } from "../../logger";
1010
import { ConfigYaml } from "../../types";
1111
import decorator from "./dashboard.decorator.json";
12-
import {
13-
build as buildError,
14-
log as logError,
15-
build,
16-
} from "../../lib/errorBuilder";
12+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
1713
import { errorStatusCode } from "../../lib/errorStatusCode";
1814

1915
export interface IntrospectionManifest {

src/commands/hld/pipeline.decorator.json

+15-5
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,37 @@
1111
{
1212
"arg": "-a, --personal-access-token <personal-access-token>",
1313
"description": "Personal Access Token",
14-
"defaultValue": ""
14+
"defaultValue": "",
15+
"inherit": "azure_devops.access_token",
16+
"required": true
1517
},
1618
{
1719
"arg": "-o, --org-name <organization-name>",
1820
"description": "Organization Name for Azure DevOps",
19-
"defaultValue": ""
21+
"defaultValue": "",
22+
"inherit": "azure_devops.org",
23+
"required": true
2024
},
2125
{
2226
"arg": "-u, --hld-url <hld-url>",
2327
"description": "HLD Repository URL",
24-
"defaultValue": ""
28+
"defaultValue": "",
29+
"inherit": "azure_devops.hld_repository",
30+
"required": true
2531
},
2632
{
2733
"arg": "-m, --manifest-url <manifest-url>",
2834
"description": "Manifest Repository URL",
29-
"defaultValue": ""
35+
"defaultValue": "",
36+
"inherit": "azure_devops.manifest_repository",
37+
"required": true
3038
},
3139
{
3240
"arg": "-d, --devops-project <devops-project>",
3341
"description": "Azure DevOps Project",
34-
"defaultValue": ""
42+
"defaultValue": "",
43+
"inherit": "azure_devops.project",
44+
"required": true
3545
},
3646
{
3747
"arg": "-b, --build-script-url <build-script-url>",

src/commands/hld/pipeline.test.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const orgNameTest = (hasVal: boolean): void => {
8282
hldUrl: "https://dev.azure.com/mocked/fabrikam/_git/hld",
8383
manifestUrl: "https://dev.azure.com/mocked/fabrikam/_git/materialized",
8484
orgName: hasVal ? "org Name" : "",
85-
personalAccessToken: "",
85+
personalAccessToken: "somesecret",
8686
pipelineName: "",
8787
yamlFileBranch: "",
8888
};
@@ -93,7 +93,8 @@ const orgNameTest = (hasVal: boolean): void => {
9393
);
9494
} else {
9595
expect(() => populateValues(data)).toThrow(
96-
"value for -o, --org-name <organization-name> is missing"
96+
`The following arguments are required:
97+
-o, --org-name <organization-name>`
9798
);
9899
}
99100
};
@@ -107,7 +108,7 @@ const projectNameTest = (hasVal: boolean): void => {
107108
hldUrl: "https://dev.azure.com/mocked/fabrikam/_git/hld",
108109
manifestUrl: "https://dev.azure.com/mocked/fabrikam/_git/materialized",
109110
orgName: "orgName",
110-
personalAccessToken: "",
111+
personalAccessToken: "somesecret",
111112
pipelineName: "",
112113
yamlFileBranch: "",
113114
};
@@ -118,7 +119,8 @@ const projectNameTest = (hasVal: boolean): void => {
118119
);
119120
} else {
120121
expect(() => populateValues(data)).toThrow(
121-
"value for -d, --devops-project <devops-project> is missing"
122+
`The following arguments are required:
123+
-d, --devops-project <devops-project>`
122124
);
123125
}
124126
};
@@ -176,7 +178,7 @@ describe("test populateValues function", () => {
176178
hldUrl: "https://github.com/fabrikam/hld",
177179
manifestUrl: "https://github.com/fabrikam/materialized",
178180
orgName: "orgName",
179-
personalAccessToken: "",
181+
personalAccessToken: "somevalue",
180182
pipelineName: "",
181183
yamlFileBranch: "",
182184
})

src/commands/hld/pipeline.ts

+8-42
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { validateRepository } from "../../lib/azdoClient";
99
import {
1010
build as buildCmd,
1111
exit as exitCmd,
12-
getOption as getCmdOption,
12+
populateInheritValueFromConfig,
13+
validateForRequiredValues,
1314
} from "../../lib/commandBuilder";
1415
import {
1516
BUILD_SCRIPT_URL,
@@ -24,13 +25,12 @@ import {
2425
IAzureRepoPipelineConfig,
2526
queueBuild,
2627
} from "../../lib/pipelines/pipelines";
27-
import { logger } from "../../logger";
28-
import decorator from "./pipeline.decorator.json";
2928
import {
30-
hasValue,
3129
validateOrgNameThrowable,
3230
validateProjectNameThrowable,
3331
} from "../../lib/validator";
32+
import { logger } from "../../logger";
33+
import decorator from "./pipeline.decorator.json";
3434

3535
export interface CommandOptions {
3636
pipelineName: string;
@@ -57,47 +57,13 @@ const validateRepos = (hldRepoUrl: string, manifestRepoUrl: string): void => {
5757
};
5858

5959
export const populateValues = (opts: CommandOptions): CommandOptions => {
60-
// NOTE: all the values in opts are defaulted to ""
61-
6260
// exception will be thrown if spk's config.yaml is missing
63-
const { azure_devops } = Config();
64-
65-
opts.hldUrl =
66-
opts.hldUrl || emptyStringIfUndefined(azure_devops?.hld_repository);
67-
68-
opts.manifestUrl =
69-
opts.manifestUrl ||
70-
emptyStringIfUndefined(azure_devops?.manifest_repository);
61+
populateInheritValueFromConfig(decorator, Config(), opts);
62+
validateForRequiredValues(decorator, opts, true);
63+
validateOrgNameThrowable(opts.orgName);
64+
validateProjectNameThrowable(opts.devopsProject);
7165

7266
opts.hldName = getRepositoryName(opts.hldUrl);
73-
74-
opts.orgName = opts.orgName || emptyStringIfUndefined(azure_devops?.org);
75-
76-
if (hasValue(opts.orgName)) {
77-
validateOrgNameThrowable(opts.orgName);
78-
} else {
79-
throw Error(
80-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
81-
`value for ${getCmdOption(decorator, "org-name")!.arg} is missing`
82-
);
83-
}
84-
85-
opts.personalAccessToken =
86-
opts.personalAccessToken ||
87-
emptyStringIfUndefined(azure_devops?.access_token);
88-
89-
opts.devopsProject =
90-
opts.devopsProject || emptyStringIfUndefined(azure_devops?.project);
91-
92-
if (hasValue(opts.devopsProject)) {
93-
validateProjectNameThrowable(opts.devopsProject);
94-
} else {
95-
throw Error(
96-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
97-
`value for ${getCmdOption(decorator, "devops-project")!.arg} is missing`
98-
);
99-
}
100-
10167
opts.pipelineName =
10268
opts.hldName + "-to-" + getRepositoryName(opts.manifestUrl);
10369

src/lib/commandBuilder.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ describe("test populateInheritValueFromConfig function", () => {
5353
inherit: "introspection.test",
5454
},
5555
],
56-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5756
},
57+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5858
{} as any,
5959
opts
6060
);

src/lib/commandBuilder.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,13 @@ export const validateForRequiredValues = (
120120
const errors = missingItems.map((item) => item.opt.arg);
121121

122122
if (toThrow && errors.length !== 0) {
123-
throw `The following arguments are required: ${errors.join("\n ")}`;
123+
throw `The following arguments are required:\n ${errors.join("\n ")}`;
124124
}
125125

126126
if (errors.length !== 0) {
127-
logger.error(`the following arguments are required: ${errors.join("\n ")}`);
127+
logger.error(
128+
`the following arguments are required:\n ${errors.join("\n ")}`
129+
);
128130
}
129131
return errors;
130132
};

src/lib/gitutils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export const validateRepoUrl = (
357357
opts: CommandOptions,
358358
gitOriginUrl: string
359359
): string => {
360-
return opts.repoUrl || getRepositoryUrl(gitOriginUrl)
360+
return opts.repoUrl || getRepositoryUrl(gitOriginUrl);
361361
};
362362

363363
/**

0 commit comments

Comments
 (0)