Skip to content

Commit 6d7f32d

Browse files
authored
[bbs] fix incorrect revision fetch EXP-496 (#18572)
* [bbs] fix incorrect revision fetch * Fixup * fixup * 1 * Revert tests and address feedback * fixup
1 parent 158a948 commit 6d7f32d

File tree

3 files changed

+107
-13
lines changed

3 files changed

+107
-13
lines changed

components/server/src/bitbucket-server/bitbucket-server-api.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { inject, injectable } from "inversify";
1010
import { AuthProviderParams } from "../auth/auth-provider";
1111
import { BitbucketServerTokenHelper } from "./bitbucket-server-token-handler";
1212
import { CancellationToken } from "vscode-jsonrpc";
13+
import * as qs from "node:querystring";
1314

1415
@injectable()
1516
export class BitbucketServerApi {
@@ -190,6 +191,51 @@ export class BitbucketServerApi {
190191
);
191192
}
192193

194+
async getBranchLatestCommit(
195+
user: User,
196+
params: {
197+
repoKind: "projects" | "users" | string;
198+
owner: string;
199+
repositorySlug: string;
200+
branch: string;
201+
},
202+
): Promise<BitbucketServer.Branch | undefined> {
203+
// @see https://developer.atlassian.com/server/bitbucket/rest/v811/api-group-repository/#api-api-latest-projects-projectkey-repos-repositoryslug-branches-get
204+
// @see https://bitbucket.gitpod-dev.com/rest/api/1.0/users/huiwen/repos/mustard/branches?filterText=develop
205+
const queryParam = qs.stringify({
206+
filterText: params.branch,
207+
boostMatches: true,
208+
});
209+
const q = "?" + queryParam;
210+
const list = await this.runQuery<BitbucketServer.Paginated<BitbucketServer.Branch>>(
211+
user,
212+
`/${params.repoKind}/${params.owner}/repos/${params.repositorySlug}/branches${q}`,
213+
);
214+
return list.values?.find((e) => e.displayId === params.branch);
215+
}
216+
217+
async getTagLatestCommit(
218+
user: User,
219+
params: {
220+
repoKind: "projects" | "users" | string;
221+
owner: string;
222+
repositorySlug: string;
223+
tag: string;
224+
},
225+
): Promise<BitbucketServer.Tag | undefined> {
226+
// @see https://developer.atlassian.com/server/bitbucket/rest/v811/api-group-repository/#api-api-latest-projects-projectkey-repos-repositoryslug-tags-get
227+
// @see https://bitbucket.gitpod-dev.com/rest/api/1.0/users/huiwen/repos/mustard/tags?filterText=11
228+
const queryParam = qs.stringify({
229+
filterText: params.tag,
230+
});
231+
const q = "?" + queryParam;
232+
const list = await this.runQuery<BitbucketServer.Paginated<BitbucketServer.Tag>>(
233+
user,
234+
`/${params.repoKind}/${params.owner}/repos/${params.repositorySlug}/tags${q}`,
235+
);
236+
return list.values?.find((e) => e.displayId === params.tag);
237+
}
238+
193239
async getDefaultBranch(
194240
user: User,
195241
params: { repoKind: "projects" | "users"; owner: string; repositorySlug: string },
@@ -411,6 +457,13 @@ export namespace BitbucketServer {
411457
isDefault: boolean;
412458
}
413459

460+
export interface Tag {
461+
id: string;
462+
displayId: string;
463+
type: "TAG" | string;
464+
latestCommit: string;
465+
}
466+
414467
export interface BranchWithMeta extends Branch {
415468
latestCommitMetadata: Commit;
416469
htmlUrl: string;

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

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,60 @@ export class BitbucketServerContextParser extends AbstractContextParser implemen
160160
more.ref = more.ref || repository.defaultBranch;
161161
}
162162
more.refType = more.refType || "branch";
163-
164163
if (!more.revision) {
165-
const tipCommitOnDefaultBranch = await this.api.getCommits(user, {
166-
repoKind,
167-
owner,
168-
repositorySlug: repoName,
169-
query: { limit: 1 },
170-
});
171-
const commits = tipCommitOnDefaultBranch?.values || [];
172-
if (commits.length === 0) {
164+
switch (more.refType) {
165+
case "branch": {
166+
if (!more.ref) {
167+
break;
168+
}
169+
const info = await this.api.getBranchLatestCommit(user, {
170+
repoKind,
171+
owner,
172+
repositorySlug: repoName,
173+
branch: more.ref!,
174+
});
175+
if (info) {
176+
more.revision = info.latestCommit;
177+
}
178+
break;
179+
}
180+
case "tag": {
181+
if (!more.ref) {
182+
break;
183+
}
184+
const info = await this.api.getTagLatestCommit(user, {
185+
repoKind,
186+
owner,
187+
repositorySlug: repoName,
188+
tag: more.ref!,
189+
});
190+
if (info) {
191+
more.revision = info.latestCommit;
192+
}
193+
break;
194+
}
195+
case "revision":
196+
default: {
197+
const tipCommitOnDefaultBranch = await this.api.getCommits(user, {
198+
repoKind,
199+
owner,
200+
repositorySlug: repoName,
201+
query: { limit: 1 },
202+
});
203+
const commits = tipCommitOnDefaultBranch?.values || [];
204+
if (commits.length === 0) {
205+
break;
206+
} else {
207+
more.revision = commits[0].id;
208+
// more.refType = "revision";
209+
}
210+
}
211+
}
212+
if (!more.revision) {
173213
// empty repo
174214
more.ref = undefined;
175215
more.revision = "";
176216
more.refType = undefined;
177-
} else {
178-
more.revision = commits[0].id;
179-
// more.refType = "revision";
180217
}
181218
}
182219

components/server/src/bitbucket-server/bitbucket-server-file-provider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export class BitbucketServerFileProvider implements FileProvider {
4545
const { owner, name, repoKind } = commit.repository;
4646

4747
try {
48-
const result = await this.api.fetchContent(user, `/${repoKind}/${owner}/repos/${name}/raw/${path}`);
48+
// @see https://developer.atlassian.com/server/bitbucket/rest/v811/api-group-repository/#api-api-latest-projects-projectkey-repos-repositoryslug-raw-path-get
49+
const result = await this.api.fetchContent(
50+
user,
51+
`/${repoKind}/${owner}/repos/${name}/raw/${path}?at=${commit.revision}`,
52+
);
4953
return result;
5054
} catch (err) {
5155
console.debug({ userId: user.id }, err);

0 commit comments

Comments
 (0)