diff --git a/src/commands/task/run.ts b/src/commands/task/run.ts index e4e9add60..d916333b5 100644 --- a/src/commands/task/run.ts +++ b/src/commands/task/run.ts @@ -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'; @@ -100,12 +102,26 @@ export class TaskRunCommand extends ApifyCommand { }; } - // 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 { diff --git a/test/api/commands/task/run.test.ts b/test/api/commands/task/run.test.ts index 9fb0c773b..4f53db871 100644 --- a/test/api/commands/task/run.test.ts +++ b/test/api/commands/task/run.test.ts @@ -31,6 +31,7 @@ const expectedOutput = { describe('[api] apify task run', () => { let actorId: string; let taskId: string; + let rawTaskId: string; beforeAll(async () => { await beforeAllCalls(); @@ -77,6 +78,7 @@ describe('[api] apify task run', () => { input: expectedOutput, }); + rawTaskId = task.id; taskId = `${username}/${task.name}`; }, TEST_TIMEOUT); @@ -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); + }); });