From e7f62164792058006a22221ffaa666ec72a33a3c Mon Sep 17 00:00:00 2001 From: Vikranth Thati Date: Mon, 23 Jul 2018 15:42:53 +0530 Subject: [PATCH 1/5] Download Helm from redirect url. --- .../Common/utility-common/downloadutility.ts | 37 ++++++++++++++++--- Tasks/HelmInstallerV0/task.json | 2 +- Tasks/HelmInstallerV0/task.loc.json | 2 +- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Tasks/Common/utility-common/downloadutility.ts b/Tasks/Common/utility-common/downloadutility.ts index 964e705daef0..3b3344fec351 100644 --- a/Tasks/Common/utility-common/downloadutility.ts +++ b/Tasks/Common/utility-common/downloadutility.ts @@ -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 { +export async function download(options: any, downloadPath: string, printData: boolean, isRedirectUrl: boolean = false): Promise { var file = fs.createWriteStream(downloadPath); var body = '' return new Promise((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)); @@ -20,9 +21,35 @@ 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 + && res.headers + && res.headers.location) { + file.end(null, null, file.close); + var redirectUrl = res.headers.location; + tl.debug("Download latest release from redirect uri: " + redirectUrl); + if (typeof options === 'string') { + options = redirectUrl; + } + else { + 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"); diff --git a/Tasks/HelmInstallerV0/task.json b/Tasks/HelmInstallerV0/task.json index 9514b010aec9..646a910ab3eb 100644 --- a/Tasks/HelmInstallerV0/task.json +++ b/Tasks/HelmInstallerV0/task.json @@ -13,7 +13,7 @@ "version": { "Major": 0, "Minor": 1, - "Patch": 5 + "Patch": 6 }, "demands": [], "satisfies": ["Helm"], diff --git a/Tasks/HelmInstallerV0/task.loc.json b/Tasks/HelmInstallerV0/task.loc.json index 33d49acdc8ff..aefd2574724f 100644 --- a/Tasks/HelmInstallerV0/task.loc.json +++ b/Tasks/HelmInstallerV0/task.loc.json @@ -13,7 +13,7 @@ "version": { "Major": 0, "Minor": 1, - "Patch": 5 + "Patch": 6 }, "demands": [], "satisfies": [ From 068eb90b377df11ce1818c3ace1fd07b62a93928 Mon Sep 17 00:00:00 2001 From: Vikranth Thati Date: Tue, 24 Jul 2018 14:21:47 +0530 Subject: [PATCH 2/5] . --- Tasks/Common/utility-common/downloadutility.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/Tasks/Common/utility-common/downloadutility.ts b/Tasks/Common/utility-common/downloadutility.ts index 3b3344fec351..945da632e29f 100644 --- a/Tasks/Common/utility-common/downloadutility.ts +++ b/Tasks/Common/utility-common/downloadutility.ts @@ -26,7 +26,6 @@ export async function download(options: any, downloadPath: string, printData: bo && !isRedirectUrl && res.headers && res.headers.location) { - file.end(null, null, file.close); var redirectUrl = res.headers.location; tl.debug("Download latest release from redirect uri: " + redirectUrl); if (typeof options === 'string') { From 0c108ea123e85ecb3d17a0b9153be33aa217b884 Mon Sep 17 00:00:00 2001 From: Vikranth Thati Date: Thu, 26 Jul 2018 17:42:53 +0530 Subject: [PATCH 3/5] Addressed comments. --- .../Common/utility-common/downloadutility.ts | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/Tasks/Common/utility-common/downloadutility.ts b/Tasks/Common/utility-common/downloadutility.ts index 945da632e29f..7e5dd4509c3a 100644 --- a/Tasks/Common/utility-common/downloadutility.ts +++ b/Tasks/Common/utility-common/downloadutility.ts @@ -5,7 +5,35 @@ var fs = require('fs'); var url = require('url'); import * as tl from "vsts-task-lib/task"; -export async function download(options: any, downloadPath: string, printData: boolean, isRedirectUrl: boolean = false): Promise { +function isDownloadSucceeded(res: any): boolean { + return res.statusCode >= 200 && res.statusCode < 300; +} + +function isRedirect(res: any): boolean { + return res.statusCode >= 300 && res.statusCode < 400 && res.headers && res.headers.location; +} + +function getRedirectOptions(options: any, redirectUrl: string): any { + tl.debug("Download latest release from redirect uri: " + 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 { var file = fs.createWriteStream(downloadPath); var body = '' return new Promise((resolve, reject) => { @@ -21,39 +49,18 @@ export async function download(options: any, downloadPath: string, printData: bo tl.debug(body); } - if(res.statusCode < 200 || res.statusCode >= 300) { - if((res.statusCode >= 300 || res.statusCode < 400) - && !isRedirectUrl - && 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 { - 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 { + 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 => { From e3210225c47e511aad4ef30f7a4ba36ed5d42fbf Mon Sep 17 00:00:00 2001 From: Vikranth Thati Date: Thu, 26 Jul 2018 17:45:30 +0530 Subject: [PATCH 4/5] . --- Tasks/Common/utility-common/downloadutility.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Tasks/Common/utility-common/downloadutility.ts b/Tasks/Common/utility-common/downloadutility.ts index 7e5dd4509c3a..557fbb5fb295 100644 --- a/Tasks/Common/utility-common/downloadutility.ts +++ b/Tasks/Common/utility-common/downloadutility.ts @@ -5,12 +5,15 @@ var fs = require('fs'); var url = require('url'); import * as tl from "vsts-task-lib/task"; -function isDownloadSucceeded(res: any): boolean { - return res.statusCode >= 200 && res.statusCode < 300; +function isDownloadSucceeded(response: any): boolean { + return response.statusCode >= 200 && response.statusCode < 300; } -function isRedirect(res: any): boolean { - return res.statusCode >= 300 && res.statusCode < 400 && res.headers && res.headers.location; +function isRedirect(response: any): boolean { + return response.statusCode >= 300 + && response.statusCode < 400 + && response.headers + && response.headers.location; } function getRedirectOptions(options: any, redirectUrl: string): any { From 75498fda2e5566791509a60446c4f445a08a3d7e Mon Sep 17 00:00:00 2001 From: Vikranth Thati Date: Thu, 26 Jul 2018 18:35:30 +0530 Subject: [PATCH 5/5] . --- Tasks/Common/utility-common/downloadutility.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tasks/Common/utility-common/downloadutility.ts b/Tasks/Common/utility-common/downloadutility.ts index 557fbb5fb295..94e190ef6606 100644 --- a/Tasks/Common/utility-common/downloadutility.ts +++ b/Tasks/Common/utility-common/downloadutility.ts @@ -17,7 +17,7 @@ function isRedirect(response: any): boolean { } function getRedirectOptions(options: any, redirectUrl: string): any { - tl.debug("Download latest release from redirect uri: " + redirectUrl); + tl.debug("redirect url: " + redirectUrl); if (typeof options === 'string') { options = redirectUrl; }