diff --git a/AGENT.md b/AGENT.md index d1f98a2..b580279 100644 --- a/AGENT.md +++ b/AGENT.md @@ -127,12 +127,17 @@ openhack-cli --json projects create --name "My App" [--slug my-app] [--org ] [--limit 20] [--offset 0] openhack-cli --json scans get [--project ] [--findings/--no-findings] openhack-cli --json scans trigger-full [] [--branch main] +openhack-cli --json scans cancel [--project ] [--yes] ``` `scans list --json` returns `{ "scans": [...], "consolidatedCounts": {...}, "pagination": {...} }`; each scan carries `scanStatus`, per-severity counts, and a `findings` array. `scans get` includes the full per-finding breakdown. +`scans cancel` stops a pending or running scan. Interactive output asks for +confirmation unless `--yes` is passed; `--json` never prompts, so automation +cannot hang waiting for hidden input. A completed scan cannot be cancelled. + --- ## 7. Project vulnerabilities (`vulns`) diff --git a/README.md b/README.md index 531ea6c..7f85613 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ openhack-cli auth logout # remove local credentials | `auth` | `login`, `logout`, `status`/`whoami`, `token` | Manage credentials | | `orgs` | `list`, `use ` | List / select organizations | | `projects` | `list`, `get`, `use`, `create` | Manage projects | -| `scans` | `list`, `get `, `trigger-full` | View / trigger scans | +| `scans` | `list`, `get `, `trigger-full`, `cancel ` | View, trigger, and cancel scans | | `vulns` | `list`, `groups`, `report`, `get`, `edit` | View, report, and edit project vulnerabilities | | `pentest` | `list`, `get`, `create` | Pentesting engagements | | `pentest findings` | `[engagement]` | Findings in an engagement (defaults to latest) | diff --git a/openhack_cli/commands/scans.py b/openhack_cli/commands/scans.py index 85e2003..c2543d9 100644 --- a/openhack_cli/commands/scans.py +++ b/openhack_cli/commands/scans.py @@ -123,6 +123,45 @@ def render(payload): output.emit(ctx.obj, data, render) +@scans.command(name="cancel") +@click.argument("scan_id") +@click.option("--project", default=None, help="Project id (defaults to active).") +@click.option("--yes", "-y", is_flag=True, + help="Skip the confirmation prompt.") +@click.pass_context +def cancel_scan(ctx: click.Context, scan_id: str, project: str | None, + yes: bool) -> None: + """Cancel a running or pending scan. + + Stops the underlying workflow and marks the scan cancelled. Scans that + already finished cannot be cancelled. + """ + project_id = resolve_project_id(ctx, project) + + # Prompt on an interactive run, but never when scripting: --json is the + # machine-readable mode and a hidden prompt would hang it. + if not yes and not ctx.obj.get("json"): + click.confirm(f"Cancel scan {scan_id}?", abort=True) + + client = get_client(ctx) + data = client.post(f"/api/projects/{project_id}/scans/{scan_id}/cancel", json={}) + + def render(payload): + scan = payload.get("scan", {}) if isinstance(payload, dict) else {} + # The workflow may already have been gone — the scan is still marked + # cancelled, so report which actually happened rather than implying we + # stopped something that was still running. + if payload.get("workflow_cancelled") is False: + output.success("Scan marked cancelled (it was no longer running).") + else: + output.success("Scan cancelled.") + if scan.get("number") is not None: + output.console.print(f" Scan: #{scan['number']}") + output.console.print(f" Status: {scan.get('scanStatus', 'cancelled')}") + + output.emit(ctx.obj, data, render) + + @scans.command(name="trigger-full") @click.argument("project", required=False) @click.option("--branch", default=None, diff --git a/openhack_cli/output.py b/openhack_cli/output.py index 1c473ac..67e3300 100644 --- a/openhack_cli/output.py +++ b/openhack_cli/output.py @@ -36,7 +36,9 @@ "pending": "yellow", "draft": "dim", "failed": "red", - "cancelled": "red", + # A deliberate cancel is not a failure — the dashboard renders it muted, so + # don't colour it like an error here either. + "cancelled": "dim", }