Skip to content
TypeScript

fix(bitbucket): honor the repository in pull request URLs (#25052)

mariabb7ed7381a03
diff --git a/packages/source-control-bitbucket/src/server/BitbucketApi.test.ts b/packages/source-control-bitbucket/src/server/BitbucketApi.test.ts
index 944397e51e7..f18c8f72421 100644
--- a/packages/source-control-bitbucket/src/server/BitbucketApi.test.ts
+++ b/packages/source-control-bitbucket/src/server/BitbucketApi.test.ts
@@ -185,6 +185,114 @@ it.effect("parses pull request responses from the Bitbucket REST API", () => {
}).pipe(Effect.provide(layer));
});
+it.effect.each([false, true])("honors PR URL repositories, context=%s", (withContext) => {
+ const { execute, git, layer } = makeLayer({
+ response: (request) =>
+ Response.json(
+ request.url.endsWith("/pullrequests/42")
+ ? {
+ ...bitbucketPullRequest,
+ source: {
+ branch: { name: "feature/source-control" },
+ repository: { full_name: "another/app" },
+ },
+ destination: { branch: { name: "main" }, repository: { full_name: "another/app" } },
+ }
+ : {
+ ...repositoryJson,
+ full_name: "another/app",
+ links: { clone: [{ name: "ssh", href: "[email protected]:another/app.git" }] },
+ },
+ ),
+ });
+ return Effect.gen(function* () {
+ const bitbucket = yield* BitbucketApi.BitbucketApi;
+ const input = {
+ cwd: "/repo",
+ reference: "https://bitbucket.org/another/app/pull-requests/42",
+ ...(withContext
+ ? {
+ context: {
+ provider: {
+ kind: SourceControlProviderKind.make("bitbucket"),
+ name: "Bitbucket",
+ baseUrl: "https://bitbucket.org",
+ },
+ remoteName: "origin",
+ remoteUrl: "[email protected]:pingdotgg/t3code.git",
+ },
+ }
+ : {}),
+ };
+ yield* bitbucket.getPullRequest(input);
+ yield* bitbucket.checkoutPullRequest(input);
+ assert.deepStrictEqual(
+ execute.mock.calls.slice(0, 2).map(([request]) => request.url),
+ [
+ "https://api.test.local/2.0/repositories/another/app/pullrequests/42",
+ "https://api.test.local/2.0/repositories/another/app/pullrequests/42",
+ ],
+ );
+ assert.deepStrictEqual(git.ensureRemote.mock.calls[0]?.[0], {
+ cwd: "/repo",
+ preferredName: "another",
+ url: "[email protected]:another/app.git",
+ });
+ assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], {
+ cwd: "/repo",
+ remoteName: "octocat",
+ remoteBranch: "feature/source-control",
+ localBranch: "feature/source-control",
+ });
+ assert.deepStrictEqual(git.setBranchUpstream.mock.calls[0]?.[0], {
+ cwd: "/repo",
+ branch: "feature/source-control",
+ remoteName: "octocat",
+ remoteBranch: "feature/source-control",
+ });
+ }).pipe(Effect.provide(layer));
+});
+
+it.effect.each([false, true])(
+ "reuses matching checkout remotes for numeric references, context=%s",
+ (withContext) => {
+ const { git, layer } = makeLayer({
+ response: () =>
+ Response.json({
+ ...bitbucketPullRequest,
+ source: { ...bitbucketPullRequest.source, repository: { full_name: "pingdotgg/t3code" } },
+ }),
+ });
+ return Effect.gen(function* () {
+ const bitbucket = yield* BitbucketApi.BitbucketApi;
+ yield* bitbucket.checkoutPullRequest({
+ cwd: "/repo",
+ reference: "#42",
+ ...(withContext
+ ? {
+ context: {
+ provider: {
+ kind: SourceControlProviderKind.make("bitbucket"),
+ name: "Bitbucket",
+ baseUrl: "https://bitbucket.org",
+ },
+ remoteName: "upstream",
+ remoteUrl: "[email protected]:PingDotGG/T3Code.git",
+ },
+ }
+ : {}),
+ });
+ assert.strictEqual(git.ensureRemote.mock.calls.length, 0);
+ assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], {
+ cwd: "/repo",
+ remoteName: withContext ? "upstream" : "origin",
+ remoteBranch: "feature/source-control",
+ localBranch: "feature/source-control",
+ });
+ }).pipe(Effect.provide(layer));
+ },
+);
+
it.effect("lists pull requests with Bitbucket state and source branch query params", () => {
const { execute, layer } = makeLayer({
response: () =>
diff --git a/packages/source-control-bitbucket/src/server/BitbucketApi.ts b/packages/source-control-bitbucket/src/server/BitbucketApi.ts
index 511b6f2a07a..7d81f272a36 100644
--- a/packages/source-control-bitbucket/src/server/BitbucketApi.ts
+++ b/packages/source-control-bitbucket/src/server/BitbucketApi.ts
@@ -769,6 +769,7 @@ export const make = Effect.gen(function* () {
const resolveRepository = Effect.fn("BitbucketApi.resolveRepository")(function* (input: {
readonly cwd: string;
+ readonly reference?: string;
readonly context?: SourceControlProvider.SourceControlProviderContext;
readonly repository?: string;
}) {
@@ -780,6 +781,10 @@ export const make = Effect.gen(function* () {
return yield* requireRepositoryLocator(input.repository);
}
+ const url = input.reference ? URL.parse(input.reference) : null;
+ const link = url ? definition.parseChangeRequestUrl(url) : null;
+ if (link) return yield* requireRepositoryLocator(link.repository);
+
const fromContext =
input.context?.provider.kind === "bitbucket"
? parseBitbucketRemoteUrl(input.context.remoteUrl)
@@ -877,19 +882,27 @@ export const make = Effect.gen(function* () {
readonly sourceRepositoryName: string;
readonly isCrossRepository: boolean;
}) {
- if (
- input.context?.provider.kind === "bitbucket" &&
- !input.isCrossRepository &&
- parseBitbucketRemoteUrl(input.context.remoteUrl) !== null
- ) {
- return input.context.remoteName;
- }
-
if (!input.isCrossRepository) {
- const remoteName = yield* git
- .resolvePrimaryRemoteName(input.cwd)
- .pipe(Effect.orElseSucceed(() => null));
- if (remoteName) return remoteName;
+ const context = input.context?.provider.kind === "bitbucket" ? input.context : undefined;
+ const remoteName =
+ context?.remoteName ??
+ (yield* git.resolvePrimaryRemoteName(input.cwd).pipe(Effect.orElseSucceed(() => null)));
+ if (remoteName) {
+ const remoteUrl =
+ context?.remoteUrl ??
+ (yield* readConfigValueNullable(input.cwd, `remote.${remoteName}.url`));
+ const repository =
+ remoteUrl && (context || recognizeRemoteWith(definition, remoteUrl))
+ ? parseBitbucketRemoteUrl(remoteUrl)
+ : null;
+ if (
+ repository &&
+ `${repository.workspace}/${repository.repoSlug}`.toLowerCase() ===
+ input.sourceRepositoryName.toLowerCase()
+ ) {
+ return remoteName;
+ }
+ }
}
const cloneUrls = yield* getRepository({
diff --git a/packages/source-control-bitbucket/src/shared/definition.test.ts b/packages/source-control-bitbucket/src/shared/definition.test.ts
index c8640b42936..f10b9c7e6bd 100644
--- a/packages/source-control-bitbucket/src/shared/definition.test.ts
+++ b/packages/source-control-bitbucket/src/shared/definition.test.ts
@@ -7,6 +7,11 @@ const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
describe("Bitbucket change request URLs", () => {
it("reads a pull request on Bitbucket hosts", () => {
+ expect(
+ definition.isChangeRequestReference(
+ "https://bitbucket.org/workspace/repo/pull-requests/5/diff",
+ ),
+ ).toBe(true);
expect(parse("https://bitbucket.org/workspace/repo/pull-requests/5/diff")).toEqual({
host: "bitbucket.org",
repository: "workspace/repo",
@@ -21,6 +26,14 @@ describe("Bitbucket change request URLs", () => {
it("leaves other hosts alone", () => {
expect(parse("https://scm.example.test/workspace/repo/pull-requests/5")).toBeNull();
+ expect(
+ definition.isChangeRequestReference(
+ "https://scm.example.test/workspace/repo/pull-requests/5",
+ ),
+ ).toBe(false);
+ expect(
+ definition.isChangeRequestReference("https://bitbucket.org/workspace/repo/src/main"),
+ ).toBe(false);
});
it("builds pull request URLs below the repository", () => {
diff --git a/packages/source-control-bitbucket/src/shared/definition.ts b/packages/source-control-bitbucket/src/shared/definition.ts
index a8e786cb0ba..77d34dc6d04 100644
--- a/packages/source-control-bitbucket/src/shared/definition.ts
+++ b/packages/source-control-bitbucket/src/shared/definition.ts
@@ -107,7 +107,14 @@ export const definition = defineSourceControl({
authorProfileUrl: () => null,
reviewSummaryRequired: () => false,
checkoutCommandArgument: () => null,
- isChangeRequestReference: () => false,
+ isChangeRequestReference: (reference) => {
+ const url = URL.parse(reference);
+ return (
+ url !== null &&
+ isBitbucketHost(url.hostname) &&
+ parseChangeRequestPath(url, CHANGE_REQUEST_PATH) !== null
+ );
+ },
checkoutChangeRequestHost: () => null,
recognizeRemote: ({ host, hostname }) =>
hostname === "bitbucket.org" || hostname.split(".").includes("bitbucket")