-
Notifications
You must be signed in to change notification settings - Fork 39
feat(builds): adds build tag commands #1004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8ace241
feat(builds): adds build tag commands
l2ysho eec0e5a
refactor(builds): changes error to warning
l2ysho cf3abe4
feat(docs): enable tags in docs
l2ysho 2c11ea7
chore(test): fix test
l2ysho de3d71d
refactor(tests): refactor test assertions
l2ysho 5711bc2
Refactor(test): remove .only
l2ysho 530cf0e
refactor: fix failing test
l2ysho 6ba8e47
Merge branch 'master' into 997-add-command-to-enable-setting-build-tags
l2ysho 3e18aab
chore: updates apify-client to v2.22.0
l2ysho a9dd9ec
Update package.json
vladfrangu 1090773
chore: yarn lock
vladfrangu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import type { ActorTaggedBuild, ApifyApiError } from 'apify-client'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| import { ApifyCommand } from '../../lib/command-framework/apify-command.js'; | ||
| import { Flags } from '../../lib/command-framework/flags.js'; | ||
| import { error, success, warning } from '../../lib/outputs.js'; | ||
| import { getLoggedClientOrThrow } from '../../lib/utils.js'; | ||
|
|
||
| export class BuildsAddTagCommand extends ApifyCommand<typeof BuildsAddTagCommand> { | ||
| static override name = 'add-tag' as const; | ||
|
|
||
| static override description = 'Adds a tag to a specific Actor build.'; | ||
|
|
||
| static override flags = { | ||
| build: Flags.string({ | ||
| char: 'b', | ||
| description: 'The build ID to tag.', | ||
| required: true, | ||
| }), | ||
| tag: Flags.string({ | ||
| char: 't', | ||
| description: 'The tag to add to the build.', | ||
| required: true, | ||
| }), | ||
| }; | ||
|
|
||
| async run() { | ||
| const { build: buildId, tag } = this.flags; | ||
|
|
||
| const apifyClient = await getLoggedClientOrThrow(); | ||
|
|
||
| const build = await apifyClient.build(buildId).get(); | ||
|
|
||
| if (!build) { | ||
| error({ message: `Build with ID "${buildId}" was not found on your account.`, stdout: true }); | ||
| return; | ||
| } | ||
|
|
||
| if (build.status !== 'SUCCEEDED') { | ||
| error({ | ||
| message: `Build with ID "${buildId}" has status "${build.status}". Only successful builds can be tagged.`, | ||
| stdout: true, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const actor = await apifyClient.actor(build.actId).get(); | ||
|
|
||
| if (!actor) { | ||
| error({ message: `Actor with ID "${build.actId}" was not found.`, stdout: true }); | ||
| return; | ||
| } | ||
|
|
||
| // Check if this tag already points to the same build | ||
| const existingTaggedBuilds = (actor.taggedBuilds ?? {}) as Record<string, ActorTaggedBuild>; | ||
| const existingTagData = existingTaggedBuilds[tag]; | ||
|
|
||
| if (existingTagData?.buildId === buildId) { | ||
| warning({ | ||
| message: `Build "${buildId}" is already tagged as "${tag}".`, | ||
| stdout: true, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // Update only the specific tag | ||
| await apifyClient.actor(build.actId).update({ | ||
| taggedBuilds: { | ||
| [tag]: { | ||
| buildId: build.id, | ||
| }, | ||
| }, | ||
| } as never); | ||
|
|
||
| const previousBuildInfo = existingTagData?.buildNumber | ||
| ? ` (previously pointed to build ${chalk.gray(existingTagData.buildNumber)})` | ||
| : ''; | ||
|
|
||
| success({ | ||
| message: `Tag "${chalk.yellow(tag)}" added to build ${chalk.gray(build.buildNumber)} (${chalk.gray(buildId)})${previousBuildInfo}`, | ||
| stdout: true, | ||
| }); | ||
| } catch (err) { | ||
| const casted = err as ApifyApiError; | ||
| error({ | ||
| message: `Failed to add tag "${tag}" to build "${buildId}".\n ${casted.message || casted}`, | ||
| stdout: true, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import type { ActorTaggedBuild, ApifyApiError } from 'apify-client'; | ||
| import chalk from 'chalk'; | ||
|
|
||
| import { ApifyCommand } from '../../lib/command-framework/apify-command.js'; | ||
| import { Flags } from '../../lib/command-framework/flags.js'; | ||
| import { useYesNoConfirm } from '../../lib/hooks/user-confirmations/useYesNoConfirm.js'; | ||
| import { error, info, success } from '../../lib/outputs.js'; | ||
| import { getLoggedClientOrThrow } from '../../lib/utils.js'; | ||
|
|
||
| export class BuildsRemoveTagCommand extends ApifyCommand<typeof BuildsRemoveTagCommand> { | ||
| static override name = 'remove-tag' as const; | ||
|
|
||
| static override description = 'Removes a tag from a specific Actor build.'; | ||
|
|
||
| static override flags = { | ||
| build: Flags.string({ | ||
| char: 'b', | ||
| description: 'The build ID to remove the tag from.', | ||
| required: true, | ||
| }), | ||
| tag: Flags.string({ | ||
| char: 't', | ||
| description: 'The tag to remove from the build.', | ||
| required: true, | ||
| }), | ||
| yes: Flags.boolean({ | ||
| char: 'y', | ||
| description: 'Automatic yes to prompts; assume "yes" as answer to all prompts.', | ||
| default: false, | ||
| }), | ||
| }; | ||
|
|
||
| async run() { | ||
| const { build: buildId, tag, yes } = this.flags; | ||
|
|
||
| const apifyClient = await getLoggedClientOrThrow(); | ||
|
|
||
| const build = await apifyClient.build(buildId).get(); | ||
|
|
||
| if (!build) { | ||
| error({ message: `Build with ID "${buildId}" was not found on your account.`, stdout: true }); | ||
| return; | ||
| } | ||
|
|
||
| const actor = await apifyClient.actor(build.actId).get(); | ||
|
|
||
| if (!actor) { | ||
| error({ message: `Actor with ID "${build.actId}" was not found.`, stdout: true }); | ||
| return; | ||
| } | ||
|
|
||
| const existingTaggedBuilds = (actor.taggedBuilds ?? {}) as Record<string, ActorTaggedBuild>; | ||
| const existingTagData = existingTaggedBuilds[tag]; | ||
|
|
||
| // Check if the tag exists | ||
| if (!existingTagData) { | ||
| error({ | ||
| message: `Tag "${tag}" does not exist on Actor "${actor.name}".`, | ||
| stdout: true, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // Check if the tag points to this build | ||
| if (existingTagData.buildId !== buildId) { | ||
| error({ | ||
| message: `Tag "${tag}" is not associated with build "${buildId}". It points to build "${existingTagData.buildNumber}" (${existingTagData.buildId}).`, | ||
| stdout: true, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| // Confirm removal | ||
| const confirmed = await useYesNoConfirm({ | ||
| message: `Are you sure you want to remove tag "${chalk.yellow(tag)}" from build ${chalk.gray(build.buildNumber)}?`, | ||
| providedConfirmFromStdin: yes || undefined, | ||
| }); | ||
|
|
||
| if (!confirmed) { | ||
| info({ | ||
| message: `Tag removal was canceled.`, | ||
| stdout: true, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| // To remove a tag, set it to null | ||
| await apifyClient.actor(build.actId).update({ | ||
| taggedBuilds: { | ||
| [tag]: null, | ||
| }, | ||
| } as never); | ||
|
|
||
| success({ | ||
| message: `Tag "${chalk.yellow(tag)}" removed from build ${chalk.gray(build.buildNumber)} (${chalk.gray(buildId)})`, | ||
| stdout: true, | ||
| }); | ||
| } catch (err) { | ||
| const casted = err as ApifyApiError; | ||
| error({ | ||
| message: `Failed to remove tag "${tag}" from build "${buildId}".\n ${casted.message || casted}`, | ||
| stdout: true, | ||
| }); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.