Skip to content

Commit d7e376b

Browse files
committed
Implements getAccountForCommit for Bitbucket DataCenter
(#4192, #4243)
1 parent b531500 commit d7e376b

File tree

3 files changed

+94
-8
lines changed

3 files changed

+94
-8
lines changed

src/plus/integrations/providers/bitbucket-server.ts

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AuthenticationSession, CancellationToken } from 'vscode';
22
import { md5 } from '@env/crypto';
33
import { SelfHostedIntegrationId } from '../../../constants.integrations';
44
import type { Container } from '../../../container';
5-
import type { Account } from '../../../git/models/author';
5+
import type { Account, UnidentifiedAuthor } from '../../../git/models/author';
66
import type { DefaultBranch } from '../../../git/models/defaultBranch';
77
import type { Issue, IssueShape } from '../../../git/models/issue';
88
import type { IssueOrPullRequest, IssueOrPullRequestType } from '../../../git/models/issueOrPullRequest';
@@ -64,14 +64,24 @@ export class BitbucketServerIntegration extends HostingIntegration<
6464
}
6565

6666
protected override async getProviderAccountForCommit(
67-
_session: AuthenticationSession,
68-
_repo: BitbucketRepositoryDescriptor,
69-
_rev: string,
70-
_options?: {
67+
{ accessToken }: AuthenticationSession,
68+
repo: BitbucketRepositoryDescriptor,
69+
rev: string,
70+
options?: {
7171
avatarSize?: number;
7272
},
73-
): Promise<Account | undefined> {
74-
return Promise.resolve(undefined);
73+
): Promise<Account | UnidentifiedAuthor | undefined> {
74+
return (await this.container.bitbucket)?.getServerAccountForCommit(
75+
this,
76+
accessToken,
77+
repo.owner,
78+
repo.name,
79+
rev,
80+
this.apiBaseUrl,
81+
{
82+
avatarSize: options?.avatarSize,
83+
},
84+
);
7585
}
7686

7787
protected override async getProviderAccountForEmail(

src/plus/integrations/providers/bitbucket-server/models.ts

+22
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ export interface BitbucketServerPullRequestRef {
5151
};
5252
}
5353

54+
export interface BitbucketAuthor {
55+
name: string;
56+
emailAddress: string;
57+
id: undefined;
58+
}
59+
5460
export interface BitbucketServerUser {
5561
name: string;
5662
emailAddress: string;
@@ -73,6 +79,22 @@ export interface BitbucketServerPullRequestUser {
7379
status: 'UNAPPROVED' | 'NEEDS_WORK' | 'APPROVED';
7480
}
7581

82+
export interface BitbucketServerBriefCommit {
83+
displayId: string;
84+
id: string;
85+
}
86+
87+
export interface BitbucketServerCommit {
88+
author: BitbucketServerUser | BitbucketAuthor;
89+
authorTimestamp: number;
90+
committer: BitbucketServerUser | BitbucketAuthor;
91+
committerTimestamp: number;
92+
displayId: string;
93+
id: string;
94+
message: string;
95+
parents: (BitbucketServerCommit | BitbucketServerBriefCommit)[];
96+
}
97+
7698
export interface BitbucketServerPullRequest {
7799
id: number;
78100
version: number;

src/plus/integrations/providers/bitbucket/bitbucket.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { Logger } from '../../../../system/logger';
2626
import type { LogScope } from '../../../../system/logger.scope';
2727
import { getLogScope } from '../../../../system/logger.scope';
2828
import { maybeStopWatch } from '../../../../system/stopwatch';
29-
import type { BitbucketServerPullRequest } from '../bitbucket-server/models';
29+
import type { BitbucketServerCommit, BitbucketServerPullRequest } from '../bitbucket-server/models';
3030
import { normalizeBitbucketServerPullRequest } from '../bitbucket-server/models';
3131
import { fromProviderPullRequest } from '../models';
3232
import type { BitbucketCommit, BitbucketIssue, BitbucketPullRequest, BitbucketRepository } from './models';
@@ -519,6 +519,60 @@ export class BitbucketApi implements Disposable {
519519
}
520520
}
521521

522+
@debug<BitbucketApi['getServerAccountForCommit']>({ args: { 0: p => p.name, 1: '<token>' } })
523+
async getServerAccountForCommit(
524+
provider: Provider,
525+
token: string,
526+
owner: string,
527+
repo: string,
528+
rev: string,
529+
baseUrl: string,
530+
_options?: {
531+
avatarSize?: number;
532+
},
533+
cancellation?: CancellationToken,
534+
): Promise<Account | UnidentifiedAuthor | undefined> {
535+
const scope = getLogScope();
536+
537+
try {
538+
const commit = await this.request<BitbucketServerCommit>(
539+
provider,
540+
token,
541+
baseUrl,
542+
`projects/${owner}/repos/${repo}/commits/${rev}`,
543+
{
544+
method: 'GET',
545+
},
546+
scope,
547+
cancellation,
548+
);
549+
if (!commit?.author) {
550+
return undefined;
551+
}
552+
if (commit.author.id != null) {
553+
return {
554+
provider: provider,
555+
id: commit.author.id.toString(),
556+
username: commit.author.name,
557+
name: commit.author.name,
558+
email: commit.author.emailAddress,
559+
avatarUrl: commit.author?.avatarUrl,
560+
} satisfies Account;
561+
}
562+
return {
563+
provider: provider,
564+
id: undefined,
565+
username: undefined,
566+
name: commit.author.name,
567+
email: commit.author.emailAddress,
568+
avatarUrl: undefined,
569+
} satisfies UnidentifiedAuthor;
570+
} catch (ex) {
571+
Logger.error(ex, scope);
572+
return undefined;
573+
}
574+
}
575+
522576
private async request<T>(
523577
provider: Provider,
524578
token: string,

0 commit comments

Comments
 (0)