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

[protocol] Make ContextURL.parseToURL support the newly-accepted 'git@{host}:{user}/{repo}.git' format #8100

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions components/gitpod-protocol/src/context-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]: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");
Expand All @@ -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()
51 changes: 29 additions & 22 deletions components/gitpod-protocol/src/context-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}