Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/commands/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as config from "../config";
import * as azdoClient from "../lib/azdoClient";
import * as azureContainerRegistryService from "../lib/azure/containerRegistryService";
import * as resourceService from "../lib/azure/resourceService";
import { getErrorMessage as errorMessage } from "../lib/errorBuilder";
import { createTempDir } from "../lib/ioUtil";
import { RequestContext, WORKSPACE } from "../lib/setup/constants";
import * as fsUtil from "../lib/setup/fsUtil";
Expand All @@ -20,6 +21,7 @@ import {
createAppRepoTasks,
createSPKConfig,
execute,
getAPIClients,
getErrorMessage,
} from "./setup";
import * as setup from "./setup";
Expand Down Expand Up @@ -390,3 +392,40 @@ describe("test createAppRepoTasks function", () => {
await testCreateAppRepoTasks(false);
});
});

describe("test getAPIClients function", () => {
it("negative test: getGitAPI failed", async () => {
jest.spyOn(azdoClient, "getWebApi").mockResolvedValueOnce({
getCoreApi: async () => {
return {};
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);
jest
.spyOn(gitService, "getGitApi")
.mockRejectedValueOnce(new Error("fake"));

await expect(getAPIClients()).rejects.toThrow(
errorMessage("setup-cmd-git-api-err")
);
});
it("negative test: getGitAPI failed", async () => {
jest.spyOn(azdoClient, "getWebApi").mockResolvedValueOnce({
getCoreApi: async () => {
return {};
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
jest.spyOn(gitService, "getGitApi").mockResolvedValueOnce({} as any);

jest
.spyOn(azdoClient, "getBuildApi")
.mockRejectedValueOnce(new Error("fake"));

await expect(getAPIClients()).rejects.toThrow(
errorMessage("setup-cmd-build-api-err")
);
});
});
55 changes: 51 additions & 4 deletions src/commands/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IBuildApi } from "azure-devops-node-api/BuildApi";
import { ICoreApi } from "azure-devops-node-api/CoreApi";
import { IGitApi } from "azure-devops-node-api/GitApi";
import commander from "commander";
import fs from "fs";
Expand Down Expand Up @@ -53,6 +54,12 @@ interface APIError {
statusCode: number;
}

interface APIClients {
coreAPI: ICoreApi;
gitAPI: IGitApi;
buildAPI: IBuildApi;
}

/**
* Creates SPK config file under `user-home/.spk` folder
*
Expand Down Expand Up @@ -179,6 +186,49 @@ export const createAppRepoTasks = async (
}
};

export const getAPIClients = async (): Promise<APIClients> => {
const webAPI = await getWebApi();
let coreAPI: ICoreApi;
let gitAPI: IGitApi;
let buildAPI: IBuildApi;

try {
coreAPI = await webAPI.getCoreApi();
} catch (err) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"setup-cmd-core-api-err",
err
);
}

try {
gitAPI = await getGitApi(webAPI);
} catch (err) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"setup-cmd-git-api-err",
err
);
}

try {
buildAPI = await getBuildApi();
} catch (err) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"setup-cmd-build-api-err",
err
);
}

return {
coreAPI,
gitAPI,
buildAPI,
};
};

/**
* Executes the command, can all exit function with 0 or 1
* when command completed successfully or failed respectively.
Expand All @@ -198,10 +248,7 @@ export const execute = async (
createDirectory(WORKSPACE, true);
createSPKConfig(rc);

const webAPI = await getWebApi();
const coreAPI = await webAPI.getCoreApi();
const gitAPI = await getGitApi(webAPI);
const buildAPI = await getBuildApi();
const { coreAPI, gitAPI, buildAPI } = await getAPIClients();

await createProjectIfNotExist(coreAPI, rc);
await hldRepo(gitAPI, rc);
Expand Down
3 changes: 3 additions & 0 deletions src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
"setup-cmd-prompt-err-no-subscriptions": "No subscriptions found.",
"setup-cmd-prompt-err-subscription-missing": "Subscription Identifier was missing.",
"setup-cmd-prompt-err-input-file-missing": "{0} did not exist or not accessible. Make sure that it is accessible.",
"setup-cmd-core-api-err": "Could not get Core API client. Check the Azure credential.",
"setup-cmd-git-api-err": "Could not get Git API client. Check the Azure DevOps credential.",
"setup-cmd-build-api-err": "Could not get Build API client. Check the Azure DevOps credential.",

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