Skip to content

Commit 542e7c9

Browse files
AlexTugarevroboquat
authored andcommitted
[bitbucket-server] fix parsing of branch name
Context URLs of BitBucket Server may include a search param "at" to specify a branch fully qualified. GItpod's context parser results are expected to provide a simple "ref" name for branches.
1 parent 11c412b commit 542e7c9

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Diff for: components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Config } from "../config";
1919
import { TokenProvider } from "../user/token-provider";
2020
import { BitbucketServerApi } from "./bitbucket-server-api";
2121
import { HostContextProvider } from "../auth/host-context-provider";
22+
import { URL } from "url";
2223

2324
@suite(timeout(10000), skipIfEnvVarNotSet("GITPOD_TEST_TOKEN_BITBUCKET_SERVER"))
2425
class TestBitbucketServerContextParser {
@@ -304,6 +305,14 @@ class TestBitbucketServerContextParser {
304305
},
305306
});
306307
}
308+
309+
@test.only test_toSimpleBranchName() {
310+
const url = new URL(
311+
"https://bitbucket.gitpod-self-hosted.com/projects/FOO/repos/repo123/browse?at=refs%2Fheads%2Ffoo",
312+
);
313+
const branchName = this.parser.toSimpleBranchName(url.searchParams.get("at")!);
314+
expect(branchName).to.equal("foo");
315+
}
307316
}
308317

309318
module.exports = new TestBitbucketServerContextParser();

Diff for: components/server/src/bitbucket-server/bitbucket-server-context-parser.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export class BitbucketServerContextParser extends AbstractContextParser implemen
3232
);
3333

3434
if (searchParams.has("at")) {
35-
more.ref = decodeURIComponent(searchParams.get("at")!);
35+
const branchName = this.toSimpleBranchName(decodeURIComponent(searchParams.get("at")!));
36+
more.ref = branchName;
3637
more.refType = "branch";
3738
}
3839

@@ -58,6 +59,12 @@ export class BitbucketServerContextParser extends AbstractContextParser implemen
5859
}
5960
}
6061

62+
// Example: For a given context URL https://HOST/projects/FOO/repos/repo123/browse?at=refs%2Fheads%2Ffoo
63+
// we need to parse the simple branch name `foo`.
64+
public toSimpleBranchName(qualifiedBranchName: string | undefined) {
65+
return qualifiedBranchName?.replace("refs/heads/", "");
66+
}
67+
6168
public async parseURL(user: User, contextUrl: string): Promise<{ repoKind: "projects" | "users" } & URLParts> {
6269
const url = new URL(contextUrl);
6370
const pathname = url.pathname.replace(/^\//, "").replace(/\/$/, ""); // pathname without leading and trailing slash

0 commit comments

Comments
 (0)