From 09611c352cb790760804bb586cc3e981112d2a0a Mon Sep 17 00:00:00 2001 From: Robert Gaggl Date: Mon, 16 Aug 2021 14:47:39 +0200 Subject: [PATCH] map custom git schemes to their supported one git+file is only used to distinguish between archive and repository file urls --- tools/admin/test/specs_test.js | 43 +++++++++++++++++++--------------- tools/admin/utils/specs.js | 10 +++++++- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/tools/admin/test/specs_test.js b/tools/admin/test/specs_test.js index e4478079..75d9c325 100644 --- a/tools/admin/test/specs_test.js +++ b/tools/admin/test/specs_test.js @@ -3,6 +3,8 @@ const assert = require("assert"); const specs = require("../utils/specs"); const constants = require("../constants"); +const {URI} = java.net; + const GIT_URLS = { negative: [ "http://example.com/xyz", @@ -96,29 +98,32 @@ exports.testNewGitHubSpec = () => { exports.testNewGitSpec = () => { const schemes = [ - "git", - "git+ssh", - "ssh", - "git+http", - "git+https", - "git+file" + {value: "git", expected: "git"}, + {value: "git+ssh", expected: "ssh"}, + {value: "ssh", expected: "ssh"}, + {value: "git+http", expected: "http"}, + {value: "git+https", expected: "https"}, + {value: "git+file", expected: "file"} ]; const uris = [ - "example.com/path/to/repo", - "example.com/path/to/repo#HEAD", - "example.com/path/to/repo#v2.x", - "example.com/path/to/repo#82116b6d1a474a37fb2783a127d4168190e61745", + "//example.com/path/to/repo", + "//example.com/path/to/repo#HEAD", + "//example.com/path/to/repo#v2.x", + "//example.com/path/to/repo#82116b6d1a474a37fb2783a127d4168190e61745", ]; schemes.forEach(scheme => { - uris.map(part => [scheme, part].join("://")) - .forEach(uri => { - const spec = specs.newGitSpec(uri); - const [url, treeish] = uri.split("#"); - assert.strictEqual(spec.type, constants.TYPE_GIT, uri); - assert.strictEqual(spec.url, url, uri); - assert.strictEqual(spec.treeish, treeish || null, uri); - }); - }) + uris.forEach(uri => { + const gitUrl = [scheme.value, uri].join(":"); + const spec = specs.newGitSpec(gitUrl); + const treeish = gitUrl.split("#")[1] || null; + const receivedUri = URI.create(spec.url); + const expectedUri = URI.create(gitUrl); + assert.strictEqual(spec.type, constants.TYPE_GIT, gitUrl); + assert.strictEqual(receivedUri.getScheme(), scheme.expected, gitUrl); + assert.strictEqual(receivedUri.getSchemeSpecificPart(), expectedUri.getSchemeSpecificPart(), gitUrl); + assert.strictEqual(spec.treeish, expectedUri.getFragment(), gitUrl); + }); + }); }; exports.testNewArchiveSpec = () => { diff --git a/tools/admin/utils/specs.js b/tools/admin/utils/specs.js index 34bff4ed..e677e255 100644 --- a/tools/admin/utils/specs.js +++ b/tools/admin/utils/specs.js @@ -1,6 +1,13 @@ const {URI} = java.net; const constants = require("../constants"); +const GIT_SCHEMES = { + "git+ssh": "ssh", + "git+http": "http", + "git+https": "https", + "git+file": "file" +}; + exports.isValidUrl = (url) => { try { URI.create(url); @@ -57,8 +64,9 @@ const newGitHubSpec = exports.newGitHubSpec = (url) => { const newGitSpec = exports.newGitSpec = (url) => { const uri = URI.create(url); + const scheme = GIT_SCHEMES[uri.getScheme()] || uri.getScheme(); // additional toString to prevent java ProcessBuilder complaints about "ConsString" - const uriString = (uri.getScheme() + ":" + uri.getSchemeSpecificPart()).toString(); + const uriString = (scheme + ":" + uri.getSchemeSpecificPart()).toString(); const treeish = uri.getFragment(); return { type: constants.TYPE_GIT,