Compare commits

...
Author SHA1 Message Date
Vojtech Novak f2302d92f0 [eas-cli] Let whoami resolve its own actor 2026-11-25 08:50:57 +01:00
4 changed files with 59 additions and 19 deletions

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
'eas-cli': patch
---
Have `eas whoami` resolve the current actor once, including when a session has expired.
+13 -11
View File
@@ -31,7 +31,7 @@ import { markAsParentCommand } from '../analytics/parentCommand';
import Log, { link } from '../log';
import Sentry from '../sentry';
import SessionManager from '../user/SessionManager';
import { getActorDisplayName } from '../user/User';
import { Actor, getActorDisplayName } from '../user/User';
import { Client } from '../vcs/vcs';
export type ContextInput<
@@ -235,6 +235,16 @@ export default abstract class EasCommand extends Command {
return nullthrows(this.analyticsInternal);
}
/** Commands that report authentication failures themselves can defer the startup actor lookup. */
protected readonly deferActorLookup: boolean = false;
protected logCommand(actor: Actor | undefined): void {
if (actor) {
Sentry.setUser({ id: actor.id, username: getActorDisplayName(actor) });
}
this.analytics.logEvent(CommandEvent.ACTION, { action: `eas ${this.id}` });
}
protected abstract runAsync(): Promise<any>;
// eslint-disable-next-line async-protect/async-suffix
@@ -248,18 +258,10 @@ export default abstract class EasCommand extends Command {
this.analyticsInternal = await createAnalyticsAsync(parentCommand);
this.sessionManagerInternal = new SessionManager(this.analytics);
// this is needed for logEvent call below as it identifies the user in the analytics system
// if possible
const actor = await this.sessionManager.getUserAsync();
if (actor) {
Sentry.setUser({
id: actor.id,
username: getActorDisplayName(actor),
});
if (!this.deferActorLookup) {
this.logCommand(await this.sessionManager.getUserAsync());
}
this.analytics.logEvent(CommandEvent.ACTION, { action: command });
return await this.runAsync();
} finally {
// Commands can run inside each other, for example `eas open` runs `eas browse`.
@@ -4,9 +4,16 @@ import { getMockOclifConfig } from '../../../__tests__/commands/utils';
import { getMockViewerUserPermission } from '../../../__tests__/viewerUserPermission';
import { Permission, Role } from '../../../graphql/generated';
import Log from '../../../log';
import Sentry from '../../../sentry';
import SessionManager from '../../../user/SessionManager';
import { Actor } from '../../../user/User';
import AccountView from '../view';
jest.mock('../../../analytics/AnalyticsManager', () => ({
...jest.requireActual('../../../analytics/AnalyticsManager'),
createAnalyticsAsync: jest.fn(async () => ({ logEvent: jest.fn() })),
}));
jest.mock('../../../sentry');
jest.mock('../../../log');
const ownerPermissions = getMockViewerUserPermission(Role.Owner).permissions;
@@ -41,12 +48,23 @@ function userActor(accounts: Actor['accounts']): Actor {
};
}
async function runAsync(actor: Actor): Promise<string[]> {
const instance = new AccountView([], getMockOclifConfig());
function command(
args: string[],
getUserAsync: () => Promise<Actor | undefined>,
accessToken: string | null = null
): AccountView {
const instance = new AccountView(args, getMockOclifConfig());
// @ts-expect-error Mock the protected context without initializing a real session.
jest.spyOn(instance, 'getContextAsync').mockResolvedValue({
maybeLoggedIn: { actor, authenticationInfo: { accessToken: null, sessionSecret: 'secret' } },
sessionManager: { getUserAsync, getAccessToken: () => accessToken },
});
// @ts-expect-error Unit tests run without the command analytics lifecycle.
jest.spyOn(instance, 'logCommand').mockImplementation(() => {});
return instance;
}
async function runAsync(actor: Actor): Promise<string[]> {
const instance = command([], async () => actor);
await instance.runAsync();
return jest.mocked(Log.log).mock.calls.map(([message]) => message);
}
@@ -108,3 +126,15 @@ it('shows the effective permissions when they do not match a role', async () =>
)})`
);
});
it('attributes a successful command to the actor fetched by whoami', async () => {
const actor = userActor([personalAccount(ownerPermissions)]);
const lookup = jest.spyOn(SessionManager.prototype, 'getUserAsync').mockResolvedValue(actor);
try {
await new AccountView([], getMockOclifConfig()).run();
expect(lookup).toHaveBeenCalledTimes(1);
expect(Sentry.setUser).toHaveBeenCalledWith({ id: 'user-id', username: 'jane' });
} finally {
lookup.mockRestore();
}
});
@@ -9,17 +9,20 @@ export default class AccountView extends EasCommand {
static override description = 'show the username you are logged in as';
static override aliases = ['whoami'];
protected override readonly deferActorLookup = true;
static override contextDefinition = {
...this.ContextOptions.MaybeLoggedIn,
...this.ContextOptions.SessionManagment,
};
async runAsync(): Promise<void> {
const {
maybeLoggedIn: { actor, authenticationInfo },
} = await this.getContextAsync(AccountView, { nonInteractive: true });
const { sessionManager } = await this.getContextAsync(AccountView, { nonInteractive: true });
const accessToken = sessionManager.getAccessToken();
const actor = await sessionManager.getUserAsync();
this.logCommand(actor);
if (actor) {
const displayName = getActorDisplayName(actor);
const loggedInAs = authenticationInfo.accessToken
const loggedInAs = accessToken
? `${displayName} (authenticated using EXPO_TOKEN)`
: displayName;
Log.log(chalk.green(loggedInAs));