|
1 |
| -var fs = require('fs'); |
2 |
| -#if NODE20 |
3 |
| -import * as extract from 'extract-zip' |
4 |
| -#else |
5 |
| -var DecompressZip = require('decompress-zip'); |
6 |
| -#endif |
7 | 1 | var path = require('path')
|
8 | 2 |
|
9 |
| -import * as corem from 'azure-devops-node-api/CoreApi'; |
10 | 3 | import * as tl from 'azure-pipelines-task-lib/task';
|
11 |
| -import * as vsom from 'azure-devops-node-api/VsoClient'; |
12 |
| -import { getProjectAndFeedIdFromInputParam } from "azure-pipelines-tasks-packaging-common/util" |
13 |
| -import stream = require('stream'); |
14 |
| -import { getConnection } from './connections'; |
15 |
| -import { WebApi } from 'azure-devops-node-api'; |
16 | 4 |
|
17 | 5 | tl.setResourcePath(path.join(__dirname, 'task.json'));
|
18 | 6 |
|
19 | 7 | async function main(): Promise<void> {
|
20 |
| - tl.warning(tl.loc("DeprecatedTask")); |
21 |
| - var feed = getProjectAndFeedIdFromInputParam("feed"); |
22 |
| - if(feed.projectId) { |
23 |
| - throw new Error(tl.loc("UnsupportedProjectScopedFeeds")); |
24 |
| - } |
25 |
| - let feedId = feed.feedId; |
26 |
| - let regexGuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; |
27 |
| - let packageId = tl.getInput("definition"); |
28 |
| - |
29 |
| - if(!regexGuid.test(packageId)){ |
30 |
| - packageId = "Nuget_" + tl.getInput("definition"); |
31 |
| - } |
32 |
| - |
33 |
| - let version = tl.getInput("version"); |
34 |
| - let downloadPath = tl.getInput("downloadPath"); |
35 |
| - let collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri"); |
36 |
| - |
37 |
| - var accessToken = getAuthToken(); |
38 |
| - |
39 |
| - const feedConnection = await getConnection("7AB4E64E-C4D8-4F50-AE73-5EF2E21642A5", collectionUrl, accessToken); |
40 |
| - const pkgsConnection = await getConnection("B3BE7473-68EA-4A81-BFC7-9530BAAA19AD", collectionUrl, accessToken); |
41 |
| - |
42 |
| - const retryLimitValue: string = tl.getVariable("VSTS_HTTP_RETRY"); |
43 |
| - const retryLimit: number = (!!retryLimitValue && !isNaN(parseInt(retryLimitValue))) ? parseInt(retryLimitValue) : 4; |
44 |
| - tl.debug(`RetryLimit set to ${retryLimit}`); |
45 |
| - |
46 |
| - await executeWithRetries("downloadPackage", () => downloadPackage(feedConnection, pkgsConnection, feedId, packageId, version, downloadPath).catch((reason) => { |
47 |
| - throw reason; |
48 |
| - }), retryLimit); |
49 |
| - |
50 |
| - let shouldFail = tl.getVariable('FAIL_DEPRECATED_TASK'); |
51 |
| - |
52 |
| - if (shouldFail != null && shouldFail.toLowerCase() === 'true') { |
53 |
| - throw new Error(tl.loc("DeprecatedTask")); |
54 |
| - } |
55 |
| -} |
56 |
| - |
57 |
| -function getAuthToken() { |
58 |
| - var auth = tl.getEndpointAuthorization('SYSTEMVSSCONNECTION', false); |
59 |
| - if (auth.scheme.toLowerCase() === 'oauth') { |
60 |
| - return auth.parameters['AccessToken']; |
61 |
| - } |
62 |
| - else { |
63 |
| - throw new Error(tl.loc("CredentialsNotFound")) |
64 |
| - } |
65 |
| -} |
66 |
| - |
67 |
| -export async function downloadPackage(feedConnection: WebApi, pkgsConnection: WebApi, feedId: string, packageId: string, version: string, downloadPath: string) { |
68 |
| - var feedsClient = feedConnection.vsoClient; |
69 |
| - var packagesClient = pkgsConnection.vsoClient; |
70 |
| - |
71 |
| - var packageUrl = await getNuGetPackageUrl(feedsClient, feedId, packageId); |
72 |
| - |
73 |
| -#if NODE20 |
74 |
| - await new Promise<void>((resolve, reject) => { |
75 |
| -#else |
76 |
| - await new Promise((resolve, reject) => { |
77 |
| -#endif |
78 |
| - feedsClient.restClient.client.get(packageUrl).then(async response => { |
79 |
| - if (response.message.statusCode != 200) { |
80 |
| - return reject(tl.loc("FailedToGetPackageMetadata", response.message.statusMessage)); |
81 |
| - } |
82 |
| - |
83 |
| - var result = JSON.parse(await response.readBody()); |
84 |
| - var packageType = result.protocolType.toLowerCase(); |
85 |
| - var packageName = result.name; |
86 |
| - |
87 |
| - if (packageType == "nuget") { |
88 |
| - |
89 |
| - var getDownloadUrlPromise = getDownloadUrl(packagesClient, feedId, packageName, version) |
90 |
| - getDownloadUrlPromise.catch((error) => { |
91 |
| - return reject(error) |
92 |
| - }); |
93 |
| - var downloadUrl = await getDownloadUrlPromise; |
94 |
| - |
95 |
| - if (!tl.exist(downloadPath)) { |
96 |
| - tl.mkdirP(downloadPath); |
97 |
| - } |
98 |
| - |
99 |
| - var zipLocation = path.resolve(downloadPath, "../", packageName) + ".zip"; |
100 |
| - var unzipLocation = path.join(downloadPath, ""); |
101 |
| - |
102 |
| - console.log(tl.loc("StartingDownloadOfPackage", packageName, zipLocation)); |
103 |
| - var packagesCoreApi = await pkgsConnection.getCoreApi(); |
104 |
| - var downloadNugetPackagePromise = downloadNugetPackage(packagesCoreApi, downloadUrl, zipLocation); |
105 |
| - downloadNugetPackagePromise.catch((error) => { |
106 |
| - return reject(error) |
107 |
| - }); |
108 |
| - await downloadNugetPackagePromise; |
109 |
| - |
110 |
| - console.log(tl.loc("ExtractingNugetPackage", packageName, unzipLocation)); |
111 |
| - |
112 |
| - var unzipPromise = unzip(zipLocation, unzipLocation); |
113 |
| - unzipPromise.catch((error) => { |
114 |
| - return reject(error) |
115 |
| - }); |
116 |
| - await unzipPromise; |
117 |
| - |
118 |
| - if (tl.exist(zipLocation)) { |
119 |
| - try { |
120 |
| - tl.rmRF(zipLocation); |
121 |
| - } catch (error) { |
122 |
| - tl.warning(tl.loc("OperationFailed", "rmRF", error)); |
123 |
| - } |
124 |
| - } |
125 |
| - return resolve(); |
126 |
| - } |
127 |
| - else { |
128 |
| - return reject(tl.loc("PackageTypeNotSupported")); |
129 |
| - } |
130 |
| - }) |
131 |
| - .catch(error => { |
132 |
| - return reject(tl.loc("FailedToGetPackageMetadata", error)); |
133 |
| - }); |
134 |
| - }); |
135 |
| -} |
136 |
| - |
137 |
| -export async function getNuGetPackageUrl(vsoClient: vsom.VsoClient, feedId: string, packageId: string): Promise<string> { |
138 |
| - var PackagingAreaName = "Packaging"; |
139 |
| - var PackageAreaId = "7A20D846-C929-4ACC-9EA2-0D5A7DF1B197"; |
140 |
| - |
141 |
| - return new Promise<string>((resolve, reject) => { |
142 |
| - var getVersioningDataPromise = vsoClient.getVersioningData(null, PackagingAreaName, PackageAreaId, { feedId: feedId, packageId: packageId }); |
143 |
| - getVersioningDataPromise.then((result) => { |
144 |
| - return resolve(result.requestUrl); |
145 |
| - }); |
146 |
| - getVersioningDataPromise.catch((error) => { |
147 |
| - return reject(error) |
148 |
| - }); |
149 |
| - }); |
150 |
| -} |
151 |
| - |
152 |
| -export async function getDownloadUrl(vsoClient: vsom.VsoClient, feedId: string, packageName: string, version: string): Promise<string> { |
153 |
| - var NugetArea = "NuGet" |
154 |
| - var PackageVersionContentResourceId = "6EA81B8C-7386-490B-A71F-6CF23C80B388" |
155 |
| - return new Promise<string>((resolve, reject) => { |
156 |
| - var getVersioningDataPromise = vsoClient.getVersioningData(null, NugetArea, PackageVersionContentResourceId, { feedId: feedId, packageName: packageName, packageVersion: version }); |
157 |
| - getVersioningDataPromise.then((result) => { |
158 |
| - return resolve(result.requestUrl); |
159 |
| - }); |
160 |
| - getVersioningDataPromise.catch((error) => { |
161 |
| - return reject(error) |
162 |
| - }); |
163 |
| - }); |
164 |
| -} |
165 |
| - |
166 |
| -export async function downloadNugetPackage(coreApi: corem.ICoreApi, downloadUrl: string, downloadPath: string): Promise<void> { |
167 |
| - await new Promise<void>((resolve, reject) => { |
168 |
| - coreApi.http.get(downloadUrl).then(response => { |
169 |
| - tl.debug("Downloading package from url: " + downloadUrl); |
170 |
| - tl.debug("Download status: " + response.message.statusCode); |
171 |
| - |
172 |
| - if(response.message.statusCode != 200) { |
173 |
| - return reject(tl.loc("FailedToDownloadNugetPackage", downloadUrl, response.message.statusMessage)); |
174 |
| - } |
175 |
| - |
176 |
| - var responseStream = response.message as stream.Readable; |
177 |
| - var file = fs.createWriteStream(downloadPath); |
178 |
| - responseStream.pipe(file); |
179 |
| - responseStream.on("end", () => { |
180 |
| - console.log(tl.loc("PackageDownloadSuccessful")); |
181 |
| - file.on("close", () => { |
182 |
| - return resolve(); |
183 |
| - }); |
184 |
| - }); |
185 |
| - responseStream.on("error", err => { |
186 |
| - file.close(); |
187 |
| - return reject(tl.loc("FailedToDownloadNugetPackage", downloadUrl, err)); |
188 |
| - }); |
189 |
| - |
190 |
| - }).catch(error => { |
191 |
| - return reject(tl.loc("FailedToDownloadNugetPackage", downloadUrl, error)); |
192 |
| - }); |
193 |
| - }); |
194 |
| -} |
195 |
| - |
196 |
| -function executeWithRetries(operationName: string, operation: () => Promise<any>, retryCount): Promise<any> { |
197 |
| - var executePromise = new Promise((resolve, reject) => { |
198 |
| - executeWithRetriesImplementation(operationName, operation, retryCount, resolve, reject); |
199 |
| - }); |
200 |
| - |
201 |
| - return executePromise; |
202 |
| -} |
203 |
| - |
204 |
| -function executeWithRetriesImplementation(operationName: string, operation: () => Promise<any>, currentRetryCount, resolve, reject) { |
205 |
| - operation().then((result) => { |
206 |
| - resolve(result); |
207 |
| - }).catch((error) => { |
208 |
| - if (currentRetryCount <= 0) { |
209 |
| - tl.error(tl.loc("OperationFailed", operationName, error)); |
210 |
| - reject(error); |
211 |
| - } |
212 |
| - else { |
213 |
| - console.log(tl.loc('RetryingOperation', operationName, currentRetryCount)); |
214 |
| - currentRetryCount = currentRetryCount - 1; |
215 |
| - setTimeout(() => executeWithRetriesImplementation(operationName, operation, currentRetryCount, resolve, reject), 4 * 1000); |
216 |
| - } |
217 |
| - }); |
218 |
| -} |
219 |
| - |
220 |
| -export async function unzip(zipLocation: string, unzipLocation: string): Promise<void> { |
221 |
| - |
222 |
| - await new Promise<void>(function (resolve, reject) { |
223 |
| - if (tl.exist(unzipLocation)) { |
224 |
| - tl.rmRF(unzipLocation); |
225 |
| - } |
226 |
| - |
227 |
| - tl.debug('Extracting ' + zipLocation + ' to ' + unzipLocation); |
228 |
| -#if NODE20 |
229 |
| - tl.debug(`Using extract-zip package for extracting archive`); |
230 |
| - extract(zipLocation, { dir: unzipLocation }).then(() => { |
231 |
| - resolve(); |
232 |
| - }).catch((error) => { |
233 |
| - reject(error); |
234 |
| - }); |
235 |
| -#else |
236 |
| - var unzipper = new DecompressZip(zipLocation); |
237 |
| - unzipper.on('error', err => { |
238 |
| - return reject(tl.loc("ExtractionFailed", err)) |
239 |
| - }); |
240 |
| - unzipper.on('extract', () => { |
241 |
| - tl.debug('Extracted ' + zipLocation + ' to ' + unzipLocation + ' successfully'); |
242 |
| - return resolve(); |
243 |
| - }); |
244 |
| - |
245 |
| - unzipper.extract({ |
246 |
| - path: path.normalize(unzipLocation) |
247 |
| - }); |
248 |
| -#endif |
249 |
| - }); |
| 8 | + tl.setResult(tl.TaskResult.Failed, tl.loc("DeprecatedTask")); |
250 | 9 | }
|
251 | 10 |
|
252 | 11 | main()
|
253 |
| - .then(() => tl.setResult(tl.TaskResult.SucceededWithIssues, tl.loc("DeprecatedTask"))) |
254 |
| - .catch((error) => tl.setResult(tl.TaskResult.Failed, error)); |
0 commit comments