This repository was archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathazdoClient.ts
More file actions
149 lines (133 loc) · 3.83 KB
/
azdoClient.ts
File metadata and controls
149 lines (133 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// imports
import { getPersonalAccessTokenHandler, WebApi } from "azure-devops-node-api";
import { IBuildApi } from "azure-devops-node-api/BuildApi";
import { ITaskAgentApi } from "azure-devops-node-api/TaskAgentApi";
import { RestClient } from "typed-rest-client";
import { Config } from "../config";
import { logger } from "../logger";
import { AzureDevOpsOpts } from "./git";
import { build as buildError } from "./errorBuilder";
import { errorStatusCode } from "./errorStatusCode";
// Module state Variables
let connection: WebApi | undefined;
let restApi: RestClient | undefined;
let buildApi: IBuildApi | undefined;
let taskAgentApi: ITaskAgentApi | undefined;
/**
* Return a well-formed AzDo organization URL.
* @param orgName Azure DevOps organization name.
* @returns fully qualified AzDo Url for the organization
*/
export const azdoUrl = (orgName: string): string =>
`https://dev.azure.com/${orgName}`;
/**
* Creates AzDo `azure-devops-node-api.WebApi` with `org` and `token`
* @param opts optionally override spk config Azure DevOps access options
* @returns AzDo `WebApi` object
*/
export const getWebApi = async (
opts: AzureDevOpsOpts = {}
): Promise<WebApi> => {
if (connection) {
return connection;
}
// Load config from opts and fallback to spk config
const config = Config();
const {
personalAccessToken = config.azure_devops?.access_token,
orgName = config.azure_devops?.org,
} = opts;
// PAT and devops URL are required
if (!personalAccessToken) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"azure-client-get-web-api-err-missing-access-token"
);
}
if (!orgName) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"azure-client-get-web-api-err-missing-org"
);
}
const orgUrl = azdoUrl(orgName);
logger.debug(
`getWebApi with org url: ${orgUrl} and token: ${personalAccessToken}`
);
const authHandler = getPersonalAccessTokenHandler(personalAccessToken);
connection = new WebApi(orgUrl, authHandler);
return connection;
};
export const invalidateWebApi = (): void => {
connection = undefined;
};
/**
* Creates AzDo `azure-devops-node-api.WebApi.RestClient` with `org` and `token and returns `RestClient`
* @param opts optionally override spk config Azure DevOps access options
* @returns AzDo `RestClient` object
*/
export const getRestClient = async (
opts: AzureDevOpsOpts = {}
): Promise<RestClient> => {
if (restApi) {
return restApi;
}
try {
const webApi = await getWebApi(opts);
restApi = webApi.rest;
return restApi;
} catch (err) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"azure-client-get-rest-client-err",
err
);
}
};
/**
* Creates AzDo `azure-devops-node-api.WebApi.IBuildApi` with `org` and `token and returns `RestClient`
* @param opts optionally override spk config Azure DevOps access options
* @returns AzDo `IBuildApi` object
*/
export const getBuildApi = async (
opts: AzureDevOpsOpts = {}
): Promise<IBuildApi> => {
if (buildApi) {
return buildApi;
}
try {
const webApi = await getWebApi(opts);
buildApi = await webApi.getBuildApi();
return buildApi;
} catch (err) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"azure-client-get-build-client-err",
err
);
}
};
/**
* Get Azure DevOps Task API client
*
* @param opts for organization name and personal access token value
* @returns AzDo `IBuildApi` object
*/
export const getTaskAgentApi = async (
opts: AzureDevOpsOpts = {}
): Promise<ITaskAgentApi> => {
if (taskAgentApi) {
return taskAgentApi;
}
try {
const webApi = await getWebApi(opts);
taskAgentApi = await webApi.getTaskAgentApi();
return taskAgentApi;
} catch (err) {
throw buildError(
errorStatusCode.AZURE_CLIENT,
"azure-client-get-task-agent-client-err",
err
);
}
};