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

Commit 6b653ee

Browse files
[BUG FIX] add proper error message when PAT is incorrect. (#515)
* [BUG FIX] add proper error message when PAT is incorrect. * add unit tests Co-authored-by: Yvonne Radsmikham <[email protected]>
1 parent 6321208 commit 6b653ee

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

src/commands/setup.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as config from "../config";
44
import * as azdoClient from "../lib/azdoClient";
55
import * as azureContainerRegistryService from "../lib/azure/containerRegistryService";
66
import * as resourceService from "../lib/azure/resourceService";
7+
import { getErrorMessage as errorMessage } from "../lib/errorBuilder";
78
import { createTempDir } from "../lib/ioUtil";
89
import { RequestContext, WORKSPACE } from "../lib/setup/constants";
910
import * as fsUtil from "../lib/setup/fsUtil";
@@ -20,6 +21,7 @@ import {
2021
createAppRepoTasks,
2122
createSPKConfig,
2223
execute,
24+
getAPIClients,
2325
getErrorMessage,
2426
} from "./setup";
2527
import * as setup from "./setup";
@@ -390,3 +392,40 @@ describe("test createAppRepoTasks function", () => {
390392
await testCreateAppRepoTasks(false);
391393
});
392394
});
395+
396+
describe("test getAPIClients function", () => {
397+
it("negative test: getGitAPI failed", async () => {
398+
jest.spyOn(azdoClient, "getWebApi").mockResolvedValueOnce({
399+
getCoreApi: async () => {
400+
return {};
401+
},
402+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
403+
} as any);
404+
jest
405+
.spyOn(gitService, "getGitApi")
406+
.mockRejectedValueOnce(new Error("fake"));
407+
408+
await expect(getAPIClients()).rejects.toThrow(
409+
errorMessage("setup-cmd-git-api-err")
410+
);
411+
});
412+
it("negative test: getGitAPI failed", async () => {
413+
jest.spyOn(azdoClient, "getWebApi").mockResolvedValueOnce({
414+
getCoreApi: async () => {
415+
return {};
416+
},
417+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
418+
} as any);
419+
420+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
421+
jest.spyOn(gitService, "getGitApi").mockResolvedValueOnce({} as any);
422+
423+
jest
424+
.spyOn(azdoClient, "getBuildApi")
425+
.mockRejectedValueOnce(new Error("fake"));
426+
427+
await expect(getAPIClients()).rejects.toThrow(
428+
errorMessage("setup-cmd-build-api-err")
429+
);
430+
});
431+
});

src/commands/setup.ts

+51-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { IBuildApi } from "azure-devops-node-api/BuildApi";
2+
import { ICoreApi } from "azure-devops-node-api/CoreApi";
23
import { IGitApi } from "azure-devops-node-api/GitApi";
34
import commander from "commander";
45
import fs from "fs";
@@ -53,6 +54,12 @@ interface APIError {
5354
statusCode: number;
5455
}
5556

57+
interface APIClients {
58+
coreAPI: ICoreApi;
59+
gitAPI: IGitApi;
60+
buildAPI: IBuildApi;
61+
}
62+
5663
/**
5764
* Creates SPK config file under `user-home/.spk` folder
5865
*
@@ -179,6 +186,49 @@ export const createAppRepoTasks = async (
179186
}
180187
};
181188

189+
export const getAPIClients = async (): Promise<APIClients> => {
190+
const webAPI = await getWebApi();
191+
let coreAPI: ICoreApi;
192+
let gitAPI: IGitApi;
193+
let buildAPI: IBuildApi;
194+
195+
try {
196+
coreAPI = await webAPI.getCoreApi();
197+
} catch (err) {
198+
throw buildError(
199+
errorStatusCode.AZURE_CLIENT,
200+
"setup-cmd-core-api-err",
201+
err
202+
);
203+
}
204+
205+
try {
206+
gitAPI = await getGitApi(webAPI);
207+
} catch (err) {
208+
throw buildError(
209+
errorStatusCode.AZURE_CLIENT,
210+
"setup-cmd-git-api-err",
211+
err
212+
);
213+
}
214+
215+
try {
216+
buildAPI = await getBuildApi();
217+
} catch (err) {
218+
throw buildError(
219+
errorStatusCode.AZURE_CLIENT,
220+
"setup-cmd-build-api-err",
221+
err
222+
);
223+
}
224+
225+
return {
226+
coreAPI,
227+
gitAPI,
228+
buildAPI,
229+
};
230+
};
231+
182232
/**
183233
* Executes the command, can all exit function with 0 or 1
184234
* when command completed successfully or failed respectively.
@@ -198,10 +248,7 @@ export const execute = async (
198248
createDirectory(WORKSPACE, true);
199249
createSPKConfig(rc);
200250

201-
const webAPI = await getWebApi();
202-
const coreAPI = await webAPI.getCoreApi();
203-
const gitAPI = await getGitApi(webAPI);
204-
const buildAPI = await getBuildApi();
251+
const { coreAPI, gitAPI, buildAPI } = await getAPIClients();
205252

206253
await createProjectIfNotExist(coreAPI, rc);
207254
await hldRepo(gitAPI, rc);

src/lib/i18n.json

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
"setup-cmd-prompt-err-no-subscriptions": "No subscriptions found.",
2121
"setup-cmd-prompt-err-subscription-missing": "Subscription Identifier was missing.",
2222
"setup-cmd-prompt-err-input-file-missing": "{0} did not exist or not accessible. Make sure that it is accessible.",
23+
"setup-cmd-core-api-err": "Could not get Core API client. Check the Azure credential.",
24+
"setup-cmd-git-api-err": "Could not get Git API client. Check the Azure DevOps credential.",
25+
"setup-cmd-build-api-err": "Could not get Build API client. Check the Azure DevOps credential.",
2326

2427
"spk-config-yaml-err-readyaml": "Could not load file, {0}.",
2528
"spk-config-yaml-var-undefined": "Environment variable needs to be defined for {0} since it's referenced in the config file.",

0 commit comments

Comments
 (0)