diff --git a/components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts b/components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts index f506431b5b1bfb..2cbc7454bbb91b 100644 --- a/components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts +++ b/components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts @@ -19,6 +19,7 @@ import { Config } from "../config"; import { TokenProvider } from "../user/token-provider"; import { BitbucketServerApi } from "./bitbucket-server-api"; import { HostContextProvider } from "../auth/host-context-provider"; +import { URL } from "url"; @suite(timeout(10000), skipIfEnvVarNotSet("GITPOD_TEST_TOKEN_BITBUCKET_SERVER")) class TestBitbucketServerContextParser { @@ -304,6 +305,14 @@ class TestBitbucketServerContextParser { }, }); } + + @test.only test_toSimpleBranchName() { + const url = new URL( + "https://bitbucket.gitpod-self-hosted.com/projects/FOO/repos/repo123/browse?at=refs%2Fheads%2Ffoo", + ); + const branchName = this.parser.toSimpleBranchName(url.searchParams.get("at")!); + expect(branchName).to.equal("foo"); + } } module.exports = new TestBitbucketServerContextParser(); diff --git a/components/server/src/bitbucket-server/bitbucket-server-context-parser.ts b/components/server/src/bitbucket-server/bitbucket-server-context-parser.ts index 88e2ee2f62d2eb..f5d42b10167105 100644 --- a/components/server/src/bitbucket-server/bitbucket-server-context-parser.ts +++ b/components/server/src/bitbucket-server/bitbucket-server-context-parser.ts @@ -32,7 +32,8 @@ export class BitbucketServerContextParser extends AbstractContextParser implemen ); if (searchParams.has("at")) { - more.ref = decodeURIComponent(searchParams.get("at")!); + const branchName = this.toSimpleBranchName(decodeURIComponent(searchParams.get("at")!)); + more.ref = branchName; more.refType = "branch"; } @@ -58,6 +59,12 @@ export class BitbucketServerContextParser extends AbstractContextParser implemen } } + // Example: For a given context URL https://HOST/projects/FOO/repos/repo123/browse?at=refs%2Fheads%2Ffoo + // we need to parse the simple branch name `foo`. + public toSimpleBranchName(qualifiedBranchName: string | undefined) { + return qualifiedBranchName?.replace("refs/heads/", ""); + } + public async parseURL(user: User, contextUrl: string): Promise<{ repoKind: "projects" | "users" } & URLParts> { const url = new URL(contextUrl); const pathname = url.pathname.replace(/^\//, "").replace(/\/$/, ""); // pathname without leading and trailing slash