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

Commit 8fca041

Browse files
dennisseahmtarng
andauthored
[HOUSEKEEPING] remove page wide eslint disable in project commands (#479)
Co-authored-by: Michael Tarng <[email protected]>
1 parent c21d4aa commit 8fca041

File tree

5 files changed

+177
-146
lines changed

5 files changed

+177
-146
lines changed

src/commands/hld/reconcile.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// eslint-disable-next-line @typescript-eslint/camelcase
21
import child_process from "child_process";
32
import commander from "commander";
43
import fs from "fs";
@@ -28,7 +27,6 @@ import { errorStatusCode } from "../../lib/errorStatusCode";
2827
* reject()
2928
*/
3029
interface ExecResult {
31-
// eslint-disable-next-line @typescript-eslint/camelcase
3230
error?: child_process.ExecException;
3331
value?: { stdout: string; stderr: string };
3432
}
@@ -43,7 +41,6 @@ interface ExecResult {
4341
*/
4442
const exec = async (cmd: string, pipeIO = false): Promise<ExecResult> => {
4543
return new Promise<ExecResult>((resolve) => {
46-
// eslint-disable-next-line @typescript-eslint/camelcase
4744
const child = child_process.exec(cmd, (error, stdout, stderr) => {
4845
return resolve({
4946
error: error ?? undefined,

src/commands/project/create-variable-group.test.ts

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
21
import fs from "fs";
32
import yaml from "js-yaml";
43
import mockFs from "mock-fs";
@@ -252,7 +251,10 @@ describe("setVariableGroupInBedrockFile", () => {
252251
const bedrockFile = readBedrockFile(randomTmpDir);
253252

254253
logger.info(`filejson: ${JSON.stringify(bedrockFile)}`);
255-
expect(bedrockFile.variableGroups![0]).toBe(variableGroupName);
254+
expect(bedrockFile.variableGroups).toBeDefined();
255+
if (bedrockFile.variableGroups) {
256+
expect(bedrockFile.variableGroups[0]).toBe(variableGroupName);
257+
}
256258
});
257259

258260
test("Should pass adding a valid variable group name when bedrock file exists when variableGroups length is > 0", async () => {
@@ -273,8 +275,11 @@ describe("setVariableGroupInBedrockFile", () => {
273275

274276
const bedrockFile = readBedrockFile(randomTmpDir);
275277
logger.info(`filejson: ${JSON.stringify(bedrockFile)}`);
276-
expect(bedrockFile.variableGroups![0]).toBe(prevariableGroupName);
277-
expect(bedrockFile.variableGroups![1]).toBe(variableGroupName);
278+
expect(bedrockFile.variableGroups).toBeDefined();
279+
if (bedrockFile.variableGroups) {
280+
expect(bedrockFile.variableGroups[0]).toBe(prevariableGroupName);
281+
expect(bedrockFile.variableGroups[1]).toBe(variableGroupName);
282+
}
278283
});
279284
});
280285

@@ -343,7 +348,10 @@ describe("updateLifeCyclePipeline", () => {
343348

344349
const hldLifeCycleYaml = readYaml<AzurePipelinesYaml>(hldFilePath);
345350
logger.info(`filejson: ${JSON.stringify(hldLifeCycleYaml)}`);
346-
expect(hldLifeCycleYaml.variables!.length).toBeLessThanOrEqual(0);
351+
expect(hldLifeCycleYaml.variables).toBeDefined();
352+
if (hldLifeCycleYaml.variables) {
353+
expect(hldLifeCycleYaml.variables.length).toBeLessThanOrEqual(0);
354+
}
347355
});
348356

349357
test("Should pass adding variable groups when bedrock file exists with one variableGroup", async () => {
@@ -379,9 +387,13 @@ describe("updateLifeCyclePipeline", () => {
379387

380388
const hldLifeCycleYaml = readYaml<AzurePipelinesYaml>(hldFilePath);
381389
logger.info(`filejson: ${JSON.stringify(hldLifeCycleYaml)}`);
382-
expect(hldLifeCycleYaml.variables![0]).toEqual({
383-
group: variableGroupName,
384-
});
390+
expect(hldLifeCycleYaml.variables).toBeDefined();
391+
392+
if (hldLifeCycleYaml.variables) {
393+
expect(hldLifeCycleYaml.variables[0]).toEqual({
394+
group: variableGroupName,
395+
});
396+
}
385397
});
386398
});
387399

src/commands/project/create-variable-group.ts

+112-114
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-use-before-define */
2-
31
import { VariableGroup } from "azure-devops-node-api/interfaces/ReleaseInterfaces";
42
import commander from "commander";
53
import path from "path";
@@ -55,118 +53,6 @@ export const validateValues = (projectName: string, orgName: string): void => {
5553
validateOrgNameThrowable(orgName);
5654
};
5755

58-
/**
59-
* Executes the command.
60-
*
61-
* @param variableGroupName Variable Group Name
62-
* @param opts Option object from command
63-
*/
64-
export const execute = async (
65-
variableGroupName: string,
66-
opts: CommandOptions,
67-
exitFn: (status: number) => Promise<void>
68-
): Promise<void> => {
69-
if (!hasValue(variableGroupName)) {
70-
await exitFn(1);
71-
return;
72-
}
73-
74-
try {
75-
const projectPath = process.cwd();
76-
logger.verbose(`project path: ${projectPath}`);
77-
78-
checkDependencies(projectPath);
79-
80-
const { azure_devops } = Config();
81-
82-
const {
83-
registryName,
84-
servicePrincipalId,
85-
servicePrincipalPassword,
86-
tenant,
87-
hldRepoUrl = azure_devops?.hld_repository,
88-
orgName = azure_devops?.org,
89-
personalAccessToken = azure_devops?.access_token,
90-
devopsProject = azure_devops?.project,
91-
} = opts;
92-
93-
const accessOpts: AzureDevOpsOpts = {
94-
orgName,
95-
personalAccessToken,
96-
project: devopsProject,
97-
};
98-
99-
logger.debug(`access options: ${JSON.stringify(accessOpts)}`);
100-
101-
const errors = validateForRequiredValues(decorator, {
102-
devopsProject,
103-
hldRepoUrl,
104-
orgName,
105-
personalAccessToken,
106-
registryName,
107-
servicePrincipalId,
108-
servicePrincipalPassword,
109-
tenant,
110-
});
111-
112-
if (errors.length !== 0) {
113-
await exitFn(1);
114-
return;
115-
}
116-
117-
// validateForRequiredValues assure that devopsProject
118-
// and orgName are not empty string
119-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
120-
validateValues(devopsProject!, orgName!);
121-
122-
const variableGroup = await create(
123-
variableGroupName,
124-
registryName,
125-
hldRepoUrl,
126-
servicePrincipalId,
127-
servicePrincipalPassword,
128-
tenant,
129-
accessOpts
130-
);
131-
132-
// set the variable group name
133-
// variableGroup.name is set at this point that's it should have value
134-
// and not empty string or undefined. having || "" is just to avoid
135-
// eslint error
136-
setVariableGroupInBedrockFile(projectPath, variableGroup.name || "");
137-
138-
// update hld-lifecycle.yaml with variable groups in bedrock.yaml
139-
updateLifeCyclePipeline(projectPath);
140-
141-
// print newly created variable group
142-
echo(JSON.stringify(variableGroup, null, 2));
143-
144-
logger.info(
145-
"Successfully created a variable group in Azure DevOps project!"
146-
);
147-
await exitFn(0);
148-
} catch (err) {
149-
logger.error(`Error occurred while creating variable group`);
150-
logger.error(err);
151-
await exitFn(1);
152-
}
153-
};
154-
155-
/**
156-
* Adds the create command to the variable-group command object
157-
*
158-
* @param command Commander command object to decorate
159-
*/
160-
export const commandDecorator = (command: commander.Command): void => {
161-
buildCmd(command, decorator).action(
162-
async (variableGroupName: string, opts: CommandOptions) => {
163-
await execute(variableGroupName, opts, async (status: number) => {
164-
await exitCmd(logger, process.exit, status);
165-
});
166-
}
167-
);
168-
};
169-
17056
/**
17157
* Creates a Azure DevOps variable group
17258
*
@@ -319,3 +205,115 @@ export const updateLifeCyclePipeline = (rootProjectPath: string): void => {
319205
// Write out
320206
write(pipelineFile, absProjectRoot, fileName);
321207
};
208+
209+
/**
210+
* Executes the command.
211+
*
212+
* @param variableGroupName Variable Group Name
213+
* @param opts Option object from command
214+
*/
215+
export const execute = async (
216+
variableGroupName: string,
217+
opts: CommandOptions,
218+
exitFn: (status: number) => Promise<void>
219+
): Promise<void> => {
220+
if (!hasValue(variableGroupName)) {
221+
await exitFn(1);
222+
return;
223+
}
224+
225+
try {
226+
const projectPath = process.cwd();
227+
logger.verbose(`project path: ${projectPath}`);
228+
229+
checkDependencies(projectPath);
230+
231+
const { azure_devops } = Config();
232+
233+
const {
234+
registryName,
235+
servicePrincipalId,
236+
servicePrincipalPassword,
237+
tenant,
238+
hldRepoUrl = azure_devops?.hld_repository,
239+
orgName = azure_devops?.org,
240+
personalAccessToken = azure_devops?.access_token,
241+
devopsProject = azure_devops?.project,
242+
} = opts;
243+
244+
const accessOpts: AzureDevOpsOpts = {
245+
orgName,
246+
personalAccessToken,
247+
project: devopsProject,
248+
};
249+
250+
logger.debug(`access options: ${JSON.stringify(accessOpts)}`);
251+
252+
const errors = validateForRequiredValues(decorator, {
253+
devopsProject,
254+
hldRepoUrl,
255+
orgName,
256+
personalAccessToken,
257+
registryName,
258+
servicePrincipalId,
259+
servicePrincipalPassword,
260+
tenant,
261+
});
262+
263+
if (errors.length !== 0) {
264+
await exitFn(1);
265+
return;
266+
}
267+
268+
// validateForRequiredValues assure that devopsProject
269+
// and orgName are not empty string
270+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
271+
validateValues(devopsProject!, orgName!);
272+
273+
const variableGroup = await create(
274+
variableGroupName,
275+
registryName,
276+
hldRepoUrl,
277+
servicePrincipalId,
278+
servicePrincipalPassword,
279+
tenant,
280+
accessOpts
281+
);
282+
283+
// set the variable group name
284+
// variableGroup.name is set at this point that's it should have value
285+
// and not empty string or undefined. having || "" is just to avoid
286+
// eslint error
287+
setVariableGroupInBedrockFile(projectPath, variableGroup.name || "");
288+
289+
// update hld-lifecycle.yaml with variable groups in bedrock.yaml
290+
updateLifeCyclePipeline(projectPath);
291+
292+
// print newly created variable group
293+
echo(JSON.stringify(variableGroup, null, 2));
294+
295+
logger.info(
296+
"Successfully created a variable group in Azure DevOps project!"
297+
);
298+
await exitFn(0);
299+
} catch (err) {
300+
logger.error(`Error occurred while creating variable group`);
301+
logger.error(err);
302+
await exitFn(1);
303+
}
304+
};
305+
306+
/**
307+
* Adds the create command to the variable-group command object
308+
*
309+
* @param command Commander command object to decorate
310+
*/
311+
export const commandDecorator = (command: commander.Command): void => {
312+
buildCmd(command, decorator).action(
313+
async (variableGroupName: string, opts: CommandOptions) => {
314+
await execute(variableGroupName, opts, async (status: number) => {
315+
await exitCmd(logger, process.exit, status);
316+
});
317+
}
318+
);
319+
};

src/commands/project/pipeline.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import {
1414
checkDependencies,
1515
execute,
1616
fetchValidateValues,
17-
CommandOptions,
17+
ConfigValues,
1818
installLifecyclePipeline,
19+
CommandOptions,
1920
} from "./pipeline";
2021
import { deepClone } from "../../lib/util";
2122

@@ -29,7 +30,7 @@ afterAll(() => {
2930

3031
const gitUrl = "https://github.com/CatalystCode/spk.git";
3132

32-
const mockValues: CommandOptions = {
33+
const mockValues: ConfigValues = {
3334
buildScriptUrl: "buildScriptUrl",
3435
devopsProject: "azDoProject",
3536
orgName: "orgName",
@@ -40,7 +41,7 @@ const mockValues: CommandOptions = {
4041
yamlFileBranch: "master",
4142
};
4243

43-
jest.spyOn(azdo, "validateRepository").mockReturnValue(Promise.resolve());
44+
jest.spyOn(azdo, "validateRepository").mockResolvedValue();
4445

4546
const mockMissingValues: CommandOptions = {
4647
buildScriptUrl: undefined,

0 commit comments

Comments
 (0)