diff --git a/components/gitpod-protocol/src/context-url.spec.ts b/components/gitpod-protocol/src/context-url.spec.ts index 95b80b7f662be2..2d6e421c41bd4a 100644 --- a/components/gitpod-protocol/src/context-url.spec.ts +++ b/components/gitpod-protocol/src/context-url.spec.ts @@ -24,6 +24,12 @@ export class ContextUrlTest { expect(actual?.pathname).to.equal("/gitpod-io/gitpod-test-repo"); } + @test public parseContextUrl_withEnvVar_sshUrl() { + const actual = ContextURL.parseToURL("passedin=test%20value/git@github.com:gitpod-io/gitpod-test-repo.git"); + expect(actual?.host).to.equal("github.com"); + expect(actual?.pathname).to.equal("/gitpod-io/gitpod-test-repo.git"); + } + @test public parseContextUrl_withPrebuild() { const actual = ContextURL.parseToURL("prebuild/https://github.com/gitpod-io/gitpod-test-repo"); expect(actual?.host).to.equal("github.com"); @@ -35,5 +41,10 @@ export class ContextUrlTest { expect(actual?.host).to.equal("github.com"); expect(actual?.pathname).to.equal("/gitpod-io/gitpod-test-repo"); } + + @test public parseContextUrl_badUrl() { + const actual = ContextURL.parseToURL("[Object object]"); + expect(actual).to.be.undefined; + } } module.exports = new ContextUrlTest() \ No newline at end of file diff --git a/components/gitpod-protocol/src/context-url.ts b/components/gitpod-protocol/src/context-url.ts index fc5a9aa43545e8..e102b76165dd7f 100644 --- a/components/gitpod-protocol/src/context-url.ts +++ b/components/gitpod-protocol/src/context-url.ts @@ -21,33 +21,40 @@ export namespace ContextURL { return undefined; } - const segments = contextUrl.split("/"); - if (segments.length === 1) { - return new URL(segments[0]); // this might be something, we just try - } - - const segmentsToURL = (offset: number): URL => { - let rest = segments.slice(offset).join("/"); - if (!rest.startsWith("http")) { - rest = 'https://' + rest; + try { + const segments = contextUrl.split("/"); + if (segments.length === 1) { + return new URL(segments[0]); // this might be something, we just try } - return new URL(rest); - }; + const segmentsToURL = (offset: number): URL | undefined => { + let rest = segments.slice(offset).join("/"); + if (/^git@[^:\/]+:/.test(rest)) { + rest = rest.replace(/^git@([^:\/]+):/, 'https://$1/'); + } + if (!rest.startsWith("http")) { + rest = 'https://' + rest; + } + return new URL(rest); + }; + + const firstSegment = segments[0]; + if (firstSegment === PREBUILD_PREFIX || + firstSegment === INCREMENTAL_PREBUILD_PREFIX || + firstSegment === IMAGEBUILD_PREFIX || + firstSegment.startsWith(REFERRER_PREFIX)) { + return segmentsToURL(1); + } - const firstSegment = segments[0]; - if (firstSegment === PREBUILD_PREFIX || - firstSegment === INCREMENTAL_PREBUILD_PREFIX || - firstSegment === IMAGEBUILD_PREFIX || - firstSegment.startsWith(REFERRER_PREFIX)) { - return segmentsToURL(1); - } + // check for env vars + if (firstSegment.indexOf("=") !== -1) { + return segmentsToURL(1); + } - // check for env vars - if (firstSegment.indexOf("=") !== -1) { - return segmentsToURL(1); + return segmentsToURL(0); + } catch (error) { + console.error(error); } - return segmentsToURL(0); } } \ No newline at end of file