-
Notifications
You must be signed in to change notification settings - Fork 460
feat(agents): add diff and open commands (3/8) #8282
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
Open
VaibhavAcharya
wants to merge
1
commit into
agent/cli/pr8237-existing-cmds
Choose a base branch
from
agent/cli/pr8237-cmd-diff-open
base: agent/cli/pr8237-existing-cmds
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+705
−0
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import type { OptionValues } from 'commander' | ||
|
|
||
| import { chalk, log, logAndThrowError } from '../../utils/command-helpers.js' | ||
| import { startSpinner, stopSpinner } from '../../lib/spinner.js' | ||
| import type BaseCommand from '../base-command.js' | ||
| import { createAgentsApi, type AgentsApi } from './api.js' | ||
| import { formatDiff } from './utils.js' | ||
|
|
||
| interface AgentDiffOptions extends OptionValues { | ||
| page?: string | ||
| perPage?: string | ||
| session?: string | ||
| cumulative?: boolean | ||
| stripBinary?: boolean | ||
| color?: boolean | ||
| } | ||
|
|
||
| const parsePositiveInt = (input: string | undefined, name: string): number | undefined => { | ||
| if (input === undefined) return undefined | ||
| if (!/^[1-9]\d*$/.test(input)) { | ||
| throw new Error(`--${name} must be a positive integer`) | ||
| } | ||
| return Number.parseInt(input, 10) | ||
| } | ||
|
|
||
| const verifyRunnerExists = async (api: AgentsApi, id: string): Promise<void> => { | ||
| try { | ||
| await api.getAgentRunner(id) | ||
| } catch (error_) { | ||
| const error = error_ as Error & { status?: number } | ||
| if (error.status === 404) { | ||
| throw new Error(`Agent run not found: ${id}`) | ||
| } | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export const agentsDiff = async (id: string, options: AgentDiffOptions, command: BaseCommand) => { | ||
| if (!id) return logAndThrowError('Agent run ID is required') | ||
| await command.authenticate() | ||
| const api = createAgentsApi(command.netlify) | ||
|
|
||
| const useColor = options.color !== false && process.stdout.isTTY | ||
|
|
||
| if (options.session) { | ||
| const kind = options.cumulative ? 'cumulative' : 'result' | ||
| const spinner = startSpinner({ text: `Fetching session ${kind} diff...` }) | ||
| try { | ||
| const diff = options.cumulative | ||
| ? await api.getSessionCumulativeDiff(id, options.session) | ||
| : await api.getSessionResultDiff(id, options.session) | ||
| stopSpinner({ spinner }) | ||
| if (!diff) { | ||
| await verifyRunnerExists(api, id) | ||
| log(chalk.yellow('No diff available for this session.')) | ||
| return | ||
| } | ||
| process.stdout.write(useColor ? formatDiff(diff) : diff) | ||
| if (!diff.endsWith('\n')) process.stdout.write('\n') | ||
| return | ||
| } catch (error_) { | ||
| stopSpinner({ spinner, error: true }) | ||
| const error = error_ as Error | ||
| if (error.message.startsWith('Agent run not found:')) { | ||
| return logAndThrowError(error.message) | ||
| } | ||
| return logAndThrowError(`Failed to fetch diff: ${error.message}`) | ||
| } | ||
| } | ||
|
|
||
| let page: number | undefined | ||
| let perPage: number | undefined | ||
| try { | ||
| page = parsePositiveInt(options.page, 'page') ?? 1 | ||
| perPage = parsePositiveInt(options.perPage, 'per-page') | ||
| } catch (error_) { | ||
| return logAndThrowError((error_ as Error).message) | ||
| } | ||
|
|
||
| const spinner = startSpinner({ text: 'Fetching agent run diff...' }) | ||
| try { | ||
| const result = await api.getAgentRunnerDiff(id, { | ||
| page, | ||
| per_page: perPage, | ||
| strip_binary: options.stripBinary !== false, | ||
| }) | ||
| stopSpinner({ spinner }) | ||
|
|
||
| if (!result.data) { | ||
| await verifyRunnerExists(api, id) | ||
| log(chalk.yellow('No diff available for this agent run.')) | ||
| return | ||
| } | ||
|
|
||
| process.stdout.write(useColor ? formatDiff(result.data) : result.data) | ||
| if (!result.data.endsWith('\n')) process.stdout.write('\n') | ||
|
|
||
| log() | ||
| log(chalk.dim(formatFooter(result.page, result.perPage, result.total, result.hasNext))) | ||
| return result | ||
| } catch (error_) { | ||
| stopSpinner({ spinner, error: true }) | ||
| const error = error_ as Error | ||
| if (error.message.startsWith('Agent run not found:')) { | ||
| return logAndThrowError(error.message) | ||
| } | ||
| return logAndThrowError(`Failed to fetch diff: ${error.message}`) | ||
| } | ||
| } | ||
|
|
||
| const formatFooter = (page: number, perPage: number, total: number | undefined, hasNext: boolean): string => { | ||
| const parts: string[] = [] | ||
| if (total != null) { | ||
| const start = (page - 1) * perPage + 1 | ||
| const end = Math.min(page * perPage, total) | ||
| parts.push(`Showing files ${start.toString()}-${end.toString()} of ${total.toString()}`) | ||
| } else { | ||
| parts.push(`Showing page ${page.toString()}`) | ||
| } | ||
| if (hasNext) { | ||
| parts.push(`Use --page ${(page + 1).toString()} for the next page`) | ||
| } | ||
| return parts.join(' • ') | ||
| } |
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,81 @@ | ||
| import type { OptionValues } from 'commander' | ||
|
|
||
| import { chalk, log, logAndThrowError } from '../../utils/command-helpers.js' | ||
| import { startSpinner, stopSpinner } from '../../lib/spinner.js' | ||
| import openBrowser from '../../utils/open-browser.js' | ||
| import type BaseCommand from '../base-command.js' | ||
| import { createAgentsApi } from './api.js' | ||
| import { buildAgentDashboardUrl } from './utils.js' | ||
|
|
||
| const VALID_TARGETS = ['preview', 'dashboard', 'pr'] as const | ||
| type OpenTarget = (typeof VALID_TARGETS)[number] | ||
|
|
||
| const isOpenTarget = (input: string): input is OpenTarget => (VALID_TARGETS as readonly string[]).includes(input) | ||
|
|
||
| interface AgentOpenOptions extends OptionValues { | ||
| json?: boolean | ||
| } | ||
|
|
||
| export const agentsOpen = async ( | ||
| id: string, | ||
| targetArg: string | undefined, | ||
| _options: AgentOpenOptions, | ||
| command: BaseCommand, | ||
| ) => { | ||
| if (!id) return logAndThrowError('Agent run ID is required') | ||
|
|
||
| const candidate = targetArg ?? 'preview' | ||
| if (!isOpenTarget(candidate)) { | ||
| return logAndThrowError(`Invalid target "${candidate}". Choose one of: ${VALID_TARGETS.join(', ')}`) | ||
| } | ||
| const target: OpenTarget = candidate | ||
|
|
||
| await command.authenticate() | ||
| const { siteInfo } = command.netlify | ||
| const api = createAgentsApi(command.netlify) | ||
| const dashboardUrl = buildAgentDashboardUrl(siteInfo.name, id) | ||
|
|
||
| if (target === 'dashboard') { | ||
| return openUrl(dashboardUrl) | ||
| } | ||
|
|
||
| const spinner = startSpinner({ text: 'Looking up agent run...' }) | ||
| let runner | ||
| try { | ||
| runner = await api.getAgentRunner(id) | ||
| stopSpinner({ spinner }) | ||
| } catch (error_) { | ||
| stopSpinner({ spinner, error: true }) | ||
| const error = error_ as Error & { status?: number } | ||
| if (error.status === 404) return logAndThrowError(`Agent run not found: ${id}`) | ||
| return logAndThrowError(`Failed to fetch agent run: ${error.message}`) | ||
| } | ||
|
|
||
| if (target === 'pr') { | ||
| if (runner.pr_url) return openUrl(runner.pr_url) | ||
| if (runner.pr_is_being_created) { | ||
| log(chalk.yellow('A pull request is being created. Try again in a moment.')) | ||
| return | ||
| } | ||
| if (runner.pr_error) { | ||
| log(chalk.red(`Pull request creation failed: ${runner.pr_error}`)) | ||
| log(`Retry with: ${chalk.cyan(`netlify agents:pr ${id}`)}`) | ||
| return | ||
| } | ||
| log(chalk.yellow('No pull request exists for this agent run.')) | ||
| log(`Create one with: ${chalk.cyan(`netlify agents:pr ${id}`)}`) | ||
| return | ||
| } | ||
|
|
||
| const previewUrl = runner.latest_session_deploy_url | ||
| if (!previewUrl) { | ||
| log(chalk.yellow('No deploy preview available yet — opening dashboard instead.')) | ||
| return openUrl(dashboardUrl) | ||
| } | ||
| return openUrl(previewUrl) | ||
| } | ||
|
|
||
| const openUrl = async (url: string): Promise<void> => { | ||
| log(`Opening ${chalk.blue(url)}`) | ||
| await openBrowser({ url }) | ||
| } | ||
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
Oops, something went wrong.
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.