|
| 1 | +import type { ActorTaggedBuild, ApifyApiError } from 'apify-client'; |
| 2 | +import chalk from 'chalk'; |
| 3 | + |
| 4 | +import { ApifyCommand } from '../../lib/command-framework/apify-command.js'; |
| 5 | +import { Flags } from '../../lib/command-framework/flags.js'; |
| 6 | +import { useYesNoConfirm } from '../../lib/hooks/user-confirmations/useYesNoConfirm.js'; |
| 7 | +import { error, info, success } from '../../lib/outputs.js'; |
| 8 | +import { getLoggedClientOrThrow } from '../../lib/utils.js'; |
| 9 | + |
| 10 | +export class BuildsRemoveTagCommand extends ApifyCommand<typeof BuildsRemoveTagCommand> { |
| 11 | + static override name = 'remove-tag' as const; |
| 12 | + |
| 13 | + static override description = 'Removes a tag from a specific Actor build.'; |
| 14 | + |
| 15 | + static override flags = { |
| 16 | + build: Flags.string({ |
| 17 | + char: 'b', |
| 18 | + description: 'The build ID to remove the tag from.', |
| 19 | + required: true, |
| 20 | + }), |
| 21 | + tag: Flags.string({ |
| 22 | + char: 't', |
| 23 | + description: 'The tag to remove from the build.', |
| 24 | + required: true, |
| 25 | + }), |
| 26 | + yes: Flags.boolean({ |
| 27 | + char: 'y', |
| 28 | + description: 'Automatic yes to prompts; assume "yes" as answer to all prompts.', |
| 29 | + default: false, |
| 30 | + }), |
| 31 | + }; |
| 32 | + |
| 33 | + async run() { |
| 34 | + const { build: buildId, tag, yes } = this.flags; |
| 35 | + |
| 36 | + const apifyClient = await getLoggedClientOrThrow(); |
| 37 | + |
| 38 | + const build = await apifyClient.build(buildId).get(); |
| 39 | + |
| 40 | + if (!build) { |
| 41 | + error({ message: `Build with ID "${buildId}" was not found on your account.`, stdout: true }); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const actor = await apifyClient.actor(build.actId).get(); |
| 46 | + |
| 47 | + if (!actor) { |
| 48 | + error({ message: `Actor with ID "${build.actId}" was not found.`, stdout: true }); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const existingTaggedBuilds = (actor.taggedBuilds ?? {}) as Record<string, ActorTaggedBuild>; |
| 53 | + const existingTagData = existingTaggedBuilds[tag]; |
| 54 | + |
| 55 | + // Check if the tag exists |
| 56 | + if (!existingTagData) { |
| 57 | + error({ |
| 58 | + message: `Tag "${tag}" does not exist on Actor "${actor.name}".`, |
| 59 | + stdout: true, |
| 60 | + }); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Check if the tag points to this build |
| 65 | + if (existingTagData.buildId !== buildId) { |
| 66 | + error({ |
| 67 | + message: `Tag "${tag}" is not associated with build "${buildId}". It points to build "${existingTagData.buildNumber}" (${existingTagData.buildId}).`, |
| 68 | + stdout: true, |
| 69 | + }); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Confirm removal |
| 74 | + const confirmed = await useYesNoConfirm({ |
| 75 | + message: `Are you sure you want to remove tag "${chalk.yellow(tag)}" from build ${chalk.gray(build.buildNumber)}?`, |
| 76 | + providedConfirmFromStdin: yes || undefined, |
| 77 | + }); |
| 78 | + |
| 79 | + if (!confirmed) { |
| 80 | + info({ |
| 81 | + message: `Tag removal was canceled.`, |
| 82 | + stdout: true, |
| 83 | + }); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + // To remove a tag, set it to null |
| 89 | + await apifyClient.actor(build.actId).update({ |
| 90 | + taggedBuilds: { |
| 91 | + [tag]: null, |
| 92 | + }, |
| 93 | + } as never); |
| 94 | + |
| 95 | + success({ |
| 96 | + message: `Tag "${chalk.yellow(tag)}" removed from build ${chalk.gray(build.buildNumber)} (${chalk.gray(buildId)})`, |
| 97 | + stdout: true, |
| 98 | + }); |
| 99 | + } catch (err) { |
| 100 | + const casted = err as ApifyApiError; |
| 101 | + error({ |
| 102 | + message: `Failed to remove tag "${tag}" from build "${buildId}".\n ${casted.message || casted}`, |
| 103 | + stdout: true, |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments