9.21 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12exports.TaskAgentApi = void 0;
13const taskagentbasem = require("./TaskAgentApiBase");
14const url = require("url");
15class TaskAgentApi extends taskagentbasem.TaskAgentApiBase {
16 constructor(baseUrl, handlers, options) {
17 super(baseUrl, handlers, options);
18 // hang on to the handlers in case we need to fall back to an account-level client
19 this._handlers = handlers;
20 this._options = options;
21 }
22 /**
23 * @param {string} taskId
24 * @param onResult callback function
25 */
26 deleteTaskDefinition(taskId) {
27 let promise = this.vsoClient.beginGetLocation("distributedtask", "60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd")
28 .then((location) => {
29 if (location) {
30 // the resource exists at the url we were given. go!
31 return super.deleteTaskDefinition(taskId);
32 }
33 else {
34 // this is the case when the server doesn't support collection-level task definitions
35 var fallbackClient = this._getFallbackClient(this.baseUrl);
36 if (!fallbackClient) {
37 // couldn't convert
38 throw new Error("Failed to find api location for area: distributedtask id: 60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd");
39 }
40 else {
41 // use the fallback client
42 return fallbackClient.deleteTaskDefinition(taskId);
43 }
44 }
45 });
46 return promise;
47 }
48 /**
49 * @param {string} taskId
50 * @param {string} versionString
51 * @param {string[]} visibility
52 * @param {boolean} scopeLocal
53 * @param onResult callback function with the resulting ArrayBuffer
54 */
55 getTaskContentZip(taskId, versionString, visibility, scopeLocal) {
56 let promise = this.vsoClient.beginGetLocation("distributedtask", "60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd")
57 .then((location) => {
58 if (location) {
59 // the resource exists at the url we were given. go!
60 return super.getTaskContentZip(taskId, versionString, visibility, scopeLocal);
61 }
62 else {
63 // this is the case when the server doesn't support collection-level task definitions
64 var fallbackClient = this._getFallbackClient(this.baseUrl);
65 if (!fallbackClient) {
66 // couldn't convert
67 throw new Error("Failed to find api location for area: distributedtask id: 60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd");
68 }
69 else {
70 // use the fallback client
71 return fallbackClient.getTaskContentZip(taskId, versionString, visibility, scopeLocal);
72 }
73 }
74 });
75 return promise;
76 }
77 /**
78 * @param {string} taskId
79 * @param {string} versionString
80 * @param {string[]} visibility
81 * @param {boolean} scopeLocal
82 * @param onResult callback function with the resulting TaskAgentInterfaces.TaskDefinition
83 */
84 getTaskDefinition(taskId, versionString, visibility, scopeLocal) {
85 let promise = this.vsoClient.beginGetLocation("distributedtask", "60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd")
86 .then((location) => {
87 if (location) {
88 // the resource exists at the url we were given. go!
89 return super.getTaskDefinition(taskId, versionString, visibility, scopeLocal);
90 }
91 else {
92 // this is the case when the server doesn't support collection-level task definitions
93 var fallbackClient = this._getFallbackClient(this.baseUrl);
94 if (!fallbackClient) {
95 // couldn't convert
96 throw new Error("Failed to find api location for area: distributedtask id: 60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd");
97 }
98 else {
99 // use the fallback client
100 return fallbackClient.getTaskDefinition(taskId, versionString, visibility, scopeLocal);
101 }
102 }
103 });
104 return promise;
105 }
106 /**
107 * @param {string} taskId
108 * @param {string[]} visibility
109 * @param {boolean} scopeLocal
110 * @param onResult callback function with the resulting TaskAgentInterfaces.TaskDefinition[]
111 */
112 getTaskDefinitions(taskId, visibility, scopeLocal) {
113 let promise = this.vsoClient.beginGetLocation("distributedtask", "60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd")
114 .then((location) => {
115 if (location) {
116 // the resource exists at the url we were given. go!
117 return super.getTaskDefinitions(taskId, visibility, scopeLocal);
118 }
119 else {
120 // this is the case when the server doesn't support collection-level task definitions
121 var fallbackClient = this._getFallbackClient(this.baseUrl);
122 if (!fallbackClient) {
123 // couldn't convert
124 throw new Error("Failed to find api location for area: distributedtask id: 60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd");
125 }
126 else {
127 // use the fallback client
128 return fallbackClient.getTaskDefinitions(taskId, visibility, scopeLocal);
129 }
130 }
131 });
132 return promise;
133 }
134 /**
135 * @param {NodeJS.ReadableStream} contentStream
136 * @param {string} taskId
137 * @param {boolean} overwrite
138 * @param onResult callback function
139 */
140 uploadTaskDefinition(customHeaders, contentStream, taskId, overwrite) {
141 return __awaiter(this, void 0, void 0, function* () {
142 let routeValues = {
143 taskId: taskId
144 };
145 let queryValues = {
146 overwrite: overwrite,
147 };
148 return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
149 customHeaders = customHeaders || {};
150 customHeaders["Content-Type"] = "application/octet-stream";
151 try {
152 let verData = yield this.vsoClient.getVersioningData("3.0-preview.1", "distributedtask", "60aac929-f0cd-4bc8-9ce4-6b30e8f1b1bd", routeValues, queryValues);
153 let url = verData.requestUrl;
154 let options = this.createRequestOptions('application/json', verData.apiVersion);
155 options.additionalHeaders = customHeaders;
156 let res;
157 res = yield this.rest.uploadStream("PUT", url, contentStream, options);
158 resolve(res.result);
159 }
160 catch (err) {
161 reject(err);
162 }
163 }));
164 });
165 }
166 _getFallbackClient(baseUrl) {
167 if (!this._fallbackClient) {
168 var accountUrl = this._getAccountUrl(baseUrl);
169 if (accountUrl) {
170 this._fallbackClient = new TaskAgentApi(accountUrl, this._handlers, this._options);
171 }
172 }
173 return this._fallbackClient;
174 }
175 _getAccountUrl(collectionUrl) {
176 // converts a collection URL to an account URL
177 // returns null if the conversion can't be made
178 var purl = url.parse(collectionUrl);
179 if (!purl.protocol || !purl.host) {
180 return null;
181 }
182 var accountUrl = purl.protocol + '//' + purl.host;
183 // purl.path is something like /DefaultCollection or /tfs/DefaultCollection or /DefaultCollection/
184 var splitPath = purl.path.split('/').slice(1);
185 if (splitPath.length === 0 || (splitPath.length === 1 && splitPath[0] === '')) {
186 return null;
187 }
188 // if the first segment of the path is tfs, the second is the collection. if the url ends in / there will be a third, empty entry
189 if (splitPath[0] === 'tfs' && (splitPath.length === 2 || (splitPath.length === 3 && splitPath[2].length === 0))) {
190 //on prem
191 accountUrl += '/' + 'tfs';
192 }
193 else if (splitPath.length === 2 && splitPath[0] === '') {
194 // /DefaultCollection/
195 return accountUrl;
196 }
197 else if (splitPath.length > 1) {
198 return null;
199 }
200 return accountUrl;
201 }
202}
203exports.TaskAgentApi = TaskAgentApi;