Skip to content

Commit

Permalink
Fix for - Jfrog Artifactory plugin not handling Windows paths by defa…
Browse files Browse the repository at this point in the history
…ult #12 (#13)

* Fix for - Jfrog Artifactory plugin not handling Windows paths by default

* Fix for - Jfrog Artifactory plugin not handling Windows paths by default

* Fix for - Jfrog Artifactory plugin not handling Windows paths by default

* Fix for - Jfrog Artifactory plugin not handling Windows paths by default
  • Loading branch information
eyalbe4 authored Aug 14, 2018
1 parent 353d3f5 commit 345a2c6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
47 changes: 39 additions & 8 deletions jfrog-utils/jutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const execSync = require('child_process').execSync;

const fileName = getFileName();
const btPackage = "jfrog-cli-" + getArchitecture();
const jfrogFolderPath = path.join(tl.getVariable("Agent.WorkFolder"), "_jfrog");
const jfrogFolderPath = encodePath(path.join(tl.getVariable("Agent.WorkFolder"), "_jfrog"));
const version = "1.17.1";
const versionedCliPath = path.join(jfrogFolderPath, version, fileName);
const customCliPath = path.join(jfrogFolderPath, "current", fileName);
const versionedCliPath = encodePath(path.join(jfrogFolderPath, version, fileName));
const customCliPath = encodePath(path.join(jfrogFolderPath, "current", fileName));
const cliUrl = 'https://api.bintray.com/content/jfrog/jfrog-cli-go/' + version + '/' + btPackage + '/' + fileName + "?bt_package=" + btPackage;
const MAX_CLI_DOWNLOADS_RETRIES = 10;
const DOWNLOAD_CLI_ERR = "Failed while attempting to download JFrog CLI from " + cliUrl +
Expand All @@ -29,6 +29,7 @@ module.exports = {
addStringParam: addStringParam,
addBoolParam: addBoolParam,
fixWindowsPaths: fixWindowsPaths,
encodePath: encodePath,
getArchitecture: getArchitecture
};

Expand Down Expand Up @@ -146,10 +147,10 @@ function downloadCli(attemptNumber) {
}
};

const cliTmpPath = versionedCliPath + ".tmp";
const cliTmpPath = encodePath(versionedCliPath + ".tmp");

// Perform download
request.get(cliUrl, {json:false, resolveWithFullResponse:true}).then((response) => {
request.get(cliUrl, {json: false, resolveWithFullResponse: true}).then((response) => {
// Check valid response
if (response.statusCode < 200 || response.statusCode >= 300) {
handleError("Received http response code " + response.statusCode);
Expand All @@ -162,11 +163,11 @@ function downloadCli(attemptNumber) {
let stream = fs.createReadStream(cliTmpPath);
let digest = crypto.createHash('sha256');

stream.on('data', function(data) {
stream.on('data', function (data) {
digest.update(data, 'utf8')
});

stream.on('end', function() {
stream.on('end', function () {
let hex = digest.digest('hex');
let rawChecksum = response.headers['x-checksum-sha256'];
if (!rawChecksum) {
Expand All @@ -175,7 +176,7 @@ function downloadCli(attemptNumber) {

let trimmedChecksum = rawChecksum.split(',')[0];
if (hex === trimmedChecksum) {
fs.move(cliTmpPath, versionedCliPath).then( () => {
fs.move(cliTmpPath, versionedCliPath).then(() => {
if (!process.platform.startsWith("win")) {
fs.chmodSync(versionedCliPath, 0o555);
}
Expand Down Expand Up @@ -241,3 +242,33 @@ function validateSpecWithoutRegex(fileSpec) {
}
}
}

/**
* Encodes spaces with quotes in a path.
* a/b/Program Files/c --> a/b/"Program Files"/c
* @param str (String) - The path to encoded.
* @returns {string} - The encoded path.
*/
function encodePath(str) {
let encodedPath = "";
let arr = str.split(path.sep);
let count = 0;
for (let section of arr) {
if (section.length === 0) {
continue;
}
count++;
if (section.indexOf(" ") > 0) {
section = quote(section);
}
encodedPath += section + path.sep;
}
if (count > 0 && !str.endsWith(path.sep)) {
encodedPath = encodedPath.substring(0, encodedPath.length - 1);
}
if (str.startsWith(path.sep)) {
encodedPath = path.sep + encodedPath;
}

return encodedPath;
}
19 changes: 19 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ describe("JFrog Artifactory VSTS Extension Tests", () => {
assert.equal(specAfterFix, process.platform.startsWith("win") ? expectedSpecAfterFix : specBeforeFix, "\nSpec after fix:\n" + specAfterFix);
});

runTest("Encode paths", () => {
if (process.platform.startsWith("win")) {
assert.equal(jfrogUtils.encodePath("dir1\\dir 2\\dir 3"), "dir1\\\"dir 2\"\\\"dir 3\"");
assert.equal(jfrogUtils.encodePath("dir 1\\dir2\\a b.txt"), "\"dir 1\"\\dir2\\\"a b.txt\"");
assert.equal(jfrogUtils.encodePath("dir1\\dir2\\a.txt"), "dir1\\dir2\\a.txt");
assert.equal(jfrogUtils.encodePath("dir1\\"), "dir1\\");
assert.equal(jfrogUtils.encodePath("dir1"), "dir1");
assert.equal(jfrogUtils.encodePath("dir 1"), "\"dir 1\"");
} else {
assert.equal(jfrogUtils.encodePath("dir1/dir 2/dir 3"), "dir1/\"dir 2\"/\"dir 3\"");
assert.equal(jfrogUtils.encodePath("dir 1/dir2/a b.txt"), "\"dir 1\"/dir2/\"a b.txt\"");
assert.equal(jfrogUtils.encodePath("dir1/dir2/a.txt"), "dir1/dir2/a.txt");
assert.equal(jfrogUtils.encodePath("dir1/"), "dir1/");
assert.equal(jfrogUtils.encodePath("dir1"), "dir1");
assert.equal(jfrogUtils.encodePath("dir 1"), "\"dir 1\"");
assert.equal(jfrogUtils.encodePath("/dir1"), "/dir1");
}
});

runTest("Get architecture", () => {
let arch = jfrogUtils.getArchitecture();
switch (os.type()) {
Expand Down

0 comments on commit 345a2c6

Please sign in to comment.