Skip to content

Commit bb7ed73

Browse files
fix(bitbucket): honor the repository in pull request URLs (pingdotgg#25052)
Co-authored-by: Aditya Garud <[email protected]>
1 parent 97dbfd7 commit bb7ed73

4 files changed

Lines changed: 154 additions & 13 deletions

File tree

‎packages/source-control-bitbucket/src/server/BitbucketApi.test.ts‎

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,114 @@ it.effect("parses pull request responses from the Bitbucket REST API", () => {
185185
}).pipe(Effect.provide(layer));
186186
});
187187

188+
it.effect.each([false, true])("honors PR URL repositories, context=%s", (withContext) => {
189+
const { execute, git, layer } = makeLayer({
190+
response: (request) =>
191+
Response.json(
192+
request.url.endsWith("/pullrequests/42")
193+
? {
194+
...bitbucketPullRequest,
195+
source: {
196+
branch: { name: "feature/source-control" },
197+
repository: { full_name: "another/app" },
198+
},
199+
destination: { branch: { name: "main" }, repository: { full_name: "another/app" } },
200+
}
201+
: {
202+
...repositoryJson,
203+
full_name: "another/app",
204+
links: { clone: [{ name: "ssh", href: "[email protected]:another/app.git" }] },
205+
},
206+
),
207+
});
208+
return Effect.gen(function* () {
209+
const bitbucket = yield* BitbucketApi.BitbucketApi;
210+
const input = {
211+
cwd: "/repo",
212+
reference: "https://bitbucket.org/another/app/pull-requests/42",
213+
...(withContext
214+
? {
215+
context: {
216+
provider: {
217+
kind: SourceControlProviderKind.make("bitbucket"),
218+
name: "Bitbucket",
219+
baseUrl: "https://bitbucket.org",
220+
},
221+
remoteName: "origin",
222+
remoteUrl: "[email protected]:pingdotgg/t3code.git",
223+
},
224+
}
225+
: {}),
226+
};
227+
yield* bitbucket.getPullRequest(input);
228+
yield* bitbucket.checkoutPullRequest(input);
229+
assert.deepStrictEqual(
230+
execute.mock.calls.slice(0, 2).map(([request]) => request.url),
231+
[
232+
"https://api.test.local/2.0/repositories/another/app/pullrequests/42",
233+
"https://api.test.local/2.0/repositories/another/app/pullrequests/42",
234+
],
235+
);
236+
assert.deepStrictEqual(git.ensureRemote.mock.calls[0]?.[0], {
237+
cwd: "/repo",
238+
preferredName: "another",
239+
url: "[email protected]:another/app.git",
240+
});
241+
assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], {
242+
cwd: "/repo",
243+
remoteName: "octocat",
244+
remoteBranch: "feature/source-control",
245+
localBranch: "feature/source-control",
246+
});
247+
assert.deepStrictEqual(git.setBranchUpstream.mock.calls[0]?.[0], {
248+
cwd: "/repo",
249+
branch: "feature/source-control",
250+
remoteName: "octocat",
251+
remoteBranch: "feature/source-control",
252+
});
253+
}).pipe(Effect.provide(layer));
254+
});
255+
256+
it.effect.each([false, true])(
257+
"reuses matching checkout remotes for numeric references, context=%s",
258+
(withContext) => {
259+
const { git, layer } = makeLayer({
260+
response: () =>
261+
Response.json({
262+
...bitbucketPullRequest,
263+
source: { ...bitbucketPullRequest.source, repository: { full_name: "pingdotgg/t3code" } },
264+
}),
265+
});
266+
return Effect.gen(function* () {
267+
const bitbucket = yield* BitbucketApi.BitbucketApi;
268+
yield* bitbucket.checkoutPullRequest({
269+
cwd: "/repo",
270+
reference: "#42",
271+
...(withContext
272+
? {
273+
context: {
274+
provider: {
275+
kind: SourceControlProviderKind.make("bitbucket"),
276+
name: "Bitbucket",
277+
baseUrl: "https://bitbucket.org",
278+
},
279+
remoteName: "upstream",
280+
remoteUrl: "[email protected]:PingDotGG/T3Code.git",
281+
},
282+
}
283+
: {}),
284+
});
285+
assert.strictEqual(git.ensureRemote.mock.calls.length, 0);
286+
assert.deepStrictEqual(git.fetchRemoteBranch.mock.calls[0]?.[0], {
287+
cwd: "/repo",
288+
remoteName: withContext ? "upstream" : "origin",
289+
remoteBranch: "feature/source-control",
290+
localBranch: "feature/source-control",
291+
});
292+
}).pipe(Effect.provide(layer));
293+
},
294+
);
295+
188296
it.effect("lists pull requests with Bitbucket state and source branch query params", () => {
189297
const { execute, layer } = makeLayer({
190298
response: () =>

‎packages/source-control-bitbucket/src/server/BitbucketApi.ts‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ export const make = Effect.gen(function* () {
769769

770770
const resolveRepository = Effect.fn("BitbucketApi.resolveRepository")(function* (input: {
771771
readonly cwd: string;
772+
readonly reference?: string;
772773
readonly context?: SourceControlProvider.SourceControlProviderContext;
773774
readonly repository?: string;
774775
}) {
@@ -780,6 +781,10 @@ export const make = Effect.gen(function* () {
780781
return yield* requireRepositoryLocator(input.repository);
781782
}
782783

784+
const url = input.reference ? URL.parse(input.reference) : null;
785+
const link = url ? definition.parseChangeRequestUrl(url) : null;
786+
if (link) return yield* requireRepositoryLocator(link.repository);
787+
783788
const fromContext =
784789
input.context?.provider.kind === "bitbucket"
785790
? parseBitbucketRemoteUrl(input.context.remoteUrl)
@@ -877,19 +882,27 @@ export const make = Effect.gen(function* () {
877882
readonly sourceRepositoryName: string;
878883
readonly isCrossRepository: boolean;
879884
}) {
880-
if (
881-
input.context?.provider.kind === "bitbucket" &&
882-
!input.isCrossRepository &&
883-
parseBitbucketRemoteUrl(input.context.remoteUrl) !== null
884-
) {
885-
return input.context.remoteName;
886-
}
887-
888885
if (!input.isCrossRepository) {
889-
const remoteName = yield* git
890-
.resolvePrimaryRemoteName(input.cwd)
891-
.pipe(Effect.orElseSucceed(() => null));
892-
if (remoteName) return remoteName;
886+
const context = input.context?.provider.kind === "bitbucket" ? input.context : undefined;
887+
const remoteName =
888+
context?.remoteName ??
889+
(yield* git.resolvePrimaryRemoteName(input.cwd).pipe(Effect.orElseSucceed(() => null)));
890+
if (remoteName) {
891+
const remoteUrl =
892+
context?.remoteUrl ??
893+
(yield* readConfigValueNullable(input.cwd, `remote.${remoteName}.url`));
894+
const repository =
895+
remoteUrl && (context || recognizeRemoteWith(definition, remoteUrl))
896+
? parseBitbucketRemoteUrl(remoteUrl)
897+
: null;
898+
if (
899+
repository &&
900+
`${repository.workspace}/${repository.repoSlug}`.toLowerCase() ===
901+
input.sourceRepositoryName.toLowerCase()
902+
) {
903+
return remoteName;
904+
}
905+
}
893906
}
894907

895908
const cloneUrls = yield* getRepository({

‎packages/source-control-bitbucket/src/shared/definition.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const parse = (url: string) => definition.parseChangeRequestUrl(new URL(url));
77

88
describe("Bitbucket change request URLs", () => {
99
it("reads a pull request on Bitbucket hosts", () => {
10+
expect(
11+
definition.isChangeRequestReference(
12+
"https://bitbucket.org/workspace/repo/pull-requests/5/diff",
13+
),
14+
).toBe(true);
1015
expect(parse("https://bitbucket.org/workspace/repo/pull-requests/5/diff")).toEqual({
1116
host: "bitbucket.org",
1217
repository: "workspace/repo",
@@ -21,6 +26,14 @@ describe("Bitbucket change request URLs", () => {
2126

2227
it("leaves other hosts alone", () => {
2328
expect(parse("https://scm.example.test/workspace/repo/pull-requests/5")).toBeNull();
29+
expect(
30+
definition.isChangeRequestReference(
31+
"https://scm.example.test/workspace/repo/pull-requests/5",
32+
),
33+
).toBe(false);
34+
expect(
35+
definition.isChangeRequestReference("https://bitbucket.org/workspace/repo/src/main"),
36+
).toBe(false);
2437
});
2538

2639
it("builds pull request URLs below the repository", () => {

‎packages/source-control-bitbucket/src/shared/definition.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ export const definition = defineSourceControl({
107107
authorProfileUrl: () => null,
108108
reviewSummaryRequired: () => false,
109109
checkoutCommandArgument: () => null,
110-
isChangeRequestReference: () => false,
110+
isChangeRequestReference: (reference) => {
111+
const url = URL.parse(reference);
112+
return (
113+
url !== null &&
114+
isBitbucketHost(url.hostname) &&
115+
parseChangeRequestPath(url, CHANGE_REQUEST_PATH) !== null
116+
);
117+
},
111118
checkoutChangeRequestHost: () => null,
112119
recognizeRemote: ({ host, hostname }) =>
113120
hostname === "bitbucket.org" || hostname.split(".").includes("bitbucket")

0 commit comments

Comments
 (0)