Skip to content

Commit ed98911

Browse files
committed
Adds current user retrieval for Azure DevOps Server integration
(#4478, #4516)
1 parent 99744d8 commit ed98911

File tree

2 files changed

+87
-15
lines changed

2 files changed

+87
-15
lines changed

src/plus/integrations/providers/azure/azure.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,61 @@ export class AzureDevOpsApi implements Disposable {
373373
return undefined;
374374
}
375375

376+
@debug<AzureDevOpsApi['getCurrentUserOnServer']>({ args: { 0: p => p.name, 1: '<token>' } })
377+
async getCurrentUserOnServer(
378+
provider: Provider,
379+
token: string,
380+
baseUrl: string,
381+
): Promise<{ id: string; name?: string; email?: string; username?: string; avatarUrl?: string } | undefined> {
382+
const scope = getLogScope();
383+
384+
try {
385+
const connectionData = await this.request<{
386+
authenticatedUser?: {
387+
id: string;
388+
descriptor: string;
389+
isActive: boolean;
390+
metTypeId: number;
391+
providerDisplayName?: string;
392+
emailAddress?: string;
393+
resourceVersion: 2;
394+
subjectDescriptor: string;
395+
properties?: {
396+
Account?: {
397+
$type: string;
398+
$value: string;
399+
};
400+
};
401+
};
402+
}>(
403+
provider,
404+
token,
405+
baseUrl,
406+
'_apis/connectionData',
407+
{
408+
method: 'GET',
409+
},
410+
scope,
411+
);
412+
413+
const user = connectionData?.authenticatedUser;
414+
const username = user?.properties?.Account?.$value;
415+
if (!username) {
416+
return undefined;
417+
}
418+
419+
return {
420+
id: user.id,
421+
name: user.providerDisplayName,
422+
email: user.emailAddress,
423+
username: username,
424+
};
425+
} catch (ex) {
426+
Logger.error(ex, scope, `Failed to get current user from ${baseUrl}`);
427+
return undefined;
428+
}
429+
}
430+
376431
async getWorkItemStateCategory(
377432
issueType: string,
378433
state: string,

src/plus/integrations/providers/azureDevOps.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,28 @@ export abstract class AzureDevOpsIntegrationBase<
5050

5151
const cachedAccount = this._accounts.get(accessToken);
5252
if (cachedAccount == null) {
53-
const api = await this.getProvidersApi();
54-
const user = await api.getCurrentUser(this.id, this.getApiOptions(accessToken, true));
55-
this._accounts.set(
56-
accessToken,
57-
user
58-
? {
59-
provider: this,
60-
id: user.id,
61-
name: user.name ?? undefined,
62-
email: user.email ?? undefined,
63-
avatarUrl: user.avatarUrl ?? undefined,
64-
username: user.username ?? undefined,
65-
}
66-
: undefined,
67-
);
53+
const user = await this._requestForCurrentUser(accessToken);
54+
this._accounts.set(accessToken, user);
6855
}
6956

7057
return this._accounts.get(accessToken);
7158
}
7259

60+
protected async _requestForCurrentUser(accessToken: string): Promise<Account | undefined> {
61+
const api = await this.getProvidersApi();
62+
const user = await api.getCurrentUser(this.id, this.getApiOptions(accessToken, true));
63+
return user
64+
? {
65+
provider: this,
66+
id: user.id,
67+
name: user.name ?? undefined,
68+
email: user.email ?? undefined,
69+
avatarUrl: user.avatarUrl ?? undefined,
70+
username: user.username ?? undefined,
71+
}
72+
: undefined;
73+
}
74+
7375
private _organizations: Map<string, AzureOrganizationDescriptor[] | undefined> | undefined;
7476
private async getProviderResourcesForUser(
7577
session: AuthenticationSession,
@@ -615,6 +617,21 @@ export class AzureDevOpsServerIntegration extends AzureDevOpsIntegrationBase<Git
615617
options.baseUrl = this.apiBaseUrl;
616618
return options;
617619
}
620+
621+
protected override async _requestForCurrentUser(accessToken: string): Promise<Account | undefined> {
622+
const azure = await this.container.azure;
623+
const user = azure ? await azure.getCurrentUserOnServer(this, accessToken, this.apiBaseUrl) : undefined;
624+
return user
625+
? {
626+
provider: this,
627+
id: user.id,
628+
name: user.name ?? undefined,
629+
email: user.email ?? undefined,
630+
avatarUrl: user.avatarUrl ?? undefined,
631+
username: user.username ?? undefined,
632+
}
633+
: undefined;
634+
}
618635
}
619636

620637
const azureCloudDomainRegex = /^dev\.azure\.com$|\bvisualstudio\.com$/i;

0 commit comments

Comments
 (0)