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 2 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
36 changes: 31 additions & 5 deletions Tasks/Common/utility-common/downloadutility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

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> {
export async function download(options: any, downloadPath: string, printData: boolean, isRedirectUrl: boolean = false): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename isRedirectUrl this to handleRedirect

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,9 +21,34 @@ 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));
if(res.statusCode < 200 || res.statusCode >= 300) {
if((res.statusCode >= 300 || res.statusCode < 400)
&& !isRedirectUrl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor this to isRedirect

&& res.headers
&& res.headers.location) {
var redirectUrl = res.headers.location;
tl.debug("Download latest release from redirect uri: " + redirectUrl);
if (typeof options === 'string') {
options = redirectUrl;
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor this to a separate method call getRedirectOptions

try {
var redirectUrlOptions = url.parse(redirectUrl, true, true);
options.path = redirectUrlOptions.pathname;
options.hostname = redirectUrlOptions.hostname;
}
catch(error) {
tl.warning("Unable to parse url:" + redirectUrl);
options = redirectUrl;
}
}

resolve(this.download(options, downloadPath, printData, true));
}
else {
tl.debug("File download failed");
reject(new Error('Failed to download file status code: ' + res.statusCode));
}
}
else {
tl.debug("File download completed");
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