Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed functionality from deprecated tasks DownloadPackageV0, NuGetInstallerV0, and NuGetRestoreV1 #19908

Merged

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

953 changes: 576 additions & 377 deletions Tasks/DownloadPackageV0/_buildConfigs/Node20/package-lock.json

Large diffs are not rendered by default.

28 changes: 0 additions & 28 deletions Tasks/DownloadPackageV0/connections.ts

This file was deleted.

245 changes: 1 addition & 244 deletions Tasks/DownloadPackageV0/download.ts
MicroTuld marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,254 +1,11 @@
var fs = require('fs');
#if NODE20
import * as extract from 'extract-zip'
#else
var DecompressZip = require('decompress-zip');
#endif
var path = require('path')

import * as corem from 'azure-devops-node-api/CoreApi';
import * as tl from 'azure-pipelines-task-lib/task';
import * as vsom from 'azure-devops-node-api/VsoClient';
import { getProjectAndFeedIdFromInputParam } from "azure-pipelines-tasks-packaging-common/util"
import stream = require('stream');
import { getConnection } from './connections';
import { WebApi } from 'azure-devops-node-api';

tl.setResourcePath(path.join(__dirname, 'task.json'));

async function main(): Promise<void> {
tl.warning(tl.loc("DeprecatedTask"));
var feed = getProjectAndFeedIdFromInputParam("feed");
if(feed.projectId) {
throw new Error(tl.loc("UnsupportedProjectScopedFeeds"));
}
let feedId = feed.feedId;
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;
let packageId = tl.getInput("definition");

if(!regexGuid.test(packageId)){
packageId = "Nuget_" + tl.getInput("definition");
}

let version = tl.getInput("version");
let downloadPath = tl.getInput("downloadPath");
let collectionUrl = tl.getVariable("System.TeamFoundationCollectionUri");

var accessToken = getAuthToken();

const feedConnection = await getConnection("7AB4E64E-C4D8-4F50-AE73-5EF2E21642A5", collectionUrl, accessToken);
const pkgsConnection = await getConnection("B3BE7473-68EA-4A81-BFC7-9530BAAA19AD", collectionUrl, accessToken);

const retryLimitValue: string = tl.getVariable("VSTS_HTTP_RETRY");
const retryLimit: number = (!!retryLimitValue && !isNaN(parseInt(retryLimitValue))) ? parseInt(retryLimitValue) : 4;
tl.debug(`RetryLimit set to ${retryLimit}`);

await executeWithRetries("downloadPackage", () => downloadPackage(feedConnection, pkgsConnection, feedId, packageId, version, downloadPath).catch((reason) => {
throw reason;
}), retryLimit);

let shouldFail = tl.getVariable('FAIL_DEPRECATED_TASK');

if (shouldFail != null && shouldFail.toLowerCase() === 'true') {
throw new Error(tl.loc("DeprecatedTask"));
}
}

function getAuthToken() {
var auth = tl.getEndpointAuthorization('SYSTEMVSSCONNECTION', false);
if (auth.scheme.toLowerCase() === 'oauth') {
return auth.parameters['AccessToken'];
}
else {
throw new Error(tl.loc("CredentialsNotFound"))
}
}

export async function downloadPackage(feedConnection: WebApi, pkgsConnection: WebApi, feedId: string, packageId: string, version: string, downloadPath: string) {
var feedsClient = feedConnection.vsoClient;
var packagesClient = pkgsConnection.vsoClient;

var packageUrl = await getNuGetPackageUrl(feedsClient, feedId, packageId);

#if NODE20
await new Promise<void>((resolve, reject) => {
#else
await new Promise((resolve, reject) => {
#endif
feedsClient.restClient.client.get(packageUrl).then(async response => {
if (response.message.statusCode != 200) {
return reject(tl.loc("FailedToGetPackageMetadata", response.message.statusMessage));
}

var result = JSON.parse(await response.readBody());
var packageType = result.protocolType.toLowerCase();
var packageName = result.name;

if (packageType == "nuget") {

var getDownloadUrlPromise = getDownloadUrl(packagesClient, feedId, packageName, version)
getDownloadUrlPromise.catch((error) => {
return reject(error)
});
var downloadUrl = await getDownloadUrlPromise;

if (!tl.exist(downloadPath)) {
tl.mkdirP(downloadPath);
}

var zipLocation = path.resolve(downloadPath, "../", packageName) + ".zip";
var unzipLocation = path.join(downloadPath, "");

console.log(tl.loc("StartingDownloadOfPackage", packageName, zipLocation));
var packagesCoreApi = await pkgsConnection.getCoreApi();
var downloadNugetPackagePromise = downloadNugetPackage(packagesCoreApi, downloadUrl, zipLocation);
downloadNugetPackagePromise.catch((error) => {
return reject(error)
});
await downloadNugetPackagePromise;

console.log(tl.loc("ExtractingNugetPackage", packageName, unzipLocation));

var unzipPromise = unzip(zipLocation, unzipLocation);
unzipPromise.catch((error) => {
return reject(error)
});
await unzipPromise;

if (tl.exist(zipLocation)) {
try {
tl.rmRF(zipLocation);
} catch (error) {
tl.warning(tl.loc("OperationFailed", "rmRF", error));
}
}
return resolve();
}
else {
return reject(tl.loc("PackageTypeNotSupported"));
}
})
.catch(error => {
return reject(tl.loc("FailedToGetPackageMetadata", error));
});
});
}

export async function getNuGetPackageUrl(vsoClient: vsom.VsoClient, feedId: string, packageId: string): Promise<string> {
var PackagingAreaName = "Packaging";
var PackageAreaId = "7A20D846-C929-4ACC-9EA2-0D5A7DF1B197";

return new Promise<string>((resolve, reject) => {
var getVersioningDataPromise = vsoClient.getVersioningData(null, PackagingAreaName, PackageAreaId, { feedId: feedId, packageId: packageId });
getVersioningDataPromise.then((result) => {
return resolve(result.requestUrl);
});
getVersioningDataPromise.catch((error) => {
return reject(error)
});
});
}

export async function getDownloadUrl(vsoClient: vsom.VsoClient, feedId: string, packageName: string, version: string): Promise<string> {
var NugetArea = "NuGet"
var PackageVersionContentResourceId = "6EA81B8C-7386-490B-A71F-6CF23C80B388"
return new Promise<string>((resolve, reject) => {
var getVersioningDataPromise = vsoClient.getVersioningData(null, NugetArea, PackageVersionContentResourceId, { feedId: feedId, packageName: packageName, packageVersion: version });
getVersioningDataPromise.then((result) => {
return resolve(result.requestUrl);
});
getVersioningDataPromise.catch((error) => {
return reject(error)
});
});
}

export async function downloadNugetPackage(coreApi: corem.ICoreApi, downloadUrl: string, downloadPath: string): Promise<void> {
await new Promise<void>((resolve, reject) => {
coreApi.http.get(downloadUrl).then(response => {
tl.debug("Downloading package from url: " + downloadUrl);
tl.debug("Download status: " + response.message.statusCode);

if(response.message.statusCode != 200) {
return reject(tl.loc("FailedToDownloadNugetPackage", downloadUrl, response.message.statusMessage));
}

var responseStream = response.message as stream.Readable;
var file = fs.createWriteStream(downloadPath);
responseStream.pipe(file);
responseStream.on("end", () => {
console.log(tl.loc("PackageDownloadSuccessful"));
file.on("close", () => {
return resolve();
});
});
responseStream.on("error", err => {
file.close();
return reject(tl.loc("FailedToDownloadNugetPackage", downloadUrl, err));
});

}).catch(error => {
return reject(tl.loc("FailedToDownloadNugetPackage", downloadUrl, error));
});
});
}

function executeWithRetries(operationName: string, operation: () => Promise<any>, retryCount): Promise<any> {
var executePromise = new Promise((resolve, reject) => {
executeWithRetriesImplementation(operationName, operation, retryCount, resolve, reject);
});

return executePromise;
}

function executeWithRetriesImplementation(operationName: string, operation: () => Promise<any>, currentRetryCount, resolve, reject) {
operation().then((result) => {
resolve(result);
}).catch((error) => {
if (currentRetryCount <= 0) {
tl.error(tl.loc("OperationFailed", operationName, error));
reject(error);
}
else {
console.log(tl.loc('RetryingOperation', operationName, currentRetryCount));
currentRetryCount = currentRetryCount - 1;
setTimeout(() => executeWithRetriesImplementation(operationName, operation, currentRetryCount, resolve, reject), 4 * 1000);
}
});
}

export async function unzip(zipLocation: string, unzipLocation: string): Promise<void> {

await new Promise<void>(function (resolve, reject) {
if (tl.exist(unzipLocation)) {
tl.rmRF(unzipLocation);
}

tl.debug('Extracting ' + zipLocation + ' to ' + unzipLocation);
#if NODE20
tl.debug(`Using extract-zip package for extracting archive`);
extract(zipLocation, { dir: unzipLocation }).then(() => {
resolve();
}).catch((error) => {
reject(error);
});
#else
var unzipper = new DecompressZip(zipLocation);
unzipper.on('error', err => {
return reject(tl.loc("ExtractionFailed", err))
});
unzipper.on('extract', () => {
tl.debug('Extracted ' + zipLocation + ' to ' + unzipLocation + ' successfully');
return resolve();
});

unzipper.extract({
path: path.normalize(unzipLocation)
});
#endif
});
tl.setResult(tl.TaskResult.Failed, tl.loc("DeprecatedTask"));
}

main()
.then(() => tl.setResult(tl.TaskResult.SucceededWithIssues, tl.loc("DeprecatedTask")))
.catch((error) => tl.setResult(tl.TaskResult.Failed, error));
Loading