| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {ReactNode} from 'react'; |
| b69ab31 | | | 9 | import type {Operation} from './operations/Operation'; |
| b69ab31 | | | 10 | import type {ValidatedRepoInfo} from './types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {Banner, BannerKind} from 'isl-components/Banner'; |
| b69ab31 | | | 13 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 14 | import {Column, Row} from 'isl-components/Flex'; |
| b69ab31 | | | 15 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 16 | import {Subtle} from 'isl-components/Subtle'; |
| b69ab31 | | | 17 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 18 | import {atom, useAtom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 19 | import {notEmpty, truncate} from 'shared/utils'; |
| b69ab31 | | | 20 | import {Delayed} from './Delayed'; |
| b69ab31 | | | 21 | import {LogRenderExposures} from './analytics/LogRenderExposures'; |
| b69ab31 | | | 22 | import {codeReviewProvider} from './codeReview/CodeReviewInfo'; |
| b69ab31 | | | 23 | import {T, t} from './i18n'; |
| b69ab31 | | | 24 | import { |
| b69ab31 | | | 25 | EXIT_CODE_FORGET, |
| b69ab31 | | | 26 | operationList, |
| b69ab31 | | | 27 | queuedOperations, |
| b69ab31 | | | 28 | queuedOperationsErrorAtom, |
| b69ab31 | | | 29 | useAbortRunningOperation, |
| b69ab31 | | | 30 | } from './operationsState'; |
| b69ab31 | | | 31 | import {repositoryInfo} from './serverAPIState'; |
| b69ab31 | | | 32 | import {processTerminalLines} from './terminalOutput'; |
| b69ab31 | | | 33 | import {CommandRunner} from './types'; |
| b69ab31 | | | 34 | import {short} from './utils'; |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | import './CommandHistoryAndProgress.css'; |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | function OperationDescription(props: { |
| b69ab31 | | | 39 | info: ValidatedRepoInfo; |
| b69ab31 | | | 40 | operation: Operation; |
| b69ab31 | | | 41 | className?: string; |
| b69ab31 | | | 42 | long?: boolean; |
| b69ab31 | | | 43 | }): React.ReactElement { |
| b69ab31 | | | 44 | const {info, operation, className} = props; |
| b69ab31 | | | 45 | const desc = operation.getDescriptionForDisplay(); |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | const reviewProvider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | if (desc?.description) { |
| b69ab31 | | | 50 | return <span className={className}>{desc.description}</span>; |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const commandName = |
| b69ab31 | | | 54 | operation.runner === CommandRunner.Sapling |
| b69ab31 | | | 55 | ? (/[^\\/]+$/.exec(info.command)?.[0] ?? 'sl') |
| b69ab31 | | | 56 | : operation.runner === CommandRunner.CodeReviewProvider |
| b69ab31 | | | 57 | ? reviewProvider?.cliName |
| b69ab31 | | | 58 | : operation.runner === CommandRunner.InternalArcanist |
| b69ab31 | | | 59 | ? CommandRunner.InternalArcanist |
| b69ab31 | | | 60 | : null; |
| b69ab31 | | | 61 | return ( |
| b69ab31 | | | 62 | <code className={className}> |
| b69ab31 | | | 63 | {(commandName ?? '') + |
| b69ab31 | | | 64 | ' ' + |
| b69ab31 | | | 65 | operation |
| b69ab31 | | | 66 | .getArgs() |
| b69ab31 | | | 67 | .map(arg => { |
| b69ab31 | | | 68 | if (typeof arg === 'object') { |
| b69ab31 | | | 69 | switch (arg.type) { |
| b69ab31 | | | 70 | case 'config': |
| b69ab31 | | | 71 | // don't show configs in the UI |
| b69ab31 | | | 72 | return undefined; |
| b69ab31 | | | 73 | case 'repo-relative-file': |
| b69ab31 | | | 74 | return arg.path; |
| b69ab31 | | | 75 | case 'repo-relative-file-list': |
| b69ab31 | | | 76 | return truncate(arg.paths.join(' '), 200); |
| b69ab31 | | | 77 | case 'exact-revset': |
| b69ab31 | | | 78 | case 'succeedable-revset': |
| b69ab31 | | | 79 | case 'optimistic-revset': |
| b69ab31 | | | 80 | return props.long |
| b69ab31 | | | 81 | ? arg.revset |
| b69ab31 | | | 82 | : // truncate full commit hashes to short representation visually |
| b69ab31 | | | 83 | // revset could also be a remote bookmark, so only do this if it looks like a hash |
| b69ab31 | | | 84 | /^[a-z0-9]{40}$/.test(arg.revset) |
| b69ab31 | | | 85 | ? short(arg.revset) |
| b69ab31 | | | 86 | : truncate(arg.revset, 80); |
| b69ab31 | | | 87 | } |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | if (/\s/.test(arg)) { |
| b69ab31 | | | 90 | return `"${props.long ? arg : truncate(arg, 30)}"`; |
| b69ab31 | | | 91 | } |
| b69ab31 | | | 92 | return arg; |
| b69ab31 | | | 93 | }) |
| b69ab31 | | | 94 | .filter(notEmpty) |
| b69ab31 | | | 95 | .join(' ')} |
| b69ab31 | | | 96 | </code> |
| b69ab31 | | | 97 | ); |
| b69ab31 | | | 98 | } |
| b69ab31 | | | 99 | |
| b69ab31 | | | 100 | const nextToRunCollapsedAtom = atom(false); |
| b69ab31 | | | 101 | const queueErrorCollapsedAtom = atom(true); |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | export function CommandHistoryAndProgress() { |
| b69ab31 | | | 104 | const list = useAtomValue(operationList); |
| b69ab31 | | | 105 | const queued = useAtomValue(queuedOperations); |
| b69ab31 | | | 106 | const [queuedError, setQueuedError] = useAtom(queuedOperationsErrorAtom); |
| b69ab31 | | | 107 | const abortRunningOperation = useAbortRunningOperation(); |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | const [collapsed, setCollapsed] = useAtom(nextToRunCollapsedAtom); |
| b69ab31 | | | 110 | const [errorCollapsed, setErrorCollapsed] = useAtom(queueErrorCollapsedAtom); |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | const info = useAtomValue(repositoryInfo); |
| b69ab31 | | | 113 | if (!info) { |
| b69ab31 | | | 114 | return null; |
| b69ab31 | | | 115 | } |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | const progress = list.currentOperation; |
| b69ab31 | | | 118 | if (progress == null) { |
| b69ab31 | | | 119 | return null; |
| b69ab31 | | | 120 | } |
| b69ab31 | | | 121 | |
| b69ab31 | | | 122 | const desc = progress.operation.getDescriptionForDisplay(); |
| b69ab31 | | | 123 | const command = ( |
| b69ab31 | | | 124 | <OperationDescription |
| b69ab31 | | | 125 | info={info} |
| b69ab31 | | | 126 | operation={progress.operation} |
| b69ab31 | | | 127 | className="progress-container-command" |
| b69ab31 | | | 128 | /> |
| b69ab31 | | | 129 | ); |
| b69ab31 | | | 130 | |
| b69ab31 | | | 131 | let label; |
| b69ab31 | | | 132 | let icon; |
| b69ab31 | | | 133 | let abort = null; |
| b69ab31 | | | 134 | let showLastLineOfOutput = false; |
| b69ab31 | | | 135 | if (progress.exitCode == null) { |
| b69ab31 | | | 136 | label = desc?.description ? command : <T replace={{$command: command}}>Running $command</T>; |
| b69ab31 | | | 137 | icon = <Icon icon="loading" />; |
| b69ab31 | | | 138 | showLastLineOfOutput = desc?.tooltip == null; |
| b69ab31 | | | 139 | // Only show "Abort" for slow commands, since "Abort" might leave modified |
| b69ab31 | | | 140 | // files or pending commits around. |
| b69ab31 | | | 141 | const slowThreshold = 10000; |
| b69ab31 | | | 142 | const hideUntil = new Date((progress.startTime?.getTime() || 0) + slowThreshold); |
| b69ab31 | | | 143 | abort = ( |
| b69ab31 | | | 144 | <Delayed hideUntil={hideUntil}> |
| b69ab31 | | | 145 | <Button |
| b69ab31 | | | 146 | data-testid="abort-button" |
| b69ab31 | | | 147 | disabled={progress.aborting} |
| b69ab31 | | | 148 | onClick={() => { |
| b69ab31 | | | 149 | abortRunningOperation(progress.operation.id); |
| b69ab31 | | | 150 | }}> |
| b69ab31 | | | 151 | <Icon slot="start" icon={progress.aborting ? 'loading' : 'stop-circle'} /> |
| b69ab31 | | | 152 | <T>Abort</T> |
| b69ab31 | | | 153 | </Button> |
| b69ab31 | | | 154 | </Delayed> |
| b69ab31 | | | 155 | ); |
| b69ab31 | | | 156 | } else if (progress.exitCode === 0) { |
| b69ab31 | | | 157 | label = <span>{command}</span>; |
| b69ab31 | | | 158 | icon = <Icon icon="pass" aria-label={t('Command exited successfully')} />; |
| b69ab31 | | | 159 | } else if (progress.aborting) { |
| b69ab31 | | | 160 | // Exited (tested above) by abort. |
| b69ab31 | | | 161 | label = <T replace={{$command: command}}>Aborted $command</T>; |
| b69ab31 | | | 162 | icon = <Icon icon="stop-circle" aria-label={t('Command aborted')} />; |
| b69ab31 | | | 163 | } else if (progress.exitCode === EXIT_CODE_FORGET) { |
| b69ab31 | | | 164 | label = <span>{command}</span>; |
| b69ab31 | | | 165 | icon = ( |
| b69ab31 | | | 166 | <Icon |
| b69ab31 | | | 167 | icon="question" |
| b69ab31 | | | 168 | aria-label={t('Command ran during disconnection. Exit status is lost.')} |
| b69ab31 | | | 169 | /> |
| b69ab31 | | | 170 | ); |
| b69ab31 | | | 171 | } else { |
| b69ab31 | | | 172 | label = <span>{command}</span>; |
| b69ab31 | | | 173 | icon = <Icon icon="error" aria-label={t('Command exited unsuccessfully')} />; |
| b69ab31 | | | 174 | showLastLineOfOutput = true; |
| b69ab31 | | | 175 | } |
| b69ab31 | | | 176 | |
| b69ab31 | | | 177 | let processedLines = processTerminalLines(progress.commandOutput ?? []); |
| b69ab31 | | | 178 | if (desc?.tooltip != null) { |
| b69ab31 | | | 179 | // Output might contain a JSON string not suitable for human reading. |
| b69ab31 | | | 180 | // Filter the line out. |
| b69ab31 | | | 181 | processedLines = processedLines.filter(line => !line.startsWith('{')); |
| b69ab31 | | | 182 | } |
| b69ab31 | | | 183 | |
| b69ab31 | | | 184 | return ( |
| b69ab31 | | | 185 | <div className="progress-container" data-testid="progress-container"> |
| b69ab31 | | | 186 | {queuedError != null || queued.length > 0 ? ( |
| b69ab31 | | | 187 | <div className="queued-operations-container" data-testid="queued-commands"> |
| b69ab31 | | | 188 | {queuedError != null && ( |
| b69ab31 | | | 189 | <LogRenderExposures eventName="QueueCancelledWarningShown"> |
| b69ab31 | | | 190 | <Column alignStart data-testid="cancelled-queued-commands"> |
| b69ab31 | | | 191 | <Tooltip |
| b69ab31 | | | 192 | title={t( |
| b69ab31 | | | 193 | 'When an operation process fails or is aborted, any operations queued after that are cancelled, as they may depend on the previous operation succeeding.', |
| b69ab31 | | | 194 | )}> |
| b69ab31 | | | 195 | <Row |
| b69ab31 | | | 196 | style={{cursor: 'pointer'}} |
| b69ab31 | | | 197 | onClick={() => { |
| b69ab31 | | | 198 | setErrorCollapsed(!errorCollapsed); |
| b69ab31 | | | 199 | }}> |
| b69ab31 | | | 200 | <Icon icon={errorCollapsed ? 'chevron-right' : 'chevron-down'} /> |
| b69ab31 | | | 201 | <Banner kind={BannerKind.warning}> |
| b69ab31 | | | 202 | <Icon icon="warning" color="yellow" /> |
| b69ab31 | | | 203 | <T count={queuedError.operations.length}>queuedOperationsWereCancelled</T> |
| b69ab31 | | | 204 | </Banner> |
| b69ab31 | | | 205 | <Tooltip title={t('Dismiss')}> |
| b69ab31 | | | 206 | <Button |
| b69ab31 | | | 207 | icon |
| b69ab31 | | | 208 | onClick={() => { |
| b69ab31 | | | 209 | setQueuedError(undefined); |
| b69ab31 | | | 210 | }}> |
| b69ab31 | | | 211 | <Icon icon="x" /> |
| b69ab31 | | | 212 | </Button> |
| b69ab31 | | | 213 | </Tooltip> |
| b69ab31 | | | 214 | </Row> |
| b69ab31 | | | 215 | </Tooltip> |
| b69ab31 | | | 216 | {errorCollapsed ? null : ( |
| b69ab31 | | | 217 | <TruncatedOperationList operations={queuedError.operations} info={info} /> |
| b69ab31 | | | 218 | )} |
| b69ab31 | | | 219 | </Column> |
| b69ab31 | | | 220 | </LogRenderExposures> |
| b69ab31 | | | 221 | )} |
| b69ab31 | | | 222 | {queued.length > 0 ? ( |
| b69ab31 | | | 223 | <> |
| b69ab31 | | | 224 | <Row |
| b69ab31 | | | 225 | style={{cursor: 'pointer'}} |
| b69ab31 | | | 226 | onClick={() => { |
| b69ab31 | | | 227 | setCollapsed(!collapsed); |
| b69ab31 | | | 228 | }}> |
| b69ab31 | | | 229 | <Icon icon={collapsed ? 'chevron-right' : 'chevron-down'} /> |
| b69ab31 | | | 230 | <strong> |
| b69ab31 | | | 231 | <T>Next to run</T> |
| b69ab31 | | | 232 | </strong> |
| b69ab31 | | | 233 | </Row> |
| b69ab31 | | | 234 | {collapsed ? ( |
| b69ab31 | | | 235 | <div> |
| b69ab31 | | | 236 | <T count={queued.length}>moreCommandsToRun</T> |
| b69ab31 | | | 237 | </div> |
| b69ab31 | | | 238 | ) : ( |
| b69ab31 | | | 239 | <TruncatedOperationList operations={queued} info={info} /> |
| b69ab31 | | | 240 | )} |
| b69ab31 | | | 241 | </> |
| b69ab31 | | | 242 | ) : null} |
| b69ab31 | | | 243 | </div> |
| b69ab31 | | | 244 | ) : null} |
| b69ab31 | | | 245 | |
| b69ab31 | | | 246 | <Tooltip |
| b69ab31 | | | 247 | component={() => ( |
| b69ab31 | | | 248 | <div className="progress-command-tooltip"> |
| b69ab31 | | | 249 | {desc?.tooltip || ( |
| b69ab31 | | | 250 | <> |
| b69ab31 | | | 251 | <div className="progress-command-tooltip-command"> |
| b69ab31 | | | 252 | <strong>Command: </strong> |
| b69ab31 | | | 253 | <OperationDescription info={info} operation={progress.operation} long /> |
| b69ab31 | | | 254 | </div> |
| b69ab31 | | | 255 | </> |
| b69ab31 | | | 256 | )} |
| b69ab31 | | | 257 | <br /> |
| b69ab31 | | | 258 | <b>Command output:</b> |
| b69ab31 | | | 259 | <br /> |
| b69ab31 | | | 260 | {processedLines.length === 0 ? ( |
| b69ab31 | | | 261 | <Subtle> |
| b69ab31 | | | 262 | <T>No output</T> |
| b69ab31 | | | 263 | </Subtle> |
| b69ab31 | | | 264 | ) : ( |
| b69ab31 | | | 265 | <pre> |
| b69ab31 | | | 266 | {processedLines.map((line, i) => ( |
| b69ab31 | | | 267 | <div key={i}>{line}</div> |
| b69ab31 | | | 268 | ))} |
| b69ab31 | | | 269 | </pre> |
| b69ab31 | | | 270 | )} |
| b69ab31 | | | 271 | </div> |
| b69ab31 | | | 272 | )} |
| b69ab31 | | | 273 | interactive> |
| b69ab31 | | | 274 | <div className="progress-container-row"> |
| b69ab31 | | | 275 | {icon} |
| b69ab31 | | | 276 | {label} |
| b69ab31 | | | 277 | {progress.warnings?.map(warning => ( |
| b69ab31 | | | 278 | <Banner |
| b69ab31 | | | 279 | icon={<Icon icon="warning" color="yellow" />} |
| b69ab31 | | | 280 | alwaysShowButtons |
| b69ab31 | | | 281 | kind={BannerKind.warning}> |
| b69ab31 | | | 282 | <T replace={{$provider: warning}}>$provider</T> |
| b69ab31 | | | 283 | </Banner> |
| b69ab31 | | | 284 | ))} |
| b69ab31 | | | 285 | </div> |
| b69ab31 | | | 286 | {showLastLineOfOutput ? ( |
| b69ab31 | | | 287 | <div className="progress-container-row"> |
| b69ab31 | | | 288 | <div className="progress-container-last-output"> |
| b69ab31 | | | 289 | {progress.currentProgress != null && progress.currentProgress.unit != null ? ( |
| b69ab31 | | | 290 | <ProgressLine |
| b69ab31 | | | 291 | progress={progress.currentProgress.progress} |
| b69ab31 | | | 292 | progressTotal={progress.currentProgress.progressTotal}> |
| b69ab31 | | | 293 | {progress.currentProgress.message + |
| b69ab31 | | | 294 | ` - ${progress.currentProgress.progress}/${progress.currentProgress.progressTotal} ${progress.currentProgress.unit}`} |
| b69ab31 | | | 295 | </ProgressLine> |
| b69ab31 | | | 296 | ) : ( |
| b69ab31 | | | 297 | processedLines.length > 0 && <ProgressLine>{processedLines.at(-1)}</ProgressLine> |
| b69ab31 | | | 298 | )} |
| b69ab31 | | | 299 | </div> |
| b69ab31 | | | 300 | </div> |
| b69ab31 | | | 301 | ) : null} |
| b69ab31 | | | 302 | {abort} |
| b69ab31 | | | 303 | </Tooltip> |
| b69ab31 | | | 304 | </div> |
| b69ab31 | | | 305 | ); |
| b69ab31 | | | 306 | } |
| b69ab31 | | | 307 | |
| b69ab31 | | | 308 | const MAX_VISIBLE_NEXT_TO_RUN = 10; |
| b69ab31 | | | 309 | function TruncatedOperationList({ |
| b69ab31 | | | 310 | info, |
| b69ab31 | | | 311 | operations, |
| b69ab31 | | | 312 | }: { |
| b69ab31 | | | 313 | info: ValidatedRepoInfo; |
| b69ab31 | | | 314 | operations: Array<Operation>; |
| b69ab31 | | | 315 | }) { |
| b69ab31 | | | 316 | return ( |
| b69ab31 | | | 317 | <> |
| b69ab31 | | | 318 | {(operations.length > MAX_VISIBLE_NEXT_TO_RUN |
| b69ab31 | | | 319 | ? operations.slice(0, MAX_VISIBLE_NEXT_TO_RUN) |
| b69ab31 | | | 320 | : operations |
| b69ab31 | | | 321 | ).map(op => ( |
| b69ab31 | | | 322 | <div key={op.id} id={op.id} className="queued-operation"> |
| b69ab31 | | | 323 | <OperationDescription info={info} operation={op} /> |
| b69ab31 | | | 324 | </div> |
| b69ab31 | | | 325 | ))} |
| b69ab31 | | | 326 | {operations.length > MAX_VISIBLE_NEXT_TO_RUN && ( |
| b69ab31 | | | 327 | <div> |
| b69ab31 | | | 328 | <T replace={{$count: operations.length - MAX_VISIBLE_NEXT_TO_RUN}}>+$count more</T> |
| b69ab31 | | | 329 | </div> |
| b69ab31 | | | 330 | )} |
| b69ab31 | | | 331 | </> |
| b69ab31 | | | 332 | ); |
| b69ab31 | | | 333 | } |
| b69ab31 | | | 334 | |
| b69ab31 | | | 335 | function ProgressLine({ |
| b69ab31 | | | 336 | children, |
| b69ab31 | | | 337 | progress, |
| b69ab31 | | | 338 | progressTotal, |
| b69ab31 | | | 339 | }: { |
| b69ab31 | | | 340 | children: ReactNode; |
| b69ab31 | | | 341 | progress?: number; |
| b69ab31 | | | 342 | progressTotal?: number; |
| b69ab31 | | | 343 | }) { |
| b69ab31 | | | 344 | return ( |
| b69ab31 | | | 345 | <span className="progress-line"> |
| b69ab31 | | | 346 | {progress != null && progressTotal != null ? ( |
| b69ab31 | | | 347 | <ProgressBar progress={progress} progressTotal={progressTotal} /> |
| b69ab31 | | | 348 | ) : null} |
| b69ab31 | | | 349 | <code>{children}</code> |
| b69ab31 | | | 350 | </span> |
| b69ab31 | | | 351 | ); |
| b69ab31 | | | 352 | } |
| b69ab31 | | | 353 | |
| b69ab31 | | | 354 | function ProgressBar({progress, progressTotal}: {progress: number; progressTotal: number}) { |
| b69ab31 | | | 355 | const pct = progress / progressTotal; |
| b69ab31 | | | 356 | return ( |
| b69ab31 | | | 357 | <span className="progress-bar"> |
| b69ab31 | | | 358 | <span className="progress-bar-filled" style={{width: `${Math.round(100 * pct)}%`}} /> |
| b69ab31 | | | 359 | </span> |
| b69ab31 | | | 360 | ); |
| b69ab31 | | | 361 | } |