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
20 changes: 18 additions & 2 deletions src/commands/task/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ActorRun, ApifyClient, TaskStartOptions } from 'apify-client';

import { APIFY_ID_REGEX } from '@apify/consts';

import { ApifyCommand } from '../../lib/command-framework/apify-command.js';
import { Args } from '../../lib/command-framework/args.js';
import { runActorOrTaskOnCloud, SharedRunOnCloudFlags } from '../../lib/commands/run-on-cloud.js';
Expand Down Expand Up @@ -100,12 +102,26 @@ export class TaskRunCommand extends ApifyCommand<typeof TaskRunCommand> {
};
}

// Try fetching task directly by name
// Try fetching task directly by name or ID
if (taskId) {
// If the input looks like a raw Apify resource ID, try it directly first
if (APIFY_ID_REGEX.test(taskId)) {
const taskById = await client.task(taskId).get();

if (taskById) {
return {
id: taskById.id,
userFriendlyId: `${usernameOrId}/${taskById.name}`,
title: taskById.title,
task: taskById,
};
}
}

const task = await client.task(`${usernameOrId}/${taskId.toLowerCase()}`).get();

if (!task) {
throw new Error(`Cannot find Task with name '${taskId}' in your account.`);
throw new Error(`Cannot find Task with name or ID '${taskId}' in your account.`);
}

return {
Expand Down
16 changes: 16 additions & 0 deletions test/api/commands/task/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const expectedOutput = {
describe('[api] apify task run', () => {
let actorId: string;
let taskId: string;
let rawTaskId: string;

beforeAll(async () => {
await beforeAllCalls();
Expand Down Expand Up @@ -77,6 +78,7 @@ describe('[api] apify task run', () => {
input: expectedOutput,
});

rawTaskId = task.id;
taskId = `${username}/${task.name}`;
}, TEST_TIMEOUT);

Expand Down Expand Up @@ -114,4 +116,18 @@ describe('[api] apify task run', () => {

expect(expectedOutput).toStrictEqual(output!.value);
});

it('should work with just the task ID', async () => {
await testRunCommand(TaskRunCommand, {
args_taskId: rawTaskId,
});

const taskClient = testUserClient.task(taskId);
const runs = await taskClient.runs().list();
const lastRun = runs.items.pop();
const lastRunDetail = await testUserClient.run(lastRun!.id).get();
const output = await testUserClient.keyValueStore(lastRunDetail!.defaultKeyValueStoreId).getRecord('OUTPUT');

expect(expectedOutput).toStrictEqual(output!.value);
});
});
Loading