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

Commit 16641db

Browse files
[HOUSEKEEPING] remove page wide eslint disable for service cmds (#489)
Co-authored-by: Yvonne Radsmikham <[email protected]>
1 parent 57d23a6 commit 16641db

File tree

5 files changed

+227
-206
lines changed

5 files changed

+227
-206
lines changed

src/commands/service/create-revision.ts

+64-41
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2-
31
import commander from "commander";
42
import { join } from "path";
53
import { Bedrock, Config } from "../../config";
@@ -29,6 +27,16 @@ export interface CommandOptions {
2927
targetBranch: string | undefined;
3028
}
3129

30+
export interface CommandValues {
31+
sourceBranch: string;
32+
title: string | undefined;
33+
description: string;
34+
remoteUrl: string;
35+
personalAccessToken: string;
36+
orgName: string;
37+
targetBranch: string | undefined;
38+
}
39+
3240
export const getRemoteUrl = async (
3341
remoteUrl: string | undefined
3442
): Promise<string> => {
@@ -99,70 +107,85 @@ export const getSourceBranch = async (
99107
/**
100108
* Creates a pull request from the given source branch
101109
* @param defaultRings List of default rings
102-
* @param opts option values
110+
* @param values option values
103111
*/
104112
export const makePullRequest = async (
105113
defaultRings: string[],
106-
opts: CommandOptions
114+
values: CommandValues
107115
): Promise<void> => {
108116
for (const ring of defaultRings) {
109-
const title = opts.title || `[SPK] ${opts.sourceBranch} => ${ring}`;
110-
await createPullRequest(title, opts.sourceBranch!, ring, {
111-
description: opts.description!,
112-
orgName: opts.orgName!,
113-
originPushUrl: opts.remoteUrl!,
114-
personalAccessToken: opts.personalAccessToken!,
117+
const title = values.title || `[SPK] ${values.sourceBranch} => ${ring}`;
118+
await createPullRequest(title, values.sourceBranch, ring, {
119+
description: values.description,
120+
orgName: values.orgName,
121+
originPushUrl: values.remoteUrl,
122+
personalAccessToken: values.personalAccessToken,
115123
});
116124
}
117125
};
118126

127+
const populateValues = async (opts: CommandOptions): Promise<CommandValues> => {
128+
const { azure_devops } = Config();
129+
opts.orgName = opts.orgName || azure_devops?.org;
130+
opts.personalAccessToken =
131+
opts.personalAccessToken || azure_devops?.access_token;
132+
133+
// Default the remote to the git origin
134+
opts.remoteUrl = await getRemoteUrl(opts.remoteUrl);
135+
136+
// default pull request source branch to the current branch
137+
opts.sourceBranch = await getSourceBranch(opts.sourceBranch);
138+
139+
const errors = validateForRequiredValues(decorator, {
140+
orgName: opts.orgName,
141+
personalAccessToken: opts.personalAccessToken,
142+
remoteUrl: opts.remoteUrl,
143+
sourceBranch: opts.sourceBranch,
144+
});
145+
if (errors.length > 0) {
146+
throw Error("missing required values");
147+
}
148+
149+
return {
150+
// validateForRequiredValues confirm that sourceBranch has value
151+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
152+
sourceBranch: opts.sourceBranch!,
153+
title: opts.title,
154+
description: opts.description || "This is automated PR generated via SPK",
155+
remoteUrl: opts.remoteUrl,
156+
// validateForRequiredValues confirm that personalAccessToken has value
157+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
158+
personalAccessToken: opts.personalAccessToken!,
159+
// validateForRequiredValues confirm that orgName has value
160+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
161+
orgName: opts.orgName!,
162+
targetBranch: opts.targetBranch,
163+
};
164+
};
165+
119166
export const execute = async (
120167
opts: CommandOptions,
121168
exitFn: (status: number) => Promise<void>
122169
): Promise<void> => {
123170
try {
124-
const { azure_devops } = Config();
125-
opts.orgName = opts.orgName || azure_devops?.org;
126-
opts.personalAccessToken =
127-
opts.personalAccessToken || azure_devops?.access_token!;
128-
opts.description =
129-
opts.description || "This is automated PR generated via SPK";
130-
131-
////////////////////////////////////////////////////////////////////////
132-
// Give defaults
133-
////////////////////////////////////////////////////////////////////////
171+
const values = await populateValues(opts);
172+
134173
// default pull request against initial ring
135174
const bedrockConfig = Bedrock();
136175
// Default to the --target-branch for creating a revision; if not specified, fallback to default rings in bedrock.yaml
137-
const defaultRings = getDefaultRings(opts.targetBranch, bedrockConfig);
138-
139-
// default pull request source branch to the current branch
140-
opts.sourceBranch = await getSourceBranch(opts.sourceBranch);
176+
const defaultRings = getDefaultRings(values.targetBranch, bedrockConfig);
141177

142178
// Make sure the user isn't trying to make a PR for a branch against itself
143-
if (defaultRings.includes(opts.sourceBranch)) {
179+
if (defaultRings.includes(values.sourceBranch)) {
144180
throw Error(
145181
`A pull request for a branch cannot be made against itself. Ensure your target branch(es) '${JSON.stringify(
146182
defaultRings
147-
)}' do not include your source branch '${opts.sourceBranch}'`
183+
)}' do not include your source branch '${values.sourceBranch}'`
148184
);
149185
}
150186

151-
// Default the remote to the git origin
152-
opts.remoteUrl = await getRemoteUrl(opts.remoteUrl);
153-
const errors = validateForRequiredValues(decorator, {
154-
orgName: opts.orgName,
155-
personalAccessToken: opts.personalAccessToken,
156-
remoteUrl: opts.remoteUrl,
157-
sourceBranch: opts.sourceBranch,
158-
});
159-
160-
if (errors.length > 0) {
161-
await exitFn(1);
162-
} else {
163-
await makePullRequest(defaultRings, opts);
164-
await exitFn(0);
165-
}
187+
await makePullRequest(defaultRings, values);
188+
await exitFn(0);
166189
} catch (err) {
167190
logger.error(err);
168191
await exitFn(1);

src/commands/service/create.test.ts

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2-
/* eslint-disable @typescript-eslint/no-use-before-define */
31
import fs from "fs";
42
import path from "path";
53
import { promisify } from "util";
@@ -292,6 +290,26 @@ describe("Validate Git URLs", () => {
292290
});
293291
});
294292

293+
const writeSampleMaintainersFileToDir = async (
294+
maintainersFilePath: string
295+
): Promise<void> => {
296+
await promisify(fs.writeFile)(
297+
maintainersFilePath,
298+
createTestMaintainersYaml(),
299+
"utf8"
300+
);
301+
};
302+
303+
const writeSampleBedrockFileToDir = async (
304+
bedrockFilePath: string
305+
): Promise<void> => {
306+
await promisify(fs.writeFile)(
307+
bedrockFilePath,
308+
createTestBedrockYaml(),
309+
"utf8"
310+
);
311+
};
312+
295313
describe("Adding a service to a repo directory", () => {
296314
let randomTmpDir = "";
297315
beforeEach(() => {
@@ -460,8 +478,10 @@ describe("Adding a service to a repo directory", () => {
460478
)) {
461479
if (servicePath.includes(serviceName)) {
462480
expect(service.middlewares).toBeDefined();
463-
expect(Array.isArray(service.middlewares)).toBe(true);
464-
expect(service.middlewares!.length).toBe(0);
481+
if (service.middlewares) {
482+
expect(Array.isArray(service.middlewares)).toBe(true);
483+
expect(service.middlewares.length).toBe(0);
484+
}
465485
}
466486
}
467487
});
@@ -501,23 +521,3 @@ describe("Adding a service to a repo directory", () => {
501521
}
502522
});
503523
});
504-
505-
const writeSampleMaintainersFileToDir = async (
506-
maintainersFilePath: string
507-
): Promise<void> => {
508-
await promisify(fs.writeFile)(
509-
maintainersFilePath,
510-
createTestMaintainersYaml(),
511-
"utf8"
512-
);
513-
};
514-
515-
const writeSampleBedrockFileToDir = async (
516-
bedrockFilePath: string
517-
): Promise<void> => {
518-
await promisify(fs.writeFile)(
519-
bedrockFilePath,
520-
createTestBedrockYaml(),
521-
"utf8"
522-
);
523-
};

0 commit comments

Comments
 (0)