Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/builds/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class BuildsInfoCommand extends ApifyCommand<typeof BuildsInfoCommand> {

if (build.finishedAt) {
message.push(
` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)} (took ${chalk.gray(DurationFormatter.format(build.stats?.durationMillis ?? 0))})`,
` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)} (took ${chalk.gray(DurationFormatter.format(build.stats?.durationMillis ?? build.finishedAt.getTime() - build.startedAt.getTime()))})`,
);
} else {
const diff = Date.now() - build.startedAt.getTime();
Expand Down
7 changes: 6 additions & 1 deletion src/commands/builds/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import { isReducedBuild } from '../../lib/commands/agent-output.js';
import { info } from '../../lib/outputs.js';
import { getLoggedClientOrThrow, outputJobLog } from '../../lib/utils.js';

Expand Down Expand Up @@ -39,6 +40,10 @@ export class BuildsLogCommand extends ApifyCommand<typeof BuildsLogCommand> {

info({ message: `Log for build with ID "${buildId}":\n` });

await outputJobLog({ job: build, apifyClient });
const result = await outputJobLog({ job: build, apifyClient });

if (result === 'no-logs' && isReducedBuild(build)) {
throw new Error(`You don't have access to the log of build with ID "${buildId}".`);
}
}
}
5 changes: 5 additions & 0 deletions src/lib/commands/agent-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ export async function fetchLogTail(apifyClient: ApifyClient, jobId: string, maxL
}
}

// The API has no flag for the reduced build it returns to users who can't write to or build the Actor; only full builds carry `options`.
export function isReducedBuild(build: Build): boolean {
return build.options === undefined;
}

export function consoleActorUrl(actorId: string): string {
return `${getConsoleUrl()}/actors/${actorId}`;
}
Expand Down
11 changes: 4 additions & 7 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,14 +693,11 @@ export const outputJobLog = async ({

// In case job was already done just output log
if (ACTOR_JOB_TERMINAL_STATUSES.includes(status as never)) {
if (process.env.APIFY_NO_LOGS_IN_TESTS) {
return;
}

// Undefined when the job failed before its container started.
// Undefined when the job failed before its container started, or when the user can't read the log.
const log = await client.log(logId).get();
if (log) process.stderr.write(log);
return;
if (log === undefined) return 'no-logs';
if (!process.env.APIFY_NO_LOGS_IN_TESTS) process.stderr.write(log);
return 'finished';
}

// In other case stream it to stderr
Expand Down
67 changes: 67 additions & 0 deletions test/local/commands/builds/reduced-build.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { ApifyClient } from 'apify-client';

import { BuildsInfoCommand } from '../../../../src/commands/builds/info.js';
import { BuildsLogCommand } from '../../../../src/commands/builds/log.js';
import { testRunCommand } from '../../../../src/lib/command-framework/apify-command.js';
import { useAuthSetup } from '../../../__setup__/hooks/useAuthSetup.js';
import { useConsoleSpy } from '../../../__setup__/hooks/useConsoleSpy.js';

const { mockGetLoggedClientOrThrow } = vitest.hoisted(() => ({
mockGetLoggedClientOrThrow: vitest.fn(),
}));

vitest.mock('../../../../src/lib/utils.js', async (importOriginal) => ({
...(await importOriginal<typeof import('../../../../src/lib/utils.js')>()),
getLoggedClientOrThrow: mockGetLoggedClientOrThrow,
}));

useAuthSetup();

const { logMessages } = useConsoleSpy();

// What the API returns to users who can read the Actor but can neither write to it nor build it.
const reducedBuild = {
meta: { origin: 'CLI' },
id: 'buildId',
actId: 'actorId',
userId: 'userId',
buildNumber: '1.0.30',
status: 'FAILED',
startedAt: new Date('2026-01-01T10:00:00.000Z'),
finishedAt: new Date('2026-01-01T10:01:05.000Z'),
};

const fakeClient = {
actor: () => ({ get: async () => undefined }),
build: () => ({ get: async () => reducedBuild }),
log: () => ({ get: async () => undefined }),
} as unknown as ApifyClient;

// The command framework records a thrown error on the real `process`; put it back so it cannot leak.
let originalExitCode: typeof process.exitCode;

beforeEach(() => {
originalExitCode = process.exitCode;
mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient);
});

afterEach(() => {
process.exitCode = originalExitCode;
});

describe('builds commands with a reduced build', () => {
it('builds info computes the duration from the timestamps', async () => {
await testRunCommand(BuildsInfoCommand, { args_buildId: 'buildId' });

const output = logMessages.log.join('\n');

expect(output).toContain('took 1 minute 5 seconds');
expect(output).not.toContain('Compute Units');
});

it('builds log says the user has no access to the log', async () => {
await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' });

expect(logMessages.error.join('\n')).toContain(`You don't have access to the log of build with ID "buildId".`);
});
});
Loading