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

Download Helm from redirect url. #7805

Merged
merged 5 commits into from
Jul 26, 2018
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
50 changes: 43 additions & 7 deletions Tasks/Common/utility-common/downloadutility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,45 @@

var https = require('https');
var fs = require('fs');
var url = require('url');
import * as tl from "vsts-task-lib/task";

export async function download(url: any, downloadPath: string, printData: boolean): Promise<void> {
function isDownloadSucceeded(response: any): boolean {
return response.statusCode >= 200 && response.statusCode < 300;
}

function isRedirect(response: any): boolean {
return response.statusCode >= 300
&& response.statusCode < 400
&& response.headers
&& response.headers.location;
}

function getRedirectOptions(options: any, redirectUrl: string): any {
tl.debug("redirect url: " + redirectUrl);
if (typeof options === 'string') {
options = redirectUrl;
}
else {
try {
var redirectUrlOptions = url.parse(redirectUrl);
options.path = redirectUrlOptions.path;
options.hostname = redirectUrlOptions.hostname;
}
catch(error) {
tl.warning("Unable to parse url:" + redirectUrl);
options = redirectUrl;
}
}

return options;
}

export async function download(options: any, downloadPath: string, printData: boolean, handleRedirect: boolean = true): Promise<void> {
var file = fs.createWriteStream(downloadPath);
var body = ''
return new Promise<void>((resolve, reject) => {
var req = https.request(url, res => {
var req = https.request(options, res => {
tl.debug("statusCode: " + res.statusCode);
res.pipe(file);
res.on("error", err => reject(err));
Expand All @@ -20,14 +52,18 @@ export async function download(url: any, downloadPath: string, printData: boolea
tl.debug(body);
}

if(res.statusCode < 200 || res.statusCode >= 300) {
tl.debug("File download failed");
reject(new Error('Failed to download file status code: ' + res.statusCode));
}
else {
if (isDownloadSucceeded(res)) {
tl.debug("File download completed");
resolve();
}
else if (isRedirect(res) && handleRedirect) {
var redirectOptions = getRedirectOptions(options, res.headers.location);
resolve(this.download(redirectOptions, downloadPath, printData, false));
}
else {
tl.debug("File download failed");
reject(new Error('Failed to download file status code: ' + res.statusCode));
}
});
});
req.on("error", err => {
Expand Down
2 changes: 1 addition & 1 deletion Tasks/HelmInstallerV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 5
"Patch": 6
},
"demands": [],
"satisfies": ["Helm"],
Expand Down
2 changes: 1 addition & 1 deletion Tasks/HelmInstallerV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"version": {
"Major": 0,
"Minor": 1,
"Patch": 5
"Patch": 6
},
"demands": [],
"satisfies": [
Expand Down