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

Commit 2b76581

Browse files
authored
[HOUSEKEEPING] Remove page wide eslint disable in files in src/lib (#476)
1 parent 59a8b70 commit 2b76581

12 files changed

+94
-104
lines changed

src/lib/azure/azurecredentials.ts

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
/* eslint-disable @typescript-eslint/no-use-before-define */
2-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
31
import { ClientSecretCredential } from "@azure/identity";
42
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
53
import { Config } from "../../config";
64
import { logger } from "../../logger";
75
import { AzureAccessOpts } from "../../types";
86

7+
const verifyConfigDefined = (
8+
servicePrincipalId?: string,
9+
servicePrincipalPassword?: string,
10+
tenantId?: string
11+
): boolean => {
12+
if (servicePrincipalId && servicePrincipalPassword && tenantId) {
13+
return true;
14+
}
15+
logger.error(
16+
`Configuration is missing required fields tenant_id, service_principal_id and service_principal_secret. Please run the init command`
17+
);
18+
return false;
19+
};
20+
921
/**
1022
* Create an instance of `ClientSecretCredential` and returns for Azure data plane activities
1123
* @param opts optionally override spk config with Azure subscription access options
@@ -34,9 +46,14 @@ export const getCredentials = async (
3446
) {
3547
return undefined;
3648
}
49+
50+
// verifyConfigDefined has confirmed that these values are defined.
3751
return new ClientSecretCredential(
52+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3853
tenantId!,
54+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3955
servicePrincipalId!,
56+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
4057
servicePrincipalPassword!
4158
);
4259
};
@@ -68,23 +85,13 @@ export const getManagementCredentials = async (
6885
return undefined;
6986
}
7087

88+
// verifyConfigDefined has confirmed that these values are defined.
7189
return msRestNodeAuth.loginWithServicePrincipalSecret(
90+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7291
servicePrincipalId!,
92+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7393
servicePrincipalPassword!,
94+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7495
tenantId!
7596
);
7697
};
77-
78-
const verifyConfigDefined = (
79-
servicePrincipalId?: string,
80-
servicePrincipalPassword?: string,
81-
tenantId?: string
82-
): boolean => {
83-
if (servicePrincipalId && servicePrincipalPassword && tenantId) {
84-
return true;
85-
}
86-
logger.error(
87-
`Configuration is missing required fields tenant_id, service_principal_id and service_principal_secret. Please run the init command`
88-
);
89-
return false;
90-
};

src/lib/azure/containerRegistryService.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
21
import { ContainerRegistryManagementClient } from "@azure/arm-containerregistry";
32
import { loginWithServicePrincipalSecret } from "@azure/ms-rest-nodeauth";
43
import { logger } from "../../logger";
@@ -64,10 +63,12 @@ export const getContainerRegistries = async (
6463
const registries = await client.registries.list();
6564
logger.info("Successfully acquired Azure container registries");
6665
return registries.map((r) => {
66+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
6767
const id = r.id! as string;
6868
const match = id.match(/\/resourceGroups\/(.+?)\//);
6969
return {
7070
id,
71+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
7172
name: r.name!,
7273
resourceGroup: match ? match[1] : "",
7374
};

src/lib/azure/keyvault.test.ts

+14-33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */
2-
import {
3-
GetSecretOptions,
4-
KeyVaultSecret,
5-
SecretClient,
6-
SetSecretOptions,
7-
} from "@azure/keyvault-secrets";
1+
import { KeyVaultSecret } from "@azure/keyvault-secrets";
82
import uuid from "uuid/v4";
93
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
104
import { getSecret, setSecret } from "./keyvault";
@@ -16,10 +10,7 @@ const secretValue = uuid();
1610

1711
jest.spyOn(keyvault, "getClient").mockReturnValue(
1812
Promise.resolve({
19-
getSecret: async (
20-
secretName: string,
21-
options?: GetSecretOptions
22-
): Promise<KeyVaultSecret> => {
13+
getSecret: async (): Promise<KeyVaultSecret> => {
2314
return {
2415
name: "test",
2516
properties: {
@@ -29,11 +20,7 @@ jest.spyOn(keyvault, "getClient").mockReturnValue(
2920
value: "secretValue",
3021
};
3122
},
32-
setSecret: async (
33-
secretName: string,
34-
value: string,
35-
options?: SetSecretOptions
36-
): Promise<KeyVaultSecret> => {
23+
setSecret: async (): Promise<KeyVaultSecret> => {
3724
return {
3825
name: "test",
3926
properties: {
@@ -42,7 +29,8 @@ jest.spyOn(keyvault, "getClient").mockReturnValue(
4229
},
4330
};
4431
},
45-
} as SecretClient)
32+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
33+
} as any)
4634
);
4735

4836
beforeAll(() => {
@@ -67,14 +55,11 @@ describe("set secret", () => {
6755
test("negative test", async () => {
6856
jest.spyOn(keyvault, "getClient").mockReturnValueOnce(
6957
Promise.resolve({
70-
setSecret: (
71-
secretName: string,
72-
value: string,
73-
options?: SetSecretOptions
74-
): Promise<KeyVaultSecret> => {
58+
setSecret: (): Promise<KeyVaultSecret> => {
7559
throw new Error("fake error");
7660
},
77-
} as SecretClient)
61+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
62+
} as any)
7863
);
7964
try {
8065
await setSecret(keyVaultName, mockedName, secretValue);
@@ -100,16 +85,14 @@ describe("get secret", () => {
10085
it("negative test: secret not found", async () => {
10186
jest.spyOn(keyvault, "getClient").mockReturnValueOnce(
10287
Promise.resolve({
103-
getSecret: (
104-
secretName: string,
105-
options?: GetSecretOptions
106-
): Promise<KeyVaultSecret> => {
88+
getSecret: (): Promise<KeyVaultSecret> => {
10789
throw {
10890
code: "SecretNotFound",
10991
statusCode: 404,
11092
};
11193
},
112-
} as SecretClient)
94+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
95+
} as any)
11396
);
11497
try {
11598
const val = await getSecret(keyVaultName, mockedName);
@@ -121,16 +104,14 @@ describe("get secret", () => {
121104
it("negative test: other errors", async () => {
122105
jest.spyOn(keyvault, "getClient").mockReturnValueOnce(
123106
Promise.resolve({
124-
getSecret: (
125-
secretName: string,
126-
options?: GetSecretOptions
127-
): Promise<KeyVaultSecret> => {
107+
getSecret: (): Promise<KeyVaultSecret> => {
128108
throw {
129109
code: "something else",
130110
statusCode: 400,
131111
};
132112
},
133-
} as SecretClient)
113+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
114+
} as any)
134115
);
135116
try {
136117
await getSecret(keyVaultName, mockedName);

src/lib/azure/keyvault.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
21
import { SecretClient } from "@azure/keyvault-secrets";
32
import { logger } from "../../logger";
43
import { AzureAccessOpts } from "../../types";
@@ -30,6 +29,7 @@ export const getClient = async (
3029
): Promise<SecretClient> => {
3130
const url = `https://${keyVaultName}.vault.azure.net`;
3231
const credentials = await getCredentials(opts);
32+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
3333
return new SecretClient(url, credentials!);
3434
};
3535

src/lib/git/azure.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2-
31
import { WebApi } from "azure-devops-node-api";
42
import uuid from "uuid/v4";
53
import { Config } from "../../config";
@@ -145,7 +143,9 @@ describe("createPullRequest", () => {
145143
expect(true).toBe(false);
146144
} catch (err) {
147145
expect(err).toBeDefined();
148-
expect(err!.message).toMatch(/0 repositories found with remote url/);
146+
if (err) {
147+
expect(err.message).toMatch(/0 repositories found with remote url/);
148+
}
149149
}
150150

151151
gitApi.getBranches = originalBranches;
@@ -163,7 +163,7 @@ describe("createPullRequest", () => {
163163
});
164164
it("negative test", async () => {
165165
gitApi.createPullRequest = async (): Promise<unknown> => {
166-
throw new Error("fake");
166+
throw Error("fake");
167167
};
168168

169169
await expect(
@@ -175,7 +175,7 @@ describe("createPullRequest", () => {
175175
});
176176
it("negative test: TF401179 error", async () => {
177177
gitApi.createPullRequest = async (): Promise<unknown> => {
178-
throw new Error("TF401179");
178+
throw Error("TF401179");
179179
};
180180

181181
await expect(

src/lib/git/azure.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-non-null-assertion */
21
import * as azdo from "azure-devops-node-api";
32
import { IGitApi } from "azure-devops-node-api/GitApi";
43
import AZGitInterfaces, {
@@ -87,7 +86,7 @@ export const generatePRUrl = async (
8786
) {
8887
const gitAPI = await GitAPI();
8988
const parentRepo = await gitAPI.getRepository(pr.repository.id);
90-
return `${parentRepo.webUrl!}/pullrequest/${pr.pullRequestId}`;
89+
return `${parentRepo.webUrl}/pullrequest/${pr.pullRequestId}`;
9190
}
9291
throw Error(
9392
`Failed to generate PR URL; PR did not contain a valid repository ID`
@@ -170,10 +169,11 @@ export const getMatchingBranch = async (
170169
await Promise.all(
171170
reposWithMatchingOrigin.map(async (repo) => {
172171
logger.info(`Retrieving branches for repository '${repo.name}'`);
172+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
173173
const branches = await gitAPI.getBranches(repo.id!);
174174
return {
175175
branches: branches.filter((branch) => {
176-
return [sourceRef, targetRef].includes(branch.name!);
176+
return branch.name && [sourceRef, targetRef].includes(branch.name);
177177
}),
178178
repo,
179179
};
@@ -219,6 +219,7 @@ const handleErrorForCreatePullRequest = async (
219219
targetRefName: `refs/heads/${targetRef}`,
220220
};
221221
const existingPRs = await gitAPI.getPullRequests(
222+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
222223
repoToPRAgainst.id!,
223224
searchCriteria
224225
);
@@ -275,12 +276,13 @@ export const createPullRequest = async (
275276
);
276277

277278
logger.info(
278-
`Creating pull request in repository '${repoToPRAgainst.name!}' to merge branch '${sourceRef}' into '${targetRef}'`
279+
`Creating pull request in repository '${repoToPRAgainst.name}' to merge branch '${sourceRef}' into '${targetRef}'`
279280
);
280281

281282
try {
282283
const createdPR = await gitAPI.createPullRequest(
283284
prConfig,
285+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
284286
repoToPRAgainst.id!
285287
);
286288
logger.info(

src/lib/gitutils.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-use-before-define */
21
import GitUrlParse from "git-url-parse";
32
import path from "path";
43
import url from "url";
@@ -114,21 +113,6 @@ export const pushBranch = async (branchName: string): Promise<void> => {
114113
}
115114
};
116115

117-
/**
118-
* Tries to fetch the Git origin from an AzDo set environment variable, else falls back to fetching it from Git directly.
119-
* @param absRepoPath The Absolute Path to the Repository to fetch the origin url.
120-
*/
121-
export const tryGetGitOrigin = async (
122-
absRepoPath?: string
123-
): Promise<string> => {
124-
return getAzdoOriginUrl().catch(() => {
125-
logger.warn(
126-
"Could not get Git Origin for Azure DevOps - are you running 'spk' _not_ in a pipeline?"
127-
);
128-
return getOriginUrl(absRepoPath);
129-
});
130-
};
131-
132116
/**
133117
* Gets the origin url.
134118
* @param absRepositoryPath The Absolute Path to the Repository to fetch the origin
@@ -166,6 +150,21 @@ export const getAzdoOriginUrl = async (): Promise<string> => {
166150
}
167151
};
168152

153+
/**
154+
* Tries to fetch the Git origin from an AzDo set environment variable, else falls back to fetching it from Git directly.
155+
* @param absRepoPath The Absolute Path to the Repository to fetch the origin url.
156+
*/
157+
export const tryGetGitOrigin = async (
158+
absRepoPath?: string
159+
): Promise<string> => {
160+
return getAzdoOriginUrl().catch(() => {
161+
logger.warn(
162+
"Could not get Git Origin for Azure DevOps - are you running 'spk' _not_ in a pipeline?"
163+
);
164+
return getOriginUrl(absRepoPath);
165+
});
166+
};
167+
169168
/**
170169
* Will return the name of the repository
171170
* Currently only AzDo repos are supported

0 commit comments

Comments
 (0)