Skip to content

Commit 91e9cb4

Browse files
[Bitbucket Server] better tag context handling (#20245)
1 parent d1e314b commit 91e9cb4

File tree

2 files changed

+87
-2
lines changed

2 files changed

+87
-2
lines changed

components/server/src/bitbucket-server/bitbucket-server-context-parser.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,34 @@ class TestBitbucketServerContextParser {
265265
});
266266
}
267267

268+
@test async test_branch_context_02() {
269+
const result = await this.parser.handle(
270+
{},
271+
this.user,
272+
// here we don't provide the `refs/heads/` prefix, forcing the context parser to query the API to figure out the refType
273+
"https://bitbucket.gitpod-dev.com/users/svenefftinge/repos/browser-extension-test/commits?until=my-branch&merges=include",
274+
);
275+
276+
expect(result).to.deep.include({
277+
ref: "my-branch",
278+
refType: "branch",
279+
revision: "3ca42b45bc693973cb21a112a418c13f8b4d11a5",
280+
path: "",
281+
isFile: false,
282+
repository: {
283+
cloneUrl: "https://bitbucket.gitpod-dev.com/scm/~svenefftinge/browser-extension-test.git",
284+
defaultBranch: "main",
285+
host: "bitbucket.gitpod-dev.com",
286+
name: "browser-extension-test",
287+
owner: "svenefftinge",
288+
repoKind: "users",
289+
private: false,
290+
webUrl: "https://bitbucket.gitpod-dev.com/users/svenefftinge/repos/browser-extension-test",
291+
},
292+
title: "svenefftinge/browser-extension-test - my-branch",
293+
});
294+
}
295+
268296
@test async test_PR_context_01() {
269297
const result = await this.parser.handle(
270298
{},
@@ -370,6 +398,32 @@ class TestBitbucketServerContextParser {
370398
});
371399
}
372400

401+
@test async test_tag_context_02() {
402+
const result = await this.parser.handle(
403+
{},
404+
this.user,
405+
// here we don't provide the `refs/tags/` prefix, forcing the context parser to query the API to figure out the refType
406+
"https://bitbucket.gitpod-dev.com/projects/GIT/repos/gitpod-test-repo/browse?at=test-tag-v1.0.1",
407+
);
408+
409+
expect(result).to.deep.include({
410+
title: "GIT/gitpod-test-repo - test-tag-v1.0.1",
411+
ref: "test-tag-v1.0.1",
412+
refType: "tag",
413+
revision: "506e5aed317f28023994ecf8ca6ed91430e9c1a4",
414+
repository: {
415+
host: "bitbucket.gitpod-dev.com",
416+
owner: "GIT",
417+
name: "gitpod-test-repo",
418+
cloneUrl: "https://bitbucket.gitpod-dev.com/scm/git/gitpod-test-repo.git",
419+
webUrl: "https://bitbucket.gitpod-dev.com/projects/GIT/repos/gitpod-test-repo",
420+
defaultBranch: "master",
421+
private: true,
422+
repoKind: "projects",
423+
},
424+
});
425+
}
426+
373427
@test test_toSimpleBranchName() {
374428
const url = new URL(
375429
"https://bitbucket.gitpod-dev.com/projects/GIT/repos/gitpod-test-repo/browse?at=refs%2Fheads%2Ffoo",

components/server/src/bitbucket-server/bitbucket-server-context-parser.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,40 @@ export class BitbucketServerContextParser extends AbstractContextParser implemen
3636
if (searchParamsRef.startsWith("refs/tags/")) {
3737
more.ref = this.toSimplifiedTagName(searchParamsRef);
3838
more.refType = "tag";
39-
} else {
39+
} else if (searchParamsRef.startsWith("refs/heads/")) {
4040
more.ref = this.toSimpleBranchName(searchParamsRef);
4141
more.refType = "branch";
42+
} else {
43+
const branchCandidate = await this.api
44+
.getBranch(user, {
45+
repoKind,
46+
branchName: searchParamsRef,
47+
owner,
48+
repositorySlug: repoName,
49+
})
50+
.catch((error) => {
51+
if (error?.message?.startsWith("Could not find branch")) {
52+
return undefined;
53+
}
54+
55+
throw error;
56+
});
57+
58+
if (branchCandidate) {
59+
more.ref = branchCandidate.displayId;
60+
more.refType = "branch";
61+
} else {
62+
const tagCandidate = await this.api.getTagLatestCommit(user, {
63+
repoKind,
64+
tag: searchParamsRef,
65+
owner,
66+
repositorySlug: repoName,
67+
});
68+
if (tagCandidate) {
69+
more.ref = tagCandidate.displayId;
70+
more.refType = "tag";
71+
}
72+
}
4273
}
4374
}
4475

@@ -171,7 +202,7 @@ export class BitbucketServerContextParser extends AbstractContextParser implemen
171202
if (!more.revision) {
172203
more.ref = more.ref || repository.defaultBranch;
173204
}
174-
more.refType = more.refType || "branch";
205+
more.refType = more.refType ?? "branch";
175206
if (!more.revision) {
176207
switch (more.refType) {
177208
case "branch": {

0 commit comments

Comments
 (0)