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

[bitbucket-server] fix parsing of branch name #13745

Merged
merged 1 commit into from
Oct 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand All @@ -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
Expand Down