diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 7ea643e8..fcad9592 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -8,7 +8,6 @@ # Workflow files .github/ @utkarsh232005 -.github/workflows/changelog.yml @Yuvraj-Sarathe # Core source src/ @utkarsh232005 @@ -16,6 +15,4 @@ src/ @utkarsh232005 # Exclude dependency manifests from code ownership so Dependabot updates can auto-merge without code owner review package.json package-lock.json -.github/scripts/package.json -.github/scripts/package-lock.json diff --git a/.github/dependabot.yml b/.github/dependabot.yml index c4e3a7a2..d3450fb5 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -10,17 +10,6 @@ updates: - "dependencies" - "javascript" - # Maintain dependencies for GitHub Actions scripts - - package-ecosystem: "npm" - directory: "/.github/scripts" - schedule: - interval: "daily" - open-pull-requests-limit: 10 - labels: - - "dependencies" - - "javascript" - - "github-actions" - # Maintain dependencies for GitHub Actions workflows - package-ecosystem: "github-actions" directory: "/" diff --git a/.github/docs-template/README.md b/.github/docs-template/README.md deleted file mode 100644 index e69de29b..00000000 diff --git a/.github/docs-template/config/README.md b/.github/docs-template/config/README.md deleted file mode 100644 index 18d5ab9f..00000000 --- a/.github/docs-template/config/README.md +++ /dev/null @@ -1,134 +0,0 @@ -# `kdm config` — Configuration Management - -**Version:** v1.2.1 - -## Overview - -The `kdm config` command group manages KDM CLI configuration, including notification service setup, custom settings, and credential management. Configuration is stored locally using the `conf` package. - -## Syntax - -```bash -kdm config -``` - -## Sub-commands - -### `kdm config setup` - -Interactively sets up the notification service (Discord webhook or Email SMTP). - -**Example:** - -```bash -kdm config setup -``` - -**Interactive Flow:** - -1. Select a notification service: - - **Discord** — Send alerts to a Discord channel via webhook. - - **Email (SMTP)** — Send alerts via email SMTP. - - **None** — Disable notifications. - -2. For **Discord**, you will be prompted for a webhook URL. -3. For **Email**, you will be prompted for SMTP host, port, user, and recipient email. - -**Discord Webhook Setup:** - -1. Open your Discord server settings. -2. Go to **Integrations > Webhooks**. -3. Create a new webhook and choose the alert channel. -4. Copy the webhook URL (must start with `https://discord.com/api/webhooks/`). - -**Email SMTP Setup:** - -| Setting | Default | Description | -|---------|---------|-------------| -| Host | `smtp.gmail.com` | SMTP server address | -| Port | `587` | SMTP port (STARTTLS) | -| User | — | Your email address | -| To | — | Alert recipient email | - -> **Note:** SMTP passwords must be set via the `KDM_SMTP_PASSWORD` environment variable. - ---- - -### `kdm config set ` - -Sets a specific configuration value. - -**Example:** - -```bash -kdm config set alert_cooldown 300 -kdm config set email_port 465 -``` - -**Supported Keys:** - -| Key | Type | Description | -|-----|------|-------------| -| `alert_cooldown` | number | Cooldown period between alerts (seconds) | -| `email_port` | number | SMTP port | -| `notification_service` | string | `discord`, `email`, or `none` | -| `discord_webhook` | string | Discord webhook URL | -| `email_host` | string | SMTP host | -| `email_user` | string | SMTP username | -| `email_to` | string | Alert recipient email | - ---- - -### `kdm config list` - -Lists all current configuration values. - -**Example:** - -```bash -kdm config list -``` - -**Expected Output:** - -``` -Current KDM Configuration: -────────────────────────────────────────────────── - notification_service : discord - discord_webhook : https://discord.com/api/webhooks/... -────────────────────────────────────────────────── - - Note: SMTP passwords must be set via KDM_SMTP_PASSWORD env var. -``` - ---- - -### `kdm config clear` - -Clears all configuration and resets to defaults. - -**Example:** - -```bash -kdm config clear -``` - -**Expected Output:** - -``` -✓ Configuration cleared. -``` - -## Configuration File Location - -Configuration is stored in the default `conf` package location: - -- **macOS:** `~/Library/Application Support/kdm-cli` -- **Linux:** `~/.config/kdm-cli` -- **Windows:** `%APPDATA%\kdm-cli` - -## Common Errors - -- **Invalid webhook URL** — Must match `https://discord.com/api/webhooks//`. -- **Invalid email address** — Must be a valid email format. -- **Invalid port number** — Must be between 1 and 65535. diff --git a/.github/docs-template/health/README.md b/.github/docs-template/health/README.md deleted file mode 100644 index 4b26df3e..00000000 --- a/.github/docs-template/health/README.md +++ /dev/null @@ -1,154 +0,0 @@ -# `kdm health` — Health Status - -> **Version:** `v1.2.1` - ---- - -## Overview - -The `kdm health` command checks and reports the health status of Kubernetes pods or Docker containers. It provides a clean, color-coded table with real-time status fetched directly from Docker and Kubernetes clients. - -> **Fix (Issue #1):** -> This command previously returned only a placeholder message. It now displays real health data from Docker containers and Kubernetes pods. - ---- - -# Syntax - -```bash -kdm health -``` - ---- - -# Parameters - -| Parameter | Description | Valid Values | -| :--- | :--- | :--- | -| `target` | The workload type to check | `pods`, `containers`, `all` | - ---- - -# Usage - ---- - -## `kdm health pods` - -Checks the health status of all Kubernetes pods. - -### Example - -```bash -kdm health pods -``` - -### Expected Output - -```text -╭──────┬─────────────────────────────┬─────────┬──────────────────────────────────────╮ -│ TYPE │ NAME │ HEALTH │ DETAILS │ -├──────┼─────────────────────────────┼─────────┼──────────────────────────────────────┤ -│ pod │ node-app-6f65c56b74-7tkvf │ Running │ namespace: default, restarts: 0 │ -│ pod │ nginx-deployment-abc123 │ Failed │ namespace: production, restarts: 5 │ -╰──────┴─────────────────────────────┴─────────┴──────────────────────────────────────╯ -``` - -### Health Indicators - -| Status Color | Meaning | -| :--- | :--- | -| 🟢 Green | `healthy`, `running`, `Running` | -| 🔴 Red | `unhealthy`, `exited`, `Failed` | -| 🟡 Yellow | Other states like `Pending`, `paused` | - -### Use Cases - -- Monitor pod health in real time -- Identify failing services before outages occur - ---- - -## `kdm health containers` - -Checks the health status of all Docker containers. - -### Example - -```bash -kdm health containers -``` - -### Expected Output - -```text -╭───────────┬────────────┬─────────┬───────────────╮ -│ TYPE │ NAME │ HEALTH │ DETAILS │ -├───────────┼────────────┼─────────┼───────────────┤ -│ container │ test-nginx │ running │ Up 8 minutes │ -│ container │ my-app │ exited │ Exited (1) │ -╰───────────┴────────────┴─────────┴───────────────╯ -``` - -### Use Cases - -- Verify container health -- Detect unhealthy or restarting containers - ---- - -## `kdm health all` - -Checks health for both pods and containers in a single command. If one source (Docker or Kubernetes) is unavailable, the other still renders successfully. - -### Example - -```bash -kdm health all -``` - ---- - -### Expected Output (Kubernetes unavailable) - -```text -⚠ Kubernetes unavailable: connect ECONNREFUSED 127.0.0.1:8080 - -╭───────────┬────────────┬─────────┬──────────────╮ -│ TYPE │ NAME │ HEALTH │ DETAILS │ -├───────────┼────────────┼─────────┼──────────────┤ -│ container │ test-nginx │ running │ Up 8 minutes │ -╰───────────┴────────────┴─────────┴──────────────╯ -``` - ---- - -### Expected Output (Both available) - -```text -╭───────────┬───────────────────────────┬─────────┬──────────────────────────────────────╮ -│ TYPE │ NAME │ HEALTH │ DETAILS │ -├───────────┼───────────────────────────┼─────────┼──────────────────────────────────────┤ -│ container │ test-nginx │ running │ Up 8 minutes │ -│ pod │ node-app-6f65c56b74-7tkvf │ Running │ namespace: default, restarts: 0 │ -╰───────────┴───────────────────────────┴─────────┴──────────────────────────────────────╯ -``` - ---- - -# Common Errors - -| Error | Cause | Fix | -| :--- | :--- | :--- | -| `Unknown target` | Invalid target passed | Use `pods`, `containers`, or `all` | -| `No workloads found` | No containers or pods running | Start Docker containers or Kubernetes pods | -| `Docker unavailable` | Docker daemon not running | Run `sudo systemctl start docker` | -| `Kubernetes unavailable` | No cluster connection | Run `minikube start` or configure `kubectl` | - ---- - -# Notes - -- Supports both Docker and Kubernetes environments -- Displays partial results even if one backend fails -- Provides readable CLI tables for faster debugging and monitoring \ No newline at end of file diff --git a/.github/docs-template/logs/README.md b/.github/docs-template/logs/README.md deleted file mode 100644 index 42ace9a0..00000000 --- a/.github/docs-template/logs/README.md +++ /dev/null @@ -1,143 +0,0 @@ -# `kdm logs` — Show Logs - -> **Version:** `v1.2.1` - ---- - -## Overview - -The `kdm logs` command retrieves and displays logs from a specified Docker container or Kubernetes pod. Docker is tried first, and Kubernetes is used automatically as a fallback if no matching container is found. - -> **Fix (Issue #1):** -> This command previously returned only a placeholder message. It now fetches real logs from Docker containers with automatic fallback to Kubernetes pods. - ---- - -# Syntax - -```bash -kdm logs -``` - ---- - -# Parameters - -| Parameter | Description | -| :--- | :--- | -| `name` | Container ID prefix, Docker container name, or Kubernetes pod name | - ---- - -# Usage - ---- - -## `kdm logs ` - -Fetches and displays the last 100 lines of logs for the specified workload. - ---- - -### Example (Docker Container) - -```bash -kdm logs test-nginx -``` - -### Expected Output - -```text -✔ Fetching logs for test-nginx... (0.0s) - -/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration -/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ -2026/05/17 06:43:38 [notice] 1#1: nginx/1.31.0 -2026/05/17 06:43:38 [notice] 1#1: start worker processes -``` - ---- - -### Example (Kubernetes Pod) - -```bash -kdm logs node-app-6f65c56b74-7tkvf -``` - -### Expected Output - -```text -✔ Fetching logs for node-app-6f65c56b74-7tkvf... (0.0s) - -Server listening on port 3000 -Connected to database -GET /api/health 200 12ms -``` - ---- - -### Example (Container ID Prefix) - -```bash -kdm logs a1b2c3d4 -``` - ---- - -# How It Works - -```text -Docker containers ──► match found? ──► show logs - │ - ▼ no match -Kubernetes pods ──► match found? ──► show logs - │ - ▼ no match -"No container or pod named X found" -``` - -### Workflow - -1. **Docker First** - - Searches running and stopped containers by ID prefix or container name - -2. **Kubernetes Fallback** - - If no Docker match is found, searches all Kubernetes pods across namespaces - -3. **Error Handling** - - Displays an error if neither Docker nor Kubernetes has a matching workload - ---- - -# Finding the Right Name - -```bash -# List Docker containers -docker ps - -# List Kubernetes pods -kubectl get pods --all-namespaces - -# Or use KDM itself -kdm show containers -kdm show pods -``` - ---- - -# Common Errors - -| Error | Cause | Fix | -| :--- | :--- | :--- | -| `No container or pod named X found` | Name does not match any workload | Use `kdm show containers` or `kdm show pods` | -| `Docker unavailable` | Docker daemon is not running | Run `sudo systemctl start docker` | -| `Failed to fetch logs` | Kubernetes API unreachable | Run `minikube start` or configure `kubectl` | - ---- - -# Notes - -- Automatically falls back from Docker to Kubernetes -- Supports container name and ID prefix matching -- Displays real-time logs directly from workloads -- Retrieves the last 100 log lines for faster debugging \ No newline at end of file diff --git a/.github/docs-template/show/README.md b/.github/docs-template/show/README.md deleted file mode 100644 index b11e718c..00000000 --- a/.github/docs-template/show/README.md +++ /dev/null @@ -1,134 +0,0 @@ -# `kdm show` — Show Running Workloads - -**Version:** v1.2.1 - -## Overview - -The `kdm show` command displays information about running workloads — Docker containers, Kubernetes pods, combined runners, or Minikube status. It provides a unified view of your local development environment. - -## Syntax - -```bash -kdm show -``` - -## Parameters - -| Parameter | Description | Valid Values | -|-----------|-------------|--------------| -| `target` | The workload type to display | `runners`, `pods`, `containers`, `minikube` | - -## Sub-commands - -### `kdm show runners` - -Shows a combined view of all running Docker containers and Kubernetes pods in a single table. - -**Example:** - -```bash -kdm show runners -``` - -**Expected Output:** - -``` -┌───────────┬──────────────┬──────────────────────┬─────────┬─────────────────┐ -│ TYPE │ NAME / ID │ NAMESPACE / IMAGE │ STATUS │ NODE / STATE │ -├───────────┼──────────────┼──────────────────────┼─────────┼─────────────────┤ -│ Pod │ nginx-abc12 │ default │ Running │ minikube │ -│ Container │ my-app │ myregistry/app:v1 │ running │ host │ -└───────────┴──────────────┴──────────────────────┴─────────┴─────────────────┘ -``` - -**Use Cases:** - -- Get a quick overview of all services running locally. -- Monitor both Docker and Kubernetes workloads simultaneously. - -**Common Errors:** - -- `Docker is unreachable` — Docker daemon is not running. Start Docker Desktop or `systemctl start docker`. -- `Kubernetes is unreachable` — No kubeconfig found or cluster is down. Check `kubectl cluster-info`. - ---- - -### `kdm show pods` - -Shows all Kubernetes pods in the current namespace. - -**Example:** - -```bash -kdm show pods -``` - -**Expected Output:** - -``` -┌──────────────┬───────────┬─────────┬──────────┬──────────┐ -│ POD NAME │ NAMESPACE │ STATUS │ RESTARTS │ NODE │ -├──────────────┼───────────┼─────────┼──────────┼──────────┤ -│ nginx-abc12 │ default │ Running │ 0 │ minikube │ -│ redis-xyz34 │ default │ Running │ 2 │ minikube │ -└──────────────┴───────────┴─────────┴──────────┴──────────┘ -``` - -**Use Cases:** - -- Inspect pod status and restart counts. -- Identify pods in non-Running states. - ---- - -### `kdm show containers` - -Shows all running Docker containers. - -**Example:** - -```bash -kdm show containers -``` - -**Expected Output:** - -``` -┌────────────┬────────────┬────────────────┬────────┬─────────┐ -│ CONTAINER │ NAME │ IMAGE │ STATUS │ STATE │ -│ ID │ │ │ │ │ -├────────────┼────────────┼────────────────┼────────┼─────────┤ -│ a1b2c3d4e5 │ my-app │ myregistry/app │ Up │ running │ -└────────────┴────────────┴────────────────┴────────┴─────────┘ -``` - -**Use Cases:** - -- List all active containers. -- Verify container states at a glance. - ---- - -### `kdm show minikube` - -Shows the status of Minikube profiles on the local machine. - -**Example:** - -```bash -kdm show minikube -``` - -**Expected Output:** - -``` -┌─────────┬─────────┬─────────┬─────────────┬─────────┐ -│ NAME │ HOST │ KUBELET │ APISERVER │ MESSAGE │ -├─────────┼─────────┼─────────┼─────────────┼─────────┤ -│ default │ Running │ Running │ Running │ │ -└─────────┴─────────┴─────────┴─────────────┴─────────┘ -``` - -**Common Errors:** - -- `Minikube is not installed on this system` — Install Minikube from https://minikube.sigs.k8s.io. diff --git a/.github/docs-template/watch/README.md b/.github/docs-template/watch/README.md deleted file mode 100644 index 66f6a99f..00000000 --- a/.github/docs-template/watch/README.md +++ /dev/null @@ -1,52 +0,0 @@ -# `kdm watch` — Live Monitoring - -**Version:** v1.2.1 - -## Overview - -The `kdm watch` command enters a live monitoring mode, continuously updating the terminal with real-time resource usage and status of running Docker containers and Kubernetes pods. Powered by Ink for an interactive terminal UI. - -## Syntax - -```bash -kdm watch -``` - -## Parameters - -This command takes no arguments. - -## Usage - -### `kdm watch` - -Starts a real-time monitoring dashboard. - -**Example:** - -```bash -kdm watch -``` - -**Expected Output:** - -The terminal will display an interactive, auto-refreshing table showing: - -- Container/pod names -- Current resource usage (CPU, memory) -- Health status -- Restart counts - -The view updates automatically. Press `Ctrl+C` to exit. - -**Use Cases:** - -- Continuously monitor service health during development. -- Watch for resource spikes or pod restarts in real time. -- Debug intermittent failures by observing live status changes. - -## Common Errors - -- **No workloads found** — Ensure at least one Docker container or Kubernetes pod is running. -- **Docker daemon not running** — Start Docker Desktop or `systemctl start docker`. -- **Kubernetes context not configured** — Verify with `kubectl cluster-info`. diff --git a/.github/scripts/bot-inactivity-comments.cjs b/.github/scripts/bot-inactivity-comments.cjs deleted file mode 100644 index 36ce087d..00000000 --- a/.github/scripts/bot-inactivity-comments.cjs +++ /dev/null @@ -1,116 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// bot-inactivity-comments.cjs -// -// Comment builders for the inactivity bot. Pure formatting functions -// separated from the scheduled execution logic for readability. -// -// Duration values (WARN_AFTER_MS, CLOSE_AFTER_MS, BLOCKED_CHECKIN_AFTER_MS) -// remain in bot-inactivity.cjs and are passed in as parameters here so this -// file stays free of module-level constants that belong to the scheduler. - -const { LABELS } = require('./helpers/constants'); - -// ─── HTML markers ──────────────────────────────────────────────────────────── - -const WARN_MARKER = ''; -const BLOCKED_CHECKIN_MARKER = ''; - -// ─── Comment builders ───────────────────────────────────────────────────────── - -/** - * Builds the inactivity warning comment body. - * @param {string[]} assigneeLogins - * @param {string} itemType - 'issue' or 'PR' - * @param {number} warnAfterMs - Duration in ms after which a warning is issued. - * @param {number} closeAfterMs - Duration in ms after which the item is closed. - * @returns {string} - */ -function buildWarningComment(assigneeLogins, itemType, warnAfterMs, closeAfterMs) { - const mentions = assigneeLogins.length > 0 - ? assigneeLogins.map(l => `@${l}`).join(', ') - : 'there'; - - const activityHint = itemType === 'PR' - ? 'To stay active, leave a comment on this PR or the linked **issue**, or push a new commit.' - : "If you're still on it, leave a comment to let us know!"; - - const warnDays = warnAfterMs / (24 * 60 * 60 * 1000); - const remainingDays = (closeAfterMs - warnAfterMs) / (24 * 60 * 60 * 1000); - - return [ - WARN_MARKER, - `👋 Hey ${mentions}! This ${itemType} has been inactive for ${warnDays} days.`, - '', - `Are you still working on this? We will close this in ${remainingDays} days if we see no further activity.`, - '', - `${activityHint} If you'd like to step down, comment \`/unassign\`.`, - ].join('\n'); -} - -/** - * Builds the closure comment body. - * @param {string} itemType - 'issue' or 'PR' - * @param {number} closeAfterMs - Duration in ms after which the item is closed. - * @returns {string} - */ -function buildClosureComment(itemType, closeAfterMs) { - const closeDays = closeAfterMs / (24 * 60 * 60 * 1000); - - if (itemType === 'issue') { - return [ - `⏱️ This issue has been unassigned and reset to \`${LABELS.READY_FOR_DEV}\` due to ${closeDays} days of inactivity.`, - '', - "If you'd like to continue working on this, feel free to comment `/assign` to be reassigned.", - ].join('\n'); - } - return [ - `⏱️ This PR has been closed due to ${closeDays} days of inactivity.`, - '', - `It has been unassigned and reset to \`${LABELS.READY_FOR_DEV}\` so another contributor can pick it up.`, - '', - "If you'd like to continue working on this, feel free to comment `/assign` to be reassigned.", - ].join('\n'); -} - -/** - * Builds the comment posted on an issue when its linked PR was closed for inactivity. - * @returns {string} - */ -function buildLinkedPRClosedComment() { - return [ - '🔗 The pull request linked to this issue was closed due to inactivity.', - '', - `This issue has been unassigned and reset to \`${LABELS.READY_FOR_DEV}\`.`, - '', - "If you'd like to continue working on this, feel free to comment `/assign` to be reassigned.", - ].join('\n'); -} - -/** - * Builds the 30-day blocked check-in comment body. - * @param {string[]} assigneeLogins - * @param {string} itemType - 'issue' or 'PR' - * @returns {string} - */ -function buildBlockedCheckinComment(assigneeLogins, itemType) { - const mentions = assigneeLogins.length > 0 - ? assigneeLogins.map(l => `@${l}`).join(', ') - : 'there'; - - return [ - BLOCKED_CHECKIN_MARKER, - `👋 Hey ${mentions}, just checking in! Is this ${itemType} still blocked?`, - '', - `If it has been unblocked, please remove the \`${LABELS.BLOCKED}\` label so we can track progress.`, - ].join('\n'); -} - -module.exports = { - WARN_MARKER, - BLOCKED_CHECKIN_MARKER, - buildWarningComment, - buildClosureComment, - buildLinkedPRClosedComment, - buildBlockedCheckinComment, -}; diff --git a/.github/scripts/bot-inactivity.cjs b/.github/scripts/bot-inactivity.cjs deleted file mode 100644 index 9072f7be..00000000 --- a/.github/scripts/bot-inactivity.cjs +++ /dev/null @@ -1,629 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// bot-inactivity.cjs -// -// Scheduled inactivity bot. Runs daily to detect stale assigned issues and PRs. -// -// Timeline (for non-blocked items): -// 5 days of inactivity → warning comment (idempotent via HTML marker) -// 7 days of inactivity → close, remove assignees, reset to "status: ready for dev" -// -// Activity signals that reset the 5-day clock: -// - A non-bot comment on the item by the author or any assignee -// - A non-bot comment by PR participants on a linked issue -// - A commit pushed to a PR branch by the PR author -// - Removal of the "status: blocked" label (unblocking counts as activity) -// -// PR review state: -// - "status: needs review" → skipped entirely (waiting on maintainers) -// - "status: needs revision" → clock starts from when label was last applied -// -// Blocked items ("status: blocked" label): -// - Exempt from the inactivity close/warn flow -// - Receive a friendly check-in comment every 30 days instead -// -// Cross-referencing: -// - Issues linked to open PRs with recent activity are not flagged. -// - When a PR is closed for inactivity, its linked issues are also reset. - -const { createLogger } = require('./helpers/logger'); -const { LABELS } = require('./helpers/constants'); -const { - hasLabel, - removeLabel, - addLabels, - removeAssignees, - postComment, - postOrUpdateComment, - fetchComments, - fetchIssueEvents, - fetchPRCommits, - closeItem, -} = require('./helpers/api'); -const { parseIssueNumbers } = require('./helpers/checks'); - -// ─── Constants ─────────────────────────────────────────────────────────────── - -const WARN_AFTER_MS = 5 * 24 * 60 * 60 * 1000; // 5 days -const CLOSE_AFTER_MS = 7 * 24 * 60 * 60 * 1000; // 7 days -const BLOCKED_CHECKIN_AFTER_MS = 30 * 24 * 60 * 60 * 1000; // 30 days - -const { - WARN_MARKER, - BLOCKED_CHECKIN_MARKER, - buildWarningComment, - buildClosureComment, - buildLinkedPRClosedComment, - buildBlockedCheckinComment, -} = require('./bot-inactivity-comments'); - -// ─── Context builder ───────────────────────────────────────────────────────── - -/** - * Builds a minimal context object compatible with shared API helpers. - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {number} number - * @returns {{ github: object, owner: string, repo: string, number: number }} - */ -function buildCtx(github, owner, repo, number) { - return { github, owner, repo, number }; -} - -// ─── Activity helpers ───────────────────────────────────────────────────────── - -/** - * Returns the greater of two timestamps. If candidate is null or not later - * than current, returns current unchanged. - * @param {number} current - * @param {number|null} candidate - * @returns {number} - */ -function latestOf(current, candidate) { - if (candidate === null || candidate <= current) return current; - return candidate; -} - -/** - * Builds a Set of lowercased participant logins from an issue or PR object. - * Includes the item's user (author) and all assignees. - * @param {object} item - * @returns {Set} - */ -function collectParticipants(item) { - const logins = (item.assignees || []).map(a => a.login.toLowerCase()); - if (item.user?.login) logins.push(item.user.login.toLowerCase()); - return new Set(logins); -} - -/** - * Returns true if the commit was authored by the given (lowercased) login. - * @param {object} commit - * @param {string} login - Lowercased login to match. - * @returns {boolean} - */ -function isAuthorLogin(commit, login) { - return commit.author?.login?.toLowerCase() === login; -} - -/** - * Extracts the best available date string from a commit object. - * @param {object} commit - * @returns {string|null} - */ -function extractCommitDate(commit) { - return commit.commit?.committer?.date ?? commit.commit?.author?.date ?? null; -} - -/** - * Returns the timestamp (ms) of the most recent non-bot comment posted by any - * of the given usernames, or null if none found. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {number} number - Issue or PR number. - * @param {Set} relevantLogins - Lowercased usernames to consider. - * @returns {Promise} - */ -async function getLastRelevantCommentDate(github, owner, repo, number, relevantLogins) { - const ctx = buildCtx(github, owner, repo, number); - const comments = await fetchComments(ctx); - - let latest = null; - for (const c of comments) { - if (!c.user || c.user.type === 'Bot') continue; - if (!relevantLogins.has(c.user.login.toLowerCase())) continue; - const t = new Date(c.created_at).getTime(); - latest = latestOf(latest === null ? -Infinity : latest, t); - } - return latest === -Infinity ? null : latest; -} - -/** - * Returns the timestamp (ms) of the most recent commit authored by the given - * login on the PR, or null if none found. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {number} prNumber - * @param {string} authorLogin - * @returns {Promise} - */ -async function getLastAuthorCommitDate(github, owner, repo, prNumber, authorLogin) { - const ctx = buildCtx(github, owner, repo, prNumber); - const commits = await fetchPRCommits(ctx); - const login = authorLogin.toLowerCase(); - let latest = null; - - for (const c of commits) { - if (!isAuthorLogin(c, login)) continue; - const dateStr = extractCommitDate(c); - latest = latestOf(latest === null ? -Infinity : latest, dateStr ? new Date(dateStr).getTime() : -Infinity); - } - return latest === null || latest === -Infinity ? null : latest; -} - -/** - * Returns the timestamp (ms) of the most recent event matching predicate, - * or null if no matching event is found. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {number} number - * @param {function} predicate - Called with each event object; return true to include. - * @returns {Promise} - */ -async function getLastMatchingEventDate(github, owner, repo, number, predicate) { - const ctx = buildCtx(github, owner, repo, number); - const events = await fetchIssueEvents(ctx); - - let latest = null; - for (const e of events) { - if (!predicate(e)) continue; - const t = new Date(e.created_at).getTime(); - latest = latestOf(latest === null ? -Infinity : latest, t); - } - return latest === null || latest === -Infinity ? null : latest; -} - -/** - * Returns the timestamp (ms) of the most recent time "status: blocked" was - * removed from the item, or null if it has never been unblocked. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {number} number - * @returns {Promise} - */ -async function getLastUnblockedDate(github, owner, repo, number) { - return getLastMatchingEventDate(github, owner, repo, number, - e => e.event === 'unlabeled' && e.label?.name === LABELS.BLOCKED); -} - -/** - * Returns the timestamp (ms) of the most recent time "status: needs revision" - * was applied to the item, or null if it has never been applied. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {number} number - * @returns {Promise} - */ -async function getLastNeedsRevisionLabeledDate(github, owner, repo, number) { - return getLastMatchingEventDate(github, owner, repo, number, - e => e.event === 'labeled' && e.label?.name === LABELS.NEEDS_REVISION); -} - -/** - * Returns the timestamp (ms) of the most recent time the item was assigned, - * or null if no assigned event is found. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {number} number - * @returns {Promise} - */ -async function getLastAssignedDate(github, owner, repo, number) { - return getLastMatchingEventDate(github, owner, repo, number, - e => e.event === 'assigned'); -} - -// ─── Activity computation ──────────────────────────────────────────────────── - -/** - * Computes the last meaningful activity timestamp (ms) for a PR. - * Considers: relevant comments (by PR author/assignees) on the PR and its - * linked issues, commits by the PR author, and removal of the blocked label. - * - * Also used by computeIssueLastActivity when evaluating linked open PRs. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {object} pr - GitHub PR object. - * @returns {Promise} - */ -async function computePRLastActivity(github, owner, repo, pr) { - let latest = new Date(pr.created_at).getTime(); - const participants = collectParticipants(pr); - - if (participants.size > 0) { - const d = await getLastRelevantCommentDate(github, owner, repo, pr.number, participants); - latest = latestOf(latest, d); - } - - if (pr.user?.login) { - const d = await getLastAuthorCommitDate(github, owner, repo, pr.number, pr.user.login); - latest = latestOf(latest, d); - } - - const linkedIssueNums = parseIssueNumbers(pr.body || ''); - for (const issueNum of linkedIssueNums) { - const d = await getLastRelevantCommentDate(github, owner, repo, issueNum, participants); - latest = latestOf(latest, d); - } - - return latestOf(latest, await getLastUnblockedDate(github, owner, repo, pr.number)); -} - -/** - * Computes the last meaningful activity timestamp (ms) for an issue. - * Considers: the issue's own relevant comments, removal of the blocked label, - * and activity on any linked open PRs. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {object} issue - GitHub issue object. - * @param {object[]} linkedOpenPRs - Open PR objects whose bodies reference this issue. - * @returns {Promise} - */ -async function computeIssueLastActivity(github, owner, repo, issue, linkedOpenPRs) { - const assignedAt = await getLastAssignedDate(github, owner, repo, issue.number); - if (assignedAt === null) return null; - - let latest = assignedAt; - const assigneeLogins = new Set((issue.assignees || []).map(a => a.login.toLowerCase())); - - if (assigneeLogins.size > 0) { - const d = await getLastRelevantCommentDate(github, owner, repo, issue.number, assigneeLogins); - latest = latestOf(latest, d); - } - - latest = latestOf(latest, await getLastUnblockedDate(github, owner, repo, issue.number)); - - for (const pr of linkedOpenPRs) { - latest = latestOf(latest, await computePRLastActivity(github, owner, repo, pr)); - } - - return latest; -} - -// ─── (Comment builders moved to bot-inactivity-comments.cjs) ───────────────── - -// ─── State mutation ─────────────────────────────────────────────────────────── - -/** - * Resets an item back to "ready for dev": removes assignees, removes - * "status: in progress", adds "status: ready for dev". - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {object} item - Issue or PR object with .number, .assignees, .labels. - * @returns {Promise} - */ -async function resetItem(github, owner, repo, item, { addReadyForDev = true } = {}) { - const ctx = buildCtx(github, owner, repo, item.number); - const assigneeLogins = (item.assignees || []).map(a => a.login); - - if (assigneeLogins.length > 0) { - await removeAssignees(ctx, assigneeLogins); - } - const statusLabels = (item.labels || []) - .map(label => label.name) - .filter(name => name.startsWith("status:")); - for (const label of statusLabels) { - await removeLabel(ctx, label); - } - if (addReadyForDev) { - await addLabels(ctx, [LABELS.READY_FOR_DEV]); - } -} - -// ─── Stale and blocked handlers ─────────────────────────────────────────────── - -/** - * Formats a one-line per-item activity summary log. - * - * @param {object} item - Issue or PR object with .number and .assignees. - * @param {string} itemType - 'issue' or 'PR' - * @param {number} lastActivityMs - Timestamp of last meaningful activity. - * @param {number} nowMs - Current time in ms. - * @param {'closed'|'warned'|'none'} result - * @returns {string} - */ -function formatActivitySummary(item, itemType, lastActivityMs, nowMs, result) { - const days = Math.floor((nowMs - lastActivityMs) / (24 * 60 * 60 * 1000)); - const assigneeLogins = (item.assignees || []).map(a => a.login); - const assignees = assigneeLogins.length > 0 ? assigneeLogins.join(', ') : 'none'; - - let action = 'no action needed'; - if (result === 'warned') { - action = 'posting inactivity warning'; - } else if (result === 'closed') { - action = itemType === 'PR' ? 'closing PR' : 'unassigning and resetting issue'; - } - - return `#${item.number} (${itemType}): last activity ${days}d ago (assigned: ${assignees}), ${action}`; -} - -/** - * Applies the appropriate stale action to an item. - * - If >= 7 days inactive: close, post closure comment, reset. - * - If >= 5 days inactive: post or update warning comment. - * - Otherwise: no action. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {object} item - Issue or PR object. - * @param {number} lastActivityMs - Timestamp of last meaningful activity. - * @param {string} itemType - 'issue' or 'PR' - * @param {number} nowMs - Current time in ms (injectable for testing). - * @returns {Promise<'closed'|'warned'|'none'>} - */ -async function handleStaleItem(github, owner, repo, item, lastActivityMs, itemType, nowMs) { - const ctx = buildCtx(github, owner, repo, item.number); - const elapsed = nowMs - lastActivityMs; - const assigneeLogins = (item.assignees || []).map(a => a.login); - - if (elapsed >= CLOSE_AFTER_MS) { - if (itemType === 'PR') { - await closeItem(ctx); - } - await resetItem(github, owner, repo, item, { addReadyForDev: itemType !== 'PR' }); - await postComment(ctx, buildClosureComment(itemType, CLOSE_AFTER_MS)); - return 'closed'; - } - - if (elapsed >= WARN_AFTER_MS) { - await postOrUpdateComment(ctx, WARN_MARKER, buildWarningComment(assigneeLogins, itemType, WARN_AFTER_MS, CLOSE_AFTER_MS)); - return 'warned'; - } - - return 'none'; -} - -/** - * Handles a blocked item: posts or refreshes a check-in comment if 30 days - * have passed since the last one. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {object} item - Issue or PR object. - * @param {string} itemType - 'issue' or 'PR' - * @param {number} nowMs - Current time in ms (injectable for testing). - * @param {object} logger - * @returns {Promise} - */ -async function handleBlockedItem(github, owner, repo, item, itemType, nowMs, logger) { - const ctx = buildCtx(github, owner, repo, item.number); - const comments = await fetchComments(ctx); - - // Find the most recently updated check-in comment (there should be at most one). - const checkinComment = comments - .filter(c => c.body && c.body.startsWith(BLOCKED_CHECKIN_MARKER)) - .sort((a, b) => new Date(b.updated_at || b.created_at) - new Date(a.updated_at || a.created_at))[0]; - - const lastCheckinMs = checkinComment - ? new Date(checkinComment.updated_at || checkinComment.created_at).getTime() - : null; - - if (lastCheckinMs === null || (nowMs - lastCheckinMs) >= BLOCKED_CHECKIN_AFTER_MS) { - logger.log(`#${item.number} (${itemType}): posting blocked check-in`); - const assigneeLogins = (item.assignees || []).map(a => a.login); - await postOrUpdateComment(ctx, BLOCKED_CHECKIN_MARKER, buildBlockedCheckinComment(assigneeLogins, itemType)); - } else { - logger.log(`#${item.number} (${itemType}): blocked check-in not due yet`); - } -} - -// ─── Data fetching ──────────────────────────────────────────────────────────── - -/** - * Fetches all open assigned issues (paginated). Includes issues with any label - * (blocked or in-progress) so both are handled in the main loop. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @returns {Promise} - */ -async function fetchAssignedIssues(github, owner, repo) { - const items = []; - let page = 1; - const perPage = 100; - - while (true) { - const { data } = await github.rest.issues.listForRepo({ - owner, - repo, - state: 'open', - assignee: '*', - per_page: perPage, - page, - }); - // issues.listForRepo returns both issues and PRs — filter to issues only - items.push(...data.filter(item => !item.pull_request)); - if (data.length < perPage) break; - page++; - } - - return items; -} - -/** - * Fetches all open PRs (paginated). - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @returns {Promise} - */ -async function fetchOpenPRs(github, owner, repo) { - const prs = []; - let page = 1; - const perPage = 100; - - while (true) { - const { data } = await github.rest.pulls.list({ - owner, - repo, - state: 'open', - per_page: perPage, - page, - }); - prs.push(...data); - if (data.length < perPage) break; - page++; - } - - return prs; -} - -/** - * Builds a map from issue number → array of open PR objects whose body - * references that issue via closing keywords or "related to". - * - * @param {object[]} openPRs - * @returns {Map} - */ -function buildIssueToPRsMap(openPRs) { - const map = new Map(); - for (const pr of openPRs) { - const issueNums = parseIssueNumbers(pr.body || ''); - for (const num of issueNums) { - if (!map.has(num)) map.set(num, []); - map.get(num).push(pr); - } - } - return map; -} - -// ─── Main entrypoint ────────────────────────────────────────────────────────── - -/** - * Main entrypoint for the inactivity bot. - * - * @param {{ github: object, context: object, getNow?: () => number }} args - * - getNow is injectable for testing; defaults to Date.now. - */ -module.exports = async function ({ github, context, getNow = () => Date.now() }) { - const logger = createLogger('[inactivity-bot]'); - const { owner, repo } = context.repo; - const nowMs = getNow(); - - logger.log(`Starting inactivity check (now=${new Date(nowMs).toISOString()})`); - - // ── Fetch data ──────────────────────────────────────────────────────────── - - const [assignedIssues, allOpenPRs] = await Promise.all([ - fetchAssignedIssues(github, owner, repo), - fetchOpenPRs(github, owner, repo), - ]); - - const processedIssueNumbers = new Set(); - - const assignedOpenPRs = allOpenPRs.filter(pr => (pr.assignees || []).length > 0); - const issueToOpenPRs = buildIssueToPRsMap(allOpenPRs); - - logger.log( - `Found ${assignedIssues.length} assigned issues, ` + - `${assignedOpenPRs.length} assigned open PRs` - ); - - // ── Process assigned PRs ────────────────────────────────────────────────── - - for (const pr of assignedOpenPRs) { - if (hasLabel(pr, LABELS.BLOCKED)) { - await handleBlockedItem(github, owner, repo, pr, 'PR', nowMs, logger); - continue; - } - - if (hasLabel(pr, LABELS.NEEDS_REVIEW)) { - logger.log(`#${pr.number} (PR): skipping (status: needs review)`); - continue; - } - - let lastActivity; - if (hasLabel(pr, LABELS.NEEDS_REVISION)) { - const revisionLabeledAt = await getLastNeedsRevisionLabeledDate(github, owner, repo, pr.number); - const prActivity = await computePRLastActivity(github, owner, repo, pr); - lastActivity = latestOf(prActivity, revisionLabeledAt); - } else { - lastActivity = await computePRLastActivity(github, owner, repo, pr); - } - - const result = await handleStaleItem(github, owner, repo, pr, lastActivity, 'PR', nowMs); - logger.log(formatActivitySummary(pr, 'PR', lastActivity, nowMs, result)); - - if (result === 'closed') { - // Clean up issues linked to this PR immediately - const linkedIssueNums = parseIssueNumbers(pr.body || ''); - for (const issueNum of linkedIssueNums) { - try { - const { data: linkedIssue } = await github.rest.issues.get({ - owner, repo, issue_number: issueNum, - }); - if (linkedIssue.state === 'open' && (linkedIssue.assignees || []).length > 0) { - logger.log(`Cleaning up linked issue #${issueNum} (PR #${pr.number} closed for inactivity)`); - await resetItem(github, owner, repo, linkedIssue); - const ctx = buildCtx(github, owner, repo, issueNum); - await postComment(ctx, buildLinkedPRClosedComment()); - processedIssueNumbers.add(issueNum); - } - } catch (err) { - logger.error(`Could not clean up linked issue #${issueNum}: ${err.message}`); - } - } - } - } - - // ── Process assigned issues ─────────────────────────────────────────────── - - for (const issue of assignedIssues) { - if (processedIssueNumbers.has(issue.number)) { - logger.log(`#${issue.number}: skipping (already handled via PR loop)`); - continue; - } - if (hasLabel(issue, LABELS.BLOCKED)) { - await handleBlockedItem(github, owner, repo, issue, 'issue', nowMs, logger); - continue; - } - - if (!hasLabel(issue, LABELS.IN_PROGRESS)) { - logger.log(`#${issue.number}: skipping (no in-progress or blocked label)`); - continue; - } - - const linkedPRs = issueToOpenPRs.get(issue.number) || []; - const lastActivity = await computeIssueLastActivity(github, owner, repo, issue, linkedPRs); - if (lastActivity === null) { - logger.log(`#${issue.number}: skipping (no assigned event found)`); - continue; - } - const result = await handleStaleItem(github, owner, repo, issue, lastActivity, 'issue', nowMs); - logger.log(formatActivitySummary(issue, 'issue', lastActivity, nowMs, result)); - } - - logger.log('Inactivity check complete'); -}; diff --git a/.github/scripts/bot-on-pr-merged.cjs b/.github/scripts/bot-on-pr-merged.cjs deleted file mode 100644 index 90fedb1e..00000000 --- a/.github/scripts/bot-on-pr-merged.cjs +++ /dev/null @@ -1,93 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// bot-on-pr-merged.cjs -// -// Handles pull_request closed events where the PR was merged. -// Updates the dashboard comments of sibling PRs when a newly merged PR -// introduces or resolves merge conflicts in them. - -const { - createLogger, - buildBotContext, - fetchOpenPRs, - getBotComment, - runAllChecksAndComment, - swapStatusLabel, - postComment -} = require('./helpers'); -const { checkMergeConflict } = require('./helpers/checks'); -const { MARKER, buildMergeConflictNotificationComment } = require('./helpers/comments'); - -const logger = createLogger('on-pr-merged'); - -/** - * Iterates through open PRs, checking for merge conflicts. - * If a PR's conflict state has changed (compared to its dashboard comment), - * it runs the checks again and updates the PR dashboard. - * - * @param {object} globalBotContext - Base context scoped to the repo. - */ -async function checkSiblingConflictsOnMerge(globalBotContext) { - const openPRs = await fetchOpenPRs(globalBotContext); - const mergedPRNumber = globalBotContext.number; - - for (const pr of openPRs) { - if (pr.draft || pr.number === mergedPRNumber) { - continue; - } - - // Context specific to the sibling PR - const prContext = { ...globalBotContext, number: pr.number, pr }; - - // Check actual merge conflict status - const mergeResult = await checkMergeConflict(prContext); - const actuallyHasConflict = !mergeResult.passed; - - // Determine current dashboard status shown to the user - const existingComment = await getBotComment(prContext, MARKER); - const currentlyShowsConflict = existingComment - ? existingComment.body.includes(':x: **Merge Conflicts**') - : false; - - if (currentlyShowsConflict !== actuallyHasConflict) { - logger.log(`PR #${pr.number} conflict status changed to ${actuallyHasConflict ? 'conflicted' : 'clean'}, updating...`); - const { allPassed } = await runAllChecksAndComment(prContext, { merge: mergeResult }); - const swapResult = await swapStatusLabel(prContext, allPassed); - if (!swapResult.success) { - logger.error(`Failed to swap status label for PR #${pr.number}: ${swapResult.errorDetails}`); - } - - // Post a standalone notification when a conflict is newly introduced - if (!currentlyShowsConflict && actuallyHasConflict) { - const prAuthor = pr.user?.login; - if (prAuthor) { - const notification = buildMergeConflictNotificationComment(prAuthor, mergedPRNumber); - await postComment(prContext, notification); - } - } - } else { - logger.log(`PR #${pr.number} conflict status unchanged (${actuallyHasConflict ? 'conflicted' : 'clean'}), skipping...`); - } - } -} - -module.exports = async ({ github, context }) => { - try { - const botContext = buildBotContext({ github, context }); - - if (!botContext.pr || !botContext.pr.merged) { - logger.log('PR is not merged, exiting sibling conflict check.'); - return; - } - - // Decision: Sibling-conflict check is placed in a dedicated `bot-on-pr-merged.cjs` script - // rather than appending it to `bot-on-pr-close.cjs`. This separates the core PR status - // updates (conflicts/labels/dashboard) from the contributor workflow automation - // (issue recommendation) handled by `bot-on-pr-close.cjs`. - await checkSiblingConflictsOnMerge(botContext); - - } catch (error) { - logger.error('Error in sibling conflict check:', error); - throw error; - } -}; diff --git a/.github/scripts/bot/bot-recommend-issues.cjs b/.github/scripts/bot/bot-recommend-issues.cjs deleted file mode 100644 index d44a7ecf..00000000 --- a/.github/scripts/bot/bot-recommend-issues.cjs +++ /dev/null @@ -1,505 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// commands/recommend-issues.cjs -// -// Issue recommendation command: suggests relevant issues to contributors -// after a PR is closed. Uses a history-based eligibility model to recommend issues strictly -// at the highest level the contributor is currently allowed to claim. - -const { - MAINTAINER_TEAM, - LABELS, - SKILL_HIERARCHY, - SKILL_PREREQUISITES, - ISSUE_STATE, - hasLabel, - postComment, - getHighestIssueSkillLevel, - countIssuesByAssignee, - PRIORITY_HIERARCHY, - createDelegatingLogger, -} = require('../helpers'); - -const logger = createDelegatingLogger(); - -/** - * Groups issues by their matching difficulty level. - * - * Each issue is assigned to the first matching level in levelsPriority. - * - * @param {Array} issues - * @param {string[]} levelsPriority - * @returns {Object>} Issues grouped by level. - */ -function groupIssuesByLevel(issues, levelsPriority) { - const grouped = Object.fromEntries( - levelsPriority.map(level => [level, []]) - ); - - for (const issue of issues) { - const level = levelsPriority.find(l => hasLabel(issue, l)); - if (level) grouped[level].push(issue); - } - - return grouped; -} - -/** - * Returns issues from the highest-priority level with results. - * - * Limits output to 5 issues. - * - * @param {Object>} grouped - * @param {string[]} levelsPriority - * @returns {Array} Selected issues or empty array. - */ -function pickFirstAvailableLevel(grouped, levelsPriority) { - for (const level of levelsPriority) { - if (grouped[level].length > 0) { - return grouped[level].slice(0, 5); - } - } - return []; -} - -/** - * Fetches issues for multiple levels in a single query. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @returns {Promise | null>} - */ -async function fetchIssuesBatch(github, owner, repo) { - try { - - const query = [ - `repo:${owner}/${repo}`, - 'is:issue', - 'is:open', - 'no:assignee', - `label:"${LABELS.READY_FOR_DEV}"` - ].join(' '); - - const result = await github.rest.search.issuesAndPullRequests({ - q: query, - per_page: 50, - sort: 'created', - order: 'asc', - }); - - return result.data.items || []; - } catch (error) { - logger.error('Failed to fetch issues:', { - message: error.message, - status: error.status, - }); - return null; - } -} - -/** - * Builds the success comment listing recommended issues. - * - * When `unlockedLevel` is provided, a congratulatory block is prepended to - * acknowledge that this contribution crossed the threshold into a new skill - * level. The display name is sourced from SKILL_PREREQUISITES so it stays - * consistent with the rest of the bot. - * - * @param {string} username - * @param {Array<{ title: string, html_url: string }>} issues - * @param {string|null} unlockedLevel - LABELS constant for the newly unlocked - * level, or null if no threshold was crossed. - * @returns {string} - */ -function buildRecommendationComment(username, issues, unlockedLevel = null) { - const list = issues.map( - (issue) => `- [${issue.title}](${issue.html_url})` - ); - - const congratsBlock = unlockedLevel - ? [ - `🏆 Milestone unlocked: you've just reached **${SKILL_PREREQUISITES[unlockedLevel].displayName}** level!`, - `That's a big step — well done. 🎊`, - '', - ] - : []; - - return [ - `👋 Hi @${username}! Great work on your recent contribution! 🎉`, - '', - ...congratsBlock, - `Here are some issues you might want to explore next:`, - '', - ...list, - '', - `Happy coding! 🚀`, - ].join('\n'); -} - -/** - * Builds an error comment when recommendations fail. - * - * @param {string} username - * @returns {string} - */ -function buildRecommendationErrorComment(username) { - return [ - `👋 Hi @${username}!`, - '', - `I ran into an issue while generating recommendations for you.`, - '', - `${MAINTAINER_TEAM} — could you please take a look?`, - '', - `Sorry for the inconvenience — feel free to explore open issues in the meantime!`, - ].join('\n'); -} - -/** - * Checks whether a contributor qualifies for a level via bypass. - * - * A contributor passes the bypass check if they have at least one closed - * issue at the candidate level or any higher level. This indicates prior - * experience at that difficulty, so prerequisite counts are not required. - * - * Iterates through all labels at or above the candidate level and stops - * as soon as a qualifying issue is found. - * - * API failures are treated as non-qualifying and skipped. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {string} username - * @param {string} candidate - Skill level being evaluated - * @returns {Promise} True if bypass condition is satisfied - */ -async function passesBypassCheck(github, owner, repo, username, candidate) { - const candidateIndex = SKILL_HIERARCHY.indexOf(candidate); - const atOrAboveLabels = SKILL_HIERARCHY.slice(candidateIndex); - - for (const label of atOrAboveLabels) { - const count = await countIssuesByAssignee( - github, owner, repo, username, - ISSUE_STATE.CLOSED, - label, - 1, - ); - // Skip on API failure - if (count === null) continue; - - if (count >= 1) return true; - } - return false; -} - -/** - * Checks whether a contributor qualifies for a level via prerequisite count. - * - * A contributor passes the normal check if they have completed at least - * `requiredCount` issues at the prerequisite level. - * - * The threshold is passed to countIssuesByAssignee so the query can stop - * early once the requirement is satisfied. - * - * @param {object} github - * @param {string} owner - * @param {string} repo - * @param {string} username - * @param {{ requiredLabel: string, requiredCount: number }} prereq - * @returns {Promise} - * - true → requirement met - * - false → requirement not met - * - null → API failure - */ -async function passesNormalCheck(github, owner, repo, username, prereq) { - const count = await countIssuesByAssignee( - github, owner, repo, username, - ISSUE_STATE.CLOSED, - prereq.requiredLabel, - prereq.requiredCount, - ); - - if (count === null) return null; - - return count >= prereq.requiredCount; -} - -/** - * Determines the highest skill level a contributor is eligible for - * based on their completed issues. - * - * The hierarchy is evaluated from highest to lowest level, returning - * the first level that satisfies either of the following: - * - * - Bypass check: - * The contributor has at least one closed issue at the candidate - * level or any higher level. This indicates prior experience, so - * prerequisite counts are not required. - * - * - Normal check: - * The contributor has completed at least `requiredCount` issues - * at the prerequisite level defined in SKILL_PREREQUISITES. - * - * The two checks are delegated to helper functions: - * - passesBypassCheck - * - passesNormalCheck - * - * Levels without a prerequisite label (e.g. Good First Issue) act as - * the floor and are always considered eligible. - * - * If an API call fails during evaluation, that candidate level is skipped - * and the next lower level is considered. - * - * @param {object} botContext - * @param {string} username - * @returns {Promise} The highest eligible skill level label - */ -async function resolveEligibleLevel(botContext, username) { - const { github, owner, repo } = botContext; - const topDown = [...SKILL_HIERARCHY].reverse(); - - for (const candidate of topDown) { - const prereq = SKILL_PREREQUISITES[candidate]; - - if (!prereq) continue; - - // Floor level - if (!prereq.requiredLabel) return candidate; - - // Bypass check - const bypass = await passesBypassCheck( - github, owner, repo, username, candidate - ); - if (bypass) return candidate; - - // Normal check - const normal = await passesNormalCheck( - github, owner, repo, username, prereq - ); - - // Skip candidate if API failed - if (normal === null) continue; - - if (normal) return candidate; - } - - return LABELS.GOOD_FIRST_ISSUE; -} - -/** - * Checks whether the just-completed issue caused the contributor to reach - * the threshold required to unlock the level directly above `currentLevel`. - * - * The count is evaluated using a capped query (threshold = requiredCount + 1), - * allowing the function to distinguish between: - * - reaching the threshold exactly for the first time - * - having already exceeded it earlier - * - * Only the immediate next level is considered. - * - * @param {object} botContext - * @param {string} username - * @param {string} currentLevel - * @returns {Promise} - */ -async function detectUnlockedLevel(botContext, username, currentLevel) { - const { github, owner, repo } = botContext; - const currentIndex = SKILL_HIERARCHY.indexOf(currentLevel); - const immediateNextLevel = SKILL_HIERARCHY[currentIndex + 1] ?? null; - - if (!immediateNextLevel) return null; - - const nextPrereq = SKILL_PREREQUISITES[immediateNextLevel]; - - // The next level's prerequisite must be the current level for this - // completion to be the relevant crossing event. - if (!nextPrereq?.requiredLabel || nextPrereq.requiredLabel !== currentLevel) return null; - - const count = await countIssuesByAssignee( - github, owner, repo, username, - ISSUE_STATE.CLOSED, - currentLevel, - nextPrereq.requiredCount + 1, - ); - - if (count === null) { - logger.log('detectUnlockedLevel: countIssuesByAssignee failed', { - user: username, currentLevel, - }); - return null; - } - - if (count === nextPrereq.requiredCount) { - logger.log('detectUnlockedLevel: threshold crossed', { - user: username, unlockedLevel: immediateNextLevel, count, - }); - return immediateNextLevel; - } - - return null; -} - -/** - * Sorts issues based on the PRIORITY_HIERARCHY. - * If two issues share the same priority, the oldest (earlier `created_at`) - * comes first; issues without a recognized priority label fall to the end. - * - * @param {Array} issues - * @returns {Array} Sorted copy of the issues. - */ -function sortByPriority(issues) { - return [...issues].sort((a, b) => { - const ai = PRIORITY_HIERARCHY.findIndex(p => hasLabel(a, p)); - const bi = PRIORITY_HIERARCHY.findIndex(p => hasLabel(b, p)); - const aRank = ai === -1 ? PRIORITY_HIERARCHY.length : ai; - const bRank = bi === -1 ? PRIORITY_HIERARCHY.length : bi; - if (aRank !== bRank) { - return aRank - bRank; - } - // Tiebreaker: Use the creation date (Oldest first) - return new Date(a.created_at) - new Date(b.created_at); - }); -} - -/** - * Returns recommended issues for the contributor based on their true - * eligibility level, determined by a history-based top-down walk. - * - * Issues are fetched in a single batch and filtered locally. - * Recommendations are restricted to the contributor's resolved eligible level, - * so all suggested issues are immediately assignable. - * - * Also runs a focused threshold check (detectUnlockedLevel) to determine - * whether this specific completion crossed a new level boundary, so the caller - * can include a congratulatory message when appropriate. - * - * @param {object} botContext - * @param {string} username - * @param {string} currentLevel - Skill label from the triggering PR/issue. - * @returns {Promise<{ issues: Array<{ title: string, html_url: string }>, unlockedLevel: string|null } | null>} - * Returns null on API failure (error comment already posted). - */ -async function getRecommendedIssues(botContext, username, currentLevel) { - // Run both async operations concurrently — they're independent. - const [eligibleLevel, unlockedLevel] = await Promise.all([ - resolveEligibleLevel(botContext, username), - detectUnlockedLevel(botContext, username, currentLevel), - ]); - - logger.log('getRecommendedIssues: resolved', { - user: username, - currentLevel, - eligibleLevel, - unlockedLevel, - }); - - const issues = await fetchIssuesBatch( - botContext.github, - botContext.owner, - botContext.repo, - ); - - if (issues === null) { - await postComment( - botContext, - buildRecommendationErrorComment(username) - ); - return null; - } - // Sort issues by priority. - const sorted = sortByPriority(issues); - // Filter issues to the resolved eligible level so all results are actionable. - const grouped = groupIssuesByLevel(sorted, [eligibleLevel]); - return { - issues: pickFirstAvailableLevel(grouped, [eligibleLevel]), - unlockedLevel, - }; -} - -/** - * Main handler for issue recommendations after a PR is merged. - * - * - Determines skill level - * - Fetches recommended issues - * - Posts a comment if results exist, with a congratulatory prefix when the - * just-completed issue crossed a new level threshold - * - * Skips silently if context is incomplete or no results found. - * Returns early on API failure. - * - * @param {{ - * github: object, - * owner: string, - * repo: string, - * issue: object, - * sender: { login: string } - * }} botContext - * @returns {Promise} - */ -async function handleRecommendIssues(botContext) { - const username = botContext.sender?.login; - if (!username) { - logger.log('Missing sender login, skipping recommendation'); - return; - } - - if (!botContext.issue) { - logger.log('Missing issue in context, skipping recommendation'); - return; - } - - const skillLevel = getHighestIssueSkillLevel(botContext.issue); - if (!skillLevel) { - logger.log('No skill level found, skipping recommendation', { - issueNumber: botContext.issue?.number, - }); - return; - } - - logger.log('recommendation.context', { - user: username, - level: skillLevel, - issue: botContext.issue?.number, - }); - - const result = await getRecommendedIssues( - botContext, - username, - skillLevel, - ); - - if (result === null) return; - - const { issues, unlockedLevel } = result; - - if (issues.length === 0) { - logger.log('recommendation.empty', { user: username }); - return; - } - - const comment = buildRecommendationComment(username, issues, unlockedLevel); - logger.log('recommendation.postComment', { - target: botContext.number, - issueSource: botContext.issue?.number, - recommendations: issues.length, - unlockedLevel, - }); - const postResult = await postComment(botContext, comment); - - if (!postResult.success) { - logger.error('recommendation.postCommentFailed', { - error: postResult.error, - }); - return; - } - - logger.log('recommendation.posted'); -} - -module.exports = { - handleRecommendIssues, - getRecommendedIssues, - resolveEligibleLevel, - detectUnlockedLevel, -}; diff --git a/.github/scripts/commands/assign-comments.cjs b/.github/scripts/commands/assign-comments.cjs deleted file mode 100644 index 3ea890ab..00000000 --- a/.github/scripts/commands/assign-comments.cjs +++ /dev/null @@ -1,424 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// commands/assign-comments.cjs -// -// Comment builders for the /assign command. Pure formatting functions -// separated from assignment logic for readability. - -const { - MAINTAINER_TEAM, - GFI_SUPPORT_TEAM, - LABELS, - ISSUE_STATE, - SKILL_HIERARCHY, - SKILL_PREREQUISITES, - AUTOMATION_CONFIG, -} = require("../helpers"); - -/** - * Maximum number of open (non-blocked) issues a contributor can be assigned to - * at the same time. Enforced by handleAssign in assign.cjs. - * @type {number} - */ -const MAX_OPEN_ASSIGNMENTS = AUTOMATION_CONFIG.assignmentLimits.maxOpenAssignments; - -/** - * Maximum number of Good First Issues a contributor may complete before being - * redirected to Beginner and higher-level issues. Enforced by handleAssign in assign.cjs. - * @type {number} - */ -const MAX_GFI_COMPLETIONS = AUTOMATION_CONFIG.assignmentLimits.maxGfiCompletions; - -/** - * Builds the welcome comment posted after a successful assignment. Returns a - * special first-timer welcome for Good First Issues (mentioning the support team), - * or a shorter returning-contributor message for all other skill levels. - * - * @param {string} username - The GitHub username being assigned. - * @param {string} skillLevel - The skill-level label on the issue (a LABELS constant). - * @returns {string} The formatted Markdown comment body. - */ -function buildWelcomeComment(username, skillLevel) { - const isGoodFirstIssue = skillLevel === LABELS.GOOD_FIRST_ISSUE; - const skillDisplayName = - SKILL_PREREQUISITES[skillLevel]?.displayName || "issue"; - if (isGoodFirstIssue) { - return [ - `👋 Hi @${username}, welcome to the KDM CLI community! Thank you for choosing to contribute — we're thrilled to have you here! 🎉`, - "", - `You've been assigned this **Good First Issue**, and the **Good First Issue Support Team** (${GFI_SUPPORT_TEAM}) is ready to help you succeed.`, - "", - "The issue description above has everything you need: implementation steps, contribution workflow, and links to guides. If anything is unclear, just ask — we're happy to help.", - "", - "If you realize you cannot complete this issue, simply comment `/unassign` to return it to the community pool.", - "", - "Good luck, and welcome aboard! 🚀", - ].join("\n"); - } - return [ - `👋 Hi @${username}, thanks for continuing to contribute to the KDM CLI! You've been assigned this **${skillDisplayName}** issue. 🙌`, - "", - "If this task involves any design decisions or you'd like early feedback, feel free to share your plan here before diving into the code.", - "", - "If you realize you cannot complete this issue, simply comment `/unassign` to return it to the pool.", - "", - "Good luck! 🚀", - ].join("\n"); -} - -/** - * Builds the comment posted when an issue is already assigned. If the requester - * is the current assignee, confirms they're already set; otherwise names the - * current assignee and links to unassigned ready issues. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @param {{ assignees?: Array<{ login: string }> }} issue - The issue object from the payload. - * @param {string} owner - Repository owner (for the browse-issues URL). - * @param {string} repo - Repository name (for the browse-issues URL). - * @returns {string} The formatted Markdown comment body. - */ -function buildAlreadyAssignedComment(requesterUsername, issue, owner, repo) { - const isAssignedToSelf = issue?.assignees?.some( - (a) => - (a.login || "").toLowerCase() === (requesterUsername || "").toLowerCase(), - ); - if (isAssignedToSelf) { - const otherAssignees = (issue?.assignees || []) - .map((assignee) => assignee?.login) - .filter( - (login) => - login && - login.toLowerCase() !== (requesterUsername || "").toLowerCase(), - ); - const currentAssignmentLine = - otherAssignees.length > 0 - ? `You're already assigned to this issue, which is also assigned to ${otherAssignees.map((login) => `@${login}`).join(", ")}.` - : "You're already assigned to this issue. You're all set to start working on it!"; - return [ - `👋 Hi @${requesterUsername}! ${currentAssignmentLine}`, - "", - "If you have any questions, feel free to ask here or reach out to the team.", - ].join("\n"); - } - const assigneeLogins = (issue?.assignees || []) - .map((assignee) => assignee?.login) - .filter(Boolean); - const currentAssignee = - assigneeLogins.length === 0 - ? "someone" - : assigneeLogins.map((login) => `@${login}`).join(", "); - return [ - `👋 Hi @${requesterUsername}! This issue is already assigned to ${currentAssignee}.`, - "", - "👉 **Find another issue to work on:**", - `[Browse unassigned issues](https://github.com/${owner}/${repo}/issues?q=is%3Aissue+is%3Aopen+no%3Aassignee+label%3A%22status%3A+ready+for+dev%22)`, - "", - "Once you find one you like, comment `/assign` to get started!", - ].join("\n"); -} - -/** - * Builds the comment posted when the issue is missing the "status: ready for dev" - * label. Explains the requirement and links to issues that are ready. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @param {string} owner - Repository owner (for the browse-issues URL). - * @param {string} repo - Repository name (for the browse-issues URL). - * @returns {string} The formatted Markdown comment body. - */ -function buildNotReadyComment(requesterUsername, owner, repo) { - return [ - `👋 Hi @${requesterUsername}! This issue is not ready for development yet.`, - "", - `Issues must have the \`${LABELS.READY_FOR_DEV}\` label before they can be assigned.`, - "", - "👉 **Find an issue that's ready:**", - `[Browse ready issues](https://github.com/${owner}/${repo}/issues?q=is%3Aissue+is%3Aopen+no%3Aassignee+label%3A%22status%3A+ready+for+dev%22)`, - "", - "Once you find one you like, comment `/assign` to get started!", - ].join("\n"); -} - -/** - * Builds the comment posted when a contributor hasn't completed enough prerequisite - * issues. Shows the required count, current progress, and a search link to find - * available prerequisite-level issues. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @param {string} skillLevel - The skill-level label on the issue (a LABELS constant). - * @param {number} completedCount - How many prerequisite issues the user has completed so far. - * @param {string} owner - Repository owner (for the search URL). - * @param {string} repo - Repository name (for the search URL). - * @returns {string} The formatted Markdown comment body. - */ -function buildPrerequisiteNotMetComment( - requesterUsername, - skillLevel, - completedCount, - owner, - repo, -) { - const prereq = SKILL_PREREQUISITES[skillLevel]; - const { requiredLabel, requiredCount, prerequisiteDisplayName, displayName } = - prereq; - const searchQuery = `is:issue is:open no:assignee label:"${requiredLabel}" label:"${LABELS.READY_FOR_DEV}"`; - const searchUrl = `https://github.com/${owner}/${repo}/issues?q=${encodeURIComponent(searchQuery)}`; - return [ - `👋 Hi @${requesterUsername}! Thanks for your interest in contributing!`, - "", - `This is a **${displayName}** issue. Before taking it on, you need to complete at least **${requiredCount} ${prerequisiteDisplayName}** to build familiarity with the codebase.`, - "", - `📊 **Your Progress:** You've completed **${completedCount}** so far.`, - "", - `👉 **Find ${prerequisiteDisplayName} to work on:**`, - `[Browse available ${prerequisiteDisplayName}](${searchUrl})`, - "", - `Once you've completed ${requiredCount}, come back and we'll be happy to assign this to you! 🎯`, - ].join("\n"); -} - -/** - * Builds the comment posted when the issue has no skill-level label. Tags the - * maintainer team to add one of the four skill levels, and instructs the - * requester to try /assign again once the label is added. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @returns {string} The formatted Markdown comment body. - */ -function buildNoSkillLevelComment(requesterUsername) { - return [ - `👋 Hi @${requesterUsername}! This issue doesn't have a skill level label yet.`, - "", - `${MAINTAINER_TEAM} — could you please add one of the following labels?`, - `- \`${LABELS.GOOD_FIRST_ISSUE}\``, - `- \`${LABELS.BEGINNER}\``, - `- \`${LABELS.INTERMEDIATE}\``, - `- \`${LABELS.ADVANCED}\``, - "", - `@${requesterUsername}, once a maintainer adds the label, comment \`/assign\` again to request assignment.`, - ].join("\n"); -} - -/** - * Builds the comment posted when the issue's skill level changes while the - * assignment request is queued. Tells the requester to retry so eligibility - * checks run against the latest label state. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @param {string} previousSkillLevel - The skill level from the event payload. - * @param {string} currentSkillLevel - The skill level from the fresh issue snapshot. - * @returns {string} The formatted Markdown comment body. - */ -function buildSkillLevelChangedComment( - requesterUsername, - previousSkillLevel, - currentSkillLevel, -) { - return [ - `👋 Hi @${requesterUsername}! The skill level for this issue changed while your assignment request was being processed.`, - "", - `**Current label:** \`${currentSkillLevel}\``, - `**Previous label:** \`${previousSkillLevel}\``, - "", - "Please comment `/assign` again to request assignment with the updated skill requirements.", - ].join("\n"); -} - -/** - * Builds a GitHub Issues search URL for the given repository and query string. - * - * @param {string} owner - Repository owner. - * @param {string} repo - Repository name. - * @param {string} searchQuery - Raw GitHub search query (will be URI-encoded). - * @returns {string} The full HTTPS search URL. - */ -function buildIssuesSearchUrl(owner, repo, searchQuery) { - return `https://github.com/${owner}/${repo}/issues?q=${encodeURIComponent(searchQuery)}`; -} - -/** - * Internal formatter for the assignment-limit-exceeded comment. Accepts pre-built - * URLs so it can be tested independently of URL construction logic. Explains the - * limit, shows the user's current count, and links to their assigned and - * (optionally) blocked issues. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @param {string|number} openCount - User-facing open-assignment count display (for example 2 or "3+"). - * @param {string} assignedIssuesUrl - URL to the user's open assigned issues search. - * @param {string|null} blockedIssuesUrl - URL to the user's blocked issues search, or null if none. - * @returns {string} The formatted Markdown comment body. - */ -function formatAssignmentLimitExceededComment( - requesterUsername, - openCount, - assignedIssuesUrl, - blockedIssuesUrl, -) { - const lines = [ - `👋 Hi @${requesterUsername}! Thanks for your enthusiasm to contribute!`, - "", - `To help contributors stay focused and ensure issues remain available for others, we limit assignments to **${MAX_OPEN_ASSIGNMENTS} open issues** at a time. Issues labeled \`${LABELS.BLOCKED}\` are not counted toward this limit.`, - "", - `📊 **Your Current Assignments:** You're currently assigned to **${openCount}** open issues.`, - "", - "👉 **View your assigned issues:**", - `[Your open assignments](${assignedIssuesUrl})`, - ]; - if (blockedIssuesUrl) { - lines.push( - "", - "👉 **View your blocked issues:**", - `[Your blocked issues](${blockedIssuesUrl})`, - ); - } - lines.push( - "", - `💡 **Tip:** If all of your open assigned issues have a linked PR with \`${LABELS.NEEDS_REVIEW}\`, the limit is automatically bypassed — you can request a new assignment right away.`, - "", - "Once you complete or unassign from one of your current issues, come back and we'll be happy to assign this to you! 🎯", - ); - return lines.join("\n"); -} - -/** - * Builds the comment posted when a contributor is at or above the maximum number - * of open assignments (MAX_OPEN_ASSIGNMENTS). Constructs search URLs for the - * user's assigned and blocked issues, then delegates to formatAssignmentLimitExceededComment. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @param {string|number} openCount - User-facing open-assignment count display (for example 2 or "3+"). - * @param {string} owner - Repository owner (for search URLs). - * @param {string} repo - Repository name (for search URLs). - * @param {number} [blockedCount=0] - Number of blocked issues assigned to the user (shows link if > 0). - * @returns {string} The formatted Markdown comment body. - */ -function buildAssignmentLimitExceededComment( - requesterUsername, - openCount, - owner, - repo, - blockedCount = 0, -) { - const assignedQuery = `is:issue is:${ISSUE_STATE.OPEN} assignee:${requesterUsername} -label:"${LABELS.BLOCKED}"`; - const assignedIssuesUrl = buildIssuesSearchUrl(owner, repo, assignedQuery); - const blockedIssuesUrl = - blockedCount > 0 - ? buildIssuesSearchUrl( - owner, - repo, - `is:issue is:${ISSUE_STATE.OPEN} assignee:${requesterUsername} label:"${LABELS.BLOCKED}"`, - ) - : null; - return formatAssignmentLimitExceededComment( - requesterUsername, - openCount, - assignedIssuesUrl, - blockedIssuesUrl, - ); -} - -/** - * Builds the comment posted when a GitHub API call fails during eligibility - * verification (e.g. counting open assignments or checking prerequisites). - * Tags the maintainer team for manual assistance. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @returns {string} The formatted Markdown comment body. - */ -function buildApiErrorComment(requesterUsername) { - return [ - `👋 Hi @${requesterUsername}! I encountered an error while trying to verify your eligibility for this issue.`, - "", - `${MAINTAINER_TEAM} — could you please help with this assignment request?`, - "", - `@${requesterUsername}, a maintainer will review your request and assign you manually if appropriate. Sorry for the inconvenience!`, - ].join("\n"); -} - -/** - * Builds the comment posted when the user was successfully assigned but the - * label transition ("ready for dev" -> "in progress") failed. Tags the maintainer - * team with explicit manual steps and includes the error details. - * - * @param {string} username - The GitHub username who was assigned. - * @param {string} error - The error message(s) from the failed label operations. - * @returns {string} The formatted Markdown comment body. - */ -function buildLabelUpdateFailureComment(username, error) { - return [ - `⚠️ @${username} has been successfully assigned to this issue, but I encountered an error updating the labels.`, - "", - `${MAINTAINER_TEAM} — please manually:`, - `- Remove the \`${LABELS.READY_FOR_DEV}\` label`, - `- Add the \`${LABELS.IN_PROGRESS}\` label`, - "", - `Error details: ${error}`, - ].join("\n"); -} - -/** - * Builds the comment posted when a contributor has already completed the maximum - * number of Good First Issues (MAX_GFI_COMPLETIONS). Rejects the assignment - * warmly and redirects them toward Beginner and higher-level issues. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @param {string|number} completedCount - User-facing completed-count display (for example 5 or "5+"). - * @param {string} owner - Repository owner (for the search URL). - * @param {string} repo - Repository name (for the search URL). - * @returns {string} The formatted Markdown comment body. - */ -function buildGfiLimitExceededComment( - requesterUsername, - completedCount, - owner, - repo, -) { - const searchQuery = `is:issue is:open no:assignee label:"${LABELS.BEGINNER}" label:"${LABELS.READY_FOR_DEV}"`; - const searchUrl = buildIssuesSearchUrl(owner, repo, searchQuery); - return [ - `👋 Hi @${requesterUsername}! You've completed **${completedCount} Good First Issues** — that's a fantastic achievement, and it shows you know the workflow inside and out. 🎉`, - "", - "Good First Issues are designed to help new contributors get comfortable with the process, and you've clearly mastered it. We believe you're more than ready to take on bigger challenges!", - "", - "👉 **Find Beginner and higher issues to work on:**", - `[Browse available Beginner issues](${searchUrl})`, - "", - "Come take on something more challenging — we're excited to see what you'll build next! 🚀", - ].join("\n"); -} - -/** - * Builds the comment posted when the addAssignees API call itself fails. - * Tags the maintainer team to manually assign the user and includes the error details. - * - * @param {string} requesterUsername - The GitHub username who commented /assign. - * @param {string} error - The error message from the failed assignment call. - * @returns {string} The formatted Markdown comment body. - */ -function buildAssignmentFailureComment(requesterUsername, error) { - return [ - `⚠️ Hi @${requesterUsername}! I tried to assign you to this issue, but encountered an error.`, - "", - `${MAINTAINER_TEAM} — could you please manually assign @${requesterUsername} to this issue?`, - "", - `Error details: ${error}`, - ].join("\n"); -} - -module.exports = { - MAX_OPEN_ASSIGNMENTS, - MAX_GFI_COMPLETIONS, - SKILL_HIERARCHY, - SKILL_PREREQUISITES, - buildWelcomeComment, - buildAlreadyAssignedComment, - buildNotReadyComment, - buildPrerequisiteNotMetComment, - buildNoSkillLevelComment, - buildSkillLevelChangedComment, - buildAssignmentLimitExceededComment, - buildGfiLimitExceededComment, - buildApiErrorComment, - buildLabelUpdateFailureComment, - buildAssignmentFailureComment, -}; diff --git a/.github/scripts/commands/assign.cjs b/.github/scripts/commands/assign.cjs deleted file mode 100644 index b703841d..00000000 --- a/.github/scripts/commands/assign.cjs +++ /dev/null @@ -1,650 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// commands/assign.cjs -// -// /assign command: assigns the commenter to the issue. Enforces skill-level -// prerequisites, assignment limits, and status labels. See bot-on-comment.cjs -// for high-level docs (limits, skill levels, required labels). - -const { - LABELS, - ISSUE_STATE, - createDelegatingLogger, - isNonNegativeInteger, - hasLabel, - swapLabels, - addAssignees, - postComment, - acknowledgeComment, - getHighestIssueSkillLevel, - countIssuesByAssignee, - listAssignedIssues, - hasNeedsReviewPR, -} = require('../helpers'); - -const { - MAX_OPEN_ASSIGNMENTS, - MAX_GFI_COMPLETIONS, - SKILL_HIERARCHY, - SKILL_PREREQUISITES, - buildWelcomeComment, - buildAlreadyAssignedComment, - buildNotReadyComment, - buildPrerequisiteNotMetComment, - buildNoSkillLevelComment, - buildSkillLevelChangedComment, - buildAssignmentLimitExceededComment, - buildGfiLimitExceededComment, - buildApiErrorComment, - buildLabelUpdateFailureComment, - buildAssignmentFailureComment, -} = require("./assign-comments"); - -// Delegate to the active logger set by the dispatcher (bot-on-comment.cjs). -// This ensures the correct prefix is used after command parsing. -const logger = createDelegatingLogger(); - -/** - * Formats a count for user-facing text when a threshold short-circuit may have - * capped the value. If count reaches the short-circuit threshold, return an - * "at least" style display (e.g. "3+") rather than implying an exact value. - * - * @param {number} count - Count returned by countIssuesByAssignee. - * @param {number} threshold - Threshold used for short-circuiting. - * @returns {string} User-facing display string. - */ -function formatThresholdedCount(count, threshold) { - return count >= threshold ? `${threshold}+` : String(count); -} - -/** - * Returns the number of open issues assigned to the user that carry the - * "status: blocked" label. Used to provide context in the assignment-limit-exceeded - * comment (blocked issues don't count toward the limit). - * - * @param {object} github - Octokit GitHub API client. - * @param {string} owner - Repository owner. - * @param {string} repo - Repository name. - * @param {string} username - GitHub username to search for. - * @returns {Promise} The blocked-issue count (defaults to 0 on any error). - */ -async function getBlockedCount(github, owner, repo, username) { - const count = await countIssuesByAssignee( - github, - owner, - repo, - username, - ISSUE_STATE.OPEN, - LABELS.BLOCKED, - 1, - ); - if (count !== null && isNonNegativeInteger(count)) return Math.floor(count); - return 0; -} - -/** - * Checks skill-level prerequisites for the requesting user. Looks up the - * required number of completed issues at the prerequisite level (e.g. a - * Beginner issue requires 2 completed Good First Issues). Posts a comment - * and returns false when the check fails or an API error occurs; returns - * true when prerequisites are satisfied (or not required for the level). - * - * @param {object} botContext - Bot context from buildBotContext (github, owner, repo, number). - * @param {string} skillLevel - The skill-level label on the issue (a LABELS constant). - * @param {string} requesterUsername - The GitHub username requesting assignment. - * @returns {Promise} True if prerequisites are met or none required; false otherwise. - */ -async function checkPrerequisites(botContext, skillLevel, requesterUsername) { - const prereq = SKILL_PREREQUISITES[skillLevel]; - if (!prereq.requiredLabel || prereq.requiredCount <= 0) return true; - - // Bypass: If the user already has worked on any level or higher then bypass them. - const skillIndex = SKILL_HIERARCHY.indexOf(skillLevel); - if (skillIndex !== -1) { - for (let i = skillIndex; i < SKILL_HIERARCHY.length; i++) { - const checkCurrLevel = SKILL_HIERARCHY[i]; - const countAtLevel = await countIssuesByAssignee( - botContext.github, - botContext.owner, - botContext.repo, - requesterUsername, - ISSUE_STATE.CLOSED, - checkCurrLevel, - 1, - ); - - if (countAtLevel !== null && countAtLevel > 0) { - logger.log( - `Bypassing prerequisites: user has completed ${countAtLevel} issues with label "${checkCurrLevel}"`, - ); - return true; - } - } - } - - // Normal validation - const completedCount = await countIssuesByAssignee( - botContext.github, - botContext.owner, - botContext.repo, - requesterUsername, - ISSUE_STATE.CLOSED, - prereq.requiredLabel, - prereq.requiredCount, - ); - if (completedCount === null) { - logger.log("Exit: could not verify prerequisites due to API error"); - await postComment(botContext, buildApiErrorComment(requesterUsername)); - logger.log("Posted API error comment, tagged maintainers"); - return false; - } - if (completedCount < prereq.requiredCount) { - logger.log("Exit: prerequisites not met", { - required: prereq.requiredCount, - completed: completedCount, - }); - await postComment( - botContext, - buildPrerequisiteNotMetComment( - requesterUsername, - skillLevel, - completedCount, - botContext.owner, - botContext.repo, - ), - ); - logger.log("Posted prerequisite-not-met comment"); - return false; - } - logger.log("Prerequisites met:", { - required: prereq.requiredCount, - completed: completedCount, - }); - return true; -} - -/** - * Transitions issue status labels after a successful assignment: removes - * "status: ready for dev" and adds "status: in progress". If either label - * operation fails, posts a comment tagging maintainers with manual instructions. - * - * @param {object} botContext - Bot context from buildBotContext (github, owner, repo, number). - * @param {string} requesterUsername - The username being assigned (for the failure comment). - * @returns {Promise} - */ -async function updateLabels(botContext, requesterUsername) { - const { success, errorDetails } = await swapLabels( - botContext, - LABELS.READY_FOR_DEV, - LABELS.IN_PROGRESS, - ); - if (!success) { - await postComment( - botContext, - buildLabelUpdateFailureComment(requesterUsername, errorDetails), - ); - logger.log("Posted label update failure comment, tagged maintainers"); - } -} - -/** - * Checks whether the requester has reached the GFI completion cap. Only applies - * when skillLevel is LABELS.GOOD_FIRST_ISSUE. Posts an encouraging comment and - * returns false when the cap is reached; returns true otherwise. - * - * @param {object} botContext - Bot context from buildBotContext. - * @param {string} skillLevel - The skill-level label on the issue (a LABELS constant). - * @param {string} requesterUsername - GitHub username requesting assignment. - * @returns {Promise} True if under the cap or not a GFI; false otherwise. - */ -async function enforceGfiCompletionLimit( - botContext, - skillLevel, - requesterUsername, -) { - if (skillLevel !== LABELS.GOOD_FIRST_ISSUE) return true; - - const completedCount = await countIssuesByAssignee( - botContext.github, - botContext.owner, - botContext.repo, - requesterUsername, - ISSUE_STATE.CLOSED, - LABELS.GOOD_FIRST_ISSUE, - MAX_GFI_COMPLETIONS + 1, - ); - if (completedCount === null) { - logger.log("Exit: could not verify GFI completion count due to API error"); - await postComment(botContext, buildApiErrorComment(requesterUsername)); - logger.log("Posted API error comment, tagged maintainers"); - return false; - } - if (completedCount >= MAX_GFI_COMPLETIONS) { - logger.log("Exit: contributor has reached GFI completion cap", { - maxAllowed: MAX_GFI_COMPLETIONS, - completedCount, - }); - await postComment( - botContext, - buildGfiLimitExceededComment( - requesterUsername, - formatThresholdedCount(completedCount, MAX_GFI_COMPLETIONS + 1), - botContext.owner, - botContext.repo, - ), - ); - logger.log("Posted GFI-limit-exceeded comment"); - return false; - } - logger.log("GFI completion count OK:", { - maxAllowed: MAX_GFI_COMPLETIONS, - completedCount, - }); - return true; -} - -/** - * Validates that the issue is in a state that allows assignment. Checks three - * gates in order: no existing assignees, "status: ready for dev" label present, - * and a skill-level label present. Posts an informative comment and returns null - * when any gate fails. - * - * @param {object} botContext - Bot context from buildBotContext. - * @param {string} requesterUsername - GitHub username requesting assignment. - * @returns {Promise} The skill-level label, or null if a gate failed. - */ -async function validateIssueState(botContext, requesterUsername) { - if (botContext.issue.assignees?.length > 0) { - logger.log( - "Exit: issue already assigned to", - botContext.issue.assignees.map((a) => a.login), - ); - await postComment( - botContext, - buildAlreadyAssignedComment( - requesterUsername, - botContext.issue, - botContext.owner, - botContext.repo, - ), - ); - logger.log("Posted already-assigned comment"); - return null; - } - - if (!hasLabel(botContext.issue, LABELS.READY_FOR_DEV)) { - logger.log("Exit: issue missing ready for dev label"); - await postComment( - botContext, - buildNotReadyComment( - requesterUsername, - botContext.owner, - botContext.repo, - ), - ); - logger.log("Posted not-ready comment"); - return null; - } - - const skillLevel = getHighestIssueSkillLevel(botContext.issue); - if (!skillLevel) { - logger.log("Exit: issue has no skill level label"); - await postComment(botContext, buildNoSkillLevelComment(requesterUsername)); - logger.log("Posted no-skill-level comment"); - return null; - } - logger.log("Issue skill level:", skillLevel); - return skillLevel; -} - -/** - * Verifies the requester has not reached the open-assignment limit. Posts an - * API-error or limit-exceeded comment and returns false when the check fails; - * returns true when the user is within limits. - * - * @param {object} botContext - Bot context from buildBotContext. - * @param {string} requesterUsername - GitHub username requesting assignment. - * @returns {Promise} True if within assignment limits; false otherwise. - */ -async function enforceAssignmentLimit(botContext, requesterUsername) { - const openAssignmentCount = await countIssuesByAssignee( - botContext.github, - botContext.owner, - botContext.repo, - requesterUsername, - ISSUE_STATE.OPEN, - null, - MAX_OPEN_ASSIGNMENTS + 1, - ); - if (openAssignmentCount === null) { - logger.log("Exit: could not verify open assignments due to API error"); - await postComment(botContext, buildApiErrorComment(requesterUsername)); - logger.log("Posted API error comment, tagged maintainers"); - return false; - } - - if (openAssignmentCount >= MAX_OPEN_ASSIGNMENTS) { - logger.log("Contributor at or above assignment cap", { - maxAllowed: MAX_OPEN_ASSIGNMENTS, - currentCount: openAssignmentCount, - }); - - // Suppress limit failures caused by a duplicate /assign for this issue. - let freshIssue; - try { - const response = await botContext.github.rest.issues.get({ - owner: botContext.owner, - repo: botContext.repo, - issue_number: botContext.number, - }); - freshIssue = response.data; - } catch (err) { - const errorMsg = err instanceof Error ? err.message : String(err); - logger.error("Failed to fetch fresh issue state:", errorMsg); - await postComment(botContext, buildApiErrorComment(requesterUsername)); - return false; - } - - const requesterAlreadyAssigned = freshIssue.assignees?.some( - (assignee) => - (assignee.login || "").toLowerCase() === - requesterUsername.toLowerCase(), - ); - - if (requesterAlreadyAssigned) { - logger.log("Exit: user is already assigned (caught during limit check)"); - await postComment( - botContext, - buildAlreadyAssignedComment( - requesterUsername, - freshIssue, - botContext.owner, - botContext.repo, - ), - ); - return false; - } - - // --- Needs-review bypass check --- - // If every open (non-blocked) assigned issue has an open PR authored by - // the contributor with "status: needs review", allow the bypass. - const assignedIssues = await listAssignedIssues( - botContext.github, - botContext.owner, - botContext.repo, - requesterUsername, - ); - - if (assignedIssues === null) { - logger.log("Exit: could not fetch assigned issues due to API error"); - await postComment(botContext, buildApiErrorComment(requesterUsername)); - logger.log("Posted API error comment, tagged maintainers"); - return false; - } - - // Guard against vacuous truth: [].every(fn) === true in JavaScript. - // An empty array can occur from eventual consistency or race conditions; - // it must NOT silently trigger the bypass. - if (assignedIssues.length > 0) { - let allHaveNeedsReviewPR = true; - for (const issue of assignedIssues) { - const result = await hasNeedsReviewPR( - botContext.github, - botContext.owner, - botContext.repo, - requesterUsername, - issue.number, - ); - if (result === null) { - logger.log( - `Exit: API error checking needs-review PR for issue #${issue.number}`, - ); - await postComment( - botContext, - buildApiErrorComment(requesterUsername), - ); - logger.log("Posted API error comment, tagged maintainers"); - return false; - } - if (!result) { - allHaveNeedsReviewPR = false; - break; - } - } - - if (allHaveNeedsReviewPR) { - logger.log( - "Bypass: all assigned issues have linked needs-review PRs", - { issueCount: assignedIssues.length }, - ); - return true; - } - } else { - logger.log( - "Bypass skipped: listAssignedIssues returned 0 issues despite count >= limit", - ); - } - - // Fall through to the existing limit-exceeded path - logger.log("Exit: contributor has too many open assignments", { - maxAllowed: MAX_OPEN_ASSIGNMENTS, - currentCount: openAssignmentCount, - }); - const blockedCount = await getBlockedCount( - botContext.github, - botContext.owner, - botContext.repo, - requesterUsername, - ); - await postComment( - botContext, - buildAssignmentLimitExceededComment( - requesterUsername, - formatThresholdedCount(openAssignmentCount, MAX_OPEN_ASSIGNMENTS + 1), - botContext.owner, - botContext.repo, - blockedCount, - ), - ); - logger.log("Posted assignment-limit-exceeded comment"); - return false; - } - - logger.log("Open assignment count OK:", { - maxAllowed: MAX_OPEN_ASSIGNMENTS, - currentCount: openAssignmentCount, - }); - return true; -} - -/** - * Performs the actual issue assignment with a same-issue race defense: - * - * 1. Pre-flight check: fetches fresh issue state via issues.get() to detect - * same-issue collisions (another user assigned while this run was queued; - * the workflow concurrency key is serialized per issue). - * - * On success, posts a welcome comment and transitions status labels. - * Posts failure comments if any API call fails. - * - * @param {object} botContext - Bot context from buildBotContext. - * @param {string} requesterUsername - GitHub username being assigned. - * @param {string} skillLevel - The skill-level label on the issue. - * @returns {Promise} - */ -async function assignAndFinalize(botContext, requesterUsername, skillLevel) { - logger.log("Assigning issue to", requesterUsername); - - // Pre-flight same-issue collision check: fetch live issue state BEFORE writing. - // The per-issue concurrency key serializes runs on the same issue, so by the - // time this run executes, another user may already be assigned. - let freshIssue; - try { - const response = await botContext.github.rest.issues.get({ - owner: botContext.owner, - repo: botContext.repo, - issue_number: botContext.number, - }); - freshIssue = response.data; - } catch (err) { - const errorMsg = err instanceof Error ? err.message : String(err); - logger.error("Failed to pre-fetch fresh issue state:", errorMsg); - await postComment(botContext, buildApiErrorComment(requesterUsername)); - return; - } - - const alreadyAssigned = freshIssue.assignees?.some( - (a) => a.login === requesterUsername, - ); - - if (alreadyAssigned) { - logger.log("Exit: user is already assigned (caught during fresh fetch)"); - await postComment( - botContext, - buildAlreadyAssignedComment( - requesterUsername, - freshIssue, - botContext.owner, - botContext.repo, - ), - ); - return; - } - - if (freshIssue.assignees?.length > 0) { - logger.log("Exit: issue was assigned by another user while queued"); - await postComment( - botContext, - buildAlreadyAssignedComment( - requesterUsername, - freshIssue, - botContext.owner, - botContext.repo, - ), - ); - return; - } - - // Revalidate labels from the fresh issue snapshot to prevent stale-event - // payload eligibility checks when labels change while this run is queued. - if (!hasLabel(freshIssue, LABELS.READY_FOR_DEV)) { - logger.log("Exit: fresh issue state is missing ready for dev label"); - await postComment( - botContext, - buildNotReadyComment( - requesterUsername, - botContext.owner, - botContext.repo, - ), - ); - return; - } - - const freshSkillLevel = getHighestIssueSkillLevel(freshIssue); - if (!freshSkillLevel) { - logger.log("Exit: fresh issue state has no skill level label"); - await postComment(botContext, buildNoSkillLevelComment(requesterUsername)); - return; - } - - // If the skill level changed while this run was queued, the earlier prerequisite - // and GFI-cap gates were run against a stale label. Abort and ask user to retry. - if (freshSkillLevel !== skillLevel) { - logger.log("Exit: skill level changed while queued", { - stalePayloadLevel: skillLevel, - freshReadLevel: freshSkillLevel, - }); - await postComment( - botContext, - buildSkillLevelChangedComment( - requesterUsername, - skillLevel, - freshSkillLevel, - ), - ); - logger.log("Posted skill-level-changed comment"); - return; - } - - const assignResult = await addAssignees(botContext, [requesterUsername]); - if (!assignResult.success) { - await postComment( - botContext, - buildAssignmentFailureComment(requesterUsername, assignResult.error), - ); - logger.log("Posted assignment failure comment, tagged maintainers"); - return; - } - - await postComment( - botContext, - buildWelcomeComment(requesterUsername, freshSkillLevel), - ); - logger.log("Posted welcome comment"); - await updateLabels(botContext, requesterUsername); - logger.log("Assignment flow completed successfully"); -} - -/** - * Main handler for the /assign command. Runs the following gates in order, posting - * an informative comment and returning early if any gate fails: - * - * 1. Acknowledge the comment with a thumbs-up reaction. - * 2. Issue already assigned? -> already-assigned comment. - * 3. Missing "status: ready for dev" label? -> not-ready comment. - * 4. No skill-level label? -> no-skill-level comment (tags maintainers). - * 5. Open-assignment count API error? -> API-error comment (tags maintainers). - * 6. At or above MAX_OPEN_ASSIGNMENTS? -> limit-exceeded comment. - * 7. GFI cap reached (skill: good first issue only)? -> GFI-limit-exceeded comment. - * 8. Skill prerequisites not met? -> prerequisite-not-met comment. - * 9. Issue snatched while queued (fresh fetch)? -> already-assigned comment. - * 10. Fresh labels invalid while queued? -> not-ready/no-skill-level comment. - * 11. Skill level changed while queued? -> skill-level-changed comment (ask to retry). - * 12. Assignment API failure? -> assignment-failure comment (tags maintainers). - * - * If all checks pass, the bot assigns the issue, posts a welcome comment, - * and swaps the "status: ready for dev" label with "status: in progress". - * - * @param {{ github: object, owner: string, repo: string, number: number, - * issue: object, comment: { id: number, user: { login: string } } }} botContext - * Bot context from buildBotContext (issue_comment event). - * @returns {Promise} - */ -async function handleAssign(botContext) { - const requesterUsername = botContext.comment.user.login; - - const ack = await acknowledgeComment(botContext, botContext.comment.id); - if (!ack.success) { - logger.log( - "Aborting /assign: triggering comment was deleted or could not be acknowledged", - ); - return; - } - - const skillLevel = await validateIssueState(botContext, requesterUsername); - if (!skillLevel) return; - - const withinLimit = await enforceAssignmentLimit( - botContext, - requesterUsername, - ); - if (!withinLimit) return; - - const withinGfiCap = await enforceGfiCompletionLimit( - botContext, - skillLevel, - requesterUsername, - ); - if (!withinGfiCap) return; - - const prereqsPassed = await checkPrerequisites( - botContext, - skillLevel, - requesterUsername, - ); - if (!prereqsPassed) return; - - await assignAndFinalize(botContext, requesterUsername, skillLevel); -} - -module.exports = { handleAssign }; diff --git a/.github/scripts/commands/finalize-comments.cjs b/.github/scripts/commands/finalize-comments.cjs deleted file mode 100644 index 7b69c69f..00000000 --- a/.github/scripts/commands/finalize-comments.cjs +++ /dev/null @@ -1,447 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// commands/finalize-comments.cjs -// -// Comment builders and per-skill-level boilerplate for the /finalize command. -// Pure formatting functions separated from finalize logic for readability and testability. - -const { MAINTAINER_TEAM, LABELS, DOCUMENTATION, COMMUNITY } = require('../helpers'); - -// ============================================================================= -// TITLE PREFIX MAP -// ============================================================================= - -/** - * Maps skill-level label constants to their title prefix strings. - * Used by /finalize to update the issue title. - * @type {Object} - */ -const SKILL_TITLE_PREFIXES = { - [LABELS.GOOD_FIRST_ISSUE]: '[Good First Issue]: ', - [LABELS.BEGINNER]: '[Beginner]: ', - [LABELS.INTERMEDIATE]: '[Intermediate]: ', - [LABELS.ADVANCED]: '[Advanced]: ', -}; - -// ============================================================================= -// SKILL-LEVEL BOILERPLATE BLOCKS -// ============================================================================= -// Each entry mirrors the intro textarea and [!IMPORTANT] markdown block from -// the original skill-level issue YAML templates. Used by /finalize to prepend -// the appropriate skill context to the issue body. - -/** - * Per-skill-level boilerplate for body reconstruction. - * - introLabel: the H3 header label for the intro section - * - introContent: the body of the intro textarea - * - importantBlock: the [!IMPORTANT] GitHub callout markdown - * - * @type {Object} - */ -const SKILL_BOILERPLATE = { - [LABELS.GOOD_FIRST_ISSUE]: { - introLabel: '🆕🐥 First-Time Friendly', - introContent: [ - 'This issue is especially welcoming for people who are new to contributing to the **KDM CLI**.', - '', - 'We know that opening your first pull request can feel like a big step. Issues labeled **Good First Issue** are designed to make that experience easier, clearer, and more comfortable.', - '', - 'No prior knowledge of KDM, Hedera, or distributed ledger technology is required - just a basic familiarity with C++ and Git is more than enough to get started.', - ].join('\n'), - importantBlock: [ - '> [!IMPORTANT]', - '> ### 📋 About Good First Issues', - '>', - '> Good First Issues are designed to make getting started as smooth and stress-free as possible.', - '>', - '> They usually focus on:', - '> - Small, clearly scoped changes ', - '> - Straightforward updates to existing code or docs ', - '> - Simple refactors or clarity improvements ', - '>', - '> Other kinds of contributions — like larger features, deeper technical changes, or design-focused work — are just as valuable and often use the beginner, intermediate, or advanced labels.', - ].join('\n'), - }, - - [LABELS.BEGINNER]: { - introLabel: '🐥 Beginner Friendly', - introContent: [ - 'This issue is a great fit for contributors who are ready to explore the KDM C++ codebase a little more and take on slightly more independent work.', - '', - 'Beginner Issues often involve reading existing C++ code, understanding how different parts of the SDK fit together, and making small, thoughtful updates that follow established patterns.', - '', - 'The goal is to support skill growth while keeping the experience approachable, well-scoped, and enjoyable.', - ].join('\n'), - importantBlock: [ - '> [!IMPORTANT]', - '> ### 🐥 About Beginner Issues', - '>', - '> Beginner Issues are a great next step for contributors who feel comfortable with the basic project workflow and want to explore the codebase a little more.', - '>', - '> These issues often involve:', - '> - Reading existing C++ code ', - '> - Understanding how different parts of the SDK fit together ', - '> - Making small, thoughtful updates that follow established patterns ', - '>', - '> You\'ll usually see Beginner Issues focused on things like:', - '> - Small, well-scoped improvements to existing tests ', - '> - Narrow updates to `src` functionality (e.g. refining helpers or improving readability) ', - '> - Documentation or comment clarity ', - '> - Enhancements to existing examples ', - '>', - '> Other types of contributions — such as brand-new features, broader system changes, or deeper technical work — are just as valuable and may use different labels.', - ].join('\n'), - }, - - [LABELS.INTERMEDIATE]: { - introLabel: '🧩 Intermediate Friendly', - introContent: [ - 'This issue is a good fit for contributors who are already familiar with the KDM CLI and feel comfortable navigating the codebase.', - '', - 'Intermediate Issues often involve:', - '- Exploring existing implementations ', - '- Understanding how different components work together ', - '- Making thoughtful changes that follow established patterns ', - '', - 'The goal is to support deeper problem-solving while keeping the task clear, focused, and enjoyable to work on.', - ].join('\n'), - importantBlock: [ - '> [!IMPORTANT]', - '> ### 🧭 About Intermediate Issues', - '>', - '> Intermediate Issues are a great next step for contributors who enjoy digging into the codebase and reasoning about how things work.', - '>', - '> These issues often:', - '> - Involve multiple related files or components ', - '> - Encourage investigation and understanding of existing behavior ', - '> - Leave room for thoughtful implementation choices ', - '> - Stay focused on a clearly defined goal ', - '>', - '> Other kinds of contributions — from beginner-friendly tasks to large system-level changes — are just as valuable and use different labels.', - ].join('\n'), - }, - - [LABELS.ADVANCED]: { - introLabel: '🧠 Advanced', - introContent: [ - 'This issue is well-suited for contributors who are very familiar with the KDM CLI and enjoy working with its core abstractions and design patterns.', - '', - 'Advanced Issues often involve:', - '- Exploring and shaping SDK architecture ', - '- Reasoning about trade-offs and long-term impact ', - '- Working across multiple modules or systems ', - '- Updating tests, examples, and documentation alongside code ', - '', - 'The goal is to support thoughtful, high-impact contributions in a clear and collaborative way.', - ].join('\n'), - importantBlock: [ - '> [!IMPORTANT]', - '> ### 🧭 About Advanced Issues', - '>', - '> Advanced Issues usually focus on larger changes that influence how the SDK works as a whole.', - '>', - '> These issues often:', - '> - Touch core abstractions or shared utilities ', - '> - Span multiple parts of the codebase ', - '> - Involve design decisions and trade-offs ', - '> - Consider long-term maintainability and compatibility ', - '>', - '> Smaller fixes, focused refactors, and onboarding-friendly tasks are just as valuable and often use different labels.', - ].join('\n'), - }, -}; - -// ============================================================================= -// CONTRIBUTION GUIDE BOILERPLATE -// ============================================================================= - -/** - * Standard contribution guide section appended to all finalized issue bodies. - * Mirrors the "Step-by-Step Contribution Guide" textarea from the skill-level templates. - */ -const CONTRIBUTION_GUIDE_LABEL = '📋 Step-by-Step Contribution Guide'; - -const CONTRIBUTION_GUIDE_CONTENT = [ - 'To help keep contributions consistent and easy to review, we recommend following these steps:', - '', - '- [ ] Comment `/assign` to request the issue', - '- [ ] Wait for assignment', - '- [ ] Fork the repository and create a branch', - '- [ ] Set up the project using the instructions in `README.md`', - '- [ ] Make the requested changes', - '- [ ] Sign each commit using `-s -S`', - '- [ ] Push your branch and open a pull request', - '', - `Read [Workflow Guide](${DOCUMENTATION.workflowGuide}) for step-by-step workflow guidance.`, - `Read [README.md](${DOCUMENTATION.readme}) for setup instructions.`, - '', - '❗ Pull requests **cannot be merged** without `S` and `s` signed commits.', - `See the [Signing Guide](${DOCUMENTATION.signingGuide}).`, -].join('\n'); - -// ============================================================================= -// DEFAULT ADDITIONAL INFORMATION CONTENT -// ============================================================================= - -/** - * Default additional information content used when the submitter left the field - * empty or with the default placeholder ("Optional."). - */ -const DEFAULT_ADDITIONAL_INFO_LABEL = '🤔 Additional Information'; - -const DEFAULT_ADDITIONAL_INFO_CONTENT = [ - 'If you have questions while working on this issue, feel free to ask!', - '', - `You can reach the community and maintainers here: [KDM-CLI Discord](${COMMUNITY.discordChannel})`, - '', - 'Whether you need help finding the right file, understanding existing code, or confirming your approach — we\'re happy to help.', -].join('\n'); - -// ============================================================================= -// BODY PARSING & RECONSTRUCTION -// ============================================================================= - -/** - * Parses an issue body into sections by splitting on H3 (###) markdown headers. - * GitHub form templates render each field's `label` as a `### ` header in the - * submitted issue body, making this a reliable split point. - * - * @param {string} body - The raw issue body string. - * @returns {Array<{ header: string|null, content: string }>} Ordered section list. - * The first entry may have `header: null` if content appears before the first header. - */ -function parseSections(body) { - if (!body || typeof body !== 'string') return []; - - const lines = body.split('\n'); - const sections = []; - let currentHeader = null; - let currentLines = []; - - function flush() { - if (currentHeader !== null || currentLines.some((l) => l.trim())) { - sections.push({ header: currentHeader, content: currentLines.join('\n').trim() }); - } - } - - for (const line of lines) { - const match = line.match(/^### (.+)$/); - if (match) { - flush(); - currentHeader = match[1].trim(); - currentLines = []; - } else { - currentLines.push(line); - } - } - - flush(); - return sections; -} - -/** - * Determines whether the content of a section is non-trivial (i.e., the user - * actually filled it in rather than leaving the default placeholder). - * - * @param {string|undefined} content - The section content string. - * @returns {boolean} True if the content is meaningful. - */ -function isMeaningfulContent(content) { - if (!content || typeof content !== 'string') return false; - const trimmed = content.trim(); - return trimmed.length > 0 && trimmed !== 'Optional.' && trimmed !== '_No response_'; -} - -/** - * Reconstructs the issue body in the skill-level template format. The existing - * body is parsed into sections; skill-level boilerplate is prepended; the - * Contribution Guide boilerplate is appended; Additional Information is moved - * to the very end. - * - * @param {string} existingBody - The current issue body (from the submitted template). - * @param {string} skillLevel - A LABELS skill-level constant (e.g. LABELS.BEGINNER). - * @returns {string} The fully reconstructed issue body. - */ -function reconstructBody(existingBody, skillLevel) { - const boilerplate = SKILL_BOILERPLATE[skillLevel]; - const sections = parseSections(existingBody); - - // Extract "Additional Information" section so it can be placed last - const additionalInfoIdx = sections.findIndex( - (s) => s.header && s.header.toLowerCase().replace(/[^\w\s]/g, '').trim().includes('additional information') - ); - let additionalInfoContent = null; - if (additionalInfoIdx !== -1) { - const [aiSection] = sections.splice(additionalInfoIdx, 1); - if (isMeaningfulContent(aiSection.content)) { - additionalInfoContent = aiSection.content; - } - } - - const parts = []; - - // 1. Skill-level intro block - parts.push(`### ${boilerplate.introLabel}`); - parts.push(''); - parts.push(boilerplate.introContent); - parts.push(''); - parts.push(boilerplate.importantBlock); - parts.push(''); - - // 2. User's sections in original order (skip null-header leading content) - for (const section of sections) { - if (!section.header) continue; - parts.push(`### ${section.header}`); - if (section.content) { - parts.push(''); - parts.push(section.content); - } - parts.push(''); - } - - // 3. Contribution guide boilerplate - parts.push('---'); - parts.push(''); - parts.push(`### ${CONTRIBUTION_GUIDE_LABEL}`); - parts.push(''); - parts.push(CONTRIBUTION_GUIDE_CONTENT); - parts.push(''); - - // 4. Additional information (user-provided or default) - parts.push(`### ${DEFAULT_ADDITIONAL_INFO_LABEL}`); - parts.push(''); - parts.push(additionalInfoContent !== null ? additionalInfoContent : DEFAULT_ADDITIONAL_INFO_CONTENT); - - return parts.join('\n').trimEnd(); -} - -// ============================================================================= -// COMMENT BUILDERS -// ============================================================================= - -/** - * Builds the comment posted when the commenter does not have triage-or-above - * permissions. Lists the required permission level. - * - * @param {string} username - The GitHub username who commented /finalize. - * @returns {string} The formatted Markdown comment body. - */ -function buildUnauthorizedComment(username) { - return [ - `👋 Hi @${username}! The \`/finalize\` command is reserved for maintainers and contributors with **triage** (or higher) repository permissions.`, - '', - 'If you believe you should have access, please reach out to a maintainer.', - ].join('\n'); -} - -/** - * Builds the comment posted when one or more label validation rules are violated. - * Lists every violation found so the reviewer can fix them all in one pass. - * - * @param {string} username - The GitHub username who commented /finalize. - * @param {string[]} errors - Array of human-readable error strings (one per violation). - * @returns {string} The formatted Markdown comment body. - */ -function buildValidationErrorComment(username, errors) { - const errorList = errors.map((e) => `- ${e}`).join('\n'); - return [ - `👋 Hi @${username}! The issue isn't quite ready to finalize yet. Please fix the following labeling issue(s) and then comment \`/finalize\` again:`, - '', - errorList, - '', - 'If you have questions about which labels to apply, see the maintainer documentation or ask in the team channel.', - ].join('\n'); -} - -/** - * Builds the comment posted when the GitHub API call to update the issue - * (title or body) fails. Tags the maintainer team for manual intervention. - * - * @param {string} username - The GitHub username who commented /finalize. - * @param {string} error - The error message from the failed API call. - * @returns {string} The formatted Markdown comment body. - */ -function buildUpdateFailureComment(username, error) { - return [ - `⚠️ Hi @${username}! I encountered an error while trying to update the issue title or body.`, - '', - `${MAINTAINER_TEAM} — could you please complete the finalization manually?`, - '', - `Error details: ${error}`, - ].join('\n'); -} - -/** - * Builds the comment posted when the label swap after a successful update - * fails. Tags the maintainer team with explicit manual steps. - * - * @param {string} username - The GitHub username who commented /finalize. - * @param {string} error - The error message(s) from the failed label operations. - * @returns {string} The formatted Markdown comment body. - */ -function buildLabelSwapFailureComment(username, error) { - return [ - `⚠️ The issue was updated successfully, but I encountered an error swapping the status labels.`, - '', - `${MAINTAINER_TEAM} — please manually:`, - `- Remove the \`${LABELS.AWAITING_TRIAGE}\` label`, - `- Add the \`${LABELS.READY_FOR_DEV}\` label`, - '', - `Error details: ${error}`, - ].join('\n'); -} - -/** - * Builds the comment posted when the permission check API call itself fails. - * Tags the maintainer team for manual assistance. - * - * @param {string} username - The GitHub username who commented /finalize. - * @returns {string} The formatted Markdown comment body. - */ -function buildPermissionCheckErrorComment(username) { - return [ - `👋 Hi @${username}! I encountered an error while trying to verify your permissions.`, - '', - `${MAINTAINER_TEAM} — could you please verify @${username}'s permissions and complete the finalization manually if appropriate?`, - '', - 'Sorry for the inconvenience!', - ].join('\n'); -} - -/** - * Builds the success comment posted after a successful /finalize run. - * - * @param {string} username - The GitHub username who ran /finalize. - * @param {string} skillLevel - The skill-level label that was applied (a LABELS constant). - * @param {string} priorityLabel - The priority label on the issue (e.g. 'priority: medium'). - * @returns {string} The formatted Markdown comment body. - */ -function buildSuccessComment(username, skillLevel, priorityLabel) { - const prefix = SKILL_TITLE_PREFIXES[skillLevel] || ''; - const displayLevel = prefix.replace(/[\][:]/g, '').trim(); - return [ - `✅ Issue finalized by @${username}!`, - '', - `**Skill level:** \`${displayLevel}\``, - `**Priority:** \`${priorityLabel}\``, - '', - 'The issue body has been updated with the appropriate skill-level context and contribution guide. This issue is now ready for contributors to pick up via `/assign`.', - ].join('\n'); -} - -module.exports = { - SKILL_TITLE_PREFIXES, - CONTRIBUTION_GUIDE_LABEL, - CONTRIBUTION_GUIDE_CONTENT, - DEFAULT_ADDITIONAL_INFO_LABEL, - DEFAULT_ADDITIONAL_INFO_CONTENT, - parseSections, - isMeaningfulContent, - reconstructBody, - buildUnauthorizedComment, - buildValidationErrorComment, - buildUpdateFailureComment, - buildLabelSwapFailureComment, - buildPermissionCheckErrorComment, - buildSuccessComment, -}; diff --git a/.github/scripts/commands/finalize.cjs b/.github/scripts/commands/finalize.cjs deleted file mode 100644 index 1e8152b5..00000000 --- a/.github/scripts/commands/finalize.cjs +++ /dev/null @@ -1,285 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// commands/finalize.cjs -// -// /finalize command: transitions a triaged issue from "awaiting triage" to -// "ready for dev". Validates all required labels, updates the issue title with -// the appropriate skill-level prefix, reconstructs the body in the skill-level -// template format, and swaps status labels. -// -// Only contributors with triage-or-above repository permissions may run this command. - -const { - LABELS, - createDelegatingLogger, - hasLabel, - getLabelsByPrefix, - swapLabels, - postComment, - acknowledgeComment, -} = require('../helpers'); - -const { - SKILL_TITLE_PREFIXES, - reconstructBody, - buildUnauthorizedComment, - buildValidationErrorComment, - buildUpdateFailureComment, - buildLabelSwapFailureComment, - buildPermissionCheckErrorComment, - buildSuccessComment, -} = require('./finalize-comments'); - -// Delegate to the active logger set by the dispatcher (bot-on-comment.cjs). -const logger = createDelegatingLogger(); - -// Permission levels that are allowed to run /finalize (triage and above). -const ALLOWED_ROLE_NAMES = new Set(['triage', 'write', 'maintain', 'admin']); - -// Regex that strips any existing skill-level title prefix (e.g. "[Beginner]: "). -// Built from SKILL_TITLE_PREFIXES so it stays in sync if new levels are added. -const EXISTING_PREFIX_RE = new RegExp( - `^\\[(${Object.values(SKILL_TITLE_PREFIXES) - .map((p) => p.match(/^\[(.+)\]:/)?.[1]) - .filter(Boolean) - .join('|')})\\]:\\s*`, - 'i' -); - -// Recognized GitHub issue type names set by our three templates. -const KNOWN_ISSUE_TYPES = new Set(['Bug', 'Feature', 'Task']); - -// ============================================================================= -// PERMISSION CHECK -// ============================================================================= - -/** - * Checks whether the commenter has triage-or-above repository permissions. - * Uses the GitHub REST API `getCollaboratorPermissionLevel` endpoint, which - * returns a `role_name` of 'read' | 'triage' | 'write' | 'maintain' | 'admin'. - * Non-collaborators (HTTP 404) are treated as unauthorized. - * - * @param {object} botContext - Bot context from buildBotContext. - * @param {string} username - GitHub username to check. - * @returns {Promise<'authorized'|'unauthorized'|'error'>} - */ -async function checkPermission(botContext, username) { - try { - const { data } = await botContext.github.rest.repos.getCollaboratorPermissionLevel({ - owner: botContext.owner, - repo: botContext.repo, - username, - }); - const roleName = data?.role_name; - logger.log(`Permission check for @${username}: role_name="${roleName}"`); - if (ALLOWED_ROLE_NAMES.has(roleName)) return 'authorized'; - return 'unauthorized'; - } catch (error) { - const status = error?.status ?? error?.response?.status; - if (status === 404) { - logger.log(`@${username} is not a collaborator (404) — unauthorized`); - return 'unauthorized'; - } - logger.error(`Permission check failed for @${username}:`, error.message); - return 'error'; - } -} - -// ============================================================================= -// LABEL VALIDATION -// ============================================================================= - -/** - * Returns the GitHub issue type name ('Bug', 'Feature', 'Task') from the issue - * payload, or null if not set or unrecognized. - * - * @param {object} issue - The issue object from the GitHub payload. - * @returns {string|null} - */ -function getIssueTypeName(issue) { - const name = issue?.type?.name; - if (typeof name === 'string' && KNOWN_ISSUE_TYPES.has(name)) return name; - return null; -} - -/** - * Formats an array of label names as a comma-separated inline code list. - * @param {string[]} labels - * @returns {string} e.g. "`skill: beginner`, `skill: intermediate`" - */ -function formatLabelList(labels) { - return labels.map((l) => `\`${l}\``).join(', '); -} - -/** - * Collects all label validation violations for /finalize. Returns an empty - * array when everything is valid. - * - * Rules: - * - status: awaiting triage must be present - * - exactly 1 skill: label - * - exactly 1 priority: label - * - issue type must be Bug, Feature, or Task - * - * @param {object} issue - The issue object from the GitHub payload. - * @returns {string[]} Array of human-readable error strings (one per violation). - */ -function collectLabelViolations(issue) { - const errors = []; - - const skillLabels = getLabelsByPrefix(issue, 'skill:'); - const priorityLabels = getLabelsByPrefix(issue, 'priority:'); - - const issueTypeName = getIssueTypeName(issue); - - // 1. status: awaiting triage must be present - if (!hasLabel(issue, LABELS.AWAITING_TRIAGE)) { - const statusLabels = getLabelsByPrefix(issue, 'status:'); - const currentStatus = statusLabels.length > 0 ? formatLabelList(statusLabels) : 'none'; - errors.push( - `The \`${LABELS.AWAITING_TRIAGE}\` label must be present to run \`/finalize\`. Current status label(s): ${currentStatus}.` - ); - } - - // 2. Exactly 1 skill: label - if (skillLabels.length === 0) { - errors.push( - `Exactly one \`skill:\` label is required (e.g. \`skill: beginner\`). None found. Choose from: \`${LABELS.GOOD_FIRST_ISSUE}\`, \`${LABELS.BEGINNER}\`, \`${LABELS.INTERMEDIATE}\`, \`${LABELS.ADVANCED}\`.` - ); - } else if (skillLabels.length > 1) { - errors.push( - `Exactly one \`skill:\` label is required. Found ${skillLabels.length}: ${formatLabelList(skillLabels)}. Please remove all but one.` - ); - } - - // 3. Exactly 1 priority: label - if (priorityLabels.length === 0) { - errors.push( - `Exactly one \`priority:\` label is required (e.g. \`priority: medium\`). None found.` - ); - } else if (priorityLabels.length > 1) { - errors.push( - `Exactly one \`priority:\` label is required. Found ${priorityLabels.length}: ${formatLabelList(priorityLabels)}. Please remove all but one.` - ); - } - - // 4. Issue type must be recognized - if (!issueTypeName) { - errors.push( - `The issue type (Bug, Feature, or Task) could not be determined. Ensure the issue was submitted using one of the official issue templates.` - ); - } - - return errors; -} - -// ============================================================================= -// TITLE UPDATE -// ============================================================================= - -/** - * Builds the new issue title by stripping any existing skill-level prefix and - * prepending the correct one for the given skill label. - * - * @param {string} currentTitle - The current issue title. - * @param {string} skillLevel - A LABELS skill-level constant. - * @returns {string} The updated title. - */ -function buildNewTitle(currentTitle, skillLevel) { - const strippedTitle = currentTitle.replace(EXISTING_PREFIX_RE, '').trim(); - const prefix = SKILL_TITLE_PREFIXES[skillLevel] || ''; - return `${prefix}${strippedTitle}`; -} - -// ============================================================================= -// MAIN HANDLER -// ============================================================================= - -/** - * Main handler for the /finalize command. Runs the following steps in order, - * posting an informative comment and returning early if any step fails: - * - * 1. Acknowledge the comment with a thumbs-up reaction. - * 2. Check commenter has triage+ permissions → unauthorized / API error comment. - * 3. Collect all label violations → validation error comment listing all issues. - * 4. Determine skill level and build updated title + body. - * 5. Update the issue via the GitHub API → update failure comment on error. - * 6. Swap status labels: awaiting triage → ready for dev. - * 7. Post success comment. - * - * @param {{ github: object, owner: string, repo: string, number: number, - * issue: object, comment: { id: number, user: { login: string } } }} botContext - * Bot context from buildBotContext (issue_comment event). - * @returns {Promise} - */ -async function handleFinalize(botContext) { - const finalizerUsername = botContext.comment.user.login; - - // STEP 1: Acknowledge - await acknowledgeComment(botContext, botContext.comment.id); - - // STEP 2: Permission check - const permResult = await checkPermission(botContext, finalizerUsername); - if (permResult === 'error') { - logger.log('Exit: permission check API error'); - await postComment(botContext, buildPermissionCheckErrorComment(finalizerUsername)); - return; - } - if (permResult === 'unauthorized') { - logger.log(`Exit: @${finalizerUsername} is not authorized`); - await postComment(botContext, buildUnauthorizedComment(finalizerUsername)); - return; - } - - // STEP 3: Label validation — collect ALL violations before posting - const violations = collectLabelViolations(botContext.issue); - if (violations.length > 0) { - logger.log(`Exit: ${violations.length} label violation(s) found`); - await postComment(botContext, buildValidationErrorComment(finalizerUsername, violations)); - return; - } - - // STEP 4: Determine skill level + build new title and body - const skillLabels = getLabelsByPrefix(botContext.issue, 'skill:'); - const skillLevel = skillLabels[0]; // validated above: exactly 1 exists - - const newTitle = buildNewTitle(botContext.issue.title, skillLevel); - const newBody = reconstructBody(botContext.issue.body || '', skillLevel); - - logger.log(`Updating issue #${botContext.number}: title="${newTitle}", skillLevel="${skillLevel}"`); - - // STEP 5: Update issue title and body - let updateError = null; - try { - await botContext.github.rest.issues.update({ - owner: botContext.owner, - repo: botContext.repo, - issue_number: botContext.number, - title: newTitle, - body: newBody, - }); - logger.log('Issue updated successfully'); - } catch (error) { - updateError = error instanceof Error ? error.message : String(error); - logger.error('Issue update failed:', updateError); - } - - if (updateError) { - await postComment(botContext, buildUpdateFailureComment(finalizerUsername, updateError)); - return; - } - - // STEP 6: Swap status labels: awaiting triage → ready for dev - const swapResult = await swapLabels(botContext, LABELS.AWAITING_TRIAGE, LABELS.READY_FOR_DEV); - if (!swapResult.success) { - await postComment(botContext, buildLabelSwapFailureComment(finalizerUsername, swapResult.errorDetails)); - logger.log('Posted label swap failure comment, tagged maintainers'); - } - - // STEP 7: Post success comment - const priorityLabel = getLabelsByPrefix(botContext.issue, 'priority:')[0]; - await postComment(botContext, buildSuccessComment(finalizerUsername, skillLevel, priorityLabel)); - logger.log('Finalize flow completed successfully'); -} - -module.exports = { handleFinalize }; diff --git a/.github/scripts/commands/unassign-comments.cjs b/.github/scripts/commands/unassign-comments.cjs deleted file mode 100644 index f88ba95e..00000000 --- a/.github/scripts/commands/unassign-comments.cjs +++ /dev/null @@ -1,84 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// commands/unassign-comments.cjs -// -// Comment builders for the /unassign command. Pure formatting functions -// separated from unassignment logic for readability. - -const { MAINTAINER_TEAM, LABELS } = require('../helpers'); - -/** - * Builds the comment posted after a successful unassignment. - * - * @param {string} username - The GitHub username being unassigned. - * @returns {string} The formatted Markdown comment body. - */ -function buildSuccessfulUnassignComment(username) { - return [ - `👋 Hi @${username}! You have been successfully unassigned from this issue.`, - '', - `The \`${LABELS.IN_PROGRESS}\` label has been removed, and it is now back to \`${LABELS.READY_FOR_DEV}\` for others to claim. Thanks for letting us know!`, - ].join('\n'); -} - -/** - * Builds the comment posted when a user tries to unassign an issue they don't own. - * - * @param {string} requesterUsername - The GitHub username who commented /unassign. - * @param {string} currentAssignee - The GitHub username of the actual assignee. - * @returns {string} The formatted Markdown comment body. - */ -function buildNotAssignedToUserComment(requesterUsername, currentAssignee) { - const assigneeText = currentAssignee ? `@${currentAssignee}` : 'someone else'; - return [ - `⚠️ Hi @${requesterUsername}! You cannot unassign this issue because it is currently assigned to ${assigneeText}.`, - '', - 'Only the current assignee can unassign themselves.', - ].join('\n'); -} - -/** - * Builds the comment posted when the issue has no assignees. - * - * @param {string} requesterUsername - The GitHub username who commented /unassign. - * @returns {string} The formatted Markdown comment body. - */ -function buildNoAssigneeComment(requesterUsername) { - return [ - `👋 Hi @${requesterUsername}! This issue doesn't currently have any assignees.`, - ].join('\n'); -} - -/** - * Builds the comment posted when the issue is already closed. - * - * @param {string} requesterUsername - The GitHub username who commented /unassign. - * @returns {string} The formatted Markdown comment body. - */ -function buildIssueClosedComment(requesterUsername) { - return [ - `👋 Hi @${requesterUsername}! This issue is already closed, so the \`/unassign\` command cannot be used here.`, - ].join('\n'); -} - -/** - * Builds the comment posted when the unassign API call fails. - * - * @param {string} requesterUsername - The GitHub username who commented /unassign. - * @returns {string} The formatted Markdown comment body. - */ -function buildUnassignFailureComment(requesterUsername) { - return [ - `⚠️ Hi @${requesterUsername}! I tried to unassign you, but encountered an unexpected error.`, - '', - `${MAINTAINER_TEAM} — could you please manually unassign @${requesterUsername}?`, - ].join('\n'); -} - -module.exports = { - buildSuccessfulUnassignComment, - buildNotAssignedToUserComment, - buildNoAssigneeComment, - buildIssueClosedComment, - buildUnassignFailureComment, -}; \ No newline at end of file diff --git a/.github/scripts/commands/unassign.cjs b/.github/scripts/commands/unassign.cjs deleted file mode 100644 index dc4ed113..00000000 --- a/.github/scripts/commands/unassign.cjs +++ /dev/null @@ -1,98 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// commands/unassign.cjs -// -// /unassign command: allows a currently assigned contributor to unassign themselves. -// Enforces authorization (only assignees can unassign themselves) and reverts -// status labels back to the community pool. - -const { - LABELS, - ISSUE_STATE, - createDelegatingLogger, - swapLabels, - removeAssignees, - postComment, - acknowledgeComment, -} = require('../helpers'); - -const { - buildSuccessfulUnassignComment, - buildNotAssignedToUserComment, - buildNoAssigneeComment, - buildIssueClosedComment, - buildUnassignFailureComment, -} = require('./unassign-comments'); - -// Delegate to the active logger set by the dispatcher. -const logger = createDelegatingLogger(); - -/** - * Main handler for the /unassign command. Runs the following gates in order: - * - * 1. Acknowledge the comment with a thumbs-up reaction. - * 2. Is the issue already closed? -> issue-closed comment. - * 3. Does the issue have no assignees? -> no-assignee comment. - * 4. Is the commenter not the current assignee? -> unauthorized comment. - * - * On success: removes the user as an assignee, reverts the "in progress" - * label to "ready for dev", and posts an acknowledgment comment. - * - * @param {{ github: object, owner: string, repo: string, number: number, - * issue: object, comment: { user: { login: string } } }} botContext - * @returns {Promise} - */ -async function handleUnassign(botContext) { - const requesterUsername = botContext.comment.user.login; - const issue = botContext.issue; - - await acknowledgeComment(botContext, botContext.comment.id); - - // GATE 1: Issue is closed - if (issue.state === ISSUE_STATE.CLOSED) { - logger.log('Exit: issue is closed'); - await postComment(botContext, buildIssueClosedComment(requesterUsername)); - return; - } - - const assignees = issue.assignees || []; - - // GATE 2: No one is assigned at all - if (assignees.length === 0) { - logger.log('Exit: issue has no assignees'); - await postComment(botContext, buildNoAssigneeComment(requesterUsername)); - return; - } - - // GATE 3: Authorization check (case-insensitive) - const isAssigned = assignees.some( - (a) => (a?.login || '').toLowerCase() === requesterUsername.toLowerCase() - ); - if (!isAssigned) { - logger.log(`Exit: @${requesterUsername} is not assigned to this issue`); - const currentAssignee = assignees[0]?.login; // Grab the actual assignee for the message - await postComment(botContext, buildNotAssignedToUserComment(requesterUsername, currentAssignee)); - return; - } - - // ACTION 1: Remove the assignee - logger.log(`Unassigning @${requesterUsername}`); - const removeResult = await removeAssignees(botContext, [requesterUsername]); - if (!removeResult.success) { - await postComment(botContext, buildUnassignFailureComment(requesterUsername)); - return; - } - - // ACTION 2: Label Swapping (Mirroring assign.cjs style - no stale checks) - logger.log(`Swapping labels: removing ${LABELS.IN_PROGRESS}, adding ${LABELS.READY_FOR_DEV}`); - const { success: swapSuccess, errorDetails: swapError } = await swapLabels(botContext, LABELS.IN_PROGRESS, LABELS.READY_FOR_DEV); - if (!swapSuccess) { - logger.error(`Label swap failed: ${swapError}`); - } - - // ACTION 3: Post success acknowledgment - await postComment(botContext, buildSuccessfulUnassignComment(requesterUsername)); - logger.log(`Successfully unassigned @${requesterUsername} and reverted labels`); -} - -module.exports = { handleUnassign }; \ No newline at end of file diff --git a/.github/scripts/eslint.config.mjs b/.github/scripts/eslint.config.mjs deleted file mode 100644 index 6f22a6da..00000000 --- a/.github/scripts/eslint.config.mjs +++ /dev/null @@ -1,13 +0,0 @@ -import js from "@eslint/js"; -import globals from "globals"; - -export default [ - js.configs.recommended, - { - languageOptions: { - globals: { - ...globals.node, - }, - }, - }, -]; diff --git a/.github/scripts/helpers/api.cjs b/.github/scripts/helpers/api.cjs index 9ea9f85d..6ebb3447 100644 --- a/.github/scripts/helpers/api.cjs +++ b/.github/scripts/helpers/api.cjs @@ -1,11 +1,11 @@ // SPDX-License-Identifier: Apache-2.0 const { getLogger } = require('./logger.cjs'); -const { isSafeSearchToken, requireObject, requireNonEmptyString, requirePositiveInt, requireSafeUsername } = require('./validation.cjs'); -const { LABELS, SKILL_HIERARCHY, ISSUE_STATE } = require('./constants.cjs'); -const { checkDCO, checkGPG, checkMergeConflict, checkIssueLink } = require('./checks.cjs'); -const { buildBotComment } = require('./comments.cjs'); +const { isSafeSearchToken, requireObject, requireNonEmptyString, requirePositiveInt } = require('./validation.cjs'); +/** + * Validates GitHub Action event context and normalizes owner, repo, number, and payload details. + */ function buildBotContext({ github, context }) { requireObject(github, 'github'); requireObject(context, 'context'); @@ -71,11 +71,19 @@ function buildBotContext({ github, context }) { return { ...base, eventType, ...payloadPart }; } +/** + * Adds labels to the target issue or pull request. + */ async function addLabels(botContext, labels) { if (!Array.isArray(labels)) return { success: false, error: 'labels must be an array' }; try { for (let i = 0; i < labels.length; i++) requireNonEmptyString(labels[i], `labels[${i}]`); - await botContext.github.rest.issues.addLabels({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, labels }); + await botContext.github.rest.issues.addLabels({ + owner: botContext.owner, + repo: botContext.repo, + issue_number: botContext.number, + labels, + }); getLogger().log(`Added labels: ${labels.join(', ')}`); return { success: true }; } catch (error) { @@ -84,56 +92,9 @@ async function addLabels(botContext, labels) { } } -async function removeLabel(botContext, labelName) { - try { - requireNonEmptyString(labelName, 'labelName'); - await botContext.github.rest.issues.removeLabel({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, name: labelName }); - getLogger().log(`Removed label: ${labelName}`); - return { success: true }; - } catch (error) { - getLogger().error(`Could not remove label "${labelName}": ${error.message}`); - return { success: false, error: error.message }; - } -} - -async function addAssignees(botContext, assignees) { - if (!Array.isArray(assignees)) return { success: false, error: 'assignees must be an array' }; - try { - for (let i = 0; i < assignees.length; i++) requireSafeUsername(assignees[i], `assignees[${i}]`); - await botContext.github.rest.issues.addAssignees({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, assignees }); - getLogger().log(`Added assignees: ${assignees.join(', ')}`); - return { success: true }; - } catch (error) { - getLogger().error(`Could not add assignees "${assignees.join(', ')}": ${error.message}`); - return { success: false, error: error.message }; - } -} - -async function removeAssignees(botContext, assignees) { - if (!Array.isArray(assignees)) return { success: false, error: 'assignees must be an array' }; - try { - for (let i = 0; i < assignees.length; i++) requireSafeUsername(assignees[i], `assignees[${i}]`); - await botContext.github.rest.issues.removeAssignees({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, assignees }); - getLogger().log(`Removed assignees: ${assignees.join(', ')}`); - return { success: true }; - } catch (error) { - getLogger().error(`Could not remove assignees "${assignees.join(', ')}": ${error.message}`); - return { success: false, error: error.message }; - } -} - -async function postComment(botContext, body) { - try { - requireNonEmptyString(body, 'comment body'); - await botContext.github.rest.issues.createComment({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, body }); - getLogger().log('Posted comment'); - return { success: true }; - } catch (error) { - getLogger().error(`Could not post comment: ${error.message}`); - return { success: false, error: error.message }; - } -} - +/** + * Updates the pull request title. + */ async function updatePullRequest(botContext, title) { try { requireNonEmptyString(title, 'title'); @@ -155,341 +116,8 @@ async function updatePullRequest(botContext, title) { } } -function hasLabel(issueOrPr, labelName) { - if (!issueOrPr?.labels?.length) return false; - return issueOrPr.labels.some((label) => { - const name = typeof label === 'string' ? label : label?.name; - return typeof name === 'string' && name.toLowerCase() === labelName.toLowerCase(); - }); -} - -function getLabelsByPrefix(issueOrPr, prefix) { - return (issueOrPr.labels || []) - .map((l) => (typeof l === 'string' ? l : l?.name || '')) - .filter((name) => name.toLowerCase().startsWith(prefix.toLowerCase())); -} - -async function swapLabels(botContext, fromLabel, toLabel) { - const errors = []; - const removeResult = await removeLabel(botContext, fromLabel); - if (!removeResult.success) errors.push(`Failed to remove '${fromLabel}': ${removeResult.error}`); - const addResult = await addLabels(botContext, [toLabel]); - if (!addResult.success) errors.push(`Failed to add '${toLabel}': ${addResult.error}`); - return { success: errors.length === 0, errorDetails: errors.join('; ') }; -} - -async function getBotComment(botContext, marker) { - let page = 1; - const perPage = 100; - while (true) { - const { data: comments } = await botContext.github.rest.issues.listComments({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, per_page: perPage, page }); - for (const c of comments) { if (c.body && c.body.startsWith(marker)) return c; } - if (comments.length < perPage) break; - page++; - } - return null; -} - -async function postOrUpdateComment(botContext, marker, body) { - try { - requireNonEmptyString(marker, 'marker'); - requireNonEmptyString(body, 'comment body'); - const existing = await getBotComment(botContext, marker); - if (existing) { - if (existing.body.trim() === body.trim()) { - getLogger().log('Existing bot comment is up-to-date'); - } else { - await botContext.github.rest.issues.updateComment({ owner: botContext.owner, repo: botContext.repo, comment_id: existing.id, body }); - getLogger().log('Updated existing bot comment'); - } - } else { - await botContext.github.rest.issues.createComment({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, body }); - getLogger().log('Created new bot comment'); - } - return { success: true }; - } catch (error) { - getLogger().error(`Could not post/update comment: ${error.message}`); - return { success: false, error: error.message }; - } -} - -async function fetchPRCommits(botContext) { - const commits = []; - let page = 1; - while (true) { - const response = await botContext.github.rest.pulls.listCommits({ owner: botContext.owner, repo: botContext.repo, pull_number: botContext.number, per_page: 100, page }); - commits.push(...response.data); - if (response.data.length < 100) break; - page++; - } - getLogger().log(`Fetched ${commits.length} commits for PR #${botContext.number}`); - return commits; -} - -async function fetchOpenPRs(botContext) { - const prs = []; - let page = 1; - while (true) { - const response = await botContext.github.rest.pulls.list({ owner: botContext.owner, repo: botContext.repo, state: 'open', per_page: 100, page }); - prs.push(...response.data); - if (response.data.length < 100) break; - page++; - } - getLogger().log(`Fetched ${prs.length} open PRs`); - return prs; -} - -async function fetchIssue(botContext, issueNumber) { - const { data: issue } = await botContext.github.rest.issues.get({ owner: botContext.owner, repo: botContext.repo, issue_number: issueNumber }); - return issue; -} - -async function fetchClosingIssueNumbers(botContext) { - try { - const query = `query($owner:String!,$repo:String!,$number:Int!){ - repository(owner:$owner,name:$repo){ - pullRequest(number:$number){ - closingIssuesReferences(first:10){ nodes { number } } - } - } - }`; - const result = await botContext.github.graphql(query, { owner: botContext.owner, repo: botContext.repo, number: botContext.number }); - const nodes = result.repository.pullRequest.closingIssuesReferences.nodes || []; - return nodes.map(n => n.number); - } catch (error) { - getLogger().error(`GraphQL closingIssuesReferences failed: ${error.message}`); - return []; - } -} - -async function fetchLatestMilestone(botContext) { - const milestones = []; - let page = 1; - try { - while (true) { - const { data } = await botContext.github.rest.issues.listMilestones({ owner: botContext.owner, repo: botContext.repo, state: 'open', sort: 'due_on', direction: 'desc', per_page: 100, page }); - milestones.push(...data); - if (data.length < 100) break; - page++; - } - } catch (error) { - getLogger().error(`Could not fetch milestones: ${error.message}`); - return null; - } - if (!milestones.length) { getLogger().log('No open milestones found'); return null; } - const withDue = milestones.filter(m => m.due_on); - return withDue.length ? withDue.sort((a, b) => new Date(b.due_on) - new Date(a.due_on))[0] : [...milestones].sort((a, b) => b.number - a.number)[0]; -} - -async function setMilestone(botContext, issueOrPrNumber, milestoneNumber) { - try { - requirePositiveInt(issueOrPrNumber, 'issueOrPrNumber'); - requirePositiveInt(milestoneNumber, 'milestoneNumber'); - await botContext.github.rest.issues.update({ owner: botContext.owner, repo: botContext.repo, issue_number: issueOrPrNumber, milestone: milestoneNumber }); - getLogger().log(`Set milestone #${milestoneNumber} on #${issueOrPrNumber}`); - return { success: true }; - } catch (error) { - getLogger().error(`Could not set milestone on #${issueOrPrNumber}: ${error.message}`); - return { success: false, error: error.message }; - } -} - -async function swapStatusLabel(botContext, allPassed, { force = false } = {}) { - const pr = botContext.pr; - const toAdd = allPassed ? LABELS.NEEDS_REVIEW : LABELS.NEEDS_REVISION; - const toRemove = allPassed ? LABELS.NEEDS_REVISION : LABELS.NEEDS_REVIEW; - const errors = []; - if (force || hasLabel(pr, toRemove)) { - const r = await removeLabel(botContext, toRemove); - if (!r.success) errors.push(`Failed to remove '${toRemove}': ${r.error}`); - } - if (force || hasLabel(pr, toRemove)) { - const a = await addLabels(botContext, [toAdd]); - if (!a.success) errors.push(`Failed to add '${toAdd}': ${a.error}`); - } - return { success: errors.length === 0, errorDetails: errors.join('; ') }; -} - -async function acknowledgeComment(botContext, commentId) { - try { - await botContext.github.rest.reactions.createForIssueComment({ owner: botContext.owner, repo: botContext.repo, comment_id: commentId, content: '+1' }); - getLogger().log('Added thumbs-up reaction to comment'); - return { success: true }; - } catch (error) { - getLogger().log('Could not add reaction:', error.message); - return { success: false }; - } -} - -async function runAllChecksAndComment(botContext, precomputed = {}) { - let { merge, issueLink } = precomputed; - if (!merge) { try { merge = await checkMergeConflict(botContext); } catch (e) { merge = { error: true, errorMessage: e.message }; } } - if (!issueLink) { try { issueLink = await checkIssueLink(botContext, { fetchIssue, fetchClosingIssueNumbers }); } catch (e) { issueLink = { error: true, errorMessage: e.message }; } } - const prAuthor = botContext.pr?.user?.login; - const { marker, body, allPassed } = buildBotComment({ prAuthor, merge, issueLink }); - await postOrUpdateComment(botContext, marker, body); - return { allPassed }; -} - -async function fetchIssueEvents(botContext) { - const events = []; - let page = 1; - while (true) { - const { data } = await botContext.github.rest.issues.listEvents({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, per_page: 100, page }); - events.push(...data); - if (data.length < 100) break; - page++; - } - getLogger().log(`Fetched ${events.length} events for #${botContext.number}`); - return events; -} - -async function fetchComments(botContext) { - const comments = []; - let page = 1; - while (true) { - const { data } = await botContext.github.rest.issues.listComments({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, per_page: 100, page }); - comments.push(...data); - if (data.length < 100) break; - page++; - } - getLogger().log(`Fetched ${comments.length} comments for #${botContext.number}`); - return comments; -} - -async function closeItem(botContext) { - try { - await botContext.github.rest.issues.update({ owner: botContext.owner, repo: botContext.repo, issue_number: botContext.number, state: 'closed' }); - getLogger().log(`Closed #${botContext.number}`); - return { success: true }; - } catch (error) { - getLogger().error(`Could not close #${botContext.number}: ${error.message}`); - return { success: false, error: error.message }; - } -} - -async function resolveLinkedIssue(botContext) { - try { - const issueNumbers = await fetchClosingIssueNumbers(botContext); - if (!issueNumbers.length) { getLogger().log('No linked issue found', { prNumber: botContext.number }); return null; } - if (issueNumbers.length === 1) { - const issue = await fetchIssue(botContext, issueNumbers[0]); - if (!issue || SKILL_HIERARCHY.findIndex(level => hasLabel(issue, level)) === -1) { getLogger().log('Single linked issue has no skill label', { issueNumber: issueNumbers[0] }); return null; } - return issue; - } - const issues = (await Promise.all(issueNumbers.map(n => fetchIssue(botContext, n)))).filter(Boolean); - if (!issues.length) { getLogger().log('All linked issue fetches returned empty', { issueNumbers }); return null; } - const selected = issues.reduce((best, issue) => { - const bestIdx = SKILL_HIERARCHY.findIndex(level => hasLabel(best, level)); - const curIdx = SKILL_HIERARCHY.findIndex(level => hasLabel(issue, level)); - return curIdx > bestIdx ? issue : best; - }); - if (SKILL_HIERARCHY.findIndex(level => hasLabel(selected, level)) === -1) { getLogger().log('No linked issues have a skill label', { issueNumbers }); return null; } - getLogger().log('Multiple linked issues found (using highest level)', { issueNumbers, selected: selected.number }); - return selected; - } catch (error) { - getLogger().error('Failed to resolve linked issue:', { message: error.message }); - return null; - } -} - -function getHighestIssueSkillLevel(issue) { - for (const level of [...SKILL_HIERARCHY].reverse()) { if (hasLabel(issue, level)) return level; } - return null; -} - -async function countIssuesByAssignee(github, owner, repo, username, state, label = null, threshold = null) { - if (!isSafeSearchToken(owner) || !isSafeSearchToken(repo) || !isSafeSearchToken(username)) { - getLogger().log('[assign] Invalid search inputs:', { owner, repo, username, label }); return null; - } - if (state !== ISSUE_STATE.OPEN && state !== ISSUE_STATE.CLOSED) { getLogger().log('[assign] Invalid state:', { state }); return null; } - if (label && (typeof label !== 'string' || !label.trim() || label.includes('"'))) { getLogger().log('[assign] Invalid label parameter:', { label }); return null; } - try { - let page = 1, matchingIssuesCount = 0; - getLogger().log(`[assign] Fetching ${state} assigned issues via REST...`); - while (true) { - const params = { owner, repo, state: state.toLowerCase(), assignee: username, per_page: 100, page }; - if (label) params.labels = label; - const result = await github.rest.issues.listForRepo(params); - const actualIssues = result.data.filter(item => !item.pull_request); - let pageMatchCount = 0; - if (state === ISSUE_STATE.OPEN && !label) { - pageMatchCount = actualIssues.filter(issue => !issue.labels?.some(l => (l.name || l) === LABELS.BLOCKED)).length; - } else { pageMatchCount = actualIssues.length; } - matchingIssuesCount += pageMatchCount; - if (threshold !== null && matchingIssuesCount >= threshold) { - getLogger().log(`[assign] Reached threshold (${threshold}), short-circuiting fetch.`); - matchingIssuesCount = threshold; - break; - } - if (result.data.length < 100) break; - page++; - } - const suffix = state === ISSUE_STATE.OPEN && !label ? ' (excluding blocked)' : ''; - getLogger().log(`[assign] ${state} assigned issues for ${username}${label ? ` with label ${label}` : suffix}: ${matchingIssuesCount}`); - return matchingIssuesCount; - } catch (error) { - getLogger().log(`[assign] Failed to count ${state} issues for ${username}: ${error instanceof Error ? error.message : String(error)}`); - return null; - } -} - -async function listAssignedIssues(github, owner, repo, username) { - if (!isSafeSearchToken(owner) || !isSafeSearchToken(repo) || !isSafeSearchToken(username)) { - getLogger().log('[assign] Invalid search inputs for listAssignedIssues:', { owner, repo, username }); return null; - } - try { - let page = 1; - const issues = []; - getLogger().log('[assign] Fetching open assigned issues via REST (objects)...'); - while (true) { - const result = await github.rest.issues.listForRepo({ owner, repo, state: ISSUE_STATE.OPEN, assignee: username, per_page: 100, page }); - const actualIssues = result.data.filter(item => !item.pull_request); - const nonBlocked = actualIssues.filter(issue => !issue.labels?.some(l => (l.name || l) === LABELS.BLOCKED)); - issues.push(...nonBlocked); - if (result.data.length < 100) break; - page++; - } - getLogger().log(`[assign] Open non-blocked assigned issues for ${username}: ${issues.length}`); - return issues; - } catch (error) { - getLogger().log(`[assign] Failed to list assigned issues for ${username}: ${error instanceof Error ? error.message : String(error)}`); - return null; - } -} - -// closedByPullRequestsReferences only catches closing-keyword PRs -// (Fixes/Closes #N). Sidebar-linked PRs are invisible here. -async function hasNeedsReviewPR(github, owner, repo, username, issueNumber) { - if (!isSafeSearchToken(owner) || !isSafeSearchToken(repo) || !isSafeSearchToken(username)) { - getLogger().log('[assign] Invalid search inputs for hasNeedsReviewPR:', { owner, repo, username, issueNumber }); return null; - } - if (!Number.isInteger(issueNumber) || issueNumber < 1) { getLogger().log('[assign] Invalid issue number for hasNeedsReviewPR:', { issueNumber }); return null; } - try { - getLogger().log(`[assign] Querying linked PRs for issue #${issueNumber}`); - const query = `query($owner:String!,$repo:String!,$number:Int!){ - repository(owner:$owner,name:$repo){ - issue(number:$number){ - closedByPullRequestsReferences(first:50){ - nodes { state, author { login }, labels(first:50) { nodes { name } } } - } - } - } - }`; - const result = await github.graphql(query, { owner, repo, number: issueNumber }); - const nodes = result.repository?.issue?.closedByPullRequestsReferences?.nodes || []; - const match = nodes.some(pr => pr.author?.login === username && pr.state === 'OPEN' && pr.labels?.nodes?.some(l => l.name === LABELS.NEEDS_REVIEW)); - getLogger().log(`[assign] Needs-review PR search for issue #${issueNumber}: ${match ? 1 : 0} match(es)`); - return match; - } catch (error) { - getLogger().log(`[assign] Failed to search for needs-review PRs for issue #${issueNumber}: ${error instanceof Error ? error.message : String(error)}`); - return null; - } -} - module.exports = { - buildBotContext, addLabels, removeLabel, addAssignees, removeAssignees, postComment, updatePullRequest, hasLabel, getLabelsByPrefix, - swapLabels, getBotComment, postOrUpdateComment, fetchPRCommits, fetchOpenPRs, fetchIssue, fetchClosingIssueNumbers, - fetchLatestMilestone, setMilestone, swapStatusLabel, runAllChecksAndComment, resolveLinkedIssue, acknowledgeComment, - fetchComments, fetchIssueEvents, closeItem, getHighestIssueSkillLevel, countIssuesByAssignee, listAssignedIssues, hasNeedsReviewPR, + buildBotContext, + addLabels, + updatePullRequest, }; diff --git a/.github/scripts/helpers/checks.cjs b/.github/scripts/helpers/checks.cjs deleted file mode 100644 index 1a448e3a..00000000 --- a/.github/scripts/helpers/checks.cjs +++ /dev/null @@ -1,110 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -const { getLogger } = require('./logger.cjs'); - -function hasDCOSignoff(message) { - if (!message) return false; - return /^Signed-off-by:\s+.+\s+<.+>/mi.test(message); -} - -function hasVerifiedGPGSignature(commit) { - return commit?.commit?.verification?.verified === true; -} - -function isMergeCommit(commit) { - return Array.isArray(commit?.parents) && commit.parents.length > 1; -} - -function checkDCO(commits) { - const failures = []; - let skipped = 0; - for (const c of commits) { - if (isMergeCommit(c)) { skipped++; continue; } - const msg = c.commit?.message || ''; - const sha = (c.sha || '').slice(0, 7); - if (!hasDCOSignoff(msg)) failures.push({ sha, message: msg.split('\n')[0] || '(no message)' }); - } - const checked = commits.length - skipped; - getLogger().log(`DCO check: ${checked - failures.length}/${checked} passed (${skipped} merge commit(s) skipped)`); - return { passed: failures.length === 0, failures }; -} - -function checkGPG(commits) { - const failures = []; - for (const c of commits) { - const sha = (c.sha || '').slice(0, 7); - const msg = c.commit?.message || ''; - if (!hasVerifiedGPGSignature(c)) failures.push({ sha, message: msg.split('\n')[0] || '(no message)' }); - } - getLogger().log(`GPG check: ${commits.length - failures.length}/${commits.length} passed`); - return { passed: failures.length === 0, failures }; -} - -async function checkMergeConflict(botContext) { - const logger = getLogger(); - let conflicts = false, resolved = false; - for (let attempt = 1; attempt <= 5; attempt++) { - const { data: pr } = await botContext.github.rest.pulls.get({ owner: botContext.owner, repo: botContext.repo, pull_number: botContext.number }); - if (pr.mergeable !== null) { - logger.log(`Merge conflict check: mergeable=${pr.mergeable}, state=${pr.mergeable_state}`); - conflicts = !pr.mergeable; - resolved = true; - break; - } - if (attempt < 5) { logger.log(`Mergeable state not ready, waiting 2000ms (attempt ${attempt}/5)`); await new Promise(r => setTimeout(r, 2000)); } - } - if (!resolved) logger.log('Merge conflict check: mergeable never resolved after retries, assuming no conflicts'); - logger.log(`Merge conflict check: ${conflicts ? 'has conflicts' : 'no conflicts'}`); - return { passed: !conflicts }; -} - -function parseIssueNumbers(body) { - const numbers = new Set(); - for (const re of [/(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)/gi, /related\s+to\s+#(\d+)/gi]) { - let m; while ((m = re.exec(body)) !== null) numbers.add(parseInt(m[1], 10)); - } - return numbers; -} - -function extractNumbersFromTitle(value) { - const numbers = new Set(); - if (!value) return numbers; - let m; while ((m = /#(\d+)/g.exec(value)) !== null) numbers.add(parseInt(m[1], 10)); - return numbers; -} - -async function fetchAndCheckAssignees(botContext, fetchIssue, issueNumbers, prAuthor) { - const results = []; - for (const num of issueNumbers) { - try { - const issue = await fetchIssue(botContext, num); - const isAssigned = (issue.assignees || []).some(a => a.login.toLowerCase() === prAuthor.toLowerCase()); - results.push({ number: num, title: issue.title, isAssigned }); - } catch (err) { getLogger().log(`Issue link check: could not fetch issue #${num}: ${err.message}`); } - } - return results; -} - -async function checkIssueLink(botContext, { fetchIssue, fetchClosingIssueNumbers }) { - const logger = getLogger(); - const body = botContext.pr?.body || ''; - const prAuthor = botContext.pr?.user?.login; - const issueNumbers = parseIssueNumbers(body); - if (!issueNumbers.size) { - const titleIssues = extractNumbersFromTitle(botContext.pr?.title || ''); - (await fetchClosingIssueNumbers(botContext)).filter(n => !titleIssues.has(n)).forEach(n => issueNumbers.add(n)); - } - if (!issueNumbers.size) { logger.log('Issue link check: no linked issues found'); return { passed: false, reason: 'no_issue_linked', issues: [] }; } - const linked = await fetchAndCheckAssignees(botContext, fetchIssue, issueNumbers, prAuthor); - if (!linked.length) { logger.log('Issue link check: all linked issues returned errors'); return { passed: false, reason: 'no_issue_linked', issues: [] }; } - const allAssigned = linked.every(i => i.isAssigned); - if (!allAssigned) { - const missing = linked.filter(i => !i.isAssigned).map(i => `#${i.number}`).join(', '); - logger.log(`Issue link check: author ${prAuthor} not assigned to all linked issues (missing: ${missing})`); - return { passed: false, reason: 'not_assigned', issues: linked }; - } - logger.log('Issue link check: passed (author assigned to all linked issues)'); - return { passed: true, reason: null, issues: linked }; -} - -module.exports = { hasDCOSignoff, hasVerifiedGPGSignature, isMergeCommit, checkDCO, checkGPG, checkMergeConflict, parseIssueNumbers, checkIssueLink, extractNumbersFromTitle }; diff --git a/.github/scripts/helpers/comments.cjs b/.github/scripts/helpers/comments.cjs deleted file mode 100644 index e932c7e8..00000000 --- a/.github/scripts/helpers/comments.cjs +++ /dev/null @@ -1,73 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -const { MAINTAINER_TEAM, DOCUMENTATION } = require('./constants.cjs'); -const MARKER = ''; -const SIGNING_GUIDE = DOCUMENTATION.signingGuide; -const MERGE_CONFLICTS_GUIDE = DOCUMENTATION.mergeConflictsGuide; - -function checkState(result) { - if (result.error) return 'error'; - return result.passed ? 'pass' : 'fail'; -} - -function buildSection({ title, result, passMessage }) { - const state = checkState(result); - if (state === 'error') { - return `:warning: **${title}** -- This check encountered an internal error. ${MAINTAINER_TEAM} please review manually.\n\nError: ${result.errorMessage || 'Unknown error'}`; - } - if (state === 'pass') return `:white_check_mark: **${title}** -- ${passMessage}`; - return null; -} - -function buildDCOSection(dco) { - const common = buildSection({ title: 'DCO Sign-off', result: dco, passMessage: 'All commits have valid sign-offs. Nice work!' }); - if (common) return common; - const list = (dco.failures || []).map(f => `- \`${f.sha}\` ${f.message}`).join('\n'); - return `:x: **DCO Sign-off** -- Uh oh! The following commits are missing the required DCO sign-off:\n${list}\n\nNo worries, this is an easy fix! Add \`Signed-off-by: Your Name \` to each commit (e.g. \`git commit -s\`). See the [Signing Guide](${SIGNING_GUIDE}).`; -} - -function buildGPGSection(gpg) { - const common = buildSection({ title: 'GPG Signature', result: gpg, passMessage: 'All commits have verified GPG signatures. Locked and loaded!' }); - if (common) return common; - const list = (gpg.failures || []).map(f => `- \`${f.sha}\` ${f.message}`).join('\n'); - return `:x: **GPG Signature** -- Heads up! The following commits don't have a verified GPG signature:\n${list}\n\nYou'll need to sign your commits with GPG (e.g. \`git commit -S\`). See the [Signing Guide](${SIGNING_GUIDE}) for a step-by-step walkthrough.`; -} - -function buildMergeSection(merge) { - const common = buildSection({ title: 'Merge Conflicts', result: merge, passMessage: 'No merge conflicts detected. Smooth sailing!' }); - if (common) return common; - return `:x: **Merge Conflicts** -- Oh no, this PR has merge conflicts with the base branch.\n\nLet's get this sorted! Update your branch (e.g. rebase or merge from base) and push. See the [Merge Conflicts Guide](${MERGE_CONFLICTS_GUIDE}) if you need a hand.`; -} - -function buildMergeConflictNotificationComment(prAuthor, mergedPRNumber) { - return `Hi @${prAuthor} :wave: — the recent merge of PR #${mergedPRNumber} has introduced a merge conflict in this PR. Please resolve the merge conflict so that this PR can be reviewed again. Thank you!`; -} - -function buildIssueLinkSection(issueLink) { - const linked = (issueLink.issues || []).filter(i => i.isAssigned).map(i => `#${i.number}`).join(', '); - const common = buildSection({ title: 'Issue Link', result: issueLink, passMessage: `Linked to ${linked} (assigned to you).` }); - if (common) return common; - if (issueLink.reason === 'not_assigned') { - const unassigned = (issueLink.issues || []).filter(i => !i.isAssigned).map(i => `#${i.number}`).join(', '); - return `:x: **Issue Link** -- Almost there! You are not assigned to the following linked issues: ${unassigned}.\n\nPlease ensure you are assigned to all linked issues before opening a PR. You can comment \`/assign\` on the issue to grab it!`; - } - return `:x: **Issue Link** -- This PR is not linked to any issue.\n\nPlease reference an issue using a closing keyword (e.g. \`Fixes #123\`) and ensure the issue is assigned to you. Every PR needs a home!`; -} - -function buildChecksSection({ merge, issueLink }) { - return `### PR Checks\n\n${buildMergeSection(merge)}\n\n---\n\n${buildIssueLinkSection(issueLink)}`; -} - -function allChecksPassed({ merge, issueLink }) { - return !merge.error && merge.passed && !issueLink.error && issueLink.passed; -} - -function buildBotComment({ prAuthor, merge, issueLink }) { - const greeting = `Hey @${prAuthor} :wave: thanks for the PR!\nI'm your friendly **PR Helper Bot** :robot: and I'll be riding shotgun on this one, keeping track of your PR's status to help you get it approved and merged.\n\nThis comment updates automatically as you push changes -- think of it as your PR's live scoreboard!\nHere's the latest:`; - const checksSection = buildChecksSection({ merge, issueLink }); - const passed = allChecksPassed({ merge, issueLink }); - const footer = passed ? ':tada: *All checks passed! Your PR is ready for review. Great job!*' : ':hourglass_flowing_sand: *All checks must pass before this PR can be reviewed. You\'ve got this!*'; - return { marker: MARKER, body: [MARKER, greeting, '', '---', '', checksSection, '', '---', '', footer].join('\n'), allPassed: passed }; -} - -module.exports = { MARKER, buildBotComment, buildChecksSection, allChecksPassed, buildMergeConflictNotificationComment }; diff --git a/.github/scripts/helpers/constants.cjs b/.github/scripts/helpers/constants.cjs deleted file mode 100644 index 6ef6e90a..00000000 --- a/.github/scripts/helpers/constants.cjs +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -const { loadAutomationConfig, buildConstants } = require('./config-loader.cjs'); -const AUTOMATION_CONFIG = loadAutomationConfig(); -const derived = buildConstants(AUTOMATION_CONFIG); - -const MAINTAINER_TEAM = derived.MAINTAINER_TEAM; -const GFI_SUPPORT_TEAM = derived.GFI_SUPPORT_TEAM; -const LABELS = derived.LABELS; -const SKILL_HIERARCHY = derived.SKILL_HIERARCHY; -const PRIORITY_HIERARCHY = derived.PRIORITY_HIERARCHY; -const SKILL_PREREQUISITES = derived.SKILL_PREREQUISITES; -const DOCUMENTATION = derived.DOCUMENTATION; -const COMMUNITY = derived.COMMUNITY; - -const ISSUE_STATE = Object.freeze({ OPEN: 'open', CLOSED: 'closed' }); - -module.exports = { MAINTAINER_TEAM, GFI_SUPPORT_TEAM, LABELS, ISSUE_STATE, SKILL_HIERARCHY, SKILL_PREREQUISITES, PRIORITY_HIERARCHY, DOCUMENTATION, COMMUNITY, AUTOMATION_CONFIG }; diff --git a/.github/scripts/helpers/index.cjs b/.github/scripts/helpers/index.cjs deleted file mode 100644 index ba1f362d..00000000 --- a/.github/scripts/helpers/index.cjs +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -module.exports = { - ...require('./constants.cjs'), - ...require('./logger.cjs'), - ...require('./validation.cjs'), - ...require('./api.cjs'), - ...require('./checks.cjs'), - ...require('./comments.cjs'), -}; diff --git a/.github/scripts/package-lock.json b/.github/scripts/package-lock.json deleted file mode 100644 index 9f349e24..00000000 --- a/.github/scripts/package-lock.json +++ /dev/null @@ -1,876 +0,0 @@ -{ - "name": "kdm-cli-org-bot-scripts", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "kdm-cli-org-bot-scripts", - "devDependencies": { - "eslint": "^10.4.0" - } - }, - "node_modules/@eslint-community/eslint-utils": { - "version": "4.9.1", - "resolved": "https://registry.npmjs.org/@eslint-community/eslint-utils/-/eslint-utils-4.9.1.tgz", - "integrity": "sha512-phrYmNiYppR7znFEdqgfWHXR6NCkZEK7hwWDHZUjit/2/U0r6XvkDl0SYnoM51Hq7FhCGdLDT6zxCCOY1hexsQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "eslint-visitor-keys": "^3.4.3" - }, - "engines": { - "node": "^12.22.0 || ^14.17.0 || >=16.0.0" - }, - "funding": { - "url": "https://opencollective.com/eslint" - }, - "peerDependencies": { - "eslint": "^6.0.0 || ^7.0.0 || >=8.0.0" - } - }, - "node_modules/@eslint-community/eslint-utils/node_modules/eslint-visitor-keys": { - "version": "3.4.3", - "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.4.3.tgz", - "integrity": "sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==", - "dev": true, - "license": "Apache-2.0", - "engines": { - "node": "^12.22.0 || ^14.17.0 || >=16.0.0" - }, - "funding": { - "url": "https://opencollective.com/eslint" - } - }, - "node_modules/@eslint-community/regexpp": { - "version": "4.12.2", - "resolved": "https://registry.npmjs.org/@eslint-community/regexpp/-/regexpp-4.12.2.tgz", - "integrity": "sha512-EriSTlt5OC9/7SXkRSCAhfSxxoSUgBm33OH+IkwbdpgoqsSsUg7y3uh+IICI/Qg4BBWr3U2i39RpmycbxMq4ew==", - "dev": true, - "license": "MIT", - "engines": { - "node": "^12.0.0 || ^14.0.0 || >=16.0.0" - } - }, - "node_modules/@eslint/config-array": { - "version": "0.23.5", - "resolved": "https://registry.npmjs.org/@eslint/config-array/-/config-array-0.23.5.tgz", - "integrity": "sha512-Y3kKLvC1dvTOT+oGlqNQ1XLqK6D1HU2YXPc52NmAlJZbMMWDzGYXMiPRJ8TYD39muD/OTjlZmNJ4ib7dvSrMBA==", - "dev": true, - "license": "Apache-2.0", - "dependencies": { - "@eslint/object-schema": "^3.0.5", - "debug": "^4.3.1", - "minimatch": "^10.2.4" - }, - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - } - }, - "node_modules/@eslint/config-helpers": { - "version": "0.6.0", - "resolved": "https://registry.npmjs.org/@eslint/config-helpers/-/config-helpers-0.6.0.tgz", - "integrity": "sha512-ii6Bw9jJ2zi2cWA2Z+9/QZ/+3DX6kwaV5Q986D/CdP3Lap3w/pgQZ373FV7byY/i7L4IRH/G43I5dz1ClsCbpA==", - "dev": true, - "license": "Apache-2.0", - "dependencies": { - "@eslint/core": "^1.2.1" - }, - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - } - }, - "node_modules/@eslint/core": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/@eslint/core/-/core-1.2.1.tgz", - "integrity": "sha512-MwcE1P+AZ4C6DWlpin/OmOA54mmIZ/+xZuJiQd4SyB29oAJjN30UW9wkKNptW2ctp4cEsvhlLY/CsQ1uoHDloQ==", - "dev": true, - "license": "Apache-2.0", - "dependencies": { - "@types/json-schema": "^7.0.15" - }, - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - } - }, - "node_modules/@eslint/object-schema": { - "version": "3.0.5", - "resolved": "https://registry.npmjs.org/@eslint/object-schema/-/object-schema-3.0.5.tgz", - "integrity": "sha512-vqTaUEgxzm+YDSdElad6PiRoX4t8VGDjCtt05zn4nU810UIx/uNEV7/lZJ6KwFThKZOzOxzXy48da+No7HZaMw==", - "dev": true, - "license": "Apache-2.0", - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - } - }, - "node_modules/@eslint/plugin-kit": { - "version": "0.7.1", - "resolved": "https://registry.npmjs.org/@eslint/plugin-kit/-/plugin-kit-0.7.1.tgz", - "integrity": "sha512-rZAP3aVgB9ds9KOeUSL+zZ21hPmo8dh6fnIFwRQj5EAZl9gzR7wxYbYXYysAM8CTqGmUGyp2S4kUdV17MnGuWQ==", - "dev": true, - "license": "Apache-2.0", - "dependencies": { - "@eslint/core": "^1.2.1", - "levn": "^0.4.1" - }, - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - } - }, - "node_modules/@humanfs/core": { - "version": "0.19.2", - "resolved": "https://registry.npmjs.org/@humanfs/core/-/core-0.19.2.tgz", - "integrity": "sha512-UhXNm+CFMWcbChXywFwkmhqjs3PRCmcSa/hfBgLIb7oQ5HNb1wS0icWsGtSAUNgefHeI+eBrA8I1fxmbHsGdvA==", - "dev": true, - "license": "Apache-2.0", - "dependencies": { - "@humanfs/types": "^0.15.0" - }, - "engines": { - "node": ">=18.18.0" - } - }, - "node_modules/@humanfs/node": { - "version": "0.16.8", - "resolved": "https://registry.npmjs.org/@humanfs/node/-/node-0.16.8.tgz", - "integrity": "sha512-gE1eQNZ3R++kTzFUpdGlpmy8kDZD/MLyHqDwqjkVQI0JMdI1D51sy1H958PNXYkM2rAac7e5/CnIKZrHtPh3BQ==", - "dev": true, - "license": "Apache-2.0", - "dependencies": { - "@humanfs/core": "^0.19.2", - "@humanfs/types": "^0.15.0", - "@humanwhocodes/retry": "^0.4.0" - }, - "engines": { - "node": ">=18.18.0" - } - }, - "node_modules/@humanfs/types": { - "version": "0.15.0", - "resolved": "https://registry.npmjs.org/@humanfs/types/-/types-0.15.0.tgz", - "integrity": "sha512-ZZ1w0aoQkwuUuC7Yf+7sdeaNfqQiiLcSRbfI08oAxqLtpXQr9AIVX7Ay7HLDuiLYAaFPu8oBYNq/QIi9URHJ3Q==", - "dev": true, - "license": "Apache-2.0", - "engines": { - "node": ">=18.18.0" - } - }, - "node_modules/@humanwhocodes/module-importer": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@humanwhocodes/module-importer/-/module-importer-1.0.1.tgz", - "integrity": "sha512-bxveV4V8v5Yb4ncFTT3rPSgZBOpCkjfK0y4oVVVJwIuDVBRMDXrPyXRL988i5ap9m9bnyEEjWfm5WkBmtffLfA==", - "dev": true, - "license": "Apache-2.0", - "engines": { - "node": ">=12.22" - }, - "funding": { - "type": "github", - "url": "https://github.com/sponsors/nzakas" - } - }, - "node_modules/@humanwhocodes/retry": { - "version": "0.4.3", - "resolved": "https://registry.npmjs.org/@humanwhocodes/retry/-/retry-0.4.3.tgz", - "integrity": "sha512-bV0Tgo9K4hfPCek+aMAn81RppFKv2ySDQeMoSZuvTASywNTnVJCArCZE2FWqpvIatKu7VMRLWlR1EazvVhDyhQ==", - "dev": true, - "license": "Apache-2.0", - "engines": { - "node": ">=18.18" - }, - "funding": { - "type": "github", - "url": "https://github.com/sponsors/nzakas" - } - }, - "node_modules/@types/esrecurse": { - "version": "4.3.1", - "resolved": "https://registry.npmjs.org/@types/esrecurse/-/esrecurse-4.3.1.tgz", - "integrity": "sha512-xJBAbDifo5hpffDBuHl0Y8ywswbiAp/Wi7Y/GtAgSlZyIABppyurxVueOPE8LUQOxdlgi6Zqce7uoEpqNTeiUw==", - "dev": true, - "license": "MIT" - }, - "node_modules/@types/estree": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz", - "integrity": "sha512-dWHzHa2WqEXI/O1E9OjrocMTKJl2mSrEolh1Iomrv6U+JuNwaHXsXx9bLu5gG7BUWFIN0skIQJQ/L1rIex4X6w==", - "dev": true, - "license": "MIT" - }, - "node_modules/@types/json-schema": { - "version": "7.0.15", - "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", - "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==", - "dev": true, - "license": "MIT" - }, - "node_modules/acorn": { - "version": "8.16.0", - "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.16.0.tgz", - "integrity": "sha512-UVJyE9MttOsBQIDKw1skb9nAwQuR5wuGD3+82K6JgJlm/Y+KI92oNsMNGZCYdDsVtRHSak0pcV5Dno5+4jh9sw==", - "dev": true, - "license": "MIT", - "bin": { - "acorn": "bin/acorn" - }, - "engines": { - "node": ">=0.4.0" - } - }, - "node_modules/acorn-jsx": { - "version": "5.3.2", - "resolved": "https://registry.npmjs.org/acorn-jsx/-/acorn-jsx-5.3.2.tgz", - "integrity": "sha512-rq9s+JNhf0IChjtDXxllJ7g41oZk5SlXtp0LHwyA5cejwn7vKmKp4pPri6YEePv2PU65sAsegbXtIinmDFDXgQ==", - "dev": true, - "license": "MIT", - "peerDependencies": { - "acorn": "^6.0.0 || ^7.0.0 || ^8.0.0" - } - }, - "node_modules/ajv": { - "version": "6.15.0", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.15.0.tgz", - "integrity": "sha512-fgFx7Hfoq60ytK2c7DhnF8jIvzYgOMxfugjLOSMHjLIPgenqa7S7oaagATUq99mV6IYvN2tRmC0wnTYX6iPbMw==", - "dev": true, - "license": "MIT", - "dependencies": { - "fast-deep-equal": "^3.1.1", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.4.1", - "uri-js": "^4.2.2" - }, - "funding": { - "type": "github", - "url": "https://github.com/sponsors/epoberezkin" - } - }, - "node_modules/balanced-match": { - "version": "4.0.4", - "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-4.0.4.tgz", - "integrity": "sha512-BLrgEcRTwX2o6gGxGOCNyMvGSp35YofuYzw9h1IMTRmKqttAZZVU67bdb9Pr2vUHA8+j3i2tJfjO6C6+4myGTA==", - "dev": true, - "license": "MIT", - "engines": { - "node": "18 || 20 || >=22" - } - }, - "node_modules/brace-expansion": { - "version": "5.0.9", - "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.9.tgz", - "integrity": "sha512-ScQ4IuvIEF1TMlP7Zt+vjJ//9zlPb2SDcxWxM3bk8s6t6GGdJ7KO1dCcTidOPJKePW30LE/2cT7wCyPho9/Wxg==", - "dev": true, - "license": "MIT", - "dependencies": { - "balanced-match": "^4.0.2" - }, - "engines": { - "node": "20 || >=22" - } - }, - "node_modules/cross-spawn": { - "version": "7.0.6", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", - "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", - "dev": true, - "license": "MIT", - "dependencies": { - "path-key": "^3.1.0", - "shebang-command": "^2.0.0", - "which": "^2.0.1" - }, - "engines": { - "node": ">= 8" - } - }, - "node_modules/debug": { - "version": "4.4.3", - "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", - "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", - "dev": true, - "license": "MIT", - "dependencies": { - "ms": "^2.1.3" - }, - "engines": { - "node": ">=6.0" - }, - "peerDependenciesMeta": { - "supports-color": { - "optional": true - } - } - }, - "node_modules/deep-is": { - "version": "0.1.4", - "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz", - "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", - "dev": true, - "license": "MIT" - }, - "node_modules/escape-string-regexp": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz", - "integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/eslint": { - "version": "10.4.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-10.4.0.tgz", - "integrity": "sha512-loXy6bWOoP3EP6JA7jo6p5jMpBJmHmsNZM5SFRHLdh1MGOPurMnNBj4ZlAbaqUAaQWbCr7jHV4P7gzAyryZWkQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "@eslint-community/eslint-utils": "^4.8.0", - "@eslint-community/regexpp": "^4.12.2", - "@eslint/config-array": "^0.23.5", - "@eslint/config-helpers": "^0.6.0", - "@eslint/core": "^1.2.1", - "@eslint/plugin-kit": "^0.7.1", - "@humanfs/node": "^0.16.6", - "@humanwhocodes/module-importer": "^1.0.1", - "@humanwhocodes/retry": "^0.4.2", - "@types/estree": "^1.0.6", - "ajv": "^6.14.0", - "cross-spawn": "^7.0.6", - "debug": "^4.3.2", - "escape-string-regexp": "^4.0.0", - "eslint-scope": "^9.1.2", - "eslint-visitor-keys": "^5.0.1", - "espree": "^11.2.0", - "esquery": "^1.7.0", - "esutils": "^2.0.2", - "fast-deep-equal": "^3.1.3", - "file-entry-cache": "^8.0.0", - "find-up": "^5.0.0", - "glob-parent": "^6.0.2", - "ignore": "^5.2.0", - "imurmurhash": "^0.1.4", - "is-glob": "^4.0.0", - "json-stable-stringify-without-jsonify": "^1.0.1", - "minimatch": "^10.2.4", - "natural-compare": "^1.4.0", - "optionator": "^0.9.3" - }, - "bin": { - "eslint": "bin/eslint.js" - }, - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - }, - "funding": { - "url": "https://eslint.org/donate" - }, - "peerDependencies": { - "jiti": "*" - }, - "peerDependenciesMeta": { - "jiti": { - "optional": true - } - } - }, - "node_modules/eslint-scope": { - "version": "9.1.2", - "resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-9.1.2.tgz", - "integrity": "sha512-xS90H51cKw0jltxmvmHy2Iai1LIqrfbw57b79w/J7MfvDfkIkFZ+kj6zC3BjtUwh150HsSSdxXZcsuv72miDFQ==", - "dev": true, - "license": "BSD-2-Clause", - "dependencies": { - "@types/esrecurse": "^4.3.1", - "@types/estree": "^1.0.8", - "esrecurse": "^4.3.0", - "estraverse": "^5.2.0" - }, - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - }, - "funding": { - "url": "https://opencollective.com/eslint" - } - }, - "node_modules/eslint-visitor-keys": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-5.0.1.tgz", - "integrity": "sha512-tD40eHxA35h0PEIZNeIjkHoDR4YjjJp34biM0mDvplBe//mB+IHCqHDGV7pxF+7MklTvighcCPPZC7ynWyjdTA==", - "dev": true, - "license": "Apache-2.0", - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - }, - "funding": { - "url": "https://opencollective.com/eslint" - } - }, - "node_modules/espree": { - "version": "11.2.0", - "resolved": "https://registry.npmjs.org/espree/-/espree-11.2.0.tgz", - "integrity": "sha512-7p3DrVEIopW1B1avAGLuCSh1jubc01H2JHc8B4qqGblmg5gI9yumBgACjWo4JlIc04ufug4xJ3SQI8HkS/Rgzw==", - "dev": true, - "license": "BSD-2-Clause", - "dependencies": { - "acorn": "^8.16.0", - "acorn-jsx": "^5.3.2", - "eslint-visitor-keys": "^5.0.1" - }, - "engines": { - "node": "^20.19.0 || ^22.13.0 || >=24" - }, - "funding": { - "url": "https://opencollective.com/eslint" - } - }, - "node_modules/esquery": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/esquery/-/esquery-1.7.0.tgz", - "integrity": "sha512-Ap6G0WQwcU/LHsvLwON1fAQX9Zp0A2Y6Y/cJBl9r/JbW90Zyg4/zbG6zzKa2OTALELarYHmKu0GhpM5EO+7T0g==", - "dev": true, - "license": "BSD-3-Clause", - "dependencies": { - "estraverse": "^5.1.0" - }, - "engines": { - "node": ">=0.10" - } - }, - "node_modules/esrecurse": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/esrecurse/-/esrecurse-4.3.0.tgz", - "integrity": "sha512-KmfKL3b6G+RXvP8N1vr3Tq1kL/oCFgn2NYXEtqP8/L3pKapUA4G8cFVaoF3SU323CD4XypR/ffioHmkti6/Tag==", - "dev": true, - "license": "BSD-2-Clause", - "dependencies": { - "estraverse": "^5.2.0" - }, - "engines": { - "node": ">=4.0" - } - }, - "node_modules/estraverse": { - "version": "5.3.0", - "resolved": "https://registry.npmjs.org/estraverse/-/estraverse-5.3.0.tgz", - "integrity": "sha512-MMdARuVEQziNTeJD8DgMqmhwR11BRQ/cBP+pLtYdSTnf3MIO8fFeiINEbX36ZdNlfU/7A9f3gUw49B3oQsvwBA==", - "dev": true, - "license": "BSD-2-Clause", - "engines": { - "node": ">=4.0" - } - }, - "node_modules/esutils": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", - "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", - "dev": true, - "license": "BSD-2-Clause", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/fast-deep-equal": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", - "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", - "dev": true, - "license": "MIT" - }, - "node_modules/fast-json-stable-stringify": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", - "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==", - "dev": true, - "license": "MIT" - }, - "node_modules/fast-levenshtein": { - "version": "2.0.6", - "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", - "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", - "dev": true, - "license": "MIT" - }, - "node_modules/file-entry-cache": { - "version": "8.0.0", - "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-8.0.0.tgz", - "integrity": "sha512-XXTUwCvisa5oacNGRP9SfNtYBNAMi+RPwBFmblZEF7N7swHYQS6/Zfk7SRwx4D5j3CH211YNRco1DEMNVfZCnQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "flat-cache": "^4.0.0" - }, - "engines": { - "node": ">=16.0.0" - } - }, - "node_modules/find-up": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/find-up/-/find-up-5.0.0.tgz", - "integrity": "sha512-78/PXT1wlLLDgTzDs7sjq9hzz0vXD+zn+7wypEe4fXQxCmdmqfGsEPQxmiCSQI3ajFV91bVSsvNtrJRiW6nGng==", - "dev": true, - "license": "MIT", - "dependencies": { - "locate-path": "^6.0.0", - "path-exists": "^4.0.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/flat-cache": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/flat-cache/-/flat-cache-4.0.1.tgz", - "integrity": "sha512-f7ccFPK3SXFHpx15UIGyRJ/FJQctuKZ0zVuN3frBo4HnK3cay9VEW0R6yPYFHC0AgqhukPzKjq22t5DmAyqGyw==", - "dev": true, - "license": "MIT", - "dependencies": { - "flatted": "^3.2.9", - "keyv": "^4.5.4" - }, - "engines": { - "node": ">=16" - } - }, - "node_modules/flatted": { - "version": "3.4.2", - "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.4.2.tgz", - "integrity": "sha512-PjDse7RzhcPkIJwy5t7KPWQSZ9cAbzQXcafsetQoD7sOJRQlGikNbx7yZp2OotDnJyrDcbyRq3Ttb18iYOqkxA==", - "dev": true, - "license": "ISC" - }, - "node_modules/glob-parent": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", - "integrity": "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==", - "dev": true, - "license": "ISC", - "dependencies": { - "is-glob": "^4.0.3" - }, - "engines": { - "node": ">=10.13.0" - } - }, - "node_modules/ignore": { - "version": "5.3.2", - "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", - "integrity": "sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">= 4" - } - }, - "node_modules/imurmurhash": { - "version": "0.1.4", - "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", - "integrity": "sha512-JmXMZ6wuvDmLiHEml9ykzqO6lwFbof0GG4IkcGaENdCRDDmMVnny7s5HsIgHCbaq0w2MyPhDqkhTUgS2LU2PHA==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.8.19" - } - }, - "node_modules/is-extglob": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", - "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/is-glob": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", - "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", - "dev": true, - "license": "MIT", - "dependencies": { - "is-extglob": "^2.1.1" - }, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/isexe": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz", - "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", - "dev": true, - "license": "ISC" - }, - "node_modules/json-buffer": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", - "integrity": "sha512-4bV5BfR2mqfQTJm+V5tPPdf+ZpuhiIvTuAB5g8kcrXOZpTT/QwwVRWBywX1ozr6lEuPdbHxwaJlm9G6mI2sfSQ==", - "dev": true, - "license": "MIT" - }, - "node_modules/json-schema-traverse": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", - "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==", - "dev": true, - "license": "MIT" - }, - "node_modules/json-stable-stringify-without-jsonify": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz", - "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==", - "dev": true, - "license": "MIT" - }, - "node_modules/keyv": { - "version": "4.5.4", - "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", - "integrity": "sha512-oxVHkHR/EJf2CNXnWxRLW6mg7JyCCUcG0DtEGmL2ctUo1PNTin1PUil+r/+4r5MpVgC/fn1kjsx7mjSujKqIpw==", - "dev": true, - "license": "MIT", - "dependencies": { - "json-buffer": "3.0.1" - } - }, - "node_modules/levn": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/levn/-/levn-0.4.1.tgz", - "integrity": "sha512-+bT2uH4E5LGE7h/n3evcS/sQlJXCpIp6ym8OWJ5eV6+67Dsql/LaaT7qJBAt2rzfoa/5QBGBhxDix1dMt2kQKQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "prelude-ls": "^1.2.1", - "type-check": "~0.4.0" - }, - "engines": { - "node": ">= 0.8.0" - } - }, - "node_modules/locate-path": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-6.0.0.tgz", - "integrity": "sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==", - "dev": true, - "license": "MIT", - "dependencies": { - "p-locate": "^5.0.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/minimatch": { - "version": "10.2.5", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-10.2.5.tgz", - "integrity": "sha512-MULkVLfKGYDFYejP07QOurDLLQpcjk7Fw+7jXS2R2czRQzR56yHRveU5NDJEOviH+hETZKSkIk5c+T23GjFUMg==", - "dev": true, - "license": "BlueOak-1.0.0", - "dependencies": { - "brace-expansion": "^5.0.5" - }, - "engines": { - "node": "18 || 20 || >=22" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, - "node_modules/ms": { - "version": "2.1.3", - "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", - "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", - "dev": true, - "license": "MIT" - }, - "node_modules/natural-compare": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", - "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", - "dev": true, - "license": "MIT" - }, - "node_modules/optionator": { - "version": "0.9.4", - "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.4.tgz", - "integrity": "sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g==", - "dev": true, - "license": "MIT", - "dependencies": { - "deep-is": "^0.1.3", - "fast-levenshtein": "^2.0.6", - "levn": "^0.4.1", - "prelude-ls": "^1.2.1", - "type-check": "^0.4.0", - "word-wrap": "^1.2.5" - }, - "engines": { - "node": ">= 0.8.0" - } - }, - "node_modules/p-limit": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", - "integrity": "sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "yocto-queue": "^0.1.0" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/p-locate": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/p-locate/-/p-locate-5.0.0.tgz", - "integrity": "sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==", - "dev": true, - "license": "MIT", - "dependencies": { - "p-limit": "^3.0.2" - }, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/path-exists": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", - "integrity": "sha512-ak9Qy5Q7jYb2Wwcey5Fpvg2KoAc/ZIhLSLOSBmRmygPsGwkVVt0fZa0qrtMz+m6tJTAHfZQ8FnmB4MG4LWy7/w==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=8" - } - }, - "node_modules/path-key": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", - "integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=8" - } - }, - "node_modules/prelude-ls": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", - "integrity": "sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">= 0.8.0" - } - }, - "node_modules/punycode": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", - "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=6" - } - }, - "node_modules/shebang-command": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", - "integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==", - "dev": true, - "license": "MIT", - "dependencies": { - "shebang-regex": "^3.0.0" - }, - "engines": { - "node": ">=8" - } - }, - "node_modules/shebang-regex": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz", - "integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=8" - } - }, - "node_modules/type-check": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.4.0.tgz", - "integrity": "sha512-XleUoc9uwGXqjWwXaUTZAmzMcFZ5858QA2vvx1Ur5xIcixXIP+8LnFDgRplU30us6teqdlskFfu+ae4K79Ooew==", - "dev": true, - "license": "MIT", - "dependencies": { - "prelude-ls": "^1.2.1" - }, - "engines": { - "node": ">= 0.8.0" - } - }, - "node_modules/uri-js": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", - "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", - "dev": true, - "license": "BSD-2-Clause", - "dependencies": { - "punycode": "^2.1.0" - } - }, - "node_modules/which": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", - "integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==", - "dev": true, - "license": "ISC", - "dependencies": { - "isexe": "^2.0.0" - }, - "bin": { - "node-which": "bin/node-which" - }, - "engines": { - "node": ">= 8" - } - }, - "node_modules/word-wrap": { - "version": "1.2.5", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.5.tgz", - "integrity": "sha512-BN22B5eaMMI9UMtjrGd5g5eCYPpCPDUy0FJXbYsaT5zYxjFOckS53SQDE3pWkVoWpHXVb3BrYcEN4Twa55B5cA==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/yocto-queue": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", - "integrity": "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - } - } -} diff --git a/.github/scripts/package.json b/.github/scripts/package.json deleted file mode 100644 index d322db2d..00000000 --- a/.github/scripts/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "kdm-cli-org-bot-scripts", - "private": true, - "type": "commonjs", - "devDependencies": { - "eslint": "^10.4.0" - } -} diff --git a/.github/scripts/tests/test-api.cjs b/.github/scripts/tests/test-api.cjs deleted file mode 100644 index 4ac86d44..00000000 --- a/.github/scripts/tests/test-api.cjs +++ /dev/null @@ -1,850 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-api.cjs -// -// Unit tests for helpers/api.cjs (postOrUpdateComment, fetchPRCommits, swapStatusLabel, etc.). -// Run with: node .github/scripts/tests/test-api.cjs - -const { runTestSuite } = require('./test-utils'); -const { - getBotComment, - postOrUpdateComment, - fetchPRCommits, - fetchOpenPRs, - fetchIssue, - fetchClosingIssueNumbers, - fetchLatestMilestone, - setMilestone, - swapStatusLabel, - hasLabel, - resolveLinkedIssue, - getHighestIssueSkillLevel, -} = require('../helpers/api'); -const { LABELS } = require('../helpers/constants'); -const { isSafeSearchToken } = require('../helpers/validation'); - -// ============================================================================= -// MOCK FACTORY -// ============================================================================= - -function createMockBotContext(overrides = {}) { - const calls = { - created: [], - updated: [], - labelsAdded: [], - labelsRemoved: [], - milestonesUpdated: [], - }; - const comments = overrides.comments || []; - return { - botContext: { - github: { - rest: { - issues: { - listComments: async ({ page, per_page }) => { - const start = (page - 1) * per_page; - const slice = comments.slice(start, start + per_page); - return { data: slice }; - }, - createComment: async (params) => { - calls.created.push(params); - }, - updateComment: async (params) => { - calls.updated.push(params); - }, - addLabels: async (params) => { - calls.labelsAdded.push(params.labels); - }, - removeLabel: async (params) => { - calls.labelsRemoved.push(params.name); - }, - listMilestones: async ({ page, per_page }) => { - const allMilestones = overrides.milestones || []; - const start = (page - 1) * per_page; - const slice = allMilestones.slice(start, start + per_page); - return { data: slice }; - }, - update: async (params) => { - calls.milestonesUpdated.push(params); - return {}; - }, - get: async ({ issue_number }) => { - const issue = (overrides.issues || {})[issue_number]; - if (!issue) throw new Error('Not Found'); - return { data: issue }; - }, - }, - pulls: { - list: async ({ page, per_page }) => { - const allPRs = overrides.openPRs || []; - const start = (page - 1) * per_page; - const slice = allPRs.slice(start, start + per_page); - return { data: slice }; - }, - listCommits: async ({ page, per_page }) => { - const allCommits = overrides.commits || []; - const start = (page - 1) * per_page; - const slice = allCommits.slice(start, start + per_page); - return { data: slice }; - }, - }, - }, - graphql: - overrides.graphql || - (async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { nodes: [] }, - }, - }, - })), - }, - owner: 'test', - repo: 'repo', - number: 1, - pr: overrides.pr || { labels: [] }, - }, - calls, - }; -} - -// ============================================================================= -// UNIT TESTS -// ============================================================================= - -const unitTests = [ - // --------------------------------------------------------------------------- - // hasLabel - // --------------------------------------------------------------------------- - { - name: 'hasLabel: PR with matching label object → true', - test: () => { - const pr = { - labels: [{ name: 'status: needs review' }], - }; - return hasLabel(pr, LABELS.NEEDS_REVIEW) === true; - }, - }, - { - name: 'hasLabel: PR with no matching label → false', - test: () => { - const pr = { - labels: [{ name: 'bug' }, { name: 'enhancement' }], - }; - return hasLabel(pr, LABELS.NEEDS_REVIEW) === false; - }, - }, - { - name: 'hasLabel: PR with no labels → false', - test: () => { - const pr = { labels: [] }; - return hasLabel(pr, LABELS.NEEDS_REVIEW) === false; - }, - }, - { - name: 'hasLabel: PR with null/undefined labels → false', - test: () => { - return ( - hasLabel({ labels: null }, LABELS.NEEDS_REVIEW) === false && - hasLabel({}, LABELS.NEEDS_REVIEW) === false - ); - }, - }, - { - name: 'hasLabel: case insensitive match → true', - test: () => { - const pr = { - labels: [{ name: 'STATUS: NEEDS REVIEW' }], - }; - return hasLabel(pr, 'status: needs review') === true; - }, - }, - { - name: 'hasLabel: string labels → true', - test: () => { - const pr = { - labels: ['status: needs review', 'bug'], - }; - return hasLabel(pr, LABELS.NEEDS_REVIEW) === true; - }, - }, - - // --------------------------------------------------------------------------- - // getBotComment - // --------------------------------------------------------------------------- - { - name: 'getBotComment: returns comment matched by marker', - test: async () => { - const marker = ''; - const { botContext } = createMockBotContext({ - comments: [ - { id: 1, body: 'User comment 1' }, - { id: 2, body: '\nBot comment' }, - ], - }); - const result = await getBotComment(botContext, marker); - return result !== null && result.id === 2; - }, - }, - { - name: 'getBotComment: returns null if no marker match', - test: async () => { - const marker = ''; - const { botContext } = createMockBotContext({ - comments: [ - { id: 1, body: 'User comment 1' }, - ], - }); - const result = await getBotComment(botContext, marker); - return result === null; - }, - }, - { - name: 'getBotComment: searches across pages', - test: async () => { - const marker = ''; - const page1 = Array(100).fill(null).map((_, i) => ({ id: i + 1, body: `Comment ${i}` })); - const page2 = [{ id: 101, body: '\nFound on page 2' }]; - const { botContext } = createMockBotContext({ - comments: [...page1, ...page2], - }); - const result = await getBotComment(botContext, marker); - return result !== null && result.id === 101; - }, - }, - - // --------------------------------------------------------------------------- - // postOrUpdateComment - // --------------------------------------------------------------------------- - { - name: 'postOrUpdateComment: no existing comment → creates new', - test: async () => { - const { botContext, calls } = createMockBotContext({ - comments: [], - }); - const marker = ''; - const body = '\nHello'; - const result = await postOrUpdateComment(botContext, marker, body); - return ( - result.success === true && - calls.created.length === 1 && - calls.updated.length === 0 && - calls.created[0].body === body - ); - }, - }, - { - name: 'postOrUpdateComment: existing comment with marker → updates', - test: async () => { - const marker = ''; - const { botContext, calls } = createMockBotContext({ - comments: [ - { id: 999, body: '\nOld content' }, - ], - }); - const body = '\nNew content'; - const result = await postOrUpdateComment(botContext, marker, body); - return ( - result.success === true && - calls.created.length === 0 && - calls.updated.length === 1 && - calls.updated[0].comment_id === 999 && - calls.updated[0].body === body - ); - }, - }, - { - name: 'postOrUpdateComment: multiple comments, one has marker → updates correct one', - test: async () => { - const marker = ''; - const { botContext, calls } = createMockBotContext({ - comments: [ - { id: 1, body: 'User comment 1' }, - { id: 2, body: '\nBot comment' }, - { id: 3, body: 'User comment 2' }, - ], - }); - const body = '\nUpdated bot'; - const result = await postOrUpdateComment(botContext, marker, body); - return ( - result.success === true && - calls.updated.length === 1 && - calls.updated[0].comment_id === 2 - ); - }, - }, - { - name: 'postOrUpdateComment: empty comment list → creates new', - test: async () => { - const { botContext, calls } = createMockBotContext({ - comments: [], - }); - const result = await postOrUpdateComment( - botContext, - '', - '\nEmpty' - ); - return result.success === true && calls.created.length === 1 && calls.updated.length === 0; - }, - }, - { - name: 'postOrUpdateComment: comment on second page → finds and updates', - test: async () => { - const marker = ''; - const page1 = Array(100) - .fill(null) - .map((_, i) => ({ id: i + 1, body: `Comment ${i}` })); - const page2 = [ - { id: 101, body: '\nFound on page 2' }, - ]; - const { botContext, calls } = createMockBotContext({ - comments: [...page1, ...page2], - }); - const body = '\nUpdated'; - const result = await postOrUpdateComment(botContext, marker, body); - return ( - result.success === true && - calls.updated.length === 1 && - calls.updated[0].comment_id === 101 - ); - }, - }, - - // --------------------------------------------------------------------------- - // fetchPRCommits - // --------------------------------------------------------------------------- - { - name: 'fetchPRCommits: single page (< 100 commits) → returns all', - test: async () => { - const commits = [ - { sha: 'a1', commit: { message: 'First' } }, - { sha: 'b2', commit: { message: 'Second' } }, - ]; - const { botContext } = createMockBotContext({ commits }); - const result = await fetchPRCommits(botContext); - return ( - Array.isArray(result) && - result.length === 2 && - result[0].sha === 'a1' && - result[1].sha === 'b2' - ); - }, - }, - { - name: 'fetchPRCommits: multiple pages → paginates and returns all', - test: async () => { - const commits = Array(150) - .fill(null) - .map((_, i) => ({ sha: `c${i}`, commit: { message: `Commit ${i}` } })); - const { botContext } = createMockBotContext({ commits }); - const result = await fetchPRCommits(botContext); - return result.length === 150; - }, - }, - { - name: 'fetchPRCommits: empty PR → returns []', - test: async () => { - const { botContext } = createMockBotContext({ commits: [] }); - const result = await fetchPRCommits(botContext); - return Array.isArray(result) && result.length === 0; - }, - }, - - // --------------------------------------------------------------------------- - // fetchOpenPRs - // --------------------------------------------------------------------------- - { - name: 'fetchOpenPRs: single page (< 100 PRs) → returns all', - test: async () => { - const openPRs = [ - { number: 1, title: 'First PR' }, - { number: 2, title: 'Second PR' }, - ]; - const { botContext } = createMockBotContext({ openPRs }); - const result = await fetchOpenPRs(botContext); - return ( - Array.isArray(result) && - result.length === 2 && - result[0].number === 1 && - result[1].number === 2 - ); - }, - }, - { - name: 'fetchOpenPRs: multiple pages → paginates and returns all', - test: async () => { - const openPRs = Array(150) - .fill(null) - .map((_, i) => ({ number: i + 1, title: `PR ${i + 1}` })); - const { botContext } = createMockBotContext({ openPRs }); - const result = await fetchOpenPRs(botContext); - return result.length === 150; - }, - }, - { - name: 'fetchOpenPRs: zero PRs → returns []', - test: async () => { - const { botContext } = createMockBotContext({ openPRs: [] }); - const result = await fetchOpenPRs(botContext); - return Array.isArray(result) && result.length === 0; - }, - }, - - // --------------------------------------------------------------------------- - // fetchIssue - // --------------------------------------------------------------------------- - { - name: 'fetchIssue: valid issue → returns issue data', - test: async () => { - const issueData = { number: 5, title: 'Bug report', state: 'open' }; - const { botContext } = createMockBotContext({ - issues: { 5: issueData }, - }); - const result = await fetchIssue(botContext, 5); - return result.number === 5 && result.title === 'Bug report'; - }, - }, - { - name: 'fetchIssue: missing issue (API throws) → throws', - test: async () => { - const { botContext } = createMockBotContext({ issues: {} }); - try { - await fetchIssue(botContext, 999); - return false; - } catch (err) { - return err.message === 'Not Found'; - } - }, - }, - - // --------------------------------------------------------------------------- - // fetchClosingIssueNumbers - // --------------------------------------------------------------------------- - { - name: 'fetchClosingIssueNumbers: 1 closing reference → returns [number]', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { - nodes: [{ number: 42 }], - }, - }, - }, - }), - }); - const result = await fetchClosingIssueNumbers(botContext); - return result.length === 1 && result[0] === 42; - }, - }, - { - name: 'fetchClosingIssueNumbers: 0 references → returns []', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { nodes: [] }, - }, - }, - }), - }); - const result = await fetchClosingIssueNumbers(botContext); - return Array.isArray(result) && result.length === 0; - }, - }, - { - name: 'fetchClosingIssueNumbers: multiple references → returns all', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { - nodes: [{ number: 1 }, { number: 2 }, { number: 3 }], - }, - }, - }, - }), - }); - const result = await fetchClosingIssueNumbers(botContext); - return ( - result.length === 3 && - result[0] === 1 && - result[1] === 2 && - result[2] === 3 - ); - }, - }, - { - name: 'fetchClosingIssueNumbers: GraphQL fails → returns [] (graceful)', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => { - throw new Error('GraphQL error'); - }, - }); - const result = await fetchClosingIssueNumbers(botContext); - return Array.isArray(result) && result.length === 0; - }, - }, - - // --------------------------------------------------------------------------- - // fetchLatestMilestone - // --------------------------------------------------------------------------- - { - name: 'fetchLatestMilestone: milestones with due dates → returns latest due date', - test: async () => { - const { botContext } = createMockBotContext({ - milestones: [ - { number: 1, title: 'Earlier', due_on: '2026-05-01T00:00:00Z' }, - { number: 2, title: 'Later', due_on: '2026-06-01T00:00:00Z' }, - { number: 3, title: 'No due date', due_on: null }, - ], - }); - const result = await fetchLatestMilestone(botContext); - return result !== null && result.number === 2; - }, - }, - { - name: 'fetchLatestMilestone: no due dates → returns highest milestone number', - test: async () => { - const { botContext } = createMockBotContext({ - milestones: [ - { number: 4, title: 'Older', due_on: null }, - { number: 7, title: 'Newest', due_on: null }, - { number: 5, title: 'Middle', due_on: null }, - ], - }); - const result = await fetchLatestMilestone(botContext); - return result !== null && result.number === 7; - }, - }, - { - name: 'fetchLatestMilestone: no open milestones → returns null', - test: async () => { - const { botContext } = createMockBotContext({ milestones: [] }); - const result = await fetchLatestMilestone(botContext); - return result === null; - }, - }, - - // --------------------------------------------------------------------------- - // setMilestone - // --------------------------------------------------------------------------- - { - name: 'setMilestone: updates issue milestone', - test: async () => { - const { botContext, calls } = createMockBotContext(); - const result = await setMilestone(botContext, 42, 9); - return ( - result.success === true && - calls.milestonesUpdated.length === 1 && - calls.milestonesUpdated[0].issue_number === 42 && - calls.milestonesUpdated[0].milestone === 9 - ); - }, - }, - - // --------------------------------------------------------------------------- - // resolveLinkedIssue - // --------------------------------------------------------------------------- - { - name: 'resolveLinkedIssue: no linked issues → returns null', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { nodes: [] }, - }, - }, - }), - }); - const result = await resolveLinkedIssue(botContext); - return result === null; - }, - }, - { - name: 'resolveLinkedIssue: single linked issue with skill label → returns it', - test: async () => { - const issueData = { number: 10, title: 'Fix bug', labels: [{ name: LABELS.BEGINNER }] }; - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { nodes: [{ number: 10 }] }, - }, - }, - }), - issues: { 10: issueData }, - }); - const result = await resolveLinkedIssue(botContext); - return result !== null && result.number === 10; - }, - }, - { - name: 'resolveLinkedIssue: single linked issue with no skill label → returns null', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { - nodes: [{ number: 7 }], - }, - }, - }, - }), - issues: { - 7: { number: 7, title: 'Issue 7', labels: [{ name: 'bug' }] }, - }, - }); - const result = await resolveLinkedIssue(botContext); - return result === null; - }, - }, - { - name: 'resolveLinkedIssue: multiple linked issues with skill label → returns highest skill level', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { - nodes: [{ number: 1 }, { number: 2 }, { number: 3 }], - }, - }, - }, - }), - issues: { - 1: { number: 1, title: 'GFI issue', labels: [{ name: LABELS.GOOD_FIRST_ISSUE }] }, - 2: { number: 2, title: 'Intermediate issue', labels: [{ name: LABELS.INTERMEDIATE }] }, - 3: { number: 3, title: 'Beginner issue', labels: [{ name: LABELS.BEGINNER }] }, - }, - }); - const result = await resolveLinkedIssue(botContext); - return result !== null && result.number === 2; // INTERMEDIATE is highest - }, - }, - { - name: 'resolveLinkedIssue: multiple linked issues with no skill label → returns null', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { - nodes: [{ number: 4 }, { number: 5 }], - }, - }, - }, - }), - issues: { - 4: { number: 4, title: 'Issue 4', labels: [{ name: 'bug' }] }, - 5: { number: 5, title: 'Issue 5', labels: [{ name: 'enhancement' }] }, - }, - }); - const result = await resolveLinkedIssue(botContext); - return result === null; - }, - }, - { - name: 'resolveLinkedIssue: GraphQL fails → returns null gracefully', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => { throw new Error('GraphQL error'); }, - }); - const result = await resolveLinkedIssue(botContext); - return result === null; - }, - }, - { - name: 'resolveLinkedIssue: issue fetch fails for all linked issues → returns null', - test: async () => { - const { botContext } = createMockBotContext({ - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { - nodes: [{ number: 999 }, { number: 998 }], - }, - }, - }, - }), - issues: {}, // no issues → fetchIssue throws for all - }); - const result = await resolveLinkedIssue(botContext); - return result === null; - }, - }, - - // --------------------------------------------------------------------------- - // swapStatusLabel - // --------------------------------------------------------------------------- - { - name: 'swapStatusLabel: allPassed true, has NEEDS_REVISION → removes revision, adds review', - test: async () => { - const { botContext, calls } = createMockBotContext({ - pr: { labels: [{ name: LABELS.NEEDS_REVISION }] }, - }); - await swapStatusLabel(botContext, true); - return ( - calls.labelsRemoved.length === 1 && - calls.labelsRemoved[0] === LABELS.NEEDS_REVISION && - calls.labelsAdded.length === 1 && - Array.isArray(calls.labelsAdded[0]) && - calls.labelsAdded[0][0] === LABELS.NEEDS_REVIEW - ); - }, - }, - { - name: 'swapStatusLabel: allPassed true, has NEEDS_REVIEW → no-op', - test: async () => { - const { botContext, calls } = createMockBotContext({ - pr: { labels: [{ name: LABELS.NEEDS_REVIEW }] }, - }); - await swapStatusLabel(botContext, true); - return calls.labelsRemoved.length === 0 && calls.labelsAdded.length === 0; - }, - }, - { - name: 'swapStatusLabel: allPassed true, no status label → no-op', - test: async () => { - const { botContext, calls } = createMockBotContext({ - pr: { labels: [{ name: 'bug' }] }, - }); - await swapStatusLabel(botContext, true); - return calls.labelsRemoved.length === 0 && calls.labelsAdded.length === 0; - }, - }, - { - name: 'swapStatusLabel: allPassed false, has NEEDS_REVIEW → removes review, adds revision', - test: async () => { - const { botContext, calls } = createMockBotContext({ - pr: { labels: [{ name: LABELS.NEEDS_REVIEW }] }, - }); - await swapStatusLabel(botContext, false); - return ( - calls.labelsRemoved.length === 1 && - calls.labelsRemoved[0] === LABELS.NEEDS_REVIEW && - calls.labelsAdded.length === 1 && - Array.isArray(calls.labelsAdded[0]) && - calls.labelsAdded[0][0] === LABELS.NEEDS_REVISION - ); - }, - }, - { - name: 'swapStatusLabel: allPassed false, has NEEDS_REVISION → no-op', - test: async () => { - const { botContext, calls } = createMockBotContext({ - pr: { labels: [{ name: LABELS.NEEDS_REVISION }] }, - }); - await swapStatusLabel(botContext, false); - return calls.labelsRemoved.length === 0 && calls.labelsAdded.length === 0; - }, - }, - { - name: 'swapStatusLabel: allPassed false, no status label → no-op', - test: async () => { - const { botContext, calls } = createMockBotContext({ - pr: { labels: [] }, - }); - await swapStatusLabel(botContext, false); - return calls.labelsRemoved.length === 0 && calls.labelsAdded.length === 0; - }, - }, - - // --------------------------------------------------------------------------- - // SafeSearchToken - // --------------------------------------------------------------------------- - { - name: 'isSafeSearchToken: dependabot[bot] → true', - test: () => isSafeSearchToken('dependabot[bot]') === true, - }, - { - name: 'isSafeSearchToken: string with spaces → false', - test: () => isSafeSearchToken('bad username') === false, - }, - { - name: 'isSafeSearchToken: string with angle brackets → false', - test: () => isSafeSearchToken('bad') === false, - }, - { - name: 'isSafeSearchToken: string with semicolon → false', - test: () => isSafeSearchToken('bad;username') === false, - }, - { - name: 'isSafeSearchToken: string with brackets but not bot inside → false', - test: () => isSafeSearchToken('bad[admin]') === false, - }, - { - name: 'isSafeSearchToken: string with multiple brackets → false', - test: () => isSafeSearchToken('bad[[admin]') === false, - }, - - // --------------------------------------------------------------------------- - // getHighestIssueSkillLevel - // --------------------------------------------------------------------------- - { - name: 'getHighestIssueSkillLevel: issue with one skill label → returns that level', - test: () => { - const issue = { number: 1, title: 'Test', labels: [{ name: LABELS.BEGINNER }, { name: LABELS.READY_FOR_DEV }] }; - return getHighestIssueSkillLevel(issue) === LABELS.BEGINNER; - }, - }, - { - name: 'getHighestIssueSkillLevel: issue with no skill labels → returns null', - test: () => { - const issue = { number: 1, title: 'Test', labels: [{ name: 'bug' }, { name: 'enhancement' }] }; - return getHighestIssueSkillLevel(issue) === null; - }, - }, - { - name: 'getHighestIssueSkillLevel: issue with multiple skill labels → returns highest', - test: () => { - const issue = { number: 1, title: 'Test', labels: [{ name: LABELS.GOOD_FIRST_ISSUE }, { name: LABELS.BEGINNER }, { name: LABELS.INTERMEDIATE }] }; - return getHighestIssueSkillLevel(issue) === LABELS.INTERMEDIATE; - }, - }, - { - name: 'getHighestIssueSkillLevel: issue with empty labels → returns null', - test: () => { - const issue = { number: 1, title: 'Test', labels: [] }; - return getHighestIssueSkillLevel(issue) === null; - }, - }, -]; -// ============================================================================= -// TEST RUNNER -// ============================================================================= - -async function runUnitTests() { - console.log('🔬 UNIT TESTS (api)'); - console.log('='.repeat(70)); - let passed = 0; - let failed = 0; - for (const test of unitTests) { - try { - const result = await Promise.resolve(test.test()); - if (result) { - console.log(`✅ ${test.name}`); - passed++; - } else { - console.log(`❌ ${test.name}`); - failed++; - } - } catch (error) { - console.log(`❌ ${test.name} - Error: ${error.message}`); - failed++; - } - } - console.log('\n' + '-'.repeat(70)); - console.log(`Unit Tests: ${passed} passed, ${failed} failed`); - return { total: unitTests.length, passed, failed }; -} - -runTestSuite('API HELPERS TEST SUITE', [], async () => true, [ - { label: 'Unit Tests', run: runUnitTests }, -]); diff --git a/.github/scripts/tests/test-assign-bot.cjs b/.github/scripts/tests/test-assign-bot.cjs deleted file mode 100644 index f4ab3f19..00000000 --- a/.github/scripts/tests/test-assign-bot.cjs +++ /dev/null @@ -1,1867 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-assign-bot.cjs -// -// Local test script for bot-on-comment.cjs -// Run with: node .github/scripts/tests/test-assign-bot.cjs -// -// This script mocks the GitHub API and runs various test scenarios -// to verify the on-comment (assign) bot behaves correctly without making real API calls. - -const { LABELS } = require("../helpers"); -const script = require("../bot-on-comment.cjs"); - -// ============================================================================= -// MOCK GITHUB API -// ============================================================================= - -function createMockGithub(options = {}, issueFromPayload = null) { - const { - completedIssueCount = 0, - completedIssueCounts = {}, - openAssignmentCount = 0, - openAssignmentCountExcludingBlocked = openAssignmentCount, - blockedIssueCount = 0, - restListClosedShouldFail = false, - restListOpenShouldFail = false, - assignShouldFail = false, - removeLabelShouldFail = false, - addLabelShouldFail = false, - reactionShouldFail = false, - issueGetShouldFail = false, - issueAssignees = null, - issueLabels = null, - openAssignedIssues = null, - searchResults = null, - searchShouldFail = false, - restListOpenFailOnCall = null, - } = options; - - let openListForRepoCallCount = 0; - - const calls = { - comments: [], - assignees: [], - labelsAdded: [], - labelsRemoved: [], - restCalls: [], - reactions: [], - }; - - return { - calls, - rest: { - reactions: { - createForIssueComment: async (params) => { - if (reactionShouldFail) { - throw new Error( - "Simulated reaction failure: comment not found (404)", - ); - } - calls.reactions.push({ - commentId: params.comment_id, - content: params.content, - }); - console.log(`\n👍 REACTION ADDED: ${params.content}`); - }, - }, - issues: { - createComment: async (params) => { - calls.comments.push(params.body); - console.log("\n📝 COMMENT POSTED:"); - console.log("─".repeat(60)); - console.log(params.body); - console.log("─".repeat(60)); - }, - addAssignees: async (params) => { - if (assignShouldFail) { - throw new Error("Simulated assignment failure"); - } - calls.assignees.push(...params.assignees); - console.log(`\n✅ ASSIGNED: ${params.assignees.join(", ")}`); - }, - addLabels: async (params) => { - if (addLabelShouldFail) { - throw new Error("Simulated add label failure"); - } - calls.labelsAdded.push(...params.labels); - console.log(`\n🏷️ LABEL ADDED: ${params.labels.join(", ")}`); - }, - removeLabel: async (params) => { - if (removeLabelShouldFail) { - throw new Error("Simulated remove label failure"); - } - calls.labelsRemoved.push(params.name); - console.log(`\n🏷️ LABEL REMOVED: ${params.name}`); - }, - get: async (params) => { - console.log( - `\n🔍 REST API CALL: get issue_number=${params.issue_number}`, - ); - if (issueGetShouldFail) { - throw new Error("Simulated issues.get failure"); - } - const freshLabels = Array.isArray(issueLabels) - ? issueLabels - : Array.isArray(issueFromPayload?.labels) - ? issueFromPayload.labels - : []; - if (Array.isArray(issueAssignees)) { - return { - data: { - assignees: issueAssignees.map((login) => ({ login })), - labels: freshLabels, - }, - }; - } - if (options.issueAlreadyAssignedTo) { - return { - data: { - assignees: [{ login: options.issueAlreadyAssignedTo }], - labels: freshLabels, - }, - }; - } - return { data: { assignees: [], labels: freshLabels } }; - }, - listForRepo: async (params) => { - calls.restCalls.push( - `REST listForRepo: state=${params.state} assignee=${params.assignee}`, - ); - console.log( - `\n🔍 REST API CALL: listForRepo state=${params.state} assignee=${params.assignee}`, - ); - - if (params.state === "open") { - openListForRepoCallCount++; - if (restListOpenShouldFail) { - throw new Error( - "Simulated REST API failure for open assignments", - ); - } - if (restListOpenFailOnCall === openListForRepoCallCount) { - throw new Error( - "Simulated REST API failure for open assignments", - ); - } - if (openAssignedIssues) { - if (params.labels) { - return { - data: openAssignedIssues.filter((issue) => - issue.labels?.some((l) => l.name === params.labels), - ), - }; - } - return { data: openAssignedIssues }; - } - const effectiveCount = openAssignmentCountExcludingBlocked; - const issues = []; - for (let i = 0; i < effectiveCount; i++) { - issues.push({ - number: 9000 + i, - labels: [{ name: "status: ready for dev" }], - }); - } - const blockedToGenerate = Math.max( - blockedIssueCount, - openAssignmentCount - openAssignmentCountExcludingBlocked, - ); - for (let i = 0; i < blockedToGenerate; i++) { - issues.push({ - number: 9100 + i, - labels: [{ name: LABELS.BLOCKED }], - }); - } - const difference = - openAssignmentCount - - (openAssignmentCountExcludingBlocked + blockedToGenerate); - if (difference > 0) { - for (let i = 0; i < difference; i++) { - issues.push({ number: 9200 + i, labels: [] }); - } - } - if (params.labels) { - return { - data: issues.filter((issue) => - issue.labels?.some((l) => l.name === params.labels), - ), - }; - } - return { data: issues }; - } - - if (params.state === "closed") { - if (restListClosedShouldFail) { - throw new Error("Simulated REST API failure"); - } - const issues = []; - for (const [labelName, count] of Object.entries( - completedIssueCounts, - )) { - for (let i = 0; i < count; i++) { - issues.push({ labels: [{ name: labelName }] }); - } - } - for (let i = 0; i < completedIssueCount; i++) { - issues.push({ labels: [] }); - } - if (params.labels) { - return { - data: issues.filter((issue) => - issue.labels?.some((l) => l.name === params.labels), - ), - }; - } - return { data: issues }; - } - - return { data: [] }; - }, - }, - search: { - issuesAndPullRequests: async (params) => { - console.log(`\n🔍 SEARCH API CALL: q=${params.q}`); - if (searchShouldFail) { - throw new Error("Simulated search API failure"); - } - const linkedMatch = params.q.match(/linked:(\d+)/); - const issueNumber = linkedMatch - ? parseInt(linkedMatch[1], 10) - : null; - if ( - searchResults && - issueNumber !== null && - issueNumber in searchResults - ) { - return { data: searchResults[issueNumber] }; - } - return { data: { total_count: 0 } }; - }, - }, - }, - graphql: async (query, vars) => { - // Stubbed just in case other things call it, though we rely on REST now - if (searchShouldFail) { - throw new Error("Simulated GraphQL failure"); - } - - if (query.includes("closedByPullRequestsReferences")) { - const issueNumber = vars.number; - const totalCount = (searchResults && issueNumber !== null && issueNumber in searchResults) - ? searchResults[issueNumber].total_count - : 0; - - let nodes = []; - if (totalCount > 0) { - // In tests, if totalCount > 0, we just return a matching PR - // The hasNeedsReviewPR function checks author matching `username`, - // but we don't have the explicit username passed into the mock. - // So we use a special placeholder, or infer from context. - // Since the test just wants a match, we can return the author as options.mockUsername - // or fallback to 'bypass-review-user-1'. - const authorLogin = options.mockUsername || "bypass-review-user-1"; - nodes.push({ - state: "OPEN", - author: { login: authorLogin }, - labels: { nodes: [{ name: "status: needs review" }] } - }); - } - - return { - repository: { - issue: { - closedByPullRequestsReferences: { - nodes - } - } - } - }; - } - - return { search: { issueCount: 0 } }; - }, - }; -} - -// ============================================================================= -// TEST SCENARIOS -// ============================================================================= - -const scenarios = [ - { - name: "Race Condition - Issue Snatched While Queued (Case 2)", - description: - "Fresh fetch shows another user was assigned between queue and execution", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 120, - assignees: [], // stale payload: appears unassigned - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1021, - body: "/assign", - user: { login: "second-requester", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { issueAlreadyAssignedTo: "first-requester" }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @second-requester! This issue is already assigned to @first-requester.\n\n👉 **Find another issue to work on:**\n[Browse unassigned issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue+is%3Aopen+no%3Aassignee+label%3A%22status%3A+ready+for+dev%22)\n\nOnce you find one you like, comment \`/assign\` to get started!`, - ], - }, - { - name: "Race Condition - Requester Already Assigned In Fresh State", - description: - "Fresh fetch shows the requester is already assigned; handler must exit without addAssignees", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 122, - assignees: [], // stale payload: appears unassigned - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1023, - body: "/assign", - user: { login: "already-assigned-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { issueAlreadyAssignedTo: "already-assigned-user" }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @already-assigned-user! You're already assigned to this issue. You're all set to start working on it!\n\nIf you have any questions, feel free to ask here or reach out to the team.`, - ], - }, - { - name: "Race Condition - Duplicate Assignment Trigger At Limit", - description: - "A duplicate queued /assign sees the current issue in the live assignment count and must not post a limit-exceeded comment", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 127, - assignees: [], // stale payload: appears unassigned - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1028, - body: "/assign", - user: { login: "duplicate-requester", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - issueAlreadyAssignedTo: "duplicate-requester", - openAssignmentCount: 2, - }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @duplicate-requester! You're already assigned to this issue. You're all set to start working on it!\n\nIf you have any questions, feel free to ask here or reach out to the team.`, - ], - }, - { - name: "Race Condition - Fresh Issue Has Multiple Assignees", - description: - "Fresh fetch reveals a corrupted multi-assignee issue; bot must reject and list all assignees", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 123, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1024, - body: "/assign", - user: { login: "third-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { issueAssignees: ["first-user", "second-user"] }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @third-user! This issue is already assigned to @first-user, @second-user.\n\n👉 **Find another issue to work on:**\n[Browse unassigned issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue+is%3Aopen+no%3Aassignee+label%3A%22status%3A+ready+for+dev%22)\n\nOnce you find one you like, comment \`/assign\` to get started!`, - ], - }, - { - name: "Race Condition - Ready Label Removed While Queued", - description: - "Fresh fetch no longer has ready-for-dev label; bot must abort assignment", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 124, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1025, - body: "/assign", - user: { login: "stale-ready-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { issueLabels: [{ name: LABELS.GOOD_FIRST_ISSUE }] }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @stale-ready-user! This issue is not ready for development yet.\n\nIssues must have the \`status: ready for dev\` label before they can be assigned.\n\n👉 **Find an issue that's ready:**\n[Browse ready issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue+is%3Aopen+no%3Aassignee+label%3A%22status%3A+ready+for+dev%22)\n\nOnce you find one you like, comment \`/assign\` to get started!`, - ], - }, - { - name: "Race Condition - Skill Label Removed While Queued", - description: - "Fresh fetch no longer has any skill label; bot must abort and tag maintainers", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 125, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1026, - body: "/assign", - user: { login: "stale-skill-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { issueLabels: [{ name: LABELS.READY_FOR_DEV }] }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @stale-skill-user! This issue doesn't have a skill level label yet.\n\n@utkarsh232005 — could you please add one of the following labels?\n- \`skill: good first issue\`\n- \`skill: beginner\`\n- \`skill: intermediate\`\n- \`skill: advanced\`\n\n@stale-skill-user, once a maintainer adds the label, comment \`/assign\` again to request assignment.`, - ], - }, - { - name: "Race Condition - Skill Label Changed to Different Level While Queued", - description: - "Fresh fetch shows a different skill level; bot must abort since prerequisite gates were run against stale level", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 126, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1027, - body: "/assign", - user: { login: "skill-changed-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - issueLabels: [{ name: LABELS.READY_FOR_DEV }, { name: LABELS.BEGINNER }], - }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @skill-changed-user! The skill level for this issue changed while your assignment request was being processed.\n\n**Current label:** \`${LABELS.BEGINNER}\`\n**Previous label:** \`${LABELS.GOOD_FIRST_ISSUE}\`\n\nPlease comment \`/assign\` again to request assignment with the updated skill requirements.`, - ], - }, - { - name: "Error - Fresh Issue Fetch API Failure", - // Note: This failure fires from inside assignAndFinalize(), unlike the - // other API failure tests which catch errors during precondition checks. - description: - "Tags maintainers when issues.get fails inside assignAndFinalize", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 121, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1022, - body: "/assign", - user: { login: "unlucky-user-4", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { issueGetShouldFail: true }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @unlucky-user-4! I encountered an error while trying to verify your eligibility for this issue.\n\n@utkarsh232005 — could you please help with this assignment request?\n\n@unlucky-user-4, a maintainer will review your request and assign you manually if appropriate. Sorry for the inconvenience!`, - ], - }, - // --------------------------------------------------------------------------- - // HAPPY PATHS (4 tests) - // Successful assignment for each skill level - // --------------------------------------------------------------------------- - - { - name: "Happy Path - Good First Issue", - description: "New contributor successfully assigned to GFI", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 100, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1001, - body: "/assign", - user: { login: "new-contributor", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: {}, - expectedAssignee: "new-contributor", - expectedComments: [ - `👋 Hi @new-contributor, welcome to the KDM CLI community! Thank you for choosing to contribute — we're thrilled to have you here! 🎉 - -You've been assigned this **Good First Issue**, and the **Good First Issue Support Team** (@utkarsh232005) is ready to help you succeed. - -The issue description above has everything you need: implementation steps, contribution workflow, and links to guides. If anything is unclear, just ask — we're happy to help. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the community pool. - -Good luck, and welcome aboard! 🚀`, - ], - }, - - { - name: "Happy Path - Beginner Issue", - description: "Contributor with 2 completed GFIs assigned to Beginner", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 101, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: beginner" }, - ], - }, - comment: { - id: 1002, - body: "/assign", - user: { login: "experienced-contributor", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { completedIssueCounts: { [LABELS.GOOD_FIRST_ISSUE]: 2 } }, - expectedAssignee: "experienced-contributor", - expectedComments: [ - `👋 Hi @experienced-contributor, thanks for continuing to contribute to the KDM CLI! You've been assigned this **Beginner** issue. 🙌 - -If this task involves any design decisions or you'd like early feedback, feel free to share your plan here before diving into the code. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the pool. - -Good luck! 🚀`, - ], - }, - - { - name: "Happy Path - Intermediate Issue", - description: - "Contributor with 3 completed Beginners assigned to Intermediate", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 102, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: intermediate" }, - ], - }, - comment: { - id: 1003, - body: "/assign", - user: { login: "growing-contributor", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { completedIssueCounts: { [LABELS.BEGINNER]: 3 } }, - expectedAssignee: "growing-contributor", - expectedComments: [ - `👋 Hi @growing-contributor, thanks for continuing to contribute to the KDM CLI! You've been assigned this **Intermediate** issue. 🙌 - -If this task involves any design decisions or you'd like early feedback, feel free to share your plan here before diving into the code. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the pool. - -Good luck! 🚀`, - ], - }, - - { - name: "Happy Path - Advanced Issue", - description: - "Contributor with 3 completed Intermediates assigned to Advanced", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 103, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: advanced" }, - ], - }, - comment: { - id: 1004, - body: "/assign", - user: { login: "senior-contributor", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { completedIssueCounts: { [LABELS.INTERMEDIATE]: 3 } }, - expectedAssignee: "senior-contributor", - expectedComments: [ - `👋 Hi @senior-contributor, thanks for continuing to contribute to the KDM CLI! You've been assigned this **Advanced** issue. 🙌 - -If this task involves any design decisions or you'd like early feedback, feel free to share your plan here before diving into the code. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the pool. - -Good luck! 🚀`, - ], - }, - - // --------------------------------------------------------------------------- - // BYPASS LOGIC TESTS - // --------------------------------------------------------------------------- - - { - name: "Bypass - Same Level Completed", - description: - "User with 1 Beginner can take another Beginner (bypasses 2 GFI prereq)", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 200, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: beginner" }, - ], - }, - comment: { - id: 2001, - body: "/assign", - user: { login: "bypass-user-1", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - completedIssueCounts: { - [LABELS.BEGINNER]: 1, - [LABELS.GOOD_FIRST_ISSUE]: 1, // Only 1 (Not enough to pass normal prereq) - }, - }, - expectedAssignee: "bypass-user-1", - expectedComments: [ - `👋 Hi @bypass-user-1, thanks for continuing to contribute to the KDM CLI! You've been assigned this **Beginner** issue. 🙌\n\nIf this task involves any design decisions or you'd like early feedback, feel free to share your plan here before diving into the code.\n\nIf you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the pool.\n\nGood luck! 🚀`, - ], - }, - - { - name: "Bypass - Higher Level Completed", - description: - "User with 1 Intermediate can take a Beginner (bypasses 2 GFI prereq)", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 201, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: beginner" }, - ], - }, - comment: { - id: 2002, - body: "/assign", - user: { login: "bypass-user-2", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - completedIssueCounts: { - [LABELS.INTERMEDIATE]: 1, - }, - }, - expectedAssignee: "bypass-user-2", - expectedComments: [ - `👋 Hi @bypass-user-2, thanks for continuing to contribute to the KDM CLI! You've been assigned this **Beginner** issue. 🙌\n\nIf this task involves any design decisions or you'd like early feedback, feel free to share your plan here before diving into the code.\n\nIf you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the pool.\n\nGood luck! 🚀`, - ], - }, - - // --------------------------------------------------------------------------- - // GFI COMPLETION CAP TESTS (3 tests) - // Gate added in enforceGfiCompletionLimit - // --------------------------------------------------------------------------- - - { - name: "GFI Cap - Exactly At Limit (5 Completed)", - description: - "Contributor with 5 completed GFIs is rejected with encouraging redirect", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 300, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 3001, - body: "/assign", - user: { login: "veteran-gfi-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { completedIssueCounts: { [LABELS.GOOD_FIRST_ISSUE]: 5 } }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @veteran-gfi-user! You've completed **5 Good First Issues** — that's a fantastic achievement, and it shows you know the workflow inside and out. 🎉 - -Good First Issues are designed to help new contributors get comfortable with the process, and you've clearly mastered it. We believe you're more than ready to take on bigger challenges! - -👉 **Find Beginner and higher issues to work on:** -[Browse available Beginner issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20no%3Aassignee%20label%3A%22skill%3A%20beginner%22%20label%3A%22status%3A%20ready%20for%20dev%22) - -Come take on something more challenging — we're excited to see what you'll build next! 🚀`, - ], - }, - - { - name: "GFI Cap - Below Limit (4 Completed)", - description: - "Contributor with 4 completed GFIs is still allowed to take another GFI", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 301, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 3002, - body: "/assign", - user: { login: "almost-capped-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { completedIssueCounts: { [LABELS.GOOD_FIRST_ISSUE]: 4 } }, - expectedAssignee: "almost-capped-user", - expectedComments: [ - `👋 Hi @almost-capped-user, welcome to the KDM CLI community! Thank you for choosing to contribute — we're thrilled to have you here! 🎉 - -You've been assigned this **Good First Issue**, and the **Good First Issue Support Team** (@utkarsh232005) is ready to help you succeed. - -The issue description above has everything you need: implementation steps, contribution workflow, and links to guides. If anything is unclear, just ask — we're happy to help. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the community pool. - -Good luck, and welcome aboard! 🚀`, - ], - }, - - { - name: "GFI Cap - Does Not Apply to Beginner Issues", - description: - "Contributor with 5 completed GFIs can still take a Beginner issue", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 302, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: beginner" }, - ], - }, - comment: { - id: 3003, - body: "/assign", - user: { login: "gfi-graduated-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { completedIssueCounts: { [LABELS.GOOD_FIRST_ISSUE]: 5 } }, - expectedAssignee: "gfi-graduated-user", - expectedComments: [ - `👋 Hi @gfi-graduated-user, thanks for continuing to contribute to the KDM CLI! You've been assigned this **Beginner** issue. 🙌 - -If this task involves any design decisions or you'd like early feedback, feel free to share your plan here before diving into the code. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the pool. - -Good luck! 🚀`, - ], - }, - - // --------------------------------------------------------------------------- - // VALIDATION FAILURES (9 tests) - // Bot rejects assignment with helpful message - // --------------------------------------------------------------------------- - - { - name: "Validation - Already Assigned to Someone Else", - description: "Issue is taken by another contributor", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 104, - assignees: [{ login: "other-user" }], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1005, - body: "/assign", - user: { login: "late-arrival", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: {}, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @late-arrival! This issue is already assigned to @other-user. - -👉 **Find another issue to work on:** -[Browse unassigned issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue+is%3Aopen+no%3Aassignee+label%3A%22status%3A+ready+for+dev%22) - -Once you find one you like, comment \`/assign\` to get started!`, - ], - }, - - { - name: "Validation - Already Assigned to Self", - description: "Contributor already owns the issue", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 105, - assignees: [{ login: "forgetful-user" }], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1006, - body: "/assign", - user: { login: "forgetful-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: {}, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @forgetful-user! You're already assigned to this issue. You're all set to start working on it! - -If you have any questions, feel free to ask here or reach out to the team.`, - ], - }, - - { - name: "Validation - Not Ready for Dev", - description: "Issue missing status: ready for dev label", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 106, - assignees: [], - labels: [{ name: "skill: good first issue" }], - }, - comment: { - id: 1007, - body: "/assign", - user: { login: "eager-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: {}, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @eager-user! This issue is not ready for development yet. - -Issues must have the \`status: ready for dev\` label before they can be assigned. - -👉 **Find an issue that's ready:** -[Browse ready issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue+is%3Aopen+no%3Aassignee+label%3A%22status%3A+ready+for+dev%22) - -Once you find one you like, comment \`/assign\` to get started!`, - ], - }, - - { - name: "Validation - No Labels At All", - description: "Issue has no labels", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 107, - assignees: [], - labels: [], - }, - comment: { - id: 1007, - body: "/assign", - user: { login: "eager-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: {}, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @eager-user! This issue is not ready for development yet. - -Issues must have the \`status: ready for dev\` label before they can be assigned. - -👉 **Find an issue that's ready:** -[Browse ready issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue+is%3Aopen+no%3Aassignee+label%3A%22status%3A+ready+for+dev%22) - -Once you find one you like, comment \`/assign\` to get started!`, - ], - }, - - { - name: "Validation - No Skill Level Label", - description: "Issue missing skill level label", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 107, - assignees: [], - labels: [{ name: "status: ready for dev" }], - }, - comment: { - id: 1008, - body: "/assign", - user: { login: "confused-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: {}, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @confused-user! This issue doesn't have a skill level label yet. - -@utkarsh232005 — could you please add one of the following labels? -- \`skill: good first issue\` -- \`skill: beginner\` -- \`skill: intermediate\` -- \`skill: advanced\` - -@confused-user, once a maintainer adds the label, comment \`/assign\` again to request assignment.`, - ], - }, - - { - name: "Validation - Prerequisites Not Met", - description: "Contributor lacks required experience", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 108, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: beginner" }, - ], - }, - comment: { - id: 1009, - body: "/assign", - user: { login: "eager-newbie", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { completedIssueCount: 0, openAssignmentCount: 0 }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @eager-newbie! Thanks for your interest in contributing! - -This is a **Beginner** issue. Before taking it on, you need to complete at least **2 Good First Issues** to build familiarity with the codebase. - -📊 **Your Progress:** You've completed **0** so far. - -👉 **Find Good First Issues to work on:** -[Browse available Good First Issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20no%3Aassignee%20label%3A%22skill%3A%20good%20first%20issue%22%20label%3A%22status%3A%20ready%20for%20dev%22) - -Once you've completed 2, come back and we'll be happy to assign this to you! 🎯`, - ], - }, - - { - name: "Validation - Too Many Open Assignments (at limit)", - description: "Contributor already has 2 open issues assigned", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 114, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1015, - body: "/assign", - user: { login: "busy-contributor", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { openAssignmentCount: 2 }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @busy-contributor! Thanks for your enthusiasm to contribute! - -To help contributors stay focused and ensure issues remain available for others, we limit assignments to **2 open issues** at a time. Issues labeled \`status: blocked\` are not counted toward this limit. - -📊 **Your Current Assignments:** You're currently assigned to **2** open issues. - -👉 **View your assigned issues:** -[Your open assignments](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20assignee%3Abusy-contributor%20-label%3A%22status%3A%20blocked%22) - -💡 **Tip:** If all of your open assigned issues have a linked PR with \`status: needs review\`, the limit is automatically bypassed — you can request a new assignment right away. - -Once you complete or unassign from one of your current issues, come back and we'll be happy to assign this to you! 🎯`, - ], - }, - - { - name: "Validation - Too Many Open Assignments (over limit)", - description: "Contributor has more than 2 open issues assigned", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 115, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1016, - body: "/assign", - user: { login: "very-busy-contributor", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { openAssignmentCount: 5 }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @very-busy-contributor! Thanks for your enthusiasm to contribute! - -To help contributors stay focused and ensure issues remain available for others, we limit assignments to **2 open issues** at a time. Issues labeled \`status: blocked\` are not counted toward this limit. - -📊 **Your Current Assignments:** You're currently assigned to **3+** open issues. - -👉 **View your assigned issues:** -[Your open assignments](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20assignee%3Avery-busy-contributor%20-label%3A%22status%3A%20blocked%22) - -💡 **Tip:** If all of your open assigned issues have a linked PR with \`status: needs review\`, the limit is automatically bypassed — you can request a new assignment right away. - -Once you complete or unassign from one of your current issues, come back and we'll be happy to assign this to you! 🎯`, - ], - }, - - { - name: "Validation - Over Limit After Issues Unblocked", - description: - "User has 3 open issues; some were blocked when they got the third. Now 3 count (excluding blocked), so over limit and cannot be assigned", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 118, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1019, - body: "/assign", - user: { login: "now-over-limit-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - openAssignmentCount: 3, - openAssignmentCountExcludingBlocked: 3, - }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @now-over-limit-user! Thanks for your enthusiasm to contribute! - -To help contributors stay focused and ensure issues remain available for others, we limit assignments to **2 open issues** at a time. Issues labeled \`status: blocked\` are not counted toward this limit. - -📊 **Your Current Assignments:** You're currently assigned to **3+** open issues. - -👉 **View your assigned issues:** -[Your open assignments](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20assignee%3Anow-over-limit-user%20-label%3A%22status%3A%20blocked%22) - -💡 **Tip:** If all of your open assigned issues have a linked PR with \`status: needs review\`, the limit is automatically bypassed — you can request a new assignment right away. - -Once you complete or unassign from one of your current issues, come back and we'll be happy to assign this to you! 🎯`, - ], - }, - - { - name: "Validation - At Limit With Blocked Issues (shows blocked link)", - description: - "User at 2 open (excluding blocked) and has 1 blocked issue; comment includes link to blocked issues", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 119, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1020, - body: "/assign", - user: { login: "at-limit-with-blocked", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - openAssignmentCount: 2, - openAssignmentCountExcludingBlocked: 2, - blockedIssueCount: 1, - }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @at-limit-with-blocked! Thanks for your enthusiasm to contribute! - -To help contributors stay focused and ensure issues remain available for others, we limit assignments to **2 open issues** at a time. Issues labeled \`status: blocked\` are not counted toward this limit. - -📊 **Your Current Assignments:** You're currently assigned to **2** open issues. - -👉 **View your assigned issues:** -[Your open assignments](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20assignee%3Aat-limit-with-blocked%20-label%3A%22status%3A%20blocked%22) - -👉 **View your blocked issues:** -[Your blocked issues](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20assignee%3Aat-limit-with-blocked%20label%3A%22status%3A%20blocked%22) - -💡 **Tip:** If all of your open assigned issues have a linked PR with \`status: needs review\`, the limit is automatically bypassed — you can request a new assignment right away. - -Once you complete or unassign from one of your current issues, come back and we'll be happy to assign this to you! 🎯`, - ], - }, - - { - name: "Validation - Under Assignment Limit (1 open issue)", - description: "Contributor with 1 open issue can take another", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 116, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1017, - body: "/assign", - user: { login: "active-contributor", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { openAssignmentCount: 1 }, - expectedAssignee: "active-contributor", - expectedComments: [ - `👋 Hi @active-contributor, welcome to the KDM CLI community! Thank you for choosing to contribute — we're thrilled to have you here! 🎉 - -You've been assigned this **Good First Issue**, and the **Good First Issue Support Team** (@utkarsh232005) is ready to help you succeed. - -The issue description above has everything you need: implementation steps, contribution workflow, and links to guides. If anything is unclear, just ask — we're happy to help. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the community pool. - -Good luck, and welcome aboard! 🚀`, - ], - }, - - { - name: "Validation - Open Assignments Exclude Blocked", - description: - "Contributor with 2 open issues both status: blocked can be assigned (blocked not counted)", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 117, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1018, - body: "/assign", - user: { login: "blocked-contributor", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - openAssignmentCount: 2, - openAssignmentCountExcludingBlocked: 0, - }, - expectedAssignee: "blocked-contributor", - expectedComments: [ - `👋 Hi @blocked-contributor, welcome to the KDM CLI community! Thank you for choosing to contribute — we're thrilled to have you here! 🎉 - -You've been assigned this **Good First Issue**, and the **Good First Issue Support Team** (@utkarsh232005) is ready to help you succeed. - -The issue description above has everything you need: implementation steps, contribution workflow, and links to guides. If anything is unclear, just ask — we're happy to help. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the community pool. - -Good luck, and welcome aboard! 🚀`, - ], - }, - - // --------------------------------------------------------------------------- - // ERROR HANDLING (4 tests) - // API failures - // --------------------------------------------------------------------------- - - { - name: "Error - Open Assignments API Failure", - description: "Tags maintainers when open assignments check fails", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 117, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1018, - body: "/assign", - user: { login: "unlucky-user-3", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { restListOpenShouldFail: true }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @unlucky-user-3! I encountered an error while trying to verify your eligibility for this issue. - -@utkarsh232005 — could you please help with this assignment request? - -@unlucky-user-3, a maintainer will review your request and assign you manually if appropriate. Sorry for the inconvenience!`, - ], - }, - - { - name: "Error - Prerequisite Check API Failure", - description: "Tags maintainers when prerequisite check fails", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 109, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: beginner" }, - ], - }, - comment: { - id: 1010, - body: "/assign", - user: { login: "unlucky-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { restListClosedShouldFail: true }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @unlucky-user! I encountered an error while trying to verify your eligibility for this issue. - -@utkarsh232005 — could you please help with this assignment request? - -@unlucky-user, a maintainer will review your request and assign you manually if appropriate. Sorry for the inconvenience!`, - ], - }, - - { - name: "Error - Assignment API Failure", - description: "Tags maintainers when assignment fails", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 110, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1011, - body: "/assign", - user: { login: "unlucky-user-2", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { assignShouldFail: true }, - expectedAssignee: null, - expectedComments: [ - `⚠️ Hi @unlucky-user-2! I tried to assign you to this issue, but encountered an error. - -@utkarsh232005 — could you please manually assign @unlucky-user-2 to this issue? - -Error details: Simulated assignment failure`, - ], - }, - - { - name: "Error - Label Update Failure", - description: "Tags maintainers when labels cannot be updated", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 111, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1012, - body: "/assign", - user: { login: "partially-lucky", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { removeLabelShouldFail: true, addLabelShouldFail: true }, - expectedAssignee: "partially-lucky", - expectedComments: [ - `👋 Hi @partially-lucky, welcome to the KDM CLI community! Thank you for choosing to contribute — we're thrilled to have you here! 🎉 - -You've been assigned this **Good First Issue**, and the **Good First Issue Support Team** (@utkarsh232005) is ready to help you succeed. - -The issue description above has everything you need: implementation steps, contribution workflow, and links to guides. If anything is unclear, just ask — we're happy to help. - -If you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the community pool. - -Good luck, and welcome aboard! 🚀`, - `⚠️ @partially-lucky has been successfully assigned to this issue, but I encountered an error updating the labels. - -@utkarsh232005 — please manually: -- Remove the \`status: ready for dev\` label -- Add the \`status: in progress\` label - -Error details: Failed to remove 'status: ready for dev': Simulated remove label failure; Failed to add 'status: in progress': Simulated add label failure`, - ], - }, - - // --------------------------------------------------------------------------- - // DELETED COMMENT ABORT (1 test) - // Bot aborts /assign when the triggering comment has been deleted - // --------------------------------------------------------------------------- - - { - name: "Abort - Triggering Comment Deleted", - description: - "Bot aborts /assign flow when acknowledgeComment fails (comment deleted)", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 400, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 4001, - body: "/assign", - user: { login: "deleted-comment-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { reactionShouldFail: true }, - expectedAssignee: null, - expectedComments: [], - expectedNoSideEffects: true, - }, - - // --------------------------------------------------------------------------- - // NO ACTION (2 tests) - // Bot stays silent and takes no action - // --------------------------------------------------------------------------- - - { - name: "No Action - Comment Without /assign", - description: "Regular comment ignored", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 112, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1013, - body: "This looks interesting, can someone help me understand it?", - user: { login: "curious-user", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: {}, - expectedAssignee: null, - expectedComments: [], - }, - - { - name: "No Action - Bot Comment", - description: "Bot users ignored to prevent loops", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 113, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 1014, - body: "/assign", - user: { login: "github-actions[bot]", type: "Bot" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: {}, - expectedAssignee: null, - expectedComments: [], - }, - - // --------------------------------------------------------------------------- - // NEEDS-REVIEW BYPASS TESTS (6 tests) - // Assignment cap bypass when all assigned issues have linked needs-review PRs - // --------------------------------------------------------------------------- - - { - name: "Bypass Cap - All Issues Have Needs-Review PRs (over limit)", - description: - "Contributor over the cap but all 3 assigned issues have linked needs-review PRs — bypass allows assignment", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 600, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 6001, - body: "/assign", - user: { login: "bypass-review-user-1", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - mockUsername: "bypass-review-user-1", - openAssignedIssues: [ - { number: 500, labels: [{ name: "status: in progress" }] }, - { number: 501, labels: [{ name: "status: in progress" }] }, - { number: 502, labels: [{ name: "status: in progress" }] }, - ], - searchResults: { - 500: { total_count: 1 }, - 501: { total_count: 1 }, - 502: { total_count: 1 }, - }, - }, - expectedAssignee: "bypass-review-user-1", - expectedComments: [ - `👋 Hi @bypass-review-user-1, welcome to the KDM CLI community! Thank you for choosing to contribute — we're thrilled to have you here! 🎉\n\nYou've been assigned this **Good First Issue**, and the **Good First Issue Support Team** (@utkarsh232005) is ready to help you succeed.\n\nThe issue description above has everything you need: implementation steps, contribution workflow, and links to guides. If anything is unclear, just ask — we're happy to help.\n\nIf you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the community pool.\n\nGood luck, and welcome aboard! 🚀`, - ], - }, - - { - name: "Bypass Cap - One Issue Missing Needs-Review PR", - description: - "One assigned issue has a needs-review PR but the other does not — bypass fails, limit-exceeded comment posted", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 601, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 6002, - body: "/assign", - user: { login: "bypass-review-user-2", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - mockUsername: "bypass-review-user-2", - openAssignedIssues: [ - { number: 503, labels: [{ name: "status: in progress" }] }, - { number: 504, labels: [{ name: "status: in progress" }] }, - ], - searchResults: { - 503: { total_count: 1 }, - 504: { total_count: 0 }, - }, - }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @bypass-review-user-2! Thanks for your enthusiasm to contribute!\n\nTo help contributors stay focused and ensure issues remain available for others, we limit assignments to **2 open issues** at a time. Issues labeled \`status: blocked\` are not counted toward this limit.\n\n📊 **Your Current Assignments:** You're currently assigned to **2** open issues.\n\n👉 **View your assigned issues:**\n[Your open assignments](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20assignee%3Abypass-review-user-2%20-label%3A%22status%3A%20blocked%22)\n\n💡 **Tip:** If all of your open assigned issues have a linked PR with \`status: needs review\`, the limit is automatically bypassed — you can request a new assignment right away.\n\nOnce you complete or unassign from one of your current issues, come back and we'll be happy to assign this to you! 🎯`, - ], - }, - - { - name: "Bypass Cap - No Issues Have Needs-Review PRs", - description: - "Neither assigned issue has a needs-review PR — bypass fails, limit-exceeded comment posted", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 602, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 6003, - body: "/assign", - user: { login: "bypass-review-user-3", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - mockUsername: "bypass-review-user-3", - openAssignedIssues: [ - { number: 505, labels: [{ name: "status: in progress" }] }, - { number: 506, labels: [{ name: "status: in progress" }] }, - ], - searchResults: { - 505: { total_count: 0 }, - 506: { total_count: 0 }, - }, - }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @bypass-review-user-3! Thanks for your enthusiasm to contribute!\n\nTo help contributors stay focused and ensure issues remain available for others, we limit assignments to **2 open issues** at a time. Issues labeled \`status: blocked\` are not counted toward this limit.\n\n📊 **Your Current Assignments:** You're currently assigned to **2** open issues.\n\n👉 **View your assigned issues:**\n[Your open assignments](https://github.com/KDM-cli/kdm-cli/issues?q=is%3Aissue%20is%3Aopen%20assignee%3Abypass-review-user-3%20-label%3A%22status%3A%20blocked%22)\n\n💡 **Tip:** If all of your open assigned issues have a linked PR with \`status: needs review\`, the limit is automatically bypassed — you can request a new assignment right away.\n\nOnce you complete or unassign from one of your current issues, come back and we'll be happy to assign this to you! 🎯`, - ], - }, - - { - name: "Bypass Cap - Search API Error", - description: - "Search API fails when checking for needs-review PRs — API error comment posted, no bypass", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 603, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 6004, - body: "/assign", - user: { login: "bypass-review-user-4", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - openAssignedIssues: [ - { number: 507, labels: [{ name: "status: in progress" }] }, - { number: 508, labels: [{ name: "status: in progress" }] }, - ], - searchShouldFail: true, - }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @bypass-review-user-4! I encountered an error while trying to verify your eligibility for this issue.\n\n@utkarsh232005 — could you please help with this assignment request?\n\n@bypass-review-user-4, a maintainer will review your request and assign you manually if appropriate. Sorry for the inconvenience!`, - ], - }, - - { - name: "Bypass Cap - Exactly At Limit With Needs-Review PRs", - description: - "Contributor at exactly MAX_OPEN_ASSIGNMENTS with all issues having needs-review PRs — bypass succeeds", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 604, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 6005, - body: "/assign", - user: { login: "bypass-review-user-5", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - mockUsername: "bypass-review-user-5", - openAssignedIssues: [ - { number: 509, labels: [{ name: "status: in progress" }] }, - { number: 510, labels: [{ name: "status: in progress" }] }, - ], - searchResults: { - 509: { total_count: 1 }, - 510: { total_count: 1 }, - }, - }, - expectedAssignee: "bypass-review-user-5", - expectedComments: [ - `👋 Hi @bypass-review-user-5, welcome to the KDM CLI community! Thank you for choosing to contribute — we're thrilled to have you here! 🎉\n\nYou've been assigned this **Good First Issue**, and the **Good First Issue Support Team** (@utkarsh232005) is ready to help you succeed.\n\nThe issue description above has everything you need: implementation steps, contribution workflow, and links to guides. If anything is unclear, just ask — we're happy to help.\n\nIf you realize you cannot complete this issue, simply comment \`/unassign\` to return it to the community pool.\n\nGood luck, and welcome aboard! 🚀`, - ], - }, - - { - name: "Bypass Cap - listAssignedIssues API Error", - description: - "listForRepo fails on the second call (listAssignedIssues) — API error comment posted, no bypass", - context: { - eventName: "issue_comment", - payload: { - issue: { - number: 605, - assignees: [], - labels: [ - { name: "status: ready for dev" }, - { name: "skill: good first issue" }, - ], - }, - comment: { - id: 6006, - body: "/assign", - user: { login: "bypass-review-user-6", type: "User" }, - }, - }, - repo: { owner: "kdm-ledger", repo: "kdm-cli" }, - }, - githubOptions: { - openAssignmentCount: 2, - restListOpenFailOnCall: 2, - }, - expectedAssignee: null, - expectedComments: [ - `👋 Hi @bypass-review-user-6! I encountered an error while trying to verify your eligibility for this issue.\n\n@utkarsh232005 — could you please help with this assignment request?\n\n@bypass-review-user-6, a maintainer will review your request and assign you manually if appropriate. Sorry for the inconvenience!`, - ], - }, -]; - -// ============================================================================= -// TEST RUNNER -// ============================================================================= - -const { verifyComments, runTestSuite } = require("./test-utils"); - -async function runTest(scenario, index) { - console.log("\n" + "=".repeat(70)); - console.log(`TEST ${index + 1}: ${scenario.name}`); - console.log(`Description: ${scenario.description}`); - console.log("=".repeat(70)); - - const mockGithub = createMockGithub( - scenario.githubOptions, - scenario.context?.payload?.issue, - ); - - try { - await script({ github: mockGithub, context: scenario.context }); - } catch (error) { - console.log(`\n❌ SCRIPT THREW ERROR: ${error.message}`); - } - - const results = { - passed: true, - details: [], - }; - - if (scenario.expectedAssignee) { - if (mockGithub.calls.assignees.includes(scenario.expectedAssignee)) { - results.details.push( - `✅ Correctly assigned to ${scenario.expectedAssignee}`, - ); - } else { - results.passed = false; - results.details.push( - `❌ Expected assignee ${scenario.expectedAssignee}, got: ${mockGithub.calls.assignees.join(", ") || "none"}`, - ); - } - } else { - if (mockGithub.calls.assignees.length === 0) { - results.details.push("✅ Correctly did not assign anyone"); - } else { - results.passed = false; - results.details.push( - `❌ Should not have assigned, but assigned: ${mockGithub.calls.assignees.join(", ")}`, - ); - } - } - - const commentResult = verifyComments( - scenario.expectedComments || [], - mockGithub.calls.comments, - ); - if (!commentResult.passed) results.passed = false; - results.details.push(...commentResult.details); - - console.log("\n📊 RESULT:"); - results.details.forEach((d) => console.log(` ${d}`)); - - return results.passed; -} - -runTestSuite("BOT-ASSIGN-ON-COMMENT TEST SUITE", scenarios, runTest); diff --git a/.github/scripts/tests/test-checks.cjs b/.github/scripts/tests/test-checks.cjs deleted file mode 100644 index e0e17866..00000000 --- a/.github/scripts/tests/test-checks.cjs +++ /dev/null @@ -1,694 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-checks.cjs -// -// Unit tests for helpers/checks.cjs (DCO, GPG, merge conflict, issue link). -// Run with: node .github/scripts/tests/test-checks.cjs - -const { runTestSuite } = require('./test-utils'); -const { - hasDCOSignoff, - hasVerifiedGPGSignature, - isMergeCommit, - checkDCO, - checkGPG, - checkMergeConflict, - checkIssueLink, -} = require('../helpers/checks'); - -// ============================================================================= -// UNIT TESTS -// ============================================================================= - -const unitTests = [ - // --------------------------------------------------------------------------- - // hasDCOSignoff - // --------------------------------------------------------------------------- - { - name: 'hasDCOSignoff: valid sign-off Signed-off-by: Name ', - test: () => hasDCOSignoff('Signed-off-by: Jane Doe ') === true, - }, - { - name: 'hasDCOSignoff: missing sign-off', - test: () => hasDCOSignoff('Just a commit message') === false, - }, - { - name: 'hasDCOSignoff: sign-off without email', - test: () => hasDCOSignoff('Signed-off-by: Jane Doe') === false, - }, - { - name: 'hasDCOSignoff: case insensitive signed-off-by', - test: () => hasDCOSignoff('signed-off-by: Jane Doe ') === true, - }, - { - name: 'hasDCOSignoff: null message', - test: () => hasDCOSignoff(null) === false, - }, - { - name: 'hasDCOSignoff: empty string', - test: () => hasDCOSignoff('') === false, - }, - { - name: 'hasDCOSignoff: multiline with sign-off in trailer', - test: () => - hasDCOSignoff( - 'Fix bug\n\nSome description\n\nSigned-off-by: Jane Doe ' - ) === true, - }, - - // --------------------------------------------------------------------------- - // hasVerifiedGPGSignature - // --------------------------------------------------------------------------- - { - name: 'hasVerifiedGPGSignature: verified true', - test: () => - hasVerifiedGPGSignature({ - commit: { verification: { verified: true } }, - }) === true, - }, - { - name: 'hasVerifiedGPGSignature: verified false', - test: () => - hasVerifiedGPGSignature({ - commit: { verification: { verified: false } }, - }) === false, - }, - { - name: 'hasVerifiedGPGSignature: no verification object', - test: () => hasVerifiedGPGSignature({ commit: {} }) === false, - }, - { - name: 'hasVerifiedGPGSignature: missing commit object', - test: () => hasVerifiedGPGSignature({}) === false, - }, - { - name: 'hasVerifiedGPGSignature: null commit', - test: () => hasVerifiedGPGSignature(null) === false, - }, - - // --------------------------------------------------------------------------- - // isMergeCommit - // --------------------------------------------------------------------------- - { - name: 'isMergeCommit: commit with two parents returns true', - test: () => isMergeCommit({ parents: [{}, {}] }) === true, - }, - { - name: 'isMergeCommit: commit with one parent returns false', - test: () => isMergeCommit({ parents: [{}] }) === false, - }, - { - name: 'isMergeCommit: commit with no parents field returns false', - test: () => isMergeCommit({}) === false, - }, - { - name: 'isMergeCommit: null commit returns false', - test: () => isMergeCommit(null) === false, - }, - { - name: 'isMergeCommit: undefined commit returns false', - test: () => isMergeCommit(undefined) === false, - }, - { - name: 'isMergeCommit: commit with empty parents array returns false', - test: () => isMergeCommit({ parents: [] }) === false, - }, - - // --------------------------------------------------------------------------- - // checkDCO - // --------------------------------------------------------------------------- - { - name: 'checkDCO: all pass', - test: () => { - const commits = [ - { sha: 'abc1234', commit: { message: 'Fix\n\nSigned-off-by: A ' } }, - { sha: 'def5678', commit: { message: 'Fix2\n\nSigned-off-by: B ' } }, - ]; - const r = checkDCO(commits); - return r.passed === true && r.failures.length === 0; - }, - }, - { - name: 'checkDCO: some fail', - test: () => { - const commits = [ - { sha: 'abc1234', commit: { message: 'Fix\n\nSigned-off-by: A ' } }, - { sha: 'def5678', commit: { message: 'No sign-off here' } }, - ]; - const r = checkDCO(commits); - return ( - r.passed === false && - r.failures.length === 1 && - r.failures[0].sha === 'def5678' && - r.failures[0].message === 'No sign-off here' - ); - }, - }, - { - name: 'checkDCO: all fail', - test: () => { - const commits = [ - { sha: 'aaa1111', commit: { message: 'No sign-off' } }, - { sha: 'bbb2222', commit: { message: 'Also no sign-off' } }, - ]; - const r = checkDCO(commits); - return r.passed === false && r.failures.length === 2; - }, - }, - { - name: 'checkDCO: empty commits', - test: () => { - const r = checkDCO([]); - return r.passed === true && r.failures.length === 0; - }, - }, - { - name: 'checkDCO: merge commit without sign-off is skipped (passes)', - test: () => { - const commits = [ - { sha: 'merge123', parents: [{}, {}], commit: { message: 'Merge branch main into feat' } }, - ]; - const r = checkDCO(commits); - return r.passed === true && r.failures.length === 0; - }, - }, - { - name: 'checkDCO: merge commit skipped, regular commits still checked', - test: () => { - const commits = [ - { sha: 'abc1234', commit: { message: 'Fix\n\nSigned-off-by: A ' } }, - { sha: 'merge56', parents: [{}, {}], commit: { message: 'Merge branch main into feat' } }, - { sha: 'def5678', commit: { message: 'No sign-off here' } }, - ]; - const r = checkDCO(commits); - return ( - r.passed === false && - r.failures.length === 1 && - r.failures[0].sha === 'def5678' - ); - }, - }, - { - name: 'checkDCO: all regular commits pass with merge commit present', - test: () => { - const commits = [ - { sha: 'abc1234', commit: { message: 'Fix\n\nSigned-off-by: A ' } }, - { sha: 'merge56', parents: [{}, {}], commit: { message: 'Merge branch main into feat' } }, - ]; - const r = checkDCO(commits); - return r.passed === true && r.failures.length === 0; - }, - }, - - // --------------------------------------------------------------------------- - // checkGPG - // --------------------------------------------------------------------------- - { - name: 'checkGPG: all pass', - test: () => { - const commits = [ - { sha: 'abc1234', commit: { message: 'Fix', verification: { verified: true } } }, - { sha: 'def5678', commit: { message: 'Fix2', verification: { verified: true } } }, - ]; - const r = checkGPG(commits); - return r.passed === true && r.failures.length === 0; - }, - }, - { - name: 'checkGPG: some fail', - test: () => { - const commits = [ - { sha: 'abc1234', commit: { message: 'Fix', verification: { verified: true } } }, - { sha: 'def5678', commit: { message: 'Fix2', verification: { verified: false } } }, - ]; - const r = checkGPG(commits); - return ( - r.passed === false && - r.failures.length === 1 && - r.failures[0].sha === 'def5678' - ); - }, - }, - { - name: 'checkGPG: all fail', - test: () => { - const commits = [ - { sha: 'aaa1111', commit: { message: 'Fix', verification: { verified: false } } }, - { sha: 'bbb2222', commit: { message: 'Fix2' } }, - ]; - const r = checkGPG(commits); - return r.passed === false && r.failures.length === 2; - }, - }, - { - name: 'checkGPG: empty commits', - test: () => { - const r = checkGPG([]); - return r.passed === true && r.failures.length === 0; - }, - }, - - // --------------------------------------------------------------------------- - // checkMergeConflict - // --------------------------------------------------------------------------- - { - name: 'checkMergeConflict: mergeable true', - test: async () => { - const botContext = { - github: { - rest: { - pulls: { - get: async () => ({ data: { mergeable: true, mergeable_state: 'clean' } }), - }, - }, - }, - owner: 'o', - repo: 'r', - number: 1, - }; - const r = await checkMergeConflict(botContext); - return r.passed === true; - }, - }, - { - name: 'checkMergeConflict: mergeable false', - test: async () => { - const botContext = { - github: { - rest: { - pulls: { - get: async () => ({ data: { mergeable: false, mergeable_state: 'dirty' } }), - }, - }, - }, - owner: 'o', - repo: 'r', - number: 1, - }; - const r = await checkMergeConflict(botContext); - return r.passed === false; - }, - }, - { - name: 'checkMergeConflict: mergeable null then true on retry', - test: async () => { - let callCount = 0; - const botContext = { - github: { - rest: { - pulls: { - get: async () => { - callCount++; - return { - data: { - mergeable: callCount === 1 ? null : true, - mergeable_state: callCount === 1 ? 'unknown' : 'clean', - }, - }; - }, - }, - }, - }, - owner: 'o', - repo: 'r', - number: 1, - }; - const r = await checkMergeConflict(botContext); - return r.passed === true && callCount === 2; - }, - }, - - // --------------------------------------------------------------------------- - // checkIssueLink - // --------------------------------------------------------------------------- - { - name: 'checkIssueLink: Fixes #123 in body, author assigned', - test: async () => { - const ctx = { - pr: { body: 'Fixes #123', user: { login: 'alice' } }, - }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'alice' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true && r.reason === null; - }, - }, - { - name: 'checkIssueLink: fixes #123 lowercase', - test: async () => { - const ctx = { pr: { body: 'fixes #123', user: { login: 'bob' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'bob' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: Fixed #123', - test: async () => { - const ctx = { pr: { body: 'Fixed #123', user: { login: 'carol' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'carol' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: Closes #456', - test: async () => { - const ctx = { pr: { body: 'Closes #456', user: { login: 'dave' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'dave' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: close #789', - test: async () => { - const ctx = { pr: { body: 'close #789', user: { login: 'eve' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'eve' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: Closed #789', - test: async () => { - const ctx = { pr: { body: 'Closed #789', user: { login: 'frank' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'frank' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: Resolves #100', - test: async () => { - const ctx = { pr: { body: 'Resolves #100', user: { login: 'grace' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'grace' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: resolve #100', - test: async () => { - const ctx = { pr: { body: 'resolve #100', user: { login: 'henry' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'henry' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: Resolved #100', - test: async () => { - const ctx = { pr: { body: 'Resolved #100', user: { login: 'ivy' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'ivy' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: Related to #200', - test: async () => { - const ctx = { pr: { body: 'Related to #200', user: { login: 'jack' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'jack' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: related to #200 lowercase', - test: async () => { - const ctx = { pr: { body: 'related to #200', user: { login: 'kate' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'kate' }] }); - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: multiple keywords Fixes #1 and Closes #2', - test: async () => { - const ctx = { pr: { body: 'Fixes #1\nCloses #2', user: { login: 'lea' } } }; - let fetched = []; - const fetchIssue = async (_, num) => { - fetched.push(num); - return { title: `Issue ${num}`, assignees: [{ login: 'lea' }] }; - }; - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true && fetched.includes(1) && fetched.includes(2); - }, - }, - { - name: 'checkIssueLink: body with #123 but no keyword, GraphQL fallback called', - test: async () => { - let graphqlCalled = false; - const ctx = { pr: { body: 'Addresses issue #123', user: { login: 'mike' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'mike' }] }); - const fetchClosing = async () => { - graphqlCalled = true; - return [123]; - }; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return graphqlCalled === true && r.passed === true; - }, - }, - { - name: 'checkIssueLink: empty body', - test: async () => { - const ctx = { pr: { body: '', user: { login: 'nancy' } } }; - let graphqlCalled = false; - const fetchIssue = async () => ({}); - const fetchClosing = async () => { - graphqlCalled = true; - return []; - }; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === false && r.reason === 'no_issue_linked' && graphqlCalled === true; - }, - }, - { - name: 'checkIssueLink: null body', - test: async () => { - const ctx = { pr: { body: null, user: { login: 'oscar' } } }; - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { - fetchIssue: async () => ({}), - fetchClosingIssueNumbers: fetchClosing, - }); - return r.passed === false && r.reason === 'no_issue_linked'; - }, - }, - { - name: 'checkIssueLink: GraphQL fallback returns issue', - test: async () => { - const ctx = { pr: { body: 'No keyword here', user: { login: 'paul' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'paul' }] }); - const fetchClosing = async () => [999]; - const r = await checkIssueLink(ctx, { fetchIssue, fetchClosingIssueNumbers: fetchClosing }); - return r.passed === true && r.issues.some(i => i.number === 999); - }, - }, - { - name: 'checkIssueLink: GraphQL returns 0', - test: async () => { - const ctx = { pr: { body: 'No keywords', user: { login: 'quinn' } } }; - const fetchClosing = async () => []; - const r = await checkIssueLink(ctx, { - fetchIssue: async () => ({}), - fetchClosingIssueNumbers: fetchClosing, - }); - return r.passed === false && r.reason === 'no_issue_linked'; - }, - }, - { - name: 'checkIssueLink: 1 linked issue, author assigned', - test: async () => { - const ctx = { pr: { body: 'Fixes #1', user: { login: 'rachel' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'rachel' }] }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: 1 linked issue, different user', - test: async () => { - const ctx = { pr: { body: 'Fixes #1', user: { login: 'sam' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'other-user' }] }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === false && r.reason === 'not_assigned'; - }, - }, - { - name: 'checkIssueLink: 1 linked issue, no assignees', - test: async () => { - const ctx = { pr: { body: 'Fixes #1', user: { login: 'tina' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [] }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === false && r.reason === 'not_assigned'; - }, - }, - { - name: 'checkIssueLink: multiple issues, author assigned to first only', - test: async () => { - const ctx = { pr: { body: 'Fixes #1\nCloses #2', user: { login: 'uma' } } }; - const fetchIssue = async (_, num) => ({ - title: `Issue ${num}`, - assignees: num === 1 ? [{ login: 'uma' }] : [{ login: 'other' }], - }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === false && r.reason === 'not_assigned'; - }, - }, - { - name: 'checkIssueLink: multiple issues, author assigned to second only', - test: async () => { - const ctx = { pr: { body: 'Fixes #1\nCloses #2', user: { login: 'vic' } } }; - const fetchIssue = async (_, num) => ({ - title: `Issue ${num}`, - assignees: num === 2 ? [{ login: 'vic' }] : [{ login: 'other' }], - }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === false && r.reason === 'not_assigned'; - }, - }, - { - name: 'checkIssueLink: multiple issues, author assigned to all', - test: async () => { - const ctx = { pr: { body: 'Fixes #1\nCloses #2', user: { login: 'zara' } } }; - const fetchIssue = async () => ({ - title: 'Bug', - assignees: [{ login: 'zara' }], - }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === true && r.issues.length === 2; - }, - }, - { - name: 'checkIssueLink: multiple issues, author assigned to none', - test: async () => { - const ctx = { pr: { body: 'Fixes #1\nCloses #2', user: { login: 'wade' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'other' }] }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === false && r.reason === 'not_assigned'; - }, - }, - { - name: 'checkIssueLink: case-insensitive author check', - test: async () => { - const ctx = { pr: { body: 'Fixes #1', user: { login: 'Alice' } } }; - const fetchIssue = async () => ({ title: 'Bug', assignees: [{ login: 'alice' }] }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === true; - }, - }, - { - name: 'checkIssueLink: fetchIssue throws 404, graceful no_issue_linked', - test: async () => { - const ctx = { pr: { body: 'Fixes #1', user: { login: 'xavier' } } }; - const fetchIssue = async () => { - throw new Error('Not Found'); - }; - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [], - }); - return r.passed === false && r.reason === 'no_issue_linked' && r.issues.length === 0; - }, - }, - { - name: 'checkIssueLink: PR with a title containing an issue number and a body with none', - test: async () => { - const ctx = { pr: { title: 'Feature relates to #314', body: 'Fixes #', user: { login: 'monte' } } }; - const fetchIssue = async () => ({ - title: 'Feature request', - assignees: [{ login: 'monte' }], - }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [314], - }); - return r.passed === false && r.reason === 'no_issue_linked' && r.issues.length === 0; - }, - }, - { - name: 'checkIssueLink: PR with a title that contains no issue references and an empty body', - test: async () => { - const ctx = { pr: { title: 'Fix', body: 'Fixing something', user: { login: 'mark' } } }; - const fetchIssue = async () => ({ - title: 'Bug', - assignees: [{ login: 'mark' }], - }); - const r = await checkIssueLink(ctx, { - fetchIssue, - fetchClosingIssueNumbers: async () => [12], - }); - return r.passed === true && r.issues.length === 1 && r.issues[0].number === 12; - }, - }, -]; - -// ============================================================================= -// TEST RUNNER -// ============================================================================= - -async function runUnitTests() { - console.log('🔬 UNIT TESTS (checks)'); - console.log('='.repeat(70)); - let passed = 0; - let failed = 0; - for (const test of unitTests) { - try { - const result = await Promise.resolve(test.test()); - if (result) { - console.log(`✅ ${test.name}`); - passed++; - } else { - console.log(`❌ ${test.name}`); - failed++; - } - } catch (error) { - console.log(`❌ ${test.name} - Error: ${error.message}`); - failed++; - } - } - console.log('\n' + '-'.repeat(70)); - console.log(`Unit Tests: ${passed} passed, ${failed} failed`); - return { total: unitTests.length, passed, failed }; -} - -runTestSuite('CHECK HELPERS TEST SUITE', [], async () => true, [ - { label: 'Unit Tests', run: runUnitTests }, -]); diff --git a/.github/scripts/tests/test-comments.cjs b/.github/scripts/tests/test-comments.cjs deleted file mode 100644 index 586c7557..00000000 --- a/.github/scripts/tests/test-comments.cjs +++ /dev/null @@ -1,466 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-comments.cjs -// -// Unit tests for helpers/comments.cjs (unified bot comment builder). -// Run with: node .github/scripts/tests/test-comments.cjs - -const { runTestSuite } = require('./test-utils'); -const { MARKER, buildBotComment, buildChecksSection, allChecksPassed, buildMergeConflictNotificationComment } = require('../helpers/comments'); - -// ============================================================================= -// TEST DATA HELPERS -// ============================================================================= - -function allPassing() { - return { - dco: { passed: true, failures: [] }, - gpg: { passed: true, failures: [] }, - merge: { passed: true }, - issueLink: { - passed: true, - reason: null, - issues: [{ number: 42, title: 'test', isAssigned: true }], - }, - }; -} - -function withDCOFailed(data = allPassing()) { - return { - ...data, - dco: { passed: false, failures: [{ sha: 'abc123', message: 'Fix bug' }] }, - }; -} - -function withDCOError(data = allPassing()) { - return { - ...data, - dco: { passed: false, error: true, errorMessage: 'API timeout' }, - }; -} - -function withGPGFailed(data = allPassing()) { - return { - ...data, - gpg: { passed: false, failures: [{ sha: 'def456', message: 'Another fix' }] }, - }; -} - -function withGPGError(data = allPassing()) { - return { - ...data, - gpg: { passed: false, error: true, errorMessage: 'Network error' }, - }; -} - -function withMergeFailed(data = allPassing()) { - return { - ...data, - merge: { passed: false }, - }; -} - - -function withIssueLinkNoIssue(data = allPassing()) { - return { - ...data, - issueLink: { passed: false, reason: 'no_issue_linked', issues: [] }, - }; -} - -function withIssueLinkNotAssigned(data = allPassing()) { - return { - ...data, - issueLink: { - passed: false, - reason: 'not_assigned', - issues: [{ number: 99, title: 'Bug', isAssigned: false }], - }, - }; -} - - -// ============================================================================= -// UNIT TESTS -// ============================================================================= - -const unitTests = [ - // --------------------------------------------------------------------------- - // Structure and greeting - // --------------------------------------------------------------------------- - { - name: 'Comment starts with marker', - test: () => { - const { body } = buildBotComment({ prAuthor: 'alice', ...allPassing() }); - return body.startsWith(''); - }, - }, - { - name: 'Greeting contains @prAuthor mention', - test: () => { - const { body } = buildBotComment({ prAuthor: 'bob-smith', ...allPassing() }); - return body.includes('@bob-smith'); - }, - }, - { - name: 'Greeting contains "PR Helper Bot"', - test: () => { - const { body } = buildBotComment({ prAuthor: 'carol', ...allPassing() }); - return body.includes('PR Helper Bot'); - }, - }, - { - name: 'marker field equals MARKER constant', - test: () => { - const { marker } = buildBotComment({ prAuthor: 'dave', ...allPassing() }); - return marker === MARKER; - }, - }, - { - name: 'allPassed: true when all checks pass', - test: () => { - const { allPassed } = buildBotComment({ prAuthor: 'eve', ...allPassing() }); - return allPassed === true; - }, - }, - { - name: 'allPassed: false when any check fails', - test: () => { - const data = withDCOFailed(); - const { allPassed } = buildBotComment({ prAuthor: 'frank', ...data }); - return allPassed === false; - }, - }, - - // --------------------------------------------------------------------------- - // DCO section - // --------------------------------------------------------------------------- - { - name: 'DCO all pass: body contains :white_check_mark: and "All commits have valid sign-offs"', - test: () => { - const { body } = buildBotComment({ prAuthor: 'gina', ...allPassing() }); - return body.includes(':white_check_mark:') && body.includes('All commits have valid sign-offs'); - }, - }, - { - name: 'DCO failures: body contains :x: and "DCO Sign-off" and lists failing commits', - test: () => { - const data = withDCOFailed(); - const { body } = buildBotComment({ prAuthor: 'henry', ...data }); - return ( - body.includes(':x:') && - body.includes('DCO Sign-off') && - body.includes('abc123') && - body.includes('Fix bug') - ); - }, - }, - { - name: 'DCO failures include link to Signing Guide', - test: () => { - const data = withDCOFailed(); - const { body } = buildBotComment({ prAuthor: 'ivy', ...data }); - return body.includes('signing.md'); - }, - }, - - // --------------------------------------------------------------------------- - // GPG section - // --------------------------------------------------------------------------- - { - name: 'GPG all pass: body contains "All commits have verified GPG signatures"', - test: () => { - const { body } = buildBotComment({ prAuthor: 'jack', ...allPassing() }); - return body.includes('All commits have verified GPG signatures'); - }, - }, - { - name: 'GPG failures: body contains :x: and "GPG Signature" and lists failing commits', - test: () => { - const data = withGPGFailed(); - const { body } = buildBotComment({ prAuthor: 'kate', ...data }); - return ( - body.includes(':x:') && - body.includes('GPG Signature') && - body.includes('def456') && - body.includes('Another fix') - ); - }, - }, - { - name: 'GPG failures include link to Signing Guide', - test: () => { - const data = withGPGFailed(); - const { body } = buildBotComment({ prAuthor: 'lea', ...data }); - return body.includes('signing.md'); - }, - }, - - // --------------------------------------------------------------------------- - // Merge conflict section - // --------------------------------------------------------------------------- - { - name: 'Merge no conflicts: body contains "No merge conflicts detected"', - test: () => { - const { body } = buildBotComment({ prAuthor: 'mike', ...allPassing() }); - return body.includes('No merge conflicts detected'); - }, - }, - { - name: 'Merge conflicts: body contains :x: and "merge conflicts" and guide link', - test: () => { - const data = withMergeFailed(); - const { body } = buildBotComment({ prAuthor: 'nancy', ...data }); - return ( - body.includes(':x:') && - body.includes('merge conflicts') && - body.includes('merge-conflicts.md') - ); - }, - }, - - // --------------------------------------------------------------------------- - // Issue link section - // --------------------------------------------------------------------------- - { - name: 'Issue link passed: body contains "Linked to #42 (assigned to you)"', - test: () => { - const { body } = buildBotComment({ prAuthor: 'oscar', ...allPassing() }); - return body.includes('Linked to #42') && body.includes('assigned to you'); - }, - }, - { - name: 'Issue link reason no_issue_linked: body contains "not linked to any issue"', - test: () => { - const data = withIssueLinkNoIssue(); - const { body } = buildBotComment({ prAuthor: 'paul', ...data }); - return body.includes('not linked to any issue'); - }, - }, - { - name: 'Issue link reason not_assigned: body shows unassigned issues', - test: () => { - const data = withIssueLinkNotAssigned(); - const { body } = buildBotComment({ prAuthor: 'quinn', ...data }); - return body.includes('not assigned to the following linked issues') && body.includes('#99'); - }, - }, - - // --------------------------------------------------------------------------- - // Footer - // --------------------------------------------------------------------------- - { - name: 'Footer all pass: body contains :tada: and "All checks passed"', - test: () => { - const { body } = buildBotComment({ prAuthor: 'rachel', ...allPassing() }); - return body.includes(':tada:') && body.includes('All checks passed'); - }, - }, - { - name: 'Footer any fail: body contains "All checks must pass"', - test: () => { - const data = withDCOFailed(); - const { body } = buildBotComment({ prAuthor: 'sam', ...data }); - return body.includes('All checks must pass'); - }, - }, - - // --------------------------------------------------------------------------- - // Error state - // --------------------------------------------------------------------------- - { - name: 'DCO errored: body contains :warning: and maintainer team tag', - test: () => { - const data = withDCOError(); - const { body } = buildBotComment({ prAuthor: 'tina', ...data }); - return ( - body.includes(':warning:') && - body.includes('@utkarsh232005') && - body.includes('internal error') - ); - }, - }, - { - name: 'Multiple errors: each shows warning', - test: () => { - const data = withDCOError(withGPGError()); - const { body } = buildBotComment({ prAuthor: 'uma', ...data }); - const dcoWarning = body.includes(':warning: **DCO Sign-off**'); - const gpgWarning = body.includes(':warning: **GPG Signature**'); - return dcoWarning && gpgWarning; - }, - }, - { - name: 'Mix of error + fail + pass: correct icons for each', - test: () => { - const data = withDCOError(withGPGFailed(withMergeFailed(allPassing()))); - const { body } = buildBotComment({ prAuthor: 'vic', ...data }); - const hasDCOWarning = body.includes(':warning: **DCO Sign-off**'); - const hasGPGX = body.includes(':x: **GPG Signature**'); - const hasMergeX = body.includes(':x: **Merge Conflicts**'); - const hasIssueCheck = body.includes(':white_check_mark: **Issue Link**'); - return hasDCOWarning && hasGPGX && hasMergeX && hasIssueCheck; - }, - }, - { - name: 'allPassed false when any check errored', - test: () => { - const data = withDCOError(); - const { allPassed } = buildBotComment({ prAuthor: 'wade', ...data }); - return allPassed === false; - }, - }, - - // --------------------------------------------------------------------------- - // Combinations - // --------------------------------------------------------------------------- - { - name: 'Single failure (DCO only)', - test: () => { - const data = withDCOFailed(); - const { allPassed, body } = buildBotComment({ prAuthor: 'xander', ...data }); - return allPassed === false && body.includes(':x: **DCO Sign-off**'); - }, - }, - { - name: 'Two failures (DCO + GPG)', - test: () => { - const data = withGPGFailed(withDCOFailed()); - const { allPassed, body } = buildBotComment({ prAuthor: 'yara', ...data }); - return ( - allPassed === false && - body.includes(':x: **DCO Sign-off**') && - body.includes(':x: **GPG Signature**') - ); - }, - }, - { - name: 'All four fail', - test: () => { - const data = withIssueLinkNoIssue( - withMergeFailed(withGPGFailed(withDCOFailed())) - ); - const { allPassed, body } = buildBotComment({ prAuthor: 'zara', ...data }); - return ( - allPassed === false && - body.includes(':x: **DCO Sign-off**') && - body.includes(':x: **GPG Signature**') && - body.includes(':x: **Merge Conflicts**') && - body.includes(':x: **Issue Link**') - ); - }, - }, - { - name: 'Issue link fail + all others pass', - test: () => { - const data = withIssueLinkNotAssigned(); - const { allPassed, body } = buildBotComment({ prAuthor: 'adam', ...data }); - const othersPass = - body.includes(':white_check_mark: **DCO Sign-off**') && - body.includes(':white_check_mark: **GPG Signature**') && - body.includes(':white_check_mark: **Merge Conflicts**'); - const issueFails = body.includes(':x: **Issue Link**'); - return allPassed === false && othersPass && issueFails; - }, - }, - - // --------------------------------------------------------------------------- - // buildChecksSection and allChecksPassed (direct) - // --------------------------------------------------------------------------- - { - name: 'buildChecksSection: contains PR Checks heading', - test: () => { - const section = buildChecksSection(allPassing()); - return section.includes('### PR Checks'); - }, - }, - { - name: 'allChecksPassed: true when all pass', - test: () => allChecksPassed(allPassing()) === true, - }, - { - name: 'allChecksPassed: false when DCO fails', - test: () => allChecksPassed(withDCOFailed()) === false, - }, - { - name: 'allChecksPassed: false when DCO errors', - test: () => allChecksPassed(withDCOError()) === false, - }, - { - name: 'Issue link assigned: multiple issues format', - test: () => { - const data = allPassing(); - data.issueLink.issues = [ - { number: 1, title: 'Bug 1', isAssigned: true }, - { number: 2, title: 'Bug 2', isAssigned: true }, - ]; - const { body } = buildBotComment({ prAuthor: 'multi', ...data }); - return body.includes('#1') && body.includes('#2') && body.includes('assigned to you'); - }, - }, - - // --------------------------------------------------------------------------- - // buildMergeConflictNotificationComment - // --------------------------------------------------------------------------- - { - name: 'Notification comment contains @prAuthor mention', - test: () => { - const comment = buildMergeConflictNotificationComment('alice', 42); - return comment.includes('@alice'); - }, - }, - { - name: 'Notification comment references merged PR number', - test: () => { - const comment = buildMergeConflictNotificationComment('bob', 123); - return comment.includes('#123'); - }, - }, - { - name: 'Notification comment includes wave emoji', - test: () => { - const comment = buildMergeConflictNotificationComment('carol', 7); - return comment.includes(':wave:'); - }, - }, - { - name: 'Notification comment asks to resolve merge conflict', - test: () => { - const comment = buildMergeConflictNotificationComment('dave', 99); - return comment.includes('resolve the merge conflict'); - }, - }, -]; - -// ============================================================================= -// TEST RUNNER -// ============================================================================= - -async function runUnitTests() { - console.log('🔬 UNIT TESTS (comments)'); - console.log('='.repeat(70)); - let passed = 0; - let failed = 0; - for (const test of unitTests) { - try { - const result = await Promise.resolve(test.test()); - if (result) { - console.log(`✅ ${test.name}`); - passed++; - } else { - console.log(`❌ ${test.name}`); - failed++; - } - } catch (error) { - console.log(`❌ ${test.name} - Error: ${error.message}`); - failed++; - } - } - console.log('\n' + '-'.repeat(70)); - console.log(`Unit Tests: ${passed} passed, ${failed} failed`); - return { total: unitTests.length, passed, failed }; -} - -runTestSuite('COMMENT HELPERS TEST SUITE', [], async () => true, [ - { label: 'Unit Tests', run: runUnitTests }, -]); diff --git a/.github/scripts/tests/test-config-loader.cjs b/.github/scripts/tests/test-config-loader.cjs deleted file mode 100644 index a7def0a4..00000000 --- a/.github/scripts/tests/test-config-loader.cjs +++ /dev/null @@ -1,575 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-config-loader.cjs -// -// Unit tests for helpers/config-loader.cjs -// Run with: node .github/scripts/tests/test-config-loader.cjs -// -// Tests cover: -// - Valid config loading and constant building -// - Missing config file error -// - Malformed JSON error -// - Validation errors for every required field and cross-reference - -const fs = require('fs'); -const path = require('path'); -const { runTestSuite } = require('./test-utils'); -const { loadAutomationConfig, buildConstants, DEFAULT_CONFIG_PATH } = require('../helpers/config-loader'); - -// ============================================================================= -// HELPERS -// ============================================================================= - -const SCRATCH_DIR = path.resolve(__dirname, '.config-loader-scratch'); - -/** - * Returns a deep clone of the production config for use as a valid baseline. - * Tests mutate this clone to create invalid configs without touching the real file. - */ -function getValidConfig() { - const raw = fs.readFileSync(DEFAULT_CONFIG_PATH, 'utf8'); - return JSON.parse(raw); -} - -/** - * Writes a config object to a temporary file and returns the path. - * @param {string} name - Filename (no directory). - * @param {object|string} content - Object (JSON-serialized) or raw string. - * @returns {string} Absolute path to the temp file. - */ -function writeTempConfig(name, content) { - if (!fs.existsSync(SCRATCH_DIR)) { - fs.mkdirSync(SCRATCH_DIR, { recursive: true }); - } - const filePath = path.join(SCRATCH_DIR, name); - const data = typeof content === 'string' ? content : JSON.stringify(content, null, 2); - fs.writeFileSync(filePath, data, 'utf8'); - return filePath; -} - -/** - * Removes the scratch directory and all temp files. - */ -function cleanupScratch() { - if (fs.existsSync(SCRATCH_DIR)) { - for (const file of fs.readdirSync(SCRATCH_DIR)) { - fs.unlinkSync(path.join(SCRATCH_DIR, file)); - } - fs.rmdirSync(SCRATCH_DIR); - } -} - -/** - * Asserts that loadAutomationConfig throws an error containing the expected substring. - * @param {string} configPath - Path to the temp config file. - * @param {string} expectedSubstring - Expected substring in the error message. - * @returns {boolean} - */ -function expectLoadError(configPath, expectedSubstring) { - try { - loadAutomationConfig(configPath); - return false; - } catch (err) { - if (!err.message.includes(expectedSubstring)) { - console.log(` Expected error containing: "${expectedSubstring}"`); - console.log(` Got: "${err.message}"`); - return false; - } - return true; - } -} - -// ============================================================================= -// UNIT TESTS -// ============================================================================= - -const unitTests = [ - - // --------------------------------------------------------------------------- - // Valid config loading - // --------------------------------------------------------------------------- - { - name: 'loadAutomationConfig: loads production config without error', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - return config !== null && typeof config === 'object'; - }, - }, - { - name: 'loadAutomationConfig: returns a frozen object', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - return Object.isFrozen(config); - }, - }, - { - name: 'buildConstants: produces MAINTAINER_TEAM string', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const derived = buildConstants(config); - return derived.MAINTAINER_TEAM === '@utkarsh232005'; - }, - }, - { - name: 'buildConstants: produces GFI_SUPPORT_TEAM string', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const derived = buildConstants(config); - return derived.GFI_SUPPORT_TEAM === '@utkarsh232005'; - }, - }, - { - name: 'buildConstants: LABELS has all expected keys', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const { LABELS } = buildConstants(config); - const expectedKeys = [ - 'AWAITING_TRIAGE', 'READY_FOR_DEV', 'IN_PROGRESS', 'BLOCKED', - 'NEEDS_REVIEW', 'NEEDS_REVISION', - 'GOOD_FIRST_ISSUE', 'BEGINNER', 'INTERMEDIATE', 'ADVANCED', - 'PRIORITY_CRITICAL', 'PRIORITY_HIGH', 'PRIORITY_MEDIUM', 'PRIORITY_LOW', - ]; - return expectedKeys.every(k => typeof LABELS[k] === 'string' && LABELS[k].length > 0); - }, - }, - { - name: 'buildConstants: LABELS is frozen', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const { LABELS } = buildConstants(config); - return Object.isFrozen(LABELS); - }, - }, - { - name: 'buildConstants: SKILL_HIERARCHY matches config order', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const { SKILL_HIERARCHY, LABELS } = buildConstants(config); - return ( - SKILL_HIERARCHY.length === 4 && - SKILL_HIERARCHY[0] === LABELS.GOOD_FIRST_ISSUE && - SKILL_HIERARCHY[3] === LABELS.ADVANCED - ); - }, - }, - { - name: 'buildConstants: PRIORITY_HIERARCHY matches config order', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const { PRIORITY_HIERARCHY, LABELS } = buildConstants(config); - return ( - PRIORITY_HIERARCHY.length === 4 && - PRIORITY_HIERARCHY[0] === LABELS.PRIORITY_CRITICAL && - PRIORITY_HIERARCHY[3] === LABELS.PRIORITY_LOW - ); - }, - }, - { - name: 'buildConstants: SKILL_PREREQUISITES has correct progression', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const { SKILL_PREREQUISITES, LABELS } = buildConstants(config); - return ( - SKILL_PREREQUISITES[LABELS.GOOD_FIRST_ISSUE].requiredLabel === null && - SKILL_PREREQUISITES[LABELS.BEGINNER].requiredLabel === LABELS.GOOD_FIRST_ISSUE && - SKILL_PREREQUISITES[LABELS.BEGINNER].requiredCount === 2 && - SKILL_PREREQUISITES[LABELS.INTERMEDIATE].requiredLabel === LABELS.BEGINNER && - SKILL_PREREQUISITES[LABELS.INTERMEDIATE].requiredCount === 3 && - SKILL_PREREQUISITES[LABELS.ADVANCED].requiredLabel === LABELS.INTERMEDIATE && - SKILL_PREREQUISITES[LABELS.ADVANCED].requiredCount === 3 - ); - }, - }, - { - name: 'buildConstants: DOCUMENTATION has all expected keys', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const { DOCUMENTATION } = buildConstants(config); - return ( - typeof DOCUMENTATION.workflowGuide === 'string' && - typeof DOCUMENTATION.readme === 'string' && - typeof DOCUMENTATION.signingGuide === 'string' && - typeof DOCUMENTATION.mergeConflictsGuide === 'string' - ); - }, - }, - { - name: 'buildConstants: COMMUNITY has discordChannel', - test: () => { - const config = loadAutomationConfig(DEFAULT_CONFIG_PATH); - const { COMMUNITY } = buildConstants(config); - return typeof COMMUNITY.discordChannel === 'string' && COMMUNITY.discordChannel.length > 0; - }, - }, - - // --------------------------------------------------------------------------- - // File-level errors - // --------------------------------------------------------------------------- - { - name: 'loadAutomationConfig: missing file → clear error', - test: () => { - return expectLoadError('/nonexistent/path/config.json', 'Failed to read automation config'); - }, - }, - { - name: 'loadAutomationConfig: malformed JSON → clear error', - test: () => { - const p = writeTempConfig('malformed.json', '{ broken json!!!'); - return expectLoadError(p, 'Failed to parse automation config'); - }, - }, - - // --------------------------------------------------------------------------- - // Validation: teams - // --------------------------------------------------------------------------- - { - name: 'validation: missing maintainerTeam → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.maintainerTeam; - const p = writeTempConfig('no-team.json', cfg); - return expectLoadError(p, 'maintainerTeam must be a non-empty string'); - }, - }, - { - name: 'validation: empty goodFirstIssueSupportTeam → error', - test: () => { - const cfg = getValidConfig(); - cfg.goodFirstIssueSupportTeam = ''; - const p = writeTempConfig('empty-gfi-team.json', cfg); - return expectLoadError(p, 'goodFirstIssueSupportTeam must be a non-empty string'); - }, - }, - - // --------------------------------------------------------------------------- - // Validation: labels - // --------------------------------------------------------------------------- - { - name: 'validation: missing labels → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.labels; - const p = writeTempConfig('no-labels.json', cfg); - return expectLoadError(p, 'labels must be an object'); - }, - }, - { - name: 'validation: missing labels.skill → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.labels.skill; - const p = writeTempConfig('no-skill-labels.json', cfg); - return expectLoadError(p, 'labels.skill must be an object'); - }, - }, - { - name: 'validation: empty label value → error', - test: () => { - const cfg = getValidConfig(); - cfg.labels.status.awaitingTriage = ''; - const p = writeTempConfig('empty-label.json', cfg); - return expectLoadError(p, 'labels.status.awaitingTriage is required and must be a non-empty string'); - }, - }, - { - name: 'validation: missing required status key (blocked) → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.labels.status.blocked; - const p = writeTempConfig('no-blocked.json', cfg); - return expectLoadError(p, 'labels.status.blocked is required and must be a non-empty string'); - }, - }, - { - name: 'validation: missing required skill key (beginner) → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.labels.skill.beginner; - const p = writeTempConfig('no-beginner.json', cfg); - return expectLoadError(p, 'labels.skill.beginner is required and must be a non-empty string'); - }, - }, - { - name: 'validation: missing required priority key (high) → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.labels.priority.high; - const p = writeTempConfig('no-high.json', cfg); - return expectLoadError(p, 'labels.priority.high is required and must be a non-empty string'); - }, - }, - - // --------------------------------------------------------------------------- - // Validation: hierarchies - // --------------------------------------------------------------------------- - { - name: 'validation: empty skillHierarchy → error', - test: () => { - const cfg = getValidConfig(); - cfg.skillHierarchy = []; - const p = writeTempConfig('empty-skill-hier.json', cfg); - return expectLoadError(p, 'skillHierarchy must be a non-empty array'); - }, - }, - { - name: 'validation: skillHierarchy entry not in labels.skill → error', - test: () => { - const cfg = getValidConfig(); - cfg.skillHierarchy.push('skill: nonexistent'); - const p = writeTempConfig('bad-skill-hier.json', cfg); - return expectLoadError(p, 'skillHierarchy entry "skill: nonexistent" not found in labels.skill values'); - }, - }, - { - name: 'validation: priorityHierarchy entry not in labels.priority → error', - test: () => { - const cfg = getValidConfig(); - cfg.priorityHierarchy.push('priority: ultra'); - const p = writeTempConfig('bad-prio-hier.json', cfg); - return expectLoadError(p, 'priorityHierarchy entry "priority: ultra" not found in labels.priority values'); - }, - }, - { - name: 'validation: duplicate skillHierarchy entry → error', - test: () => { - const cfg = getValidConfig(); - cfg.skillHierarchy.push(cfg.skillHierarchy[0]); - const p = writeTempConfig('dup-skill-hier.json', cfg); - return expectLoadError(p, 'appears more than once'); - }, - }, - { - name: 'validation: duplicate priorityHierarchy entry → error', - test: () => { - const cfg = getValidConfig(); - cfg.priorityHierarchy.push(cfg.priorityHierarchy[0]); - const p = writeTempConfig('dup-prio-hier.json', cfg); - return expectLoadError(p, 'appears more than once'); - }, - }, - - // --------------------------------------------------------------------------- - // Validation: skill prerequisites - // --------------------------------------------------------------------------- - { - name: 'validation: skillPrerequisites key not in skillHierarchy → error', - test: () => { - const cfg = getValidConfig(); - cfg.skillPrerequisites['skill: phantom'] = { requiredLabel: null, requiredCount: 0, displayName: 'Phantom' }; - const p = writeTempConfig('bad-prereq-key.json', cfg); - return expectLoadError(p, 'skillPrerequisites key "skill: phantom" not found in skillHierarchy'); - }, - }, - { - name: 'validation: non-null requiredLabel not in skillHierarchy → error', - test: () => { - const cfg = getValidConfig(); - cfg.skillPrerequisites['skill: beginner'].requiredLabel = 'skill: imaginary'; - const p = writeTempConfig('bad-prereq-label.json', cfg); - return expectLoadError(p, 'requiredLabel "skill: imaginary" not found in skillHierarchy'); - }, - }, - { - name: 'validation: skillHierarchy entry missing from skillPrerequisites → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.skillPrerequisites['skill: intermediate']; - const p = writeTempConfig('missing-prereq-entry.json', cfg); - return expectLoadError(p, 'skillPrerequisites is missing entry for skillHierarchy value "skill: intermediate"'); - }, - }, - { - name: 'validation: prerequisite missing requiredLabel → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.skillPrerequisites['skill: good first issue'].requiredLabel; - const p = writeTempConfig('no-req-label.json', cfg); - return expectLoadError(p, 'requiredLabel is required'); - }, - }, - { - name: 'validation: prerequisite missing requiredCount → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.skillPrerequisites['skill: beginner'].requiredCount; - const p = writeTempConfig('no-req-count.json', cfg); - return expectLoadError(p, 'requiredCount must be a non-negative integer'); - }, - }, - { - name: 'validation: prerequisite missing displayName → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.skillPrerequisites['skill: advanced'].displayName; - const p = writeTempConfig('no-display-name.json', cfg); - return expectLoadError(p, 'displayName is required and must be a non-empty string'); - }, - }, - { - name: 'validation: prerequisite missing prerequisiteDisplayName when requiredLabel is not null → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.skillPrerequisites['skill: intermediate'].prerequisiteDisplayName; - const p = writeTempConfig('no-prereq-display.json', cfg); - return expectLoadError(p, 'prerequisiteDisplayName is required when requiredLabel is not null'); - }, - }, - - // --------------------------------------------------------------------------- - // Validation: assignment limits - // --------------------------------------------------------------------------- - { - name: 'validation: missing assignmentLimits → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.assignmentLimits; - const p = writeTempConfig('no-limits.json', cfg); - return expectLoadError(p, 'assignmentLimits must be an object'); - }, - }, - { - name: 'validation: maxOpenAssignments = 0 → error (must be positive)', - test: () => { - const cfg = getValidConfig(); - cfg.assignmentLimits.maxOpenAssignments = 0; - const p = writeTempConfig('zero-limit.json', cfg); - return expectLoadError(p, 'maxOpenAssignments must be a positive integer'); - }, - }, - { - name: 'validation: maxGfiCompletions = -1 → error (must be positive)', - test: () => { - const cfg = getValidConfig(); - cfg.assignmentLimits.maxGfiCompletions = -1; - const p = writeTempConfig('neg-gfi.json', cfg); - return expectLoadError(p, 'maxGfiCompletions must be a positive integer'); - }, - }, - { - name: 'validation: maxOpenAssignments = 1.5 → error (must be integer)', - test: () => { - const cfg = getValidConfig(); - cfg.assignmentLimits.maxOpenAssignments = 1.5; - const p = writeTempConfig('float-limit.json', cfg); - return expectLoadError(p, 'maxOpenAssignments must be a positive integer'); - }, - }, - - // --------------------------------------------------------------------------- - // Validation: documentation - // --------------------------------------------------------------------------- - { - name: 'validation: missing documentation → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.documentation; - const p = writeTempConfig('no-docs.json', cfg); - return expectLoadError(p, 'documentation must be an object'); - }, - }, - { - name: 'validation: empty documentation value → error', - test: () => { - const cfg = getValidConfig(); - cfg.documentation.signingGuide = ''; - const p = writeTempConfig('empty-doc.json', cfg); - return expectLoadError(p, 'documentation.signingGuide is required and must be a non-empty string'); - }, - }, - { - name: 'validation: missing required documentation key (readme) → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.documentation.readme; - const p = writeTempConfig('no-readme-doc.json', cfg); - return expectLoadError(p, 'documentation.readme is required and must be a non-empty string'); - }, - }, - - // --------------------------------------------------------------------------- - // Validation: community - // --------------------------------------------------------------------------- - { - name: 'validation: missing community → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.community; - const p = writeTempConfig('no-community.json', cfg); - return expectLoadError(p, 'community must be an object'); - }, - }, - { - name: 'validation: empty community value → error', - test: () => { - const cfg = getValidConfig(); - cfg.community.discordChannel = ' '; - const p = writeTempConfig('empty-discord.json', cfg); - return expectLoadError(p, 'community.discordChannel is required and must be a non-empty string'); - }, - }, - { - name: 'validation: missing required community key (discordChannel) → error', - test: () => { - const cfg = getValidConfig(); - delete cfg.community.discordChannel; - const p = writeTempConfig('no-discord.json', cfg); - return expectLoadError(p, 'community.discordChannel is required and must be a non-empty string'); - }, - }, - - // --------------------------------------------------------------------------- - // Custom config with different values - // --------------------------------------------------------------------------- - { - name: 'loadAutomationConfig: loads a custom config with alternate values', - test: () => { - const cfg = getValidConfig(); - cfg.maintainerTeam = '@my-org/my-team'; - cfg.assignmentLimits.maxOpenAssignments = 5; - const p = writeTempConfig('custom.json', cfg); - const config = loadAutomationConfig(p); - const derived = buildConstants(config); - return ( - derived.MAINTAINER_TEAM === '@my-org/my-team' && - config.assignmentLimits.maxOpenAssignments === 5 - ); - }, - }, -]; - -// ============================================================================= -// TEST RUNNER -// ============================================================================= - -async function runUnitTests() { - console.log('🔬 UNIT TESTS (config-loader)'); - console.log('='.repeat(70)); - let passed = 0; - let failed = 0; - for (const test of unitTests) { - try { - const result = await Promise.resolve(test.test()); - if (result) { - console.log(`✅ ${test.name}`); - passed++; - } else { - console.log(`❌ ${test.name}`); - failed++; - } - } catch (error) { - console.log(`❌ ${test.name} - Error: ${error.message}`); - failed++; - } - } - console.log('\n' + '-'.repeat(70)); - console.log(`Unit Tests: ${passed} passed, ${failed} failed`); - - // Cleanup scratch files - cleanupScratch(); - - return { total: unitTests.length, passed, failed }; -} - -runTestSuite('CONFIG LOADER TEST SUITE', [], async () => true, [ - { label: 'Unit Tests', run: runUnitTests }, -]); diff --git a/.github/scripts/tests/test-finalize-bot.cjs b/.github/scripts/tests/test-finalize-bot.cjs deleted file mode 100644 index 0198afc4..00000000 --- a/.github/scripts/tests/test-finalize-bot.cjs +++ /dev/null @@ -1,710 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-finalize-bot.cjs -// -// Local test script for the /finalize command (bot-on-comment.cjs → commands/finalize.cjs). -// Run with: node .github/scripts/tests/test-finalize-bot.cjs -// -// Mocks the GitHub API and runs scenarios to verify /finalize behaves correctly -// without making real API calls. - -const { LABELS } = require('../helpers'); -const script = require('../bot-on-comment.cjs'); -const { runTestSuite, verifyComments } = require('./test-utils'); -const { - parseSections, - isMeaningfulContent, -} = require('../commands/finalize-comments'); - -// ============================================================================= -// MOCK GITHUB API -// ============================================================================= - -function createMockGithub(options = {}) { - const { - roleName = 'triage', // role_name returned by getCollaboratorPermissionLevel - permissionShouldFail = false, // throw HTTP 500 on permission check - permissionNotFound = false, // throw HTTP 404 (non-collaborator) - updateShouldFail = false, // throw on issues.update - removeLabelShouldFail = false, - addLabelShouldFail = false, - } = options; - - const calls = { - comments: [], - reactions: [], - labelsAdded: [], - labelsRemoved: [], - issueUpdates: [], - permissionChecks: [], - }; - - return { - calls, - rest: { - reactions: { - createForIssueComment: async (params) => { - calls.reactions.push({ commentId: params.comment_id, content: params.content }); - console.log(`\n👍 REACTION ADDED: ${params.content}`); - }, - }, - repos: { - getCollaboratorPermissionLevel: async (params) => { - calls.permissionChecks.push(params.username); - console.log(`\n🔐 PERMISSION CHECK: @${params.username}`); - if (permissionNotFound) { - const err = new Error('Not Found'); - err.status = 404; - throw err; - } - if (permissionShouldFail) { - const err = new Error('Simulated permission check failure'); - err.status = 500; - throw err; - } - console.log(` → role_name: ${roleName}`); - return { data: { role_name: roleName, permission: roleName } }; - }, - }, - issues: { - createComment: async (params) => { - calls.comments.push(params.body); - console.log('\n📝 COMMENT POSTED:'); - console.log('─'.repeat(60)); - console.log(params.body); - console.log('─'.repeat(60)); - }, - update: async (params) => { - if (updateShouldFail) { - throw new Error('Simulated issue update failure'); - } - calls.issueUpdates.push({ title: params.title, body: params.body }); - console.log(`\n✏️ ISSUE UPDATED: title="${params.title}"`); - }, - addLabels: async (params) => { - if (addLabelShouldFail) { - throw new Error('Simulated add label failure'); - } - calls.labelsAdded.push(...params.labels); - console.log(`\n🏷️ LABEL ADDED: ${params.labels.join(', ')}`); - }, - removeLabel: async (params) => { - if (removeLabelShouldFail) { - throw new Error('Simulated remove label failure'); - } - calls.labelsRemoved.push(params.name); - console.log(`\n🏷️ LABEL REMOVED: ${params.name}`); - }, - }, - }, - }; -} - -// ============================================================================= -// HELPERS -// ============================================================================= - -function makeIssue(overrides = {}) { - return { - number: 42, - title: 'Fix something', - state: 'open', - body: '### 👾 Description of the Issue\n\nThis thing is broken.\n\n### ✔️ Acceptance Criteria\n\n- [ ] Fixed', - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: LABELS.BEGINNER }, - { name: 'priority: medium' }, - ], - type: { name: 'Task' }, - assignees: [], - ...overrides, - }; -} - -function makeContext(issue, commentBody = '/finalize', commenter = 'maintainer') { - return { - eventName: 'issue_comment', - payload: { - issue, - comment: { - id: 9001, - body: commentBody, - user: { login: commenter, type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }; -} - -async function runScenario(scenario, index) { - console.log('\n' + '='.repeat(70)); - console.log(`TEST ${index + 1}: ${scenario.name}`); - console.log(`DESC: ${scenario.description}`); - console.log('='.repeat(70)); - - const github = createMockGithub(scenario.githubOptions || {}); - const context = scenario.context; - - let threw = null; - try { - await script({ github, context }); - } catch (e) { - threw = e; - } - - let passed = true; - const failures = []; - - // Snapshot verification for comment text - if (scenario.expectedComments !== undefined) { - const commentResult = verifyComments(scenario.expectedComments, github.calls.comments); - if (!commentResult.passed) { - passed = false; - failures.push(...commentResult.details.filter((d) => d.startsWith('❌'))); - } - } - - // Other behavioural assertions (labels, reactions, issue updates, body content) - for (const assertion of scenario.assertions || []) { - const result = assertion(github.calls, threw); - if (result !== true) { - passed = false; - failures.push(result); - } - } - - if (passed) { - console.log('\n✅ PASSED\n'); - } else { - console.log('\n❌ FAILED'); - for (const f of failures) console.log(' -', f); - console.log(); - } - - return passed; -} - -// ============================================================================= -// ASSERTION HELPERS -// ============================================================================= - -const assert = { - commentContains: (text) => (calls) => { - const found = calls.comments.some((c) => c.includes(text)); - return found || `Expected a comment containing: "${text}"`; - }, - noComments: () => (calls) => calls.comments.length === 0 || `Expected no comments, got ${calls.comments.length}`, - labelAdded: (label) => (calls) => calls.labelsAdded.includes(label) || `Expected label added: "${label}"`, - labelRemoved: (label) => (calls) => calls.labelsRemoved.includes(label) || `Expected label removed: "${label}"`, - noLabelsAdded: () => (calls) => calls.labelsAdded.length === 0 || `Expected no labels added, got: ${calls.labelsAdded}`, - noLabelsRemoved: () => (calls) => calls.labelsRemoved.length === 0 || `Expected no labels removed, got: ${calls.labelsRemoved}`, - noIssueUpdate: () => (calls) => calls.issueUpdates.length === 0 || `Expected no issue update, got ${calls.issueUpdates.length}`, - issueUpdated: () => (calls) => calls.issueUpdates.length > 0 || 'Expected issue to be updated', - titleContains: (text) => (calls) => { - const found = calls.issueUpdates.some((u) => u.title && u.title.includes(text)); - return found || `Expected updated title to contain: "${text}"`; - }, - bodyContains: (text) => (calls) => { - const found = calls.issueUpdates.some((u) => u.body && u.body.includes(text)); - return found || `Expected updated body to contain: "${text}"`; - }, - bodyNotContains: (text) => (calls) => { - const found = calls.issueUpdates.some((u) => u.body && u.body.includes(text)); - return !found || `Expected updated body NOT to contain: "${text}"`; - }, - reactionAdded: () => (calls) => calls.reactions.length > 0 || 'Expected thumbs-up reaction to be added', -}; - -// ============================================================================= -// SCENARIOS -// ============================================================================= - -// ============================================================================= -// EXPECTED COMMENT SNAPSHOTS -// ============================================================================= -// Defined as constants so the same text can be reused across scenarios that -// produce the same comment (e.g. both unauthorized scenarios). - -const COMMENT_UNAUTHORIZED = `👋 Hi @maintainer! The \`/finalize\` command is reserved for maintainers and contributors with **triage** (or higher) repository permissions. - -If you believe you should have access, please reach out to a maintainer.`; - -const COMMENT_PERMISSION_ERROR = `👋 Hi @maintainer! I encountered an error while trying to verify your permissions. - -@utkarsh232005 — could you please verify @maintainer's permissions and complete the finalization manually if appropriate? - -Sorry for the inconvenience!`; - -const COMMENT_UPDATE_FAILURE = `⚠️ Hi @maintainer! I encountered an error while trying to update the issue title or body. - -@utkarsh232005 — could you please complete the finalization manually? - -Error details: Simulated issue update failure`; - -const COMMENT_SWAP_FAILURE_REMOVE = `⚠️ The issue was updated successfully, but I encountered an error swapping the status labels. - -@utkarsh232005 — please manually: -- Remove the \`status: awaiting triage\` label -- Add the \`status: ready for dev\` label - -Error details: Failed to remove 'status: awaiting triage': Simulated remove label failure`; - -const COMMENT_SWAP_FAILURE_ADD = `⚠️ The issue was updated successfully, but I encountered an error swapping the status labels. - -@utkarsh232005 — please manually: -- Remove the \`status: awaiting triage\` label -- Add the \`status: ready for dev\` label - -Error details: Failed to add 'status: ready for dev': Simulated add label failure`; - -const COMMENT_SUCCESS_GFI_LOW = `✅ Issue finalized by @maintainer! - -**Skill level:** \`Good First Issue\` -**Priority:** \`priority: low\` - -The issue body has been updated with the appropriate skill-level context and contribution guide. This issue is now ready for contributors to pick up via \`/assign\`.`; - -const COMMENT_SUCCESS_BEGINNER_MEDIUM = `✅ Issue finalized by @maintainer! - -**Skill level:** \`Beginner\` -**Priority:** \`priority: medium\` - -The issue body has been updated with the appropriate skill-level context and contribution guide. This issue is now ready for contributors to pick up via \`/assign\`.`; - -const COMMENT_SUCCESS_INTERMEDIATE_HIGH = `✅ Issue finalized by @maintainer! - -**Skill level:** \`Intermediate\` -**Priority:** \`priority: high\` - -The issue body has been updated with the appropriate skill-level context and contribution guide. This issue is now ready for contributors to pick up via \`/assign\`.`; - -const COMMENT_SUCCESS_ADVANCED_MEDIUM = `✅ Issue finalized by @maintainer! - -**Skill level:** \`Advanced\` -**Priority:** \`priority: medium\` - -The issue body has been updated with the appropriate skill-level context and contribution guide. This issue is now ready for contributors to pick up via \`/assign\`.`; - -/** Builds the standard validation-error comment for one or more violations. */ -function validationComment(...errors) { - const errorList = errors.map((e) => `- ${e}`).join('\n'); - return `👋 Hi @maintainer! The issue isn't quite ready to finalize yet. Please fix the following labeling issue(s) and then comment \`/finalize\` again:\n\n${errorList}\n\nIf you have questions about which labels to apply, see the maintainer documentation or ask in the team channel.`; -} - -// Pre-built validation error strings matching the exact output of collectLabelViolations. -const ERR_MISSING_TRIAGE = `The \`status: awaiting triage\` label must be present to run \`/finalize\`. Current status label(s): \`status: ready for dev\`.`; -const ERR_NO_SKILL = `Exactly one \`skill:\` label is required (e.g. \`skill: beginner\`). None found. Choose from: \`skill: good first issue\`, \`skill: beginner\`, \`skill: intermediate\`, \`skill: advanced\`.`; -const ERR_MULTIPLE_SKILLS = `Exactly one \`skill:\` label is required. Found 2: \`skill: beginner\`, \`skill: intermediate\`. Please remove all but one.`; -const ERR_NO_PRIORITY = `Exactly one \`priority:\` label is required (e.g. \`priority: medium\`). None found.`; - -const ERR_UNKNOWN_TYPE = `The issue type (Bug, Feature, or Task) could not be determined. Ensure the issue was submitted using one of the official issue templates.`; - -// ============================================================================= -// SCENARIOS -// ============================================================================= - -const scenarios = [ - // --------------------------------------------------------------------------- - // AUTHORIZATION - // --------------------------------------------------------------------------- - - { - name: 'Unauthorized — read-only collaborator', - description: 'A collaborator with "read" role is rejected', - context: makeContext(makeIssue()), - githubOptions: { roleName: 'read' }, - expectedComments: [COMMENT_UNAUTHORIZED], - assertions: [ - assert.reactionAdded(), - assert.noIssueUpdate(), - assert.noLabelsAdded(), - ], - }, - - { - name: 'Unauthorized — non-collaborator (404)', - description: 'A user who is not a repo collaborator is rejected', - context: makeContext(makeIssue()), - githubOptions: { permissionNotFound: true }, - expectedComments: [COMMENT_UNAUTHORIZED], - assertions: [ - assert.reactionAdded(), - assert.noIssueUpdate(), - ], - }, - - { - name: 'Permission check API error', - description: 'When the permission API fails, posts an error comment and tags maintainers', - context: makeContext(makeIssue()), - githubOptions: { permissionShouldFail: true }, - expectedComments: [COMMENT_PERMISSION_ERROR], - assertions: [ - assert.reactionAdded(), - assert.noIssueUpdate(), - ], - }, - - // --------------------------------------------------------------------------- - // LABEL VALIDATION - // --------------------------------------------------------------------------- - - { - name: 'Validation — missing status: awaiting triage', - description: 'Issue has a different status label — should fail validation', - context: makeContext(makeIssue({ - labels: [ - { name: LABELS.READY_FOR_DEV }, - { name: LABELS.BEGINNER }, - { name: 'priority: medium' }, - ], - type: { name: 'Task' }, - })), - githubOptions: { roleName: 'triage' }, - expectedComments: [validationComment(ERR_MISSING_TRIAGE)], - assertions: [ - assert.noIssueUpdate(), - assert.noLabelsAdded(), - ], - }, - - { - name: 'Validation — no skill label', - description: 'Issue is missing a skill: label', - context: makeContext(makeIssue({ - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: 'priority: medium' }, - ], - type: { name: 'Task' }, - })), - githubOptions: { roleName: 'triage' }, - expectedComments: [validationComment(ERR_NO_SKILL)], - assertions: [ - assert.noIssueUpdate(), - ], - }, - - { - name: 'Validation — multiple skill labels', - description: 'Issue has two skill: labels — exactly one is required', - context: makeContext(makeIssue({ - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: LABELS.BEGINNER }, - { name: LABELS.INTERMEDIATE }, - { name: 'priority: medium' }, - ], - type: { name: 'Task' }, - })), - githubOptions: { roleName: 'triage' }, - expectedComments: [validationComment(ERR_MULTIPLE_SKILLS)], - assertions: [ - assert.noIssueUpdate(), - ], - }, - - { - name: 'Validation — no priority label', - description: 'Issue is missing a priority: label', - context: makeContext(makeIssue({ - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: LABELS.BEGINNER }, - ], - type: { name: 'Task' }, - })), - githubOptions: { roleName: 'triage' }, - expectedComments: [validationComment(ERR_NO_PRIORITY)], - assertions: [ - assert.noIssueUpdate(), - ], - }, - - { - name: 'Validation — Multiple violations listed in one comment', - description: 'All violations (no skill, no priority) reported together', - context: makeContext(makeIssue({ - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - ], - type: { name: 'Feature' }, - })), - githubOptions: { roleName: 'admin' }, - expectedComments: [validationComment(ERR_NO_SKILL, ERR_NO_PRIORITY)], - assertions: [ - assert.noIssueUpdate(), - ], - }, - - { - name: 'Validation — Unknown issue type', - description: 'Issue created without a recognized type triggers a validation error', - context: makeContext(makeIssue({ type: null })), - githubOptions: { roleName: 'triage' }, - expectedComments: [validationComment(ERR_UNKNOWN_TYPE)], - assertions: [ - assert.noIssueUpdate(), - ], - }, - - // --------------------------------------------------------------------------- - // HAPPY PATHS - // --------------------------------------------------------------------------- - - { - name: 'Happy Path — Good First Issue (Feature)', - description: 'Valid GFI feature issue is finalized successfully', - context: makeContext(makeIssue({ - title: 'Add batch query support', - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: LABELS.GOOD_FIRST_ISSUE }, - { name: 'priority: low' }, - ], - type: { name: 'Feature' }, - })), - githubOptions: { roleName: 'triage' }, - expectedComments: [COMMENT_SUCCESS_GFI_LOW], - assertions: [ - assert.reactionAdded(), - assert.issueUpdated(), - assert.titleContains('[Good First Issue]:'), - assert.bodyContains('First-Time Friendly'), - assert.bodyContains('About Good First Issues'), - assert.bodyContains('Step-by-Step Contribution Guide'), - assert.labelAdded(LABELS.READY_FOR_DEV), - assert.labelRemoved(LABELS.AWAITING_TRIAGE), - ], - }, - - { - name: 'Happy Path — Beginner Task', - description: 'Valid beginner task is finalized; title prefix added, body reconstructed', - context: makeContext(makeIssue()), - githubOptions: { roleName: 'triage' }, - expectedComments: [COMMENT_SUCCESS_BEGINNER_MEDIUM], - assertions: [ - assert.reactionAdded(), - assert.issueUpdated(), - assert.titleContains('[Beginner]: Fix something'), - assert.bodyContains('Beginner Friendly'), - assert.bodyContains('About Beginner Issues'), - assert.bodyContains('Step-by-Step Contribution Guide'), - assert.labelAdded(LABELS.READY_FOR_DEV), - assert.labelRemoved(LABELS.AWAITING_TRIAGE), - ], - }, - - { - name: 'Happy Path — Intermediate Bug', - description: 'Valid intermediate bug report is finalized', - context: makeContext(makeIssue({ - title: 'Client fee cap silently ignored', - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: LABELS.INTERMEDIATE }, - { name: 'priority: high' }, - ], - type: { name: 'Bug' }, - })), - githubOptions: { roleName: 'write' }, - expectedComments: [COMMENT_SUCCESS_INTERMEDIATE_HIGH], - assertions: [ - assert.issueUpdated(), - assert.titleContains('[Intermediate]:'), - assert.bodyContains('Intermediate Friendly'), - assert.bodyContains('About Intermediate Issues'), - assert.bodyContains('Step-by-Step Contribution Guide'), - assert.labelAdded(LABELS.READY_FOR_DEV), - ], - }, - - { - name: 'Happy Path — Advanced Task', - description: 'Valid advanced task is finalized', - context: makeContext(makeIssue({ - title: 'Improve issue triage workflow', - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: LABELS.ADVANCED }, - { name: 'priority: medium' }, - { name: 'scope: ci' }, - ], - type: { name: 'Task' }, - })), - githubOptions: { roleName: 'admin' }, - expectedComments: [COMMENT_SUCCESS_ADVANCED_MEDIUM], - assertions: [ - assert.issueUpdated(), - assert.titleContains('[Advanced]:'), - assert.bodyContains('🧠 Advanced'), - assert.bodyContains('About Advanced Issues'), - assert.bodyContains('Step-by-Step Contribution Guide'), - assert.labelAdded(LABELS.READY_FOR_DEV), - ], - }, - - { - name: 'Happy Path — existing prefix is replaced', - description: 'An issue that was already finalized once gets the correct prefix when re-finalized', - context: makeContext(makeIssue({ - title: '[Beginner]: Fix something', - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: LABELS.ADVANCED }, - { name: 'priority: medium' }, - ], - type: { name: 'Task' }, - })), - githubOptions: { roleName: 'maintain' }, - expectedComments: [COMMENT_SUCCESS_ADVANCED_MEDIUM], - assertions: [ - assert.issueUpdated(), - assert.titleContains('[Advanced]: Fix something'), - ], - }, - - // --------------------------------------------------------------------------- - // API FAILURE PATHS - // --------------------------------------------------------------------------- - - { - name: 'API failure — issue update fails', - description: 'When issues.update throws, a failure comment is posted and labels are NOT swapped', - context: makeContext(makeIssue()), - githubOptions: { roleName: 'triage', updateShouldFail: true }, - expectedComments: [COMMENT_UPDATE_FAILURE], - assertions: [ - assert.noLabelsAdded(), - assert.noLabelsRemoved(), - ], - }, - - { - name: 'API failure — remove label fails after successful update', - description: 'When removeLabel throws, the swap failure comment and success comment are both posted', - context: makeContext(makeIssue()), - githubOptions: { roleName: 'triage', removeLabelShouldFail: true }, - expectedComments: [COMMENT_SWAP_FAILURE_REMOVE, COMMENT_SUCCESS_BEGINNER_MEDIUM], - assertions: [ - assert.issueUpdated(), - assert.labelAdded(LABELS.READY_FOR_DEV), // add still runs and succeeds - assert.noLabelsRemoved(), // remove failed - ], - }, - - { - name: 'API failure — add label fails after successful update and remove', - description: 'When addLabels throws, the swap failure comment and success comment are both posted', - context: makeContext(makeIssue()), - githubOptions: { roleName: 'triage', addLabelShouldFail: true }, - expectedComments: [COMMENT_SWAP_FAILURE_ADD, COMMENT_SUCCESS_BEGINNER_MEDIUM], - assertions: [ - assert.issueUpdated(), - assert.labelRemoved(LABELS.AWAITING_TRIAGE), // remove succeeded - assert.noLabelsAdded(), // add failed - ], - }, - - { - name: 'Body reconstruction — user-provided Additional Information is preserved', - description: 'When the Additional Information section has real user content it should not be replaced with the default Discord link text', - context: makeContext(makeIssue({ - body: [ - '### 👾 Description of the Issue\n\nSome bug.\n\n', - '### ✔️ Acceptance Criteria\n\n- [ ] Fixed\n\n', - '### 🤔 Additional Information\n\nSee the internal bug tracker for full repro steps.', - ].join(''), - labels: [ - { name: LABELS.AWAITING_TRIAGE }, - { name: LABELS.BEGINNER }, - { name: 'priority: medium' }, - ], - type: { name: 'Task' }, - })), - githubOptions: { roleName: 'triage' }, - expectedComments: [COMMENT_SUCCESS_BEGINNER_MEDIUM], - assertions: [ - assert.issueUpdated(), - assert.bodyContains('See the internal bug tracker for full repro steps.'), - assert.bodyNotContains('If you have questions while working on this issue'), - ], - }, -]; - -// ============================================================================= -// UNIT TESTS — pure functions from finalize-comments.cjs -// ============================================================================= - -async function runUnitTests() { - let total = 0; - let passed = 0; - let failed = 0; - - function check(name, actual, expected) { - total++; - const ok = JSON.stringify(actual) === JSON.stringify(expected); - if (ok) { - passed++; - console.log(` ✅ ${name}`); - } else { - failed++; - console.log(` ❌ ${name}`); - console.log(` expected: ${JSON.stringify(expected)}`); - console.log(` actual: ${JSON.stringify(actual)}`); - } - } - - console.log('\n📐 UNIT TESTS — parseSections / isMeaningfulContent'); - console.log('─'.repeat(60)); - - // parseSections - check('parseSections: null → []', parseSections(null), []); - check('parseSections: empty string → []', parseSections(''), []); - check('parseSections: no headers → single null-header entry', - parseSections('just some text'), - [{ header: null, content: 'just some text' }] - ); - check('parseSections: single section', - parseSections('### My Header\n\nsome content'), - [{ header: 'My Header', content: 'some content' }] - ); - check('parseSections: two sections', - parseSections('### First\n\ncontent one\n\n### Second\n\ncontent two'), - [ - { header: 'First', content: 'content one' }, - { header: 'Second', content: 'content two' }, - ] - ); - check('parseSections: section with no content', - parseSections('### Header\n'), - [{ header: 'Header', content: '' }] - ); - check('parseSections: leading content then a header', - parseSections('preamble\n### Section\n\nbody'), - [ - { header: null, content: 'preamble' }, - { header: 'Section', content: 'body' }, - ] - ); - - // isMeaningfulContent - check('isMeaningfulContent: null → false', isMeaningfulContent(null), false); - check('isMeaningfulContent: empty string → false', isMeaningfulContent(''), false); - check('isMeaningfulContent: whitespace only → false', isMeaningfulContent(' '), false); - check('isMeaningfulContent: "Optional." → false', isMeaningfulContent('Optional.'), false); - check('isMeaningfulContent: "_No response_" → false', isMeaningfulContent('_No response_'), false); - check('isMeaningfulContent: real content → true', isMeaningfulContent('Some real text here.'), true); - check('isMeaningfulContent: whitespace-padded real content → true', isMeaningfulContent(' Real content '), true); - - return { total, passed, failed }; -} - -// ============================================================================= -// RUNNER -// ============================================================================= - -runTestSuite('FINALIZE COMMAND TEST SUITE', scenarios, runScenario, [ - { label: 'Unit Tests', run: runUnitTests }, -]); diff --git a/.github/scripts/tests/test-inactivity-bot.cjs b/.github/scripts/tests/test-inactivity-bot.cjs deleted file mode 100644 index 3442d46e..00000000 --- a/.github/scripts/tests/test-inactivity-bot.cjs +++ /dev/null @@ -1,1275 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-inactivity-bot.cjs -// -// Integration tests for bot-inactivity.cjs. -// Run with: node .github/scripts/tests/test-inactivity-bot.cjs - -const { runTestSuite } = require('./test-utils'); -const script = require('../bot-inactivity.cjs'); -const { LABELS } = require('../helpers/constants'); - -// ============================================================================= -// TIME HELPERS -// ============================================================================= - -// Fixed "now" for all tests -const NOW = new Date('2024-01-15T12:00:00Z').getTime(); - -function daysAgo(n) { - return new Date(NOW - n * 24 * 60 * 60 * 1000).toISOString(); -} - -// ============================================================================= -// MOCK GITHUB FACTORY -// ============================================================================= - -/** - * Creates a mock GitHub API for bot-inactivity.cjs tests. - * - * @param {object} opts - * @param {object[]} opts.assignedIssues - Items returned by issues.listForRepo (assignee:*) - * @param {object[]} opts.openPRs - Items returned by pulls.list - * @param {object} opts.commentsByNumber - number -> comment[] (issues.listComments) - * @param {object} opts.commitsByPRNumber - prNumber -> commit[] (pulls.listCommits) - * @param {object} opts.issuesByNumber - number -> issue data (issues.get) - * @param {object} opts.eventsByNumber - number -> event[] (issues.listEvents) - */ -function createMockGithub(opts = {}) { - const { - assignedIssues = [], - openPRs = [], - commentsByNumber = {}, - commitsByPRNumber = {}, - issuesByNumber = {}, - eventsByNumber = {}, - } = opts; - - const calls = { - itemsClosed: [], // issue_numbers that were closed - commentsCreated: [], // { issue_number, body } - commentsUpdated: [], // { comment_id, body } - labelsAdded: [], // { issue_number, labels } - labelsRemoved: [], // { issue_number, name } - assigneesRemoved: [], // { issue_number, assignees } - commentsList: [], // { issue_number } — tracked for verification - }; - - const perPage = 100; - - // All comments keyed by issue/PR number (includes any pre-seeded ones used - // by postOrUpdateComment to detect existing marker comments). - const allComments = { ...commentsByNumber }; - - const mock = { - calls, - rest: { - issues: { - listForRepo: async (params) => { - const page = params.page || 1; - const start = (page - 1) * perPage; - const slice = assignedIssues.slice(start, start + perPage); - return { data: slice }; - }, - - listEvents: async (params) => { - const num = params.issue_number; - const all = eventsByNumber[num] || []; - const page = params.page || 1; - const start = (page - 1) * perPage; - return { data: all.slice(start, start + perPage) }; - }, - - listComments: async (params) => { - const num = params.issue_number; - calls.commentsList.push({ issue_number: num }); - const all = allComments[num] || []; - const page = params.page || 1; - const start = (page - 1) * perPage; - return { data: all.slice(start, start + perPage) }; - }, - - createComment: async (params) => { - calls.commentsCreated.push({ issue_number: params.issue_number, body: params.body }); - console.log(`\n📝 COMMENT CREATED on #${params.issue_number}:\n${'─'.repeat(50)}\n${params.body}\n${'─'.repeat(50)}`); - }, - - updateComment: async (params) => { - calls.commentsUpdated.push({ comment_id: params.comment_id, body: params.body }); - console.log(`\n✏️ COMMENT UPDATED (id=${params.comment_id}):\n${'─'.repeat(50)}\n${params.body}\n${'─'.repeat(50)}`); - }, - - update: async (params) => { - if (params.state === 'closed') { - calls.itemsClosed.push(params.issue_number); - console.log(`\n🔒 CLOSED #${params.issue_number}`); - } - }, - - addLabels: async (params) => { - calls.labelsAdded.push({ issue_number: params.issue_number, labels: params.labels }); - console.log(`\n🏷️ LABELS ADDED on #${params.issue_number}: ${params.labels.join(', ')}`); - }, - - removeLabel: async (params) => { - calls.labelsRemoved.push({ issue_number: params.issue_number, name: params.name }); - console.log(`\n🏷️ LABEL REMOVED on #${params.issue_number}: ${params.name}`); - }, - - removeAssignees: async (params) => { - calls.assigneesRemoved.push({ issue_number: params.issue_number, assignees: params.assignees }); - console.log(`\n👤 ASSIGNEES REMOVED on #${params.issue_number}: ${params.assignees.join(', ')}`); - }, - - get: async (params) => { - const data = issuesByNumber[params.issue_number] || { - number: params.issue_number, - state: 'open', - assignees: [], - labels: [], - created_at: daysAgo(1), - }; - return { data }; - }, - }, - - pulls: { - list: async (params) => { - const page = params.page || 1; - const start = (page - 1) * perPage; - const slice = openPRs.slice(start, start + perPage); - return { data: slice }; - }, - - listCommits: async (params) => { - const all = commitsByPRNumber[params.pull_number] || []; - const page = params.page || 1; - const start = (page - 1) * perPage; - return { data: all.slice(start, start + perPage) }; - }, - }, - }, - }; - - return mock; -} - -// ============================================================================= -// HELPERS — ITEM BUILDERS -// ============================================================================= - -function makeIssue(number, { createdAt = daysAgo(1), assignees = [], labels = [] } = {}) { - return { - number, - state: 'open', - created_at: createdAt, - assignees: assignees.map(l => ({ login: l })), - labels: labels.map(l => ({ name: l })), - pull_request: undefined, - }; -} - -function makePR(number, { createdAt = daysAgo(1), assignees = [], labels = [], body = '', authorLogin = 'contributor' } = {}) { - return { - number, - state: 'open', - created_at: createdAt, - user: { login: authorLogin, type: 'User' }, - assignees: assignees.map(l => ({ login: l })), - labels: labels.map(l => ({ name: l })), - body, - }; -} - -function makeUnlabeledEvent(labelName, createdAt) { - return { event: 'unlabeled', created_at: createdAt, label: { name: labelName } }; -} - -function makeLabeledEvent(labelName, createdAt) { - return { event: 'labeled', created_at: createdAt, label: { name: labelName } }; -} - -function makeAssignedEvent(createdAt) { - return { event: 'assigned', created_at: createdAt }; -} - -function makeComment(userLogin, createdAt, { isBot = false, body = null } = {}) { - return { - id: Math.floor(Math.random() * 100000), - user: { login: userLogin, type: isBot ? 'Bot' : 'User' }, - body: body || `Comment from ${userLogin}`, - created_at: createdAt, - }; -} - -function makeCommit(authorLogin, date) { - return { - author: { login: authorLogin }, - commit: { - author: { date }, - committer: { date }, - message: 'chore: some work', - verification: { verified: true }, - }, - }; -} - -const defaultContext = { - repo: { owner: 'test-org', repo: 'test-repo' }, -}; - -// ============================================================================= -// SCENARIOS -// ============================================================================= - -const scenarios = [ - // ── 1 ────────────────────────────────────────────────────────────────────── - { - name: 'No in-progress items — no action taken', - description: 'When there are no assigned issues or PRs, the bot should be silent.', - github: createMockGithub(), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 2 ────────────────────────────────────────────────────────────────────── - { - name: 'Issue: 3 days inactive — no action', - description: 'An issue created 3 days ago with no comments should not be flagged.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(10, { createdAt: daysAgo(3), assignees: ['alice'], labels: [LABELS.IN_PROGRESS] }), - ], - eventsByNumber: { - 10: [makeAssignedEvent(daysAgo(3))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - summaryLogs: ['#10 (issue): last activity 3d ago (assigned: alice), no action needed'], - }, - }, - - // ── 3 ────────────────────────────────────────────────────────────────────── - { - name: 'Issue: 6 days inactive — warning posted', - description: 'An issue with no activity for 6 days should receive a warning comment.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(20, { createdAt: daysAgo(6), assignees: ['alice'], labels: [LABELS.IN_PROGRESS] }), - ], - eventsByNumber: { - 20: [makeAssignedEvent(daysAgo(6))], - }, - }), - expect: { - itemsClosed: [], - commentsCreatedCount: 1, - warningPostedOn: [20], - labelsAdded: 0, - assigneesRemoved: 0, - summaryLogs: ['#20 (issue): last activity 6d ago (assigned: alice), posting inactivity warning'], - }, - }, - - // ── 4 ────────────────────────────────────────────────────────────────────── - { - name: 'Issue: 8 days inactive — reset (not closed)', - description: 'An issue with no activity for 8 days should be reset but remain open.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(30, { createdAt: daysAgo(8), assignees: ['alice'], labels: [LABELS.IN_PROGRESS] }), - ], - eventsByNumber: { - 30: [makeAssignedEvent(daysAgo(8))], - }, - }), - expect: { - itemsClosed: [], - resetCommentOn: [30], - labelsAdded: [{ issue_number: 30, labels: [LABELS.READY_FOR_DEV] }], - labelsRemoved: [{ issue_number: 30, name: LABELS.IN_PROGRESS }], - assigneesRemoved: [{ issue_number: 30, assignees: ['alice'] }], - summaryLogs: ['#30 (issue): last activity 8d ago (assigned: alice), unassigning and resetting issue'], - }, - }, - - // ── 5 ────────────────────────────────────────────────────────────────────── - { - name: 'Issue: 8 days old but assignee commented 2 days ago — no action', - description: 'An assignee comment resets the inactivity clock.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(40, { createdAt: daysAgo(8), assignees: ['bob'], labels: [LABELS.IN_PROGRESS] }), - ], - commentsByNumber: { - 40: [makeComment('bob', daysAgo(2))], - }, - eventsByNumber: { - 40: [makeAssignedEvent(daysAgo(8))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 6 ────────────────────────────────────────────────────────────────────── - { - name: 'Issue: 8 days old, non-assignee commented — clock not reset, reset (not closed)', - description: 'A comment by a non-assignee (e.g. maintainer) should not reset the clock.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(50, { createdAt: daysAgo(8), assignees: ['alice'], labels: [LABELS.IN_PROGRESS] }), - ], - commentsByNumber: { - 50: [makeComment('maintainer', daysAgo(1))], - }, - eventsByNumber: { - 50: [makeAssignedEvent(daysAgo(8))], - }, - }), - expect: { - itemsClosed: [], - resetCommentOn: [50], - assigneesRemoved: [{ issue_number: 50, assignees: ['alice'] }], - }, - }, - - // ── 7 ────────────────────────────────────────────────────────────────────── - { - name: 'Issue: blocked label — skipped for inactivity, receives check-in', - description: 'Blocked issues are exempt from close/warn, but get a 30-day check-in comment.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(60, { - createdAt: daysAgo(8), - assignees: ['alice'], - labels: [LABELS.IN_PROGRESS, LABELS.BLOCKED], - }), - ], - }), - expect: { - itemsClosed: [], - checkinPostedOn: [60], - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 8 ────────────────────────────────────────────────────────────────────── - { - name: 'Issue: 8 days inactive but linked open PR has recent author commit — no action', - description: 'Activity on a linked PR (author commit 1 day ago) should protect the issue.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(70, { createdAt: daysAgo(8), assignees: ['carol'], labels: [LABELS.IN_PROGRESS] }), - ], - openPRs: [ - makePR(71, { - createdAt: daysAgo(8), - assignees: ['carol'], - authorLogin: 'carol', - body: 'Fixes #70', - }), - ], - commitsByPRNumber: { - 71: [makeCommit('carol', daysAgo(1))], - }, - eventsByNumber: { - 70: [makeAssignedEvent(daysAgo(8))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 9 ────────────────────────────────────────────────────────────────────── - { - name: 'PR: 6 days inactive — warning posted', - description: 'An assigned PR with no activity for 6 days should receive a warning.', - github: createMockGithub({ - openPRs: [ - makePR(80, { createdAt: daysAgo(6), assignees: ['dave'], authorLogin: 'dave' }), - ], - }), - expect: { - itemsClosed: [], - commentsCreatedCount: 1, - warningPostedOn: [80], - labelsAdded: 0, - assigneesRemoved: 0, - summaryLogs: ['#80 (PR): last activity 6d ago (assigned: dave), posting inactivity warning'], - }, - }, - - // ── 10 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: 8 days inactive — closed, reset, linked issue cleaned up', - description: 'A stale PR should be closed and its linked issue unassigned and reset.', - github: createMockGithub({ - openPRs: [ - makePR(90, { - createdAt: daysAgo(8), - assignees: ['eve'], - authorLogin: 'eve', - body: 'Fixes #91', - }), - ], - issuesByNumber: { - 91: { - number: 91, - state: 'open', - assignees: [{ login: 'eve' }], - labels: [{ name: LABELS.IN_PROGRESS }], - created_at: daysAgo(8), - }, - }, - }), - expect: { - itemsClosed: [90], - closureCommentOn: [90], - linkedIssueCleaned: [91], - assigneesRemovedOn: [90, 91], - summaryLogs: ['#90 (PR): last activity 8d ago (assigned: eve), closing PR'], - }, - }, - - // ── 11 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: blocked label — skipped for inactivity, receives check-in', - description: 'Blocked PRs are exempt from close/warn, but get a 30-day check-in comment.', - github: createMockGithub({ - openPRs: [ - makePR(100, { - createdAt: daysAgo(8), - assignees: ['frank'], - authorLogin: 'frank', - labels: [LABELS.BLOCKED], - }), - ], - }), - expect: { - itemsClosed: [], - checkinPostedOn: [100], - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 12 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: 8 days old but author committed 1 day ago — no action', - description: 'A recent commit by the PR author resets the inactivity clock.', - github: createMockGithub({ - openPRs: [ - makePR(110, { createdAt: daysAgo(8), assignees: ['grace'], authorLogin: 'grace' }), - ], - commitsByPRNumber: { - 110: [makeCommit('grace', daysAgo(1))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 13 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: 8 days old, commit by different author — clock not reset, closed', - description: 'A commit by someone other than the PR author must not reset the clock.', - github: createMockGithub({ - openPRs: [ - makePR(120, { createdAt: daysAgo(8), assignees: ['henry'], authorLogin: 'henry' }), - ], - commitsByPRNumber: { - // Commit is by a maintainer, not the PR author - 120: [makeCommit('maintainer', daysAgo(1))], - }, - }), - expect: { - itemsClosed: [120], - closureCommentOn: [120], - assigneesRemoved: [{ issue_number: 120, assignees: ['henry'] }], - }, - }, - - // ── 14 ───────────────────────────────────────────────────────────────────── - { - name: 'Unassigned PR — not tracked for inactivity', - description: 'Open PRs without assignees should not be processed.', - github: createMockGithub({ - openPRs: [ - makePR(130, { createdAt: daysAgo(8), assignees: [], authorLogin: 'ivan' }), - ], - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 15 ───────────────────────────────────────────────────────────────────── - { - name: 'Issue: unblocked 3 days ago (was 8 days old) — no action', - description: 'Removing the blocked label resets the 5-day clock.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(140, { createdAt: daysAgo(8), assignees: ['judy'], labels: [LABELS.IN_PROGRESS] }), - ], - eventsByNumber: { - 140: [makeAssignedEvent(daysAgo(8)), makeUnlabeledEvent(LABELS.BLOCKED, daysAgo(3))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 16 ───────────────────────────────────────────────────────────────────── - { - name: 'Issue: unblocked 8 days ago — reset (not closed)', - description: 'If the unblocked date is still more than 7 days ago, the issue should be reset but remain open.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(150, { createdAt: daysAgo(10), assignees: ['kate'], labels: [LABELS.IN_PROGRESS] }), - ], - eventsByNumber: { - 150: [makeAssignedEvent(daysAgo(10)), makeUnlabeledEvent(LABELS.BLOCKED, daysAgo(8))], - }, - }), - expect: { - itemsClosed: [], - resetCommentOn: [150], - assigneesRemoved: [{ issue_number: 150, assignees: ['kate'] }], - }, - }, - - // ── 17 ───────────────────────────────────────────────────────────────────── - { - name: 'Issue: blocked, no prior check-in — check-in comment posted', - description: 'First time the bot sees a blocked item, it should post a check-in.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(160, { - createdAt: daysAgo(35), - assignees: ['liam'], - labels: [LABELS.IN_PROGRESS, LABELS.BLOCKED], - }), - ], - }), - expect: { - itemsClosed: [], - checkinPostedOn: [160], - assigneesRemoved: 0, - }, - }, - - // ── 18 ───────────────────────────────────────────────────────────────────── - { - name: 'Issue: blocked, check-in posted 35 days ago — new check-in posted', - description: 'After 30 days the check-in comment should be refreshed.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(170, { - createdAt: daysAgo(40), - assignees: ['mia'], - labels: [LABELS.IN_PROGRESS, LABELS.BLOCKED], - }), - ], - commentsByNumber: { - 170: [{ - id: 9001, - user: { login: 'github-actions[bot]', type: 'Bot' }, - body: '\n👋 Hey @mia, just checking in!...', - created_at: daysAgo(35), - updated_at: daysAgo(35), - }], - }, - }), - expect: { - itemsClosed: [], - checkinPostedOn: [170], - assigneesRemoved: 0, - }, - }, - - // ── 19 ───────────────────────────────────────────────────────────────────── - { - name: 'Issue: blocked, check-in posted 10 days ago — no action', - description: 'If a check-in was posted within 30 days, the bot should stay quiet.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(180, { - createdAt: daysAgo(40), - assignees: ['noah'], - labels: [LABELS.IN_PROGRESS, LABELS.BLOCKED], - }), - ], - commentsByNumber: { - 180: [{ - id: 9002, - user: { login: 'github-actions[bot]', type: 'Bot' }, - body: '\n👋 Hey @noah, just checking in!...', - created_at: daysAgo(10), - updated_at: daysAgo(10), - }], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - commentsUpdated: 0, - assigneesRemoved: 0, - }, - }, - - // ── 20 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: blocked, no prior check-in — check-in posted', - description: 'Blocked PRs also receive the 30-day check-in.', - github: createMockGithub({ - openPRs: [ - makePR(190, { - createdAt: daysAgo(35), - assignees: ['olivia'], - authorLogin: 'olivia', - labels: [LABELS.BLOCKED], - }), - ], - }), - expect: { - itemsClosed: [], - checkinPostedOn: [190], - assigneesRemoved: 0, - }, - }, - - // ── 21 ───────────────────────────────────────────────────────────────────── - { - name: 'Issue: created 30 days ago but assigned 2 days ago — no action', - description: 'The inactivity clock starts from the assignment date, not creation date.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(200, { createdAt: daysAgo(30), assignees: ['pat'], labels: [LABELS.IN_PROGRESS] }), - ], - eventsByNumber: { - 200: [makeAssignedEvent(daysAgo(2))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 22 ───────────────────────────────────────────────────────────────────── - { - name: 'Issue: no assigned event — skipped without error', - description: 'If the events API returns no assigned event, the issue is skipped entirely.', - github: createMockGithub({ - assignedIssues: [ - makeIssue(210, { createdAt: daysAgo(10), assignees: ['quinn'], labels: [LABELS.IN_PROGRESS] }), - ], - eventsByNumber: { - 210: [], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 23 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: status: needs review — skipped entirely', - description: 'PRs waiting on maintainer review are exempt from inactivity tracking.', - github: createMockGithub({ - openPRs: [ - makePR(220, { - createdAt: daysAgo(8), - assignees: ['rose'], - authorLogin: 'rose', - labels: [LABELS.NEEDS_REVIEW], - }), - ], - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 24 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: status: needs revision labeled 2 days ago — no action', - description: 'Inactivity clock starts from when needs-revision was last applied; 2 days is under threshold.', - github: createMockGithub({ - openPRs: [ - makePR(230, { - createdAt: daysAgo(10), - assignees: ['sam'], - authorLogin: 'sam', - labels: [LABELS.NEEDS_REVISION], - }), - ], - eventsByNumber: { - 230: [makeLabeledEvent(LABELS.NEEDS_REVISION, daysAgo(2))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 25 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: status: needs revision labeled 6 days ago — warning posted', - description: 'Six days since needs-revision was applied triggers the 5-day warning.', - github: createMockGithub({ - openPRs: [ - makePR(240, { - createdAt: daysAgo(10), - assignees: ['taylor'], - authorLogin: 'taylor', - labels: [LABELS.NEEDS_REVISION], - }), - ], - eventsByNumber: { - 240: [makeLabeledEvent(LABELS.NEEDS_REVISION, daysAgo(6))], - }, - }), - expect: { - itemsClosed: [], - commentsCreatedCount: 1, - warningPostedOn: [240], - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 26 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: status: needs revision labeled 8 days ago — closed and reset', - description: 'Eight days since needs-revision was applied exceeds the 7-day close threshold.', - github: createMockGithub({ - openPRs: [ - makePR(250, { - createdAt: daysAgo(10), - assignees: ['uri'], - authorLogin: 'uri', - labels: [LABELS.NEEDS_REVISION], - }), - ], - eventsByNumber: { - 250: [makeLabeledEvent(LABELS.NEEDS_REVISION, daysAgo(8))], - }, - }), - expect: { - itemsClosed: [250], - closureCommentOn: [250], - assigneesRemoved: [{ issue_number: 250, assignees: ['uri'] }], - }, - }, - - // ── 27 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: status: needs revision labeled 8 days ago, author commented 2 days ago — no action', - description: 'Author activity after the label is applied resets the clock; the bot should not close an actively-engaged PR.', - github: createMockGithub({ - openPRs: [ - makePR(261, { - createdAt: daysAgo(10), - assignees: ['wren'], - authorLogin: 'wren', - labels: [LABELS.NEEDS_REVISION], - }), - ], - commentsByNumber: { - 261: [makeComment('wren', daysAgo(2))], - }, - eventsByNumber: { - 261: [makeLabeledEvent(LABELS.NEEDS_REVISION, daysAgo(8))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 28 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: status: needs revision applied twice — clock uses most recent application', - description: 'Back-and-forth review cycles must not penalize contributors; the clock resets on each new needs-revision application.', - github: createMockGithub({ - openPRs: [ - makePR(260, { - createdAt: daysAgo(12), - assignees: ['vera'], - authorLogin: 'vera', - labels: [LABELS.NEEDS_REVISION], - }), - ], - eventsByNumber: { - 260: [ - makeLabeledEvent(LABELS.NEEDS_REVISION, daysAgo(10)), - makeUnlabeledEvent(LABELS.NEEDS_REVISION, daysAgo(8)), - makeLabeledEvent(LABELS.NEEDS_REVIEW, daysAgo(8)), - makeUnlabeledEvent(LABELS.NEEDS_REVIEW, daysAgo(2)), - makeLabeledEvent(LABELS.NEEDS_REVISION, daysAgo(2)), - ], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 29 ───────────────────────────────────────────────────────────────────── - { - name: 'PR + issue both stale — issue not double-commented after PR loop resets it', - description: 'When the PR loop resets a linked issue, the issues loop should skip it — no duplicate comment.', - github: createMockGithub({ - openPRs: [ - makePR(90, { - createdAt: daysAgo(8), - assignees: ['eve'], - authorLogin: 'eve', - body: 'Fixes #91', - }), - ], - assignedIssues: [ - makeIssue(91, { - createdAt: daysAgo(8), - assignees: ['eve'], - labels: [LABELS.IN_PROGRESS], - }), - ], - issuesByNumber: { - 91: { - number: 91, - state: 'open', - assignees: [{ login: 'eve' }], - labels: [{ name: LABELS.IN_PROGRESS }], - created_at: daysAgo(8), - }, - }, - }), - expect: { - itemsClosed: [90], - closureCommentOn: [90], - linkedIssueCleaned: [91], - assigneesRemovedOn: [90, 91], - commentsCreatedCount: 2, - }, - }, - - // ── 30 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: comment on linked issue — no action (not stale)', - description: 'A participant comment on a linked issue should reset PR inactivity.', - github: createMockGithub({ - openPRs: [ - makePR(500, { - createdAt: daysAgo(8), - assignees: ['alice'], - authorLogin: 'alice', - body: 'Fixes #501', - }), - ], - assignedIssues: [ - makeIssue(501, { - createdAt: daysAgo(8), - assignees: ['alice'], - labels: [LABELS.IN_PROGRESS], - }), - ], - commentsByNumber: { - 501: [makeComment('alice', daysAgo(1), { body: '/working' })], - }, - eventsByNumber: { - 501: [makeAssignedEvent(daysAgo(8))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 31 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: regular comment on linked issue — no action (not stale)', - description: 'Any participant comment on linked issue should reset the clock.', - github: createMockGithub({ - openPRs: [ - makePR(510, { - createdAt: daysAgo(8), - assignees: ['bob'], - authorLogin: 'bob', - body: 'Fixes #511', - }), - ], - assignedIssues: [ - makeIssue(511, { - createdAt: daysAgo(8), - assignees: ['bob'], - labels: [LABELS.IN_PROGRESS], - }), - ], - commentsByNumber: { - 511: [makeComment('bob', daysAgo(1), { body: 'Still working on this, will update soon!' })], - }, - eventsByNumber: { - 511: [makeAssignedEvent(daysAgo(8))], - }, - }), - expect: { - itemsClosed: [], - commentsCreated: 0, - labelsAdded: 0, - assigneesRemoved: 0, - }, - }, - - // ── 32 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: no linked issues — stale behavior unchanged', - description: 'A PR with no linked issues should behave exactly as before.', - github: createMockGithub({ - openPRs: [ - makePR(520, { - createdAt: daysAgo(8), - assignees: ['charlie'], - authorLogin: 'charlie', - body: 'Add new feature', - }), - ], - }), - expect: { - itemsClosed: [520], - closureCommentOn: [520], - assigneesRemoved: [{ issue_number: 520, assignees: ['charlie'] }], - }, - }, - - // ── 33 ───────────────────────────────────────────────────────────────────── - { - name: 'PR: non-participant comment on linked issue — still stale, closed', - description: 'A comment from someone who is not the PR author/assignee must not reset inactivity.', - github: createMockGithub({ - openPRs: [ - makePR(530, { - createdAt: daysAgo(8), - assignees: ['diana'], - authorLogin: 'diana', - body: 'Fixes #531', - }), - ], - assignedIssues: [ - makeIssue(531, { - createdAt: daysAgo(8), - assignees: ['diana'], - labels: [LABELS.IN_PROGRESS], - }), - ], - commentsByNumber: { - 531: [makeComment('outsider', daysAgo(1), { body: 'Any update here?' })], - }, - eventsByNumber: { - 531: [makeAssignedEvent(daysAgo(8))], - }, - }), - expect: { - itemsClosed: [530], - closureCommentOn: [530], - assigneesRemoved: [{ issue_number: 530, assignees: ['diana'] }], - }, - }, - - // ── 34 ───────────────────────────────────────────────────────────────────── - { - name: 'PR closed for inactivity should not get ready-for-dev and should remove status labels', - description: 'Closed PR should have all status labels removed and not receive ready-for-dev label', - github: createMockGithub({ - openPRs: [ - makePR(30, { - createdAt: daysAgo(10), - assignees: ['alice'], - labels: [LABELS.IN_PROGRESS, LABELS.NEEDS_REVISION] - }) - ], - eventsByNumber: { - 30: [makeAssignedEvent(daysAgo(10))] - } - }), - expect: { - itemsClosed: [30], - commentsCreated: 1, - labelsAdded: 0, - labelsRemoved: [ - { issue_number: 30, name: LABELS.IN_PROGRESS }, - { issue_number: 30, name: LABELS.NEEDS_REVISION } - ], - assigneesRemoved: 1, - summaryLogs: ['#30 (PR): last activity 10d ago (assigned: alice), closing PR'], - }, - }, -]; - -// ============================================================================= -// TEST RUNNER -// ============================================================================= - -async function runScenario(scenario, index) { - const { name, description, github, expect: expected } = scenario; - - console.log(`\n${'─'.repeat(70)}`); - console.log(`[${index}] ${name}`); - if (description) console.log(` ${description}`); - - const capturedLogs = []; - const originalConsoleLog = console.log; - const originalConsoleError = console.error; - const redactSensitiveText = (text) => text - .replace(/((?:password|passwd|token|api[_-]?key|secret)\s*[:=]\s*)([^,\s}]+)/gi, '$1[REDACTED]') - .replace(/("(?:password|passwd|token|api[_-]?key|secret)"\s*:\s*")([^"]+)(")/gi, '$1[REDACTED]$3'); - const sanitizeArgsForOutput = (args) => - args.map(a => redactSensitiveText(String(a))).join(' '); - console.log = (...args) => { - capturedLogs.push(args.map(a => String(a)).join(' ')); - originalConsoleLog(sanitizeArgsForOutput(args)); - }; - console.error = (...args) => { - capturedLogs.push(args.map(a => String(a)).join(' ')); - originalConsoleError(sanitizeArgsForOutput(args)); - }; - - try { - await script({ github, context: defaultContext, getNow: () => NOW }); - } catch (err) { - console.log = originalConsoleLog; - console.error = originalConsoleError; - console.error(`❌ Script threw: ${err.message}`); - console.error(err.stack); - return false; - } - console.log = originalConsoleLog; - console.error = originalConsoleError; - - const { calls } = github; - const failures = []; - - // itemsClosed - if (expected.itemsClosed !== undefined) { - const closed = calls.itemsClosed; - if (JSON.stringify(closed.sort()) !== JSON.stringify(expected.itemsClosed.sort())) { - failures.push(`itemsClosed: expected ${JSON.stringify(expected.itemsClosed)}, got ${JSON.stringify(closed)}`); - } - } - - // commentsCreated count - if (expected.commentsCreated !== undefined) { - const count = calls.commentsCreated.length + calls.commentsUpdated.length; - if (count !== expected.commentsCreated) { - failures.push(`comments posted: expected ${expected.commentsCreated}, got ${count}`); - } - } - - if (expected.commentsCreatedCount !== undefined) { - const count = calls.commentsCreated.length; - if (count !== expected.commentsCreatedCount) { - failures.push(`commentsCreated count: expected ${expected.commentsCreatedCount}, got ${count}`); - } - } - - // warningPostedOn — check that warning marker appears in comments for these items - if (expected.warningPostedOn) { - for (const num of expected.warningPostedOn) { - const MARKER = ''; - const found = calls.commentsCreated.some(c => c.issue_number === num && c.body.startsWith(MARKER)) - || calls.commentsUpdated.some(c => c.body.startsWith(MARKER)); - if (!found) { - failures.push(`Expected warning comment on #${num}`); - } - } - } - - // closureCommentOn — check that PR closure comment appears for these items - if (expected.closureCommentOn) { - for (const num of expected.closureCommentOn) { - const found = calls.commentsCreated.some(c => c.issue_number === num && c.body.includes('closed due to')); - if (!found) { - failures.push(`Expected closure comment on #${num}`); - } - } - } - - // resetCommentOn — check that issue reset comment appears for these items (not closed) - if (expected.resetCommentOn) { - for (const num of expected.resetCommentOn) { - const found = calls.commentsCreated.some(c => c.issue_number === num && c.body.includes('unassigned and reset')); - if (!found) { - failures.push(`Expected reset comment on #${num}`); - } - } - } - - // labelsAdded - if (expected.labelsAdded !== undefined) { - if (typeof expected.labelsAdded === 'number') { - if (calls.labelsAdded.length !== expected.labelsAdded) { - failures.push(`labelsAdded count: expected ${expected.labelsAdded}, got ${calls.labelsAdded.length}`); - } - } else if (Array.isArray(expected.labelsAdded)) { - for (const exp of expected.labelsAdded) { - const found = calls.labelsAdded.some( - a => a.issue_number === exp.issue_number && - JSON.stringify(a.labels) === JSON.stringify(exp.labels) - ); - if (!found) { - failures.push(`Expected labels ${JSON.stringify(exp.labels)} added on #${exp.issue_number}`); - } - } - } - } - - // labelsRemoved - if (expected.labelsRemoved !== undefined) { - if (typeof expected.labelsRemoved === 'number') { - if (calls.labelsRemoved.length !== expected.labelsRemoved) { - failures.push(`labelsRemoved count: expected ${expected.labelsRemoved}, got ${calls.labelsRemoved.length}`); - } - } else if (Array.isArray(expected.labelsRemoved)) { - for (const exp of expected.labelsRemoved) { - const found = calls.labelsRemoved.some( - r => r.issue_number === exp.issue_number && r.name === exp.name - ); - if (!found) { - failures.push(`Expected label "${exp.name}" removed on #${exp.issue_number}`); - } - } - } - } - - // assigneesRemoved - if (expected.assigneesRemoved !== undefined) { - if (typeof expected.assigneesRemoved === 'number') { - if (calls.assigneesRemoved.length !== expected.assigneesRemoved) { - failures.push(`assigneesRemoved count: expected ${expected.assigneesRemoved}, got ${calls.assigneesRemoved.length}`); - } - } else if (Array.isArray(expected.assigneesRemoved)) { - for (const exp of expected.assigneesRemoved) { - const found = calls.assigneesRemoved.some( - r => r.issue_number === exp.issue_number && - JSON.stringify(r.assignees.sort()) === JSON.stringify(exp.assignees.sort()) - ); - if (!found) { - failures.push(`Expected assignees ${JSON.stringify(exp.assignees)} removed on #${exp.issue_number}`); - } - } - } - } - - // assigneesRemovedOn — just check items (not specific logins) - if (expected.assigneesRemovedOn) { - for (const num of expected.assigneesRemovedOn) { - const found = calls.assigneesRemoved.some(r => r.issue_number === num); - if (!found) { - failures.push(`Expected assignees removed on #${num}`); - } - } - } - - // linkedIssueCleaned — issue was reset (assignees removed AND ready-for-dev label added) - if (expected.linkedIssueCleaned) { - for (const num of expected.linkedIssueCleaned) { - const unassigned = calls.assigneesRemoved.some(r => r.issue_number === num); - const relabeled = calls.labelsAdded.some(r => r.issue_number === num && r.labels.includes(LABELS.READY_FOR_DEV)); - if (!unassigned) failures.push(`Expected assignees removed on linked issue #${num}`); - if (!relabeled) failures.push(`Expected "${LABELS.READY_FOR_DEV}" added on linked issue #${num}`); - } - } - - // checkinPostedOn — blocked check-in marker appears in created or updated comments - if (expected.checkinPostedOn) { - const CHECKIN_MARKER = ''; - for (const num of expected.checkinPostedOn) { - const found = calls.commentsCreated.some(c => c.issue_number === num && c.body.startsWith(CHECKIN_MARKER)) - || calls.commentsUpdated.some(c => c.body.startsWith(CHECKIN_MARKER)); - if (!found) { - failures.push(`Expected blocked check-in comment on #${num}`); - } - } - } - - // commentsUpdated count - if (expected.commentsUpdated !== undefined) { - if (calls.commentsUpdated.length !== expected.commentsUpdated) { - failures.push(`commentsUpdated count: expected ${expected.commentsUpdated}, got ${calls.commentsUpdated.length}`); - } - } - - if (expected.summaryLogs) { - for (const expectedLine of expected.summaryLogs) { - const found = capturedLogs.some(line => line.includes(expectedLine)); - if (!found) { - failures.push(`Expected summary log line: ${expectedLine}`); - } - } - } - - if (failures.length > 0) { - for (const f of failures) console.error(` ❌ ${f}`); - return false; - } - - console.log(' ✅ All assertions passed'); - return true; -} - -// ============================================================================= -// ENTRY POINT -// ============================================================================= - -runTestSuite('INACTIVITY BOT TEST SUITE', scenarios, runScenario); diff --git a/.github/scripts/tests/test-on-pr-merged-bot.cjs b/.github/scripts/tests/test-on-pr-merged-bot.cjs deleted file mode 100644 index c76e8838..00000000 --- a/.github/scripts/tests/test-on-pr-merged-bot.cjs +++ /dev/null @@ -1,413 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-on-pr-merged-bot.cjs -// -// Integration tests for the bot-on-pr-merged.cjs script. -// Verifies that when a PR is merged, sibling conflicts are properly evaluated -// and their components (dashboard comments and labels) updated seamlessly. - -const { runTestSuite, createMockGithub } = require('./test-utils'); -const onPrMergedBot = require('../bot-on-pr-merged'); -const { MARKER } = require('../helpers/comments'); - -// ============================================================================= -// SCENARIOS -// ============================================================================= - -const scenarios = [ - { - name: 'Merged PR triggers conflict check on siblings: one clean, one with new conflict', - run: async () => { - // Mock GitHub: - // - PR 10: Just merged - // - PR 20: Sibling PR, clean - // - PR 30: Sibling PR, now dirty (shows conflict) - - const pullsState = { - 20: { mergeable: true, mergeable_state: 'clean' }, - 30: { mergeable: false, mergeable_state: 'dirty' } - }; - - const mockGithub = createMockGithub({ - existingComments: [ - // PR 20 currently clean in its comment - { id: 201, body: `${MARKER}\nAll good` } - // PR 30 currently clean in its comment, but API says dirty! - ], - }); - - // Override pulls.list to return PR 20 and 30 - mockGithub.rest.pulls.list = async () => ({ - data: [ - { number: 20, draft: false, user: { login: 'contributor-20' } }, - { number: 30, draft: false, user: { login: 'contributor-30' } }, - { number: 40, draft: true } // skipped - ] - }); - - // Override pulls.get to return mergeable state per PR - mockGithub.rest.pulls.get = async ({ pull_number }) => { - const state = pullsState[pull_number]; - return { data: { mergeable: state.mergeable, mergeable_state: state.mergeable_state } }; - }; - - const context = { - eventName: 'pull_request_target', - repo: { owner: 'test', repo: 'repo' }, - payload: { - action: 'closed', - pull_request: { - number: 10, - merged: true, - user: { login: 'contributor' } - } - } - }; - - // Also need to stub listComments to return comments per PR - mockGithub.rest.issues.listComments = async ({ issue_number }) => { - if (issue_number === 20) { - return { data: [{ id: 201, body: `${MARKER}\nNo conflicts previously` }] }; - } - if (issue_number === 30) { - return { data: [{ id: 301, body: `${MARKER}\nNo conflicts previously` }] }; - } - return { data: [] }; - }; - - await onPrMergedBot({ github: mockGithub, context }); - - // So commentsUpdated should have length 1, for PR 30 - // And commentsCreated should have length 1 (the notification for PR 30) - if (mockGithub.calls.commentsCreated.length !== 1) { - console.log('Expected 1 notification comment created, got:', mockGithub.calls.commentsCreated.length); - return false; - } - - // Verify the notification mentions the PR author and merged PR number - const notification = mockGithub.calls.commentsCreated[0]; - if (!notification.includes('@contributor-30') || !notification.includes('#10')) { - console.log('Notification should tag PR author and reference merged PR. Got:', notification); - return false; - } - - if (mockGithub.calls.commentsUpdated.length !== 1) { - console.log('Expected 1 comment updated, got:', mockGithub.calls.commentsUpdated.length); - return false; - } - - const update = mockGithub.calls.commentsUpdated[0]; - if (update.comment_id !== 301) { - console.log('Expected PR 30 comment (301) to be updated, got:', update.comment_id); - return false; - } - - if (!update.body.includes(':x: **Merge Conflicts**')) { - console.log('Update body should contain Merge Conflicts error'); - return false; - } - - return true; - } - }, - { - name: 'Merged PR triggers conflict check: PR already showed conflict and still has it (no churn)', - run: async () => { - const mockGithub = createMockGithub(); - - mockGithub.rest.pulls.list = async () => ({ - data: [{ number: 50, draft: false }] - }); - - mockGithub.rest.pulls.get = async () => ({ - data: { mergeable: false, mergeable_state: 'dirty' } - }); - - mockGithub.rest.issues.listComments = async () => { - return { data: [{ id: 501, body: `${MARKER}\n:x: **Merge Conflicts**` }] }; - }; - - const context = { - eventName: 'pull_request', - repo: { owner: 'test', repo: 'repo' }, - payload: { pull_request: { number: 10, merged: true, user: { login: 'contributor' } } } - }; - - await onPrMergedBot({ github: mockGithub, context }); - - // No creates or updates expected because status didn't change - if (mockGithub.calls.commentsUpdated.length !== 0 || mockGithub.calls.commentsCreated.length !== 0) { - console.log('Expected no comments updated/created due to no churn'); - return false; - } - - return true; - } - }, - { - name: 'Zero open non-draft sibling PRs -> exits cleanly', - run: async () => { - const mockGithub = createMockGithub(); - - mockGithub.rest.pulls.list = async () => ({ - data: [{ number: 10, draft: false }, { number: 40, draft: true }] - }); - - const context = { - eventName: 'pull_request_target', - repo: { owner: 'test', repo: 'repo' }, - payload: { - pull_request: { number: 10, merged: true, user: { login: 'contributor' } } - } - }; - - await onPrMergedBot({ github: mockGithub, context }); - - if (mockGithub.calls.commentsUpdated.length !== 0 || mockGithub.calls.commentsCreated.length !== 0) { - console.log('Expected no comments updated/created'); - return false; - } - - return true; - } - }, - { - name: 'mergeable returns null across all retries -> treated as no conflict', - run: async () => { - const mockGithub = createMockGithub({ - existingComments: [ - { id: 601, body: `${MARKER}\nAll good` } - ], - }); - - mockGithub.rest.pulls.list = async () => ({ - data: [{ number: 60, draft: false }] - }); - - // API always returns null - mockGithub.rest.pulls.get = async () => ({ - data: { mergeable: null, mergeable_state: 'unknown' } - }); - - mockGithub.rest.issues.listComments = async () => { - return { data: [{ id: 601, body: `${MARKER}\nAll good` }] }; - }; - - const context = { - eventName: 'pull_request', - repo: { owner: 'test', repo: 'repo' }, - payload: { pull_request: { number: 10, merged: true, user: { login: 'contributor' } } } - }; - - await onPrMergedBot({ github: mockGithub, context }); - - if (mockGithub.calls.commentsUpdated.length !== 0 || mockGithub.calls.commentsCreated.length !== 0) { - console.log('Expected no comments updated/created'); - return false; - } - - return true; - } - }, - { - name: 'dirty -> clean: comment is updated to show no conflict', - run: async () => { - const mockGithub = createMockGithub({ - existingComments: [ - { id: 701, body: `${MARKER}\n:x: **Merge Conflicts**` } - ], - }); - - mockGithub.rest.pulls.list = async () => ({ - data: [{ number: 70, draft: false }] - }); - - mockGithub.rest.pulls.get = async () => ({ - data: { mergeable: true, mergeable_state: 'clean' } - }); - - mockGithub.rest.issues.listComments = async () => { - return { data: [{ id: 701, body: `${MARKER}\n:x: **Merge Conflicts**` }] }; - }; - - const context = { - eventName: 'pull_request', - repo: { owner: 'test', repo: 'repo' }, - payload: { pull_request: { number: 10, merged: true, user: { login: 'contributor' } } } - }; - - await onPrMergedBot({ github: mockGithub, context }); - - if (mockGithub.calls.commentsUpdated.length !== 1) { - console.log('Expected 1 comment updated'); - return false; - } - - const update = mockGithub.calls.commentsUpdated[0]; - if (update.body.includes(':x: **Merge Conflicts**')) { - console.log('Update body should NOT contain Merge Conflicts error. Got:', update.body); - return false; - } - - return true; - } - }, - { - name: 'New conflict detected → notification comment posted tagging PR author', - run: async () => { - const mockGithub = createMockGithub(); - - mockGithub.rest.pulls.list = async () => ({ - data: [{ number: 80, draft: false, user: { login: 'alice' } }] - }); - - mockGithub.rest.pulls.get = async () => ({ - data: { mergeable: false, mergeable_state: 'dirty' } - }); - - // Dashboard currently shows clean (no conflict marker) - mockGithub.rest.issues.listComments = async () => { - return { data: [{ id: 801, body: `${MARKER}\nAll good` }] }; - }; - - const context = { - eventName: 'pull_request', - repo: { owner: 'test', repo: 'repo' }, - payload: { pull_request: { number: 10, merged: true, user: { login: 'merger' } } } - }; - - await onPrMergedBot({ github: mockGithub, context }); - - // Dashboard should be updated - if (mockGithub.calls.commentsUpdated.length !== 1) { - console.log('Expected 1 comment updated, got:', mockGithub.calls.commentsUpdated.length); - return false; - } - - // Notification comment should be posted - if (mockGithub.calls.commentsCreated.length !== 1) { - console.log('Expected 1 notification comment, got:', mockGithub.calls.commentsCreated.length); - return false; - } - - const notification = mockGithub.calls.commentsCreated[0]; - if (!notification.includes('@alice')) { - console.log('Notification should tag PR author @alice. Got:', notification); - return false; - } - if (!notification.includes('#10')) { - console.log('Notification should reference merged PR #10. Got:', notification); - return false; - } - - return true; - } - }, - { - name: 'Conflict resolving (dirty → clean) → no notification comment posted', - run: async () => { - const mockGithub = createMockGithub({ - existingComments: [ - { id: 901, body: `${MARKER}\n:x: **Merge Conflicts**` } - ], - }); - - mockGithub.rest.pulls.list = async () => ({ - data: [{ number: 90, draft: false, user: { login: 'bob' } }] - }); - - mockGithub.rest.pulls.get = async () => ({ - data: { mergeable: true, mergeable_state: 'clean' } - }); - - mockGithub.rest.issues.listComments = async () => { - return { data: [{ id: 901, body: `${MARKER}\n:x: **Merge Conflicts**` }] }; - }; - - const context = { - eventName: 'pull_request', - repo: { owner: 'test', repo: 'repo' }, - payload: { pull_request: { number: 10, merged: true, user: { login: 'merger' } } } - }; - - await onPrMergedBot({ github: mockGithub, context }); - - // Dashboard should be updated (conflict resolved) - if (mockGithub.calls.commentsUpdated.length !== 1) { - console.log('Expected 1 comment updated, got:', mockGithub.calls.commentsUpdated.length); - return false; - } - - // NO notification comment should be posted for conflict resolution - if (mockGithub.calls.commentsCreated.length !== 0) { - console.log('Expected 0 notification comments for conflict resolution, got:', mockGithub.calls.commentsCreated.length); - return false; - } - - return true; - } - }, - { - name: 'Conflict status unchanged (still conflicted) → no notification comment', - run: async () => { - const mockGithub = createMockGithub(); - - mockGithub.rest.pulls.list = async () => ({ - data: [{ number: 100, draft: false, user: { login: 'charlie' } }] - }); - - mockGithub.rest.pulls.get = async () => ({ - data: { mergeable: false, mergeable_state: 'dirty' } - }); - - // Dashboard already shows conflict - mockGithub.rest.issues.listComments = async () => { - return { data: [{ id: 1001, body: `${MARKER}\n:x: **Merge Conflicts**` }] }; - }; - - const context = { - eventName: 'pull_request', - repo: { owner: 'test', repo: 'repo' }, - payload: { pull_request: { number: 10, merged: true, user: { login: 'merger' } } } - }; - - await onPrMergedBot({ github: mockGithub, context }); - - // No updates or notifications expected - if (mockGithub.calls.commentsUpdated.length !== 0) { - console.log('Expected 0 comment updates, got:', mockGithub.calls.commentsUpdated.length); - return false; - } - if (mockGithub.calls.commentsCreated.length !== 0) { - console.log('Expected 0 notification comments, got:', mockGithub.calls.commentsCreated.length); - return false; - } - - return true; - } - } -]; - -async function runScenario(scenario, index) { - console.log(`\n▶️ Scenario ${index}: ${scenario.name}`); - try { - const passed = await scenario.run(); - if (passed) { - console.log('✅ Passed'); - return true; - } else { - console.log('❌ Failed'); - return false; - } - } catch (err) { - console.log(`❌ Failed with error: ${err.message}`); - console.log(err.stack); - return false; - } -} - -// ============================================================================= -// RUN -// ============================================================================= - -runTestSuite('ON-PR-MERGED BOT TEST SUITE', scenarios, runScenario); diff --git a/.github/scripts/tests/test-recommend-issues-bot.cjs b/.github/scripts/tests/test-recommend-issues-bot.cjs deleted file mode 100644 index 92f73a1f..00000000 --- a/.github/scripts/tests/test-recommend-issues-bot.cjs +++ /dev/null @@ -1,623 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-recommend-issues-bot.cjs -// -// Unit tests for bot/bot-recommend-issues.cjs -// Run with: node .github/scripts/tests/test-recommend-issues-bot.cjs - -const { runTestSuite } = require('./test-utils'); -const { LABELS, SKILL_HIERARCHY, SKILL_PREREQUISITES, MAINTAINER_TEAM, PRIORITY_HIERARCHY } = require('../helpers/constants'); -const { - handleRecommendIssues, - getRecommendedIssues, - resolveEligibleLevel, - detectUnlockedLevel, -} = require('../bot/bot-recommend-issues'); - -// ============================================================================= -// MOCK FACTORY -// ============================================================================= - -function createMockBotContext(overrides = {}) { - const calls = { comments: [] }; - - // searchItems: null → simulate API failure for issue search - // searchItems: [] → API succeeds, no results - // searchItems: [...] → API succeeds with items - const searchItems = overrides.searchItems !== undefined ? overrides.searchItems : []; - - // closedCounts controls how many issues exist for each (label, user). - // null simulates API failure for all calls. - const closedCounts = overrides.closedCounts !== undefined ? overrides.closedCounts : {}; - - return { - botContext: { - github: { - rest: { - issues: { - createComment: async (params) => { - calls.comments.push(params.body); - }, - listForRepo: buildListForRepo(closedCounts), - }, - search: { - issuesAndPullRequests: async () => { - if (searchItems === null) throw new Error('Search API error'); - return { data: { items: searchItems } }; - }, - }, - }, - }, - owner: 'test', - repo: 'repo', - number: 99, - sender: overrides.sender !== undefined ? overrides.sender : { login: 'contributor' }, - issue: overrides.issue !== undefined ? overrides.issue : null, - }, - calls, - }; -} - -/** - - * Builds a mock of `github.rest.issues.listForRepo` used by - * countIssuesByAssignee. - * - * For a given (assignee, label), the mock returns a list of synthetic - * issues whose size is determined by `closedCounts["label:username"]`. - * - * Behavior: - * - `closedCounts === null` → all calls throw (simulates API failure) - * - `closedCounts[key] === null` → specific call throws - * - otherwise → returns an array of matching issues - * - * The returned items mimic real API responses: - * - include labels - * - exclude pull_request field (so they count as issues) - * - * The number of returned items is capped by `per_page`, matching how the real - * function respects pagination limits. - */ -function buildListForRepo(closedCounts) { - return async ({ assignee, labels, per_page }) => { - // Simulate full API outage. - if (closedCounts === null) { - throw new Error('listForRepo API error'); - } - - // Only the closed + label path is modeled, which is what the eligibility - // and unlock logic rely on. - const label = labels ?? null; - const key = label ? `${label}:${assignee}` : null; - - // Allow individual keys to signal failure. - if (key !== null && closedCounts[key] === null) { - throw new Error(`listForRepo API error for key ${key}`); - } - - const count = (key !== null ? closedCounts[key] : 0) ?? 0; - - // Build synthetic issue objects that pass the `!item.pull_request` filter - // and carry the requested label. - const items = Array.from({ length: count }, (_, i) => ({ - number: i + 1, - title: `Issue ${i + 1}`, - html_url: `https://github.com/test/repo/issues/${i + 1}`, - pull_request: undefined, // marks it as a real issue, not a PR - labels: label ? [{ name: label }] : [], - })); - - // The real implementation paginates but the mock returns everything at - // once since counts are small. Mimic the `{ data }` envelope. - return { data: items.slice(0, per_page) }; - }; -} - -/** - * Creates a minimal issue object with the given labels. - * Matches the structure expected by recommendation logic. - */ -function makeIssue(labels, number = 1, createdAt = '2026-03-01T00:00:00Z') { - return { - number, - title: `Issue ${number}`, - html_url: `https://github.com/test/repo/issues/${number}`, - labels: labels.map((name) => ({ name })), - created_at: createdAt, - }; -} - -// Skill levels derived from hierarchy order -const GFI = SKILL_HIERARCHY[0]; // good first issue (or equivalent) -const BEGINNER = SKILL_HIERARCHY[1]; -const MID = SKILL_HIERARCHY[2]; -const TOP = SKILL_HIERARCHY[SKILL_HIERARCHY.length - 1]; - -// Convenience: priority levels -const CRITICAL = PRIORITY_HIERARCHY[0]; -const HIGH = PRIORITY_HIERARCHY[1]; -const MEDIUM = PRIORITY_HIERARCHY[2]; -const LOW = PRIORITY_HIERARCHY[3]; - -// ============================================================================= -// UNIT TESTS -// ============================================================================= - -const unitTests = [ - - // --------------------------------------------------------------------------- - // resolveEligibleLevel - // --------------------------------------------------------------------------- - { - name: 'resolveEligibleLevel: no closed issues → defaults to GFI (floor level)', - test: async () => { - const { botContext } = createMockBotContext({ - closedCounts: {}, - }); - const level = await resolveEligibleLevel(botContext, 'contributor'); - return level === GFI; - }, - }, - { - name: 'resolveEligibleLevel: meets normal prereq for BEGINNER → returns BEGINNER', - test: async () => { - const prereq = SKILL_PREREQUISITES[BEGINNER]; - // Enough closed GFIs to satisfy the normal check for BEGINNER. - const { botContext } = createMockBotContext({ - closedCounts: { - [`${prereq.requiredLabel}:contributor`]: prereq.requiredCount, - }, - }); - const level = await resolveEligibleLevel(botContext, 'contributor'); - return level === BEGINNER; - }, - }, - { - name: 'resolveEligibleLevel: has 1 closed BEGINNER issue → bypass check passes for BEGINNER', - test: async () => { - const { botContext } = createMockBotContext({ - closedCounts: { - [`${BEGINNER}:contributor`]: 1, - }, - }); - const level = await resolveEligibleLevel(botContext, 'contributor'); - return level === BEGINNER; - }, - }, - { - name: 'resolveEligibleLevel: has 1 closed MID issue → bypass check resolves to MID', - test: async () => { - const { botContext } = createMockBotContext({ - closedCounts: { - [`${MID}:contributor`]: 1, - }, - }); - const level = await resolveEligibleLevel(botContext, 'contributor'); - return level === MID; - }, - }, - { - name: 'resolveEligibleLevel: countIssuesByAssignee API failure → degrades to GFI', - test: async () => { - const { botContext } = createMockBotContext({ - closedCounts: null, // signal all API calls fail - }); - const level = await resolveEligibleLevel(botContext, 'contributor'); - return level === GFI; - }, - }, - { - name: 'resolveEligibleLevel: API failure at top level → falls through to next eligible level', - test: async () => { - const midPrereq = SKILL_PREREQUISITES[MID]; - const { botContext } = createMockBotContext({ - closedCounts: { - // Simulate failure for the TOP-level check, pass for MID. - [`${TOP}:contributor`]: null, - [`${midPrereq.requiredLabel}:contributor`]: midPrereq.requiredCount, - }, - }); - const level = await resolveEligibleLevel(botContext, 'contributor'); - return level === MID; - }, - }, - { - name: 'resolveEligibleLevel: meets prereq for MID but not TOP → returns MID', - test: async () => { - const midPrereq = SKILL_PREREQUISITES[MID]; - const { botContext } = createMockBotContext({ - closedCounts: { - [`${midPrereq.requiredLabel}:contributor`]: midPrereq.requiredCount, - }, - }); - const level = await resolveEligibleLevel(botContext, 'contributor'); - return level === MID; - }, - }, - - // --------------------------------------------------------------------------- - // detectUnlockedLevel - // --------------------------------------------------------------------------- - { - name: 'detectUnlockedLevel: count exactly equals threshold → returns next level', - test: async () => { - const prereq = SKILL_PREREQUISITES[BEGINNER]; - const { botContext } = createMockBotContext({ - closedCounts: { - [`${GFI}:contributor`]: prereq.requiredCount, - }, - }); - const unlocked = await detectUnlockedLevel(botContext, 'contributor', GFI); - return unlocked === BEGINNER; - }, - }, - { - name: 'detectUnlockedLevel: count below threshold → returns null', - test: async () => { - const prereq = SKILL_PREREQUISITES[BEGINNER]; - const { botContext } = createMockBotContext({ - closedCounts: { - [`${GFI}:contributor`]: prereq.requiredCount - 1, - }, - }); - const unlocked = await detectUnlockedLevel(botContext, 'contributor', GFI); - return unlocked === null; - }, - }, - { - name: 'detectUnlockedLevel: count above threshold → returns null (already crossed before)', - test: async () => { - const prereq = SKILL_PREREQUISITES[BEGINNER]; - const { botContext } = createMockBotContext({ - closedCounts: { - // The mock returns raw counts, so values greater than requiredCount - // represent cases where the threshold was already exceeded. - [`${GFI}:contributor`]: prereq.requiredCount + 1, - }, - }); - const unlocked = await detectUnlockedLevel(botContext, 'contributor', GFI); - return unlocked === null; - }, - }, - { - name: 'detectUnlockedLevel: currentLevel is TOP → returns null (no higher level)', - test: async () => { - const { botContext } = createMockBotContext(); - const unlocked = await detectUnlockedLevel(botContext, 'contributor', TOP); - return unlocked === null; - }, - }, - { - name: 'detectUnlockedLevel: API failure → returns null', - test: async () => { - const { botContext } = createMockBotContext({ - closedCounts: null, - }); - const unlocked = await detectUnlockedLevel(botContext, 'contributor', GFI); - return unlocked === null; - }, - }, - - // --------------------------------------------------------------------------- - // getRecommendedIssues — returns { issues, unlockedLevel } - // --------------------------------------------------------------------------- - { - name: 'getRecommendedIssues: eligible for BEGINNER, BEGINNER issues exist → returns them', - test: async () => { - const prereq = SKILL_PREREQUISITES[BEGINNER]; - const { botContext } = createMockBotContext({ - closedCounts: { - [`${prereq.requiredLabel}:contributor`]: prereq.requiredCount, - }, - searchItems: [ - makeIssue([BEGINNER, LABELS.READY_FOR_DEV], 1), - makeIssue([MID, LABELS.READY_FOR_DEV], 2), - ], - }); - const result = await getRecommendedIssues(botContext, 'contributor', GFI); - return ( - result !== null && - result.issues.length === 1 && - result.issues[0].number === 1 - ); - }, - }, - { - name: 'getRecommendedIssues: eligible for GFI → only GFI issues returned', - test: async () => { - const { botContext } = createMockBotContext({ - closedCounts: {}, - searchItems: [ - makeIssue([GFI, LABELS.READY_FOR_DEV], 3), - makeIssue([BEGINNER, LABELS.READY_FOR_DEV], 4), - ], - }); - const result = await getRecommendedIssues(botContext, 'contributor', GFI); - return ( - result !== null && - result.issues.length === 1 && - result.issues[0].number === 3 - ); - }, - }, - { - name: 'getRecommendedIssues: no issues at eligible level → returns empty array', - test: async () => { - const { botContext } = createMockBotContext({ - closedCounts: {}, - searchItems: [ - makeIssue([BEGINNER, LABELS.READY_FOR_DEV], 5), - ], - }); - const result = await getRecommendedIssues(botContext, 'contributor', GFI); - return result !== null && result.issues.length === 0; - }, - }, - { - name: 'getRecommendedIssues: API failure on issue search → posts error comment, returns null', - test: async () => { - const { botContext, calls } = createMockBotContext({ - sender: { login: 'user' }, - issue: makeIssue([BEGINNER]), - closedCounts: {}, - searchItems: null, - }); - const result = await getRecommendedIssues(botContext, 'user', BEGINNER); - const expected = [ - '👋 Hi @user!', - '', - 'I ran into an issue while generating recommendations for you.', - '', - `${MAINTAINER_TEAM} — could you please take a look?`, - '', - 'Sorry for the inconvenience — feel free to explore open issues in the meantime!', - ].join('\n'); - return ( - result === null && - calls.comments.length === 1 && - calls.comments[0] === expected - ); - }, - }, - { - name: 'getRecommendedIssues: caps results at 5', - test: async () => { - const prereq = SKILL_PREREQUISITES[BEGINNER]; - const { botContext } = createMockBotContext({ - closedCounts: { - [`${prereq.requiredLabel}:contributor`]: prereq.requiredCount, - }, - searchItems: Array.from({ length: 10 }, (_, i) => - makeIssue([BEGINNER, LABELS.READY_FOR_DEV], i + 1) - ), - }); - const result = await getRecommendedIssues(botContext, 'contributor', GFI); - return result !== null && result.issues.length <= 5; - }, - }, - { - name: 'getRecommendedIssues: threshold crossed → unlockedLevel is set', - test: async () => { - const prereq = SKILL_PREREQUISITES[BEGINNER]; - const { botContext } = createMockBotContext({ - closedCounts: { - // Exactly meets normal prereq for BEGINNER (resolveEligibleLevel) AND - // exactly equals threshold for detectUnlockedLevel. - [`${GFI}:contributor`]: prereq.requiredCount, - }, - searchItems: [makeIssue([BEGINNER, LABELS.READY_FOR_DEV], 1)], - }); - const result = await getRecommendedIssues(botContext, 'contributor', GFI); - return result !== null && result.unlockedLevel === BEGINNER; - }, - }, - { - name: 'getRecommendedIssues: threshold not crossed → unlockedLevel is null', - test: async () => { - const { botContext } = createMockBotContext({ - closedCounts: {}, - searchItems: [makeIssue([GFI, LABELS.READY_FOR_DEV], 1)], - }); - const result = await getRecommendedIssues(botContext, 'contributor', GFI); - return result !== null && result.unlockedLevel === null; - }, - }, - - // --------------------------------------------------------------------------- - // getRecommendedIssues — Priority and Tiebreaker logic - // --------------------------------------------------------------------------- - { - name: 'getRecommendedIssues: Critical > High priority at same level', - test: async () => { - const { botContext } = createMockBotContext({ - sender: { login: 'user' }, - closedCounts: { [`${MID}:user`]: 1 }, - searchItems: [ - makeIssue([MID, LABELS.READY_FOR_DEV, HIGH], 1), - makeIssue([MID, LABELS.READY_FOR_DEV, CRITICAL], 2), - ], - }); - const result = await getRecommendedIssues(botContext, 'user', BEGINNER); - // Even though Issue 1 was "found" first by search, Issue 2 should be recommended first - return result.issues[0].number === 2 && result.issues[1].number === 1; - }, - }, - { - name: 'getRecommendedIssues: High > Medium priority at same level', - test: async () => { - const { botContext } = createMockBotContext({ - sender: { login: 'user' }, - closedCounts: { [`${MID}:user`]: 1 }, - searchItems: [ - makeIssue([MID, LABELS.READY_FOR_DEV, MEDIUM], 1), - makeIssue([MID, LABELS.READY_FOR_DEV, HIGH], 2), - ], - }); - const result = await getRecommendedIssues(botContext, 'user', BEGINNER); - return result.issues[0].number === 2 && result.issues[1].number === 1; - }, - }, - { - name: 'getRecommendedIssues: Medium > Low priority at same level', - test: async () => { - const { botContext } = createMockBotContext({ - sender: { login: 'user' }, - closedCounts: { [`${MID}:user`]: 1 }, - searchItems: [ - makeIssue([MID, LABELS.READY_FOR_DEV, LOW], 1), - makeIssue([MID, LABELS.READY_FOR_DEV, MEDIUM], 2), - ], - }); - const result = await getRecommendedIssues(botContext, 'user', BEGINNER); - return result.issues[0].number === 2 && result.issues[1].number === 1; - }, - }, - { - name: 'getRecommendedIssues: Unlabeled issues appear after all priority labels', - test: async () => { - const { botContext } = createMockBotContext({ - sender: { login: 'user' }, - closedCounts: { [`${MID}:user`]: 1 }, - searchItems: [ - makeIssue([MID, LABELS.READY_FOR_DEV], 1), // No priority label - makeIssue([MID, LABELS.READY_FOR_DEV, LOW], 2), - ], - }); - const result = await getRecommendedIssues(botContext, 'user', BEGINNER); - return result.issues[0].number === 2 && result.issues[1].number === 1; - }, - }, - { - name: 'getRecommendedIssues: Same priority tiebreaker → older issue first', - test: async () => { - const { botContext } = createMockBotContext({ - sender: { login: 'user' }, - closedCounts: { [`${MID}:user`]: 1 }, - searchItems: [ - makeIssue([MID, LABELS.READY_FOR_DEV, HIGH], 1, '2026-04-01T00:00:00Z'), // Newer - makeIssue([MID, LABELS.READY_FOR_DEV, HIGH], 2, '2026-01-01T00:00:00Z'), // Older - ], - }); - const result = await getRecommendedIssues(botContext, 'user', BEGINNER); - // Issue 2 is older, so it should be the top recommendation - return result.issues[0].number === 2 && result.issues[1].number === 1; - }, - }, - - // --------------------------------------------------------------------------- - // handleRecommendIssues — short-circuit cases - // --------------------------------------------------------------------------- - { - name: 'handleRecommendIssues: missing sender → skips silently', - test: async () => { - const { botContext, calls } = createMockBotContext({ sender: null }); - await handleRecommendIssues(botContext); - return calls.comments.length === 0; - }, - }, - { - name: 'handleRecommendIssues: missing issue → skips silently', - test: async () => { - const { botContext, calls } = createMockBotContext({ issue: null }); - await handleRecommendIssues(botContext); - return calls.comments.length === 0; - }, - }, - { - name: 'handleRecommendIssues: issue has no skill label → skips silently', - test: async () => { - const { botContext, calls } = createMockBotContext({ - issue: makeIssue(['bug', 'enhancement']), - }); - await handleRecommendIssues(botContext); - return calls.comments.length === 0; - }, - }, - { - name: 'handleRecommendIssues: no matching issues at eligible level → no comment', - test: async () => { - const { botContext, calls } = createMockBotContext({ - issue: makeIssue([BEGINNER, LABELS.READY_FOR_DEV]), - closedCounts: {}, - searchItems: [], - }); - await handleRecommendIssues(botContext); - return calls.comments.length === 0; - }, - }, - { - name: 'handleRecommendIssues: valid context, no unlock → posts standard recommendation comment', - test: async () => { - const { botContext, calls } = createMockBotContext({ - sender: { login: 'user' }, - issue: makeIssue([GFI, LABELS.READY_FOR_DEV]), - closedCounts: {}, - searchItems: [makeIssue([GFI, LABELS.READY_FOR_DEV], 1)], - }); - await handleRecommendIssues(botContext); - const expected = [ - '👋 Hi @user! Great work on your recent contribution! 🎉', - '', - 'Here are some issues you might want to explore next:', - '', - '- [Issue 1](https://github.com/test/repo/issues/1)', - '', - 'Happy coding! 🚀', - ].join('\n'); - return calls.comments.length === 1 && calls.comments[0] === expected; - }, - }, - { - name: 'handleRecommendIssues: level unlocked → comment includes congratulatory block', - test: async () => { - const prereq = SKILL_PREREQUISITES[BEGINNER]; - const { botContext, calls } = createMockBotContext({ - sender: { login: 'user' }, - issue: makeIssue([GFI, LABELS.READY_FOR_DEV]), - closedCounts: { - [`${GFI}:user`]: prereq.requiredCount, - }, - searchItems: [makeIssue([BEGINNER, LABELS.READY_FOR_DEV], 2)], - }); - await handleRecommendIssues(botContext); - return ( - calls.comments.length === 1 && - calls.comments[0].includes('Milestone unlocked') && - calls.comments[0].includes(SKILL_PREREQUISITES[BEGINNER].displayName) - ); - }, - }, -]; - -// ============================================================================= -// TEST RUNNER -// ============================================================================= - -async function runUnitTests() { - console.log('🔬 UNIT TESTS (recommend-issues)'); - console.log('='.repeat(70)); - let passed = 0; - let failed = 0; - for (const test of unitTests) { - try { - const result = await Promise.resolve(test.test()); - if (result) { - console.log(`✅ ${test.name}`); - passed++; - } else { - console.log(`❌ ${test.name}`); - failed++; - } - } catch (error) { - console.log(`❌ ${test.name} - Error: ${error.message}`); - failed++; - } - } - console.log('\n' + '-'.repeat(70)); - console.log(`Unit Tests: ${passed} passed, ${failed} failed`); - return { total: unitTests.length, passed, failed }; -} - -runTestSuite('RECOMMEND ISSUES TEST SUITE', [], async () => true, [ - { label: 'Unit Tests', run: runUnitTests }, -]); diff --git a/.github/scripts/tests/test-unassign-bot.cjs b/.github/scripts/tests/test-unassign-bot.cjs deleted file mode 100644 index 27c9c11d..00000000 --- a/.github/scripts/tests/test-unassign-bot.cjs +++ /dev/null @@ -1,529 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-unassign-bot.cjs -// -// Local test script for the /unassign command in bot-on-comment.cjs. -// Run with: node .github/scripts/tests/test-unassign-bot.cjs -// -// This script mocks the GitHub API and runs various test scenarios -// to verify the /unassign command behaves correctly. - -const { LABELS, ISSUE_STATE } = require('../helpers'); -const script = require('../bot-on-comment.cjs'); -const { verifyComments, runTestSuite } = require('./test-utils'); - -// ============================================================================= -// MOCK GITHUB API -// ============================================================================= - -/** - * Creates a mock GitHub API client for testing. - * Tracks all calls made and optionally simulates API failures. - * - * @param {object} options - * @param {boolean} [options.removeAssigneesShouldFail=false] - * @param {boolean} [options.removeLabelShouldFail=false] - * @param {boolean} [options.addLabelShouldFail=false] - * @returns {{ calls: object, rest: object }} - */ -function createMockGithub(options = {}) { - const { - removeAssigneesShouldFail = false, - removeLabelShouldFail = false, - addLabelShouldFail = false, - } = options; - - const calls = { - comments: [], - assigneesRemoved: [], - labelsAdded: [], - labelsRemoved: [], - reactions: [], - }; - - return { - calls, - rest: { - reactions: { - createForIssueComment: async (params) => { - calls.reactions.push({ commentId: params.comment_id, content: params.content }); - console.log(`\n👍 REACTION ADDED: ${params.content}`); - }, - }, - issues: { - createComment: async (params) => { - calls.comments.push(params.body); - console.log('\n📝 COMMENT POSTED:'); - console.log('─'.repeat(60)); - console.log(params.body); - console.log('─'.repeat(60)); - }, - removeAssignees: async (params) => { - if (removeAssigneesShouldFail) throw new Error('Simulated remove assignees failure'); - calls.assigneesRemoved.push(...params.assignees); - console.log(`\n✅ UNASSIGNED: ${params.assignees.join(', ')}`); - }, - addLabels: async (params) => { - if (addLabelShouldFail) throw new Error('Simulated add label failure'); - calls.labelsAdded.push(...params.labels); - console.log(`\n🏷️ LABEL ADDED: ${params.labels.join(', ')}`); - }, - removeLabel: async (params) => { - if (removeLabelShouldFail) throw new Error('Simulated remove label failure'); - calls.labelsRemoved.push(params.name); - console.log(`\n🏷️ LABEL REMOVED: ${params.name}`); - }, - }, - }, - }; -} - -// ============================================================================= -// TEST SCENARIOS -// ============================================================================= - -const scenarios = [ - // --------------------------------------------------------------------------- - // HAPPY PATHS - // --------------------------------------------------------------------------- - { - name: 'Happy Path - Unassign Success', - description: 'Current assignee successfully unassigns themselves; labels swap correctly', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 100, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'active-contributor' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1001, - body: '/unassign', - user: { login: 'active-contributor', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: true, - expectedAssigneeRemoved: 'active-contributor', - expectedLabelRemoved: LABELS.IN_PROGRESS, - expectedLabelAdded: LABELS.READY_FOR_DEV, - expectedComments: [ - `👋 Hi @active-contributor! You have been successfully unassigned from this issue.\n\nThe \`${LABELS.IN_PROGRESS}\` label has been removed, and it is now back to \`${LABELS.READY_FOR_DEV}\` for others to claim. Thanks for letting us know!`, - ], - }, - { - name: 'Happy Path - Case-Insensitive Username', - description: 'Unassign succeeds even when stored login casing differs from commenter casing', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 106, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'Active-Contributor' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1007, - body: '/unassign', - user: { login: 'active-contributor', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: true, - expectedAssigneeRemoved: 'active-contributor', - expectedLabelRemoved: LABELS.IN_PROGRESS, - expectedLabelAdded: LABELS.READY_FOR_DEV, - expectedComments: [ - `👋 Hi @active-contributor! You have been successfully unassigned from this issue.\n\nThe \`${LABELS.IN_PROGRESS}\` label has been removed, and it is now back to \`${LABELS.READY_FOR_DEV}\` for others to claim. Thanks for letting us know!`, - ], - }, - { - name: 'Happy Path - Command Casing and Whitespace', - description: 'Command successfully routes even with trailing spaces and uppercase letters', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 110, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'active-contributor' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1011, - body: ' /UNASSIGN ', - user: { login: 'active-contributor', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: true, - expectedAssigneeRemoved: 'active-contributor', - expectedLabelRemoved: LABELS.IN_PROGRESS, - expectedLabelAdded: LABELS.READY_FOR_DEV, - expectedComments: [ - `👋 Hi @active-contributor! You have been successfully unassigned from this issue.\n\nThe \`${LABELS.IN_PROGRESS}\` label has been removed, and it is now back to \`${LABELS.READY_FOR_DEV}\` for others to claim. Thanks for letting us know!`, - ], - }, - { - name: 'Happy Path - IN_PROGRESS Label Already Missing', - description: 'Unassign succeeds if a maintainer manually removed IN_PROGRESS; READY_FOR_DEV is still added', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 107, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'active-contributor' }], - labels: [], - }, - comment: { - id: 1008, - body: '/unassign', - user: { login: 'active-contributor', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: true, - expectedAssigneeRemoved: 'active-contributor', - expectedLabelRemoved: LABELS.IN_PROGRESS, - expectedLabelAdded: LABELS.READY_FOR_DEV, - expectedComments: [ - `👋 Hi @active-contributor! You have been successfully unassigned from this issue.\n\nThe \`${LABELS.IN_PROGRESS}\` label has been removed, and it is now back to \`${LABELS.READY_FOR_DEV}\` for others to claim. Thanks for letting us know!`, - ], - }, - - // --------------------------------------------------------------------------- - // VALIDATION FAILURES - // --------------------------------------------------------------------------- - { - name: 'Validation - Not Assigned to Requester', - description: 'User tries to unassign an issue assigned to someone else; gets auth error', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 101, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'other-contributor' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1002, - body: '/unassign', - user: { login: 'sneaky-user', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: true, - expectedAssigneeRemoved: null, - expectedLabelRemoved: null, - expectedLabelAdded: null, - expectedComments: [ - `⚠️ Hi @sneaky-user! You cannot unassign this issue because it is currently assigned to @other-contributor.\n\nOnly the current assignee can unassign themselves.`, - ], - }, - { - name: 'Validation - No Assignees on Issue', - description: 'User tries to unassign an issue that has no assignees at all', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 102, - state: ISSUE_STATE.OPEN, - assignees: [], - labels: [{ name: LABELS.READY_FOR_DEV }], - }, - comment: { - id: 1003, - body: '/unassign', - user: { login: 'confused-user', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: true, - expectedAssigneeRemoved: null, - expectedLabelRemoved: null, - expectedLabelAdded: null, - expectedComments: [ - `👋 Hi @confused-user! This issue doesn't currently have any assignees.`, - ], - }, - { - name: 'Validation - Issue Already Closed', - description: 'User tries to unassign from a closed issue; command is rejected', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 103, - state: ISSUE_STATE.CLOSED, - assignees: [{ login: 'active-contributor' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1004, - body: '/unassign', - user: { login: 'active-contributor', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: true, - expectedAssigneeRemoved: null, - expectedLabelRemoved: null, - expectedLabelAdded: null, - expectedComments: [ - `👋 Hi @active-contributor! This issue is already closed, so the \`/unassign\` command cannot be used here.`, - ], - }, - - // --------------------------------------------------------------------------- - // NO ACTION - // --------------------------------------------------------------------------- - { - name: 'No Action - Regular Comment Ignored', - description: 'Non-command comments on assigned issues produce zero API calls', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 109, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'active-contributor' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1010, - body: 'Looking good, will review tomorrow!', - user: { login: 'active-contributor', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: false, - expectedAssigneeRemoved: null, - expectedLabelRemoved: null, - expectedLabelAdded: null, - expectedComments: [], - }, - { - name: 'No Action - Bot Comment Ignored', - description: 'Bot-authored comments are silently ignored to prevent feedback loops', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 108, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'github-actions[bot]' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1009, - body: '/unassign', - user: { login: 'github-actions[bot]', type: 'Bot' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: {}, - expectReaction: false, - expectedAssigneeRemoved: null, - expectedLabelRemoved: null, - expectedLabelAdded: null, - expectedComments: [], - }, - - // --------------------------------------------------------------------------- - // ERROR HANDLING - // --------------------------------------------------------------------------- - { - name: 'Error - removeAssignees API Failure', - description: 'GitHub API throws on removeAssignees; failure comment posted, no label changes made', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 104, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'active-contributor' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1005, - body: '/unassign', - user: { login: 'active-contributor', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: { removeAssigneesShouldFail: true }, - expectReaction: true, - expectedAssigneeRemoved: null, - expectedLabelRemoved: null, - expectedLabelAdded: null, - expectedComments: [ - `⚠️ Hi @active-contributor! I tried to unassign you, but encountered an unexpected error.\n\n@utkarsh232005 — could you please manually unassign @active-contributor?`, - ], - }, - { - name: 'Error - Label Swap API Failure', - description: 'Label swap fails after successful unassignment; error logged, success comment still posted', - context: { - eventName: 'issue_comment', - payload: { - issue: { - number: 105, - state: ISSUE_STATE.OPEN, - assignees: [{ login: 'active-contributor' }], - labels: [{ name: LABELS.IN_PROGRESS }], - }, - comment: { - id: 1006, - body: '/unassign', - user: { login: 'active-contributor', type: 'User' }, - }, - }, - repo: { owner: 'kdm-ledger', repo: 'kdm-cli' }, - }, - githubOptions: { removeLabelShouldFail: true, addLabelShouldFail: true }, - expectReaction: true, - expectedAssigneeRemoved: 'active-contributor', - expectedLabelRemoved: null, - expectedLabelAdded: null, - expectedComments: [ - `👋 Hi @active-contributor! You have been successfully unassigned from this issue.\n\nThe \`${LABELS.IN_PROGRESS}\` label has been removed, and it is now back to \`${LABELS.READY_FOR_DEV}\` for others to claim. Thanks for letting us know!`, - ], - }, -]; - -// ============================================================================= -// TEST RUNNER -// ============================================================================= - -/** - * Runs a single test scenario and returns whether it passed. - * - * @param {object} scenario - The scenario definition. - * @param {number} index - Zero-based scenario index. - * @returns {Promise} - */ -async function runTest(scenario, index) { - console.log('\n' + '='.repeat(70)); - console.log(`TEST ${index + 1}: ${scenario.name}`); - console.log(`Description: ${scenario.description}`); - console.log('='.repeat(70)); - - const mockGithub = createMockGithub(scenario.githubOptions); - - try { - await script({ github: mockGithub, context: scenario.context }); - } catch (error) { - console.log(`\n❌ SCRIPT THREW ERROR: ${error.message}`); - } - - const results = { passed: true, details: [] }; - - // Verify: assignee removed - if (scenario.expectedAssigneeRemoved) { - if (mockGithub.calls.assigneesRemoved.includes(scenario.expectedAssigneeRemoved)) { - results.details.push(`✅ Correctly removed assignee: ${scenario.expectedAssigneeRemoved}`); - } else { - results.passed = false; - results.details.push(`❌ Expected to remove assignee ${scenario.expectedAssigneeRemoved}, got: ${mockGithub.calls.assigneesRemoved.join(', ') || 'none'}`); - } - } else { - if (mockGithub.calls.assigneesRemoved.length === 0) { - results.details.push('✅ Correctly did not remove any assignees'); - } else { - results.passed = false; - results.details.push(`❌ Should not have removed assignees, but removed: ${mockGithub.calls.assigneesRemoved.join(', ')}`); - } - } - - // Verify: label removed - if ('expectedLabelRemoved' in scenario) { - if (scenario.expectedLabelRemoved === null) { - if (mockGithub.calls.labelsRemoved.length === 0) { - results.details.push('✅ Correctly did not remove any labels'); - } else { - results.passed = false; - results.details.push(`❌ Expected NO labels removed, got: ${mockGithub.calls.labelsRemoved.join(', ')}`); - } - } else { - if (mockGithub.calls.labelsRemoved.includes(scenario.expectedLabelRemoved)) { - results.details.push(`✅ Correctly removed label: ${scenario.expectedLabelRemoved}`); - } else { - results.passed = false; - results.details.push(`❌ Expected to remove label "${scenario.expectedLabelRemoved}", got: ${mockGithub.calls.labelsRemoved.join(', ') || 'none'}`); - } - } - } - - // Verify: label added - if ('expectedLabelAdded' in scenario) { - if (scenario.expectedLabelAdded === null) { - if (mockGithub.calls.labelsAdded.length === 0) { - results.details.push('✅ Correctly did not add any labels'); - } else { - results.passed = false; - results.details.push(`❌ Expected NO labels added, got: ${mockGithub.calls.labelsAdded.join(', ')}`); - } - } else { - if (mockGithub.calls.labelsAdded.includes(scenario.expectedLabelAdded)) { - results.details.push(`✅ Correctly added label: ${scenario.expectedLabelAdded}`); - } else { - results.passed = false; - results.details.push(`❌ Expected to add label "${scenario.expectedLabelAdded}", got: ${mockGithub.calls.labelsAdded.join(', ') || 'none'}`); - } - } - } - - // Verify: comments - const commentResult = verifyComments(scenario.expectedComments || [], mockGithub.calls.comments); - if (!commentResult.passed) results.passed = false; - results.details.push(...commentResult.details); - - // Verify: reaction - if (scenario.expectReaction) { - const reacted = mockGithub.calls.reactions.some(r => r.content === '+1'); - if (reacted) { - results.details.push('✅ Correctly added thumbs-up reaction'); - } else { - results.passed = false; - results.details.push('❌ Expected thumbs-up reaction but none was added'); - } - } else { - if (mockGithub.calls.reactions.length === 0) { - results.details.push('✅ Correctly did not add any reactions'); - } else { - results.passed = false; - results.details.push(`❌ Expected NO reactions, but added: ${mockGithub.calls.reactions.map(r => r.content).join(', ')}`); - } - } - - console.log('\n📊 RESULT:'); - results.details.forEach((d) => console.log(` ${d}`)); - - return results.passed; -} - -runTestSuite('BOT-UNASSIGN-ON-COMMENT TEST SUITE', scenarios, runTest); \ No newline at end of file diff --git a/.github/scripts/tests/test-utils.cjs b/.github/scripts/tests/test-utils.cjs deleted file mode 100644 index 55ff752e..00000000 --- a/.github/scripts/tests/test-utils.cjs +++ /dev/null @@ -1,280 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// -// tests/test-utils.cjs -// -// Shared test utilities for bot script test suites. Provides commit helpers, -// mock GitHub factory, comment snapshot verification, result checking, summary -// printing, and CLI argument handling. - -/** - * Compares actual comments against expected snapshots. Returns an array of - * { passed: boolean, detail: string } results and logs mismatches. - * - * @param {string[]} expectedComments - Expected comment bodies. - * @param {string[]} actualComments - Actual comment bodies captured during the test. - * @returns {{ passed: boolean, details: string[] }} - */ -function verifyComments(expectedComments, actualComments) { - const results = { passed: true, details: [] }; - - if (expectedComments.length === 0 && actualComments.length === 0) { - results.details.push('✅ Correctly posted no comments'); - return results; - } - - if (expectedComments.length !== actualComments.length) { - results.passed = false; - results.details.push(`❌ Expected ${expectedComments.length} comment(s), got ${actualComments.length}`); - return results; - } - - for (let i = 0; i < expectedComments.length; i++) { - if (actualComments[i] === expectedComments[i]) { - results.details.push(`✅ Comment ${i + 1} matches snapshot`); - } else { - results.passed = false; - results.details.push(`❌ Comment ${i + 1} does not match snapshot`); - console.log('\n📋 EXPECTED:'); - console.log('─'.repeat(60)); - console.log(expectedComments[i]); - console.log('─'.repeat(60)); - console.log('\n📋 ACTUAL:'); - console.log('─'.repeat(60)); - console.log(actualComments[i]); - console.log('─'.repeat(60)); - } - } - - return results; -} - -/** - * Prints a summary table and exits with the appropriate code. - * - * @param {{ label: string, total: number, passed: number, failed: number }[]} sections - * One entry per test section (e.g. unit tests, integration tests). - */ -function printSummaryAndExit(sections) { - console.log('\n' + '='.repeat(70)); - console.log('📈 SUMMARY'); - console.log('='.repeat(70)); - - let anyFailed = false; - for (const { label, total, passed, failed } of sections) { - if (failed > 0) anyFailed = true; - console.log(` ${label}: ${total} total, ${passed} passed${failed > 0 ? `, ${failed} failed ❌` : ' ✅'}`); - } - - console.log('='.repeat(70)); - process.exit(anyFailed ? 1 : 0); -} - -/** - * Parses the optional test-index CLI argument and either runs a single - * scenario or all scenarios, then prints a summary and exits. - * - * @param {string} suiteName - Display name (e.g. "ON-COMMIT BOT TEST SUITE"). - * @param {object[]} scenarios - Array of scenario objects. - * @param {function(object, number): Promise} runScenario - * Async function that runs one scenario and returns true if it passed. - * @param {{ label: string, run: () => Promise<{ total: number, passed: number, failed: number }> }[]} [extraSections] - * Optional extra sections (e.g. unit tests) to run before scenarios. - */ -async function runTestSuite(suiteName, scenarios, runScenario, extraSections = []) { - const testIndex = process.argv[2]; - - if (testIndex !== undefined) { - const index = parseInt(testIndex, 10); - if (index >= 0 && index < scenarios.length) { - const ok = await runScenario(scenarios[index], index); - printSummaryAndExit([{ label: 'Single Test', total: 1, passed: ok ? 1 : 0, failed: ok ? 0 : 1 }]); - } else { - console.log(`Invalid test index. Available: 0-${scenarios.length - 1}`); - console.log('\nAvailable scenarios:'); - scenarios.forEach((s, i) => console.log(` ${i}: ${s.name}`)); - process.exit(1); - } - return; - } - - console.log(`🧪 ${suiteName}`); - console.log('='.repeat(suiteName.length + 3) + '\n'); - - const summaries = []; - - for (const section of extraSections) { - const result = await section.run(); - summaries.push({ label: section.label, ...result }); - } - - console.log('\n\n🔗 INTEGRATION TESTS'); - console.log('='.repeat(70)); - - let passed = 0; - let failed = 0; - for (let i = 0; i < scenarios.length; i++) { - const ok = await runScenario(scenarios[i], i); - if (ok) passed++; - else failed++; - } - summaries.push({ label: 'Integration Tests', total: scenarios.length, passed, failed }); - - printSummaryAndExit(summaries); -} - -// ============================================================================= -// COMMIT HELPERS -// ============================================================================= - -function commitDCOAndGPG(sha, message) { - return { - sha, - commit: { - message: `${message}\n\nSigned-off-by: Contributor `, - verification: { verified: true }, - }, - }; -} - -function commitDCOFail(sha, message) { - return { - sha, - commit: { - message, - verification: { verified: true }, - }, - }; -} - -function commitGPGFail(sha, message) { - return { - sha, - commit: { - message: `${message}\n\nSigned-off-by: Contributor `, - verification: { verified: false }, - }, - }; -} - -function commitMerge(sha, message) { - return { - sha, - parents: [{}, {}], - commit: { - message, - verification: { verified: true }, - }, - }; -} - -// ============================================================================= -// MOCK GITHUB FACTORY -// ============================================================================= - -/** - * Creates a mock GitHub API object for integration tests. - * Tracks labels, assignees, and comments via the returned calls object. - * - * @param {object} options - * @param {Array} options.commits - Commits for pulls.listCommits - * @param {boolean} options.mergeable - Merge state for pulls.get - * @param {Record} options.issues - issue_number -> issue data - * @param {number[]} options.graphqlClosingIssues - Issue numbers for graphql - * @param {Array<{id: number, body: string}>} options.existingComments - Pre-existing PR comments - * @returns {{ calls: object, rest: object, graphql: function }} - */ -function createMockGithub(options = {}) { - const { - commits = [], - mergeable = true, - issues = {}, - graphqlClosingIssues = [], - existingComments = [], - } = options; - - const calls = { - labelsAdded: [], - labelsRemoved: [], - assignees: [], - commentsCreated: [], - commentsUpdated: [], - }; - - const perPage = 100; - const listComments = async (params) => { - const page = params.page || 1; - const start = (page - 1) * perPage; - const slice = existingComments.slice(start, start + perPage); - return { data: slice }; - }; - - const mock = { - calls, - rest: { - pulls: { - listCommits: async (params) => { - const page = params.page || 1; - const start = (page - 1) * perPage; - const slice = commits.slice(start, start + perPage); - return { data: slice }; - }, - get: async () => ({ - data: { - mergeable, - mergeable_state: mergeable ? 'clean' : 'dirty', - }, - }), - }, - issues: { - listComments: async (params) => listComments(params), - createComment: async (params) => { - calls.commentsCreated.push(params.body); - return {}; - }, - updateComment: async (params) => { - calls.commentsUpdated.push({ comment_id: params.comment_id, body: params.body }); - return {}; - }, - addLabels: async (params) => { - const labels = Array.isArray(params.labels) ? params.labels : [params.labels]; - calls.labelsAdded.push(...labels); - return {}; - }, - removeLabel: async (params) => { - calls.labelsRemoved.push(params.name); - return {}; - }, - addAssignees: async (params) => { - calls.assignees.push(...(params.assignees || [])); - return {}; - }, - get: async (params) => { - const num = params.issue_number; - const issue = issues[num] || { title: 'Issue', assignees: [] }; - return { data: issue }; - }, - }, - }, - graphql: async () => ({ - repository: { - pullRequest: { - closingIssuesReferences: { - nodes: graphqlClosingIssues.map((n) => ({ number: n })), - }, - }, - }, - }), - }; - - return mock; -} - -module.exports = { - verifyComments, - runTestSuite, - commitDCOAndGPG, - commitDCOFail, - commitGPGFail, - commitMerge, - createMockGithub, -}; diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml deleted file mode 100644 index fb586035..00000000 --- a/.github/workflows/changelog.yml +++ /dev/null @@ -1,171 +0,0 @@ -name: Auto Changelog & Versioned Docs - -on: - release: - types: [published] - push: - tags: - - 'v*.*.*' - - '*.*.*' - - 'v*.*.*-*' - - '*.*.*-*' - workflow_dispatch: - inputs: - version: - description: 'Version to generate docs for (e.g., v1.2.5)' - required: true - type: string - -jobs: - generate-changelog: - runs-on: ubuntu-latest - - concurrency: - group: auto-changelog-${{ github.ref_name }} - cancel-in-progress: false - - permissions: - contents: write - - steps: - - name: Checkout Code - uses: actions/checkout@v7 - with: - fetch-depth: 0 - ref: ${{ github.event.repository.default_branch }} - - - name: Determine version range - id: version_range - run: | - VERSION="${{ github.event.inputs.version || github.ref_name }}" - echo "toTag=$VERSION" >> "$GITHUB_OUTPUT" - prev="" - while IFS= read -r tag; do - if [ "$tag" = "$VERSION" ]; then - echo "fromTag=$prev" >> "$GITHUB_OUTPUT" - echo "previous_tag=$prev" >> "$GITHUB_OUTPUT" - break - fi - prev="$tag" - done < <(git tag --sort=-version:refname) - - - name: Build Changelog/Release Notes - id: github_release - uses: mikepenz/release-changelog-builder-action@b0db9d18b53447ef9f940a46c8f31653c4d9f621 # v6.5.0 - with: - fromTag: ${{ steps.version_range.outputs.fromTag }} - toTag: ${{ steps.version_range.outputs.toTag }} - configurationJson: | - { - "categories": [ - { "title": "### 🚀 Features", "labels": ["feat", "feature"] }, - { "title": "### 🐛 Fixes", "labels": ["fix", "bug"] }, - { "title": "### 📚 Documentation", "labels": ["docs", "doc"] }, - { "title": "### ⚡ Improvements", "labels": ["perf", "refactor"] }, - { "title": "### 🔧 Chore & CI", "labels": ["chore", "ci", "build", "test", "style", "revert"] } - ], - "label_extractor": [ - { - "pattern": "^(?i)(feat|feature|fix|docs?|perf|refactor|chore|ci|build|test|style|revert)(?:\\([\\w\\-\\.]+\\))?(!)?:", - "on_property": "title", - "target": "$1" - } - ], - "template": "#{{CHANGELOG}}", - "pr_template": "- {{TITLE}} by @{{AUTHOR}} in #{{NUMBER}}", - "empty_template": "- No changes", - "sort": { - "order": "DESC", - "on_property": "mergedAt" - } - } - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Generate Flat Version File & Update README - env: - CHANGELOG: ${{ steps.github_release.outputs.changelog }} - run: | - VERSION="${{ steps.version_range.outputs.toTag }}" - FROM_TAG="${{ steps.version_range.outputs.fromTag }}" - VERSION_FILE="docs/${VERSION}.md" - - FALLBACK=false - TRIMMED=$(printf '%s' "$CHANGELOG" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//') - if [ -z "$TRIMMED" ] || [ "$TRIMMED" = "- No changes" ]; then - FALLBACK=true - echo "[!] Empty changelog — using git log fallback" - fi - - echo "# KDM CLI Documentation - ${VERSION}" > "$VERSION_FILE" - echo "Published on: $(date +'%Y-%m-%d')" >> "$VERSION_FILE" - echo "" >> "$VERSION_FILE" - - if [ "$FALLBACK" = "true" ]; then - { - echo "### 📦 Commits" - echo "" - if [ -n "$FROM_TAG" ]; then - git log --oneline --no-merges "${FROM_TAG}..${VERSION}" - else - git log --oneline --no-merges "${VERSION}" - fi | grep -vi "auto-scaffold flat documentation" | grep -vi "skip ci" | while IFS= read -r line; do - msg="${line#* }" - echo "- ${msg}" - done - } >> "$VERSION_FILE" - else - printf '%s\n' "$CHANGELOG" >> "$VERSION_FILE" - fi - - TEMP_README=$(mktemp) - - echo "# Documentation (Latest: ${VERSION})" > "$TEMP_README" - echo "" >> "$TEMP_README" - - cat "$VERSION_FILE" >> "$TEMP_README" - - echo "" >> "$TEMP_README" - echo "---" >> "$TEMP_README" - echo "## Version History" >> "$TEMP_README" - echo "" >> "$TEMP_README" - - shopt -s nullglob - - mapfile -t version_files < <(printf '%s\n' docs/*.md | grep -v -E '^docs/(README|what-is-kdm|UPGRADE-v2)\.md$' | sort -V -r) - - for file in "${version_files[@]}"; do - [ -f "$file" ] || continue - V_NAME=$(basename "$file" .md) - echo "* [$V_NAME]($V_NAME.md)" >> "$TEMP_README" - done - - mv "$TEMP_README" docs/README.md - - - name: Commit and Push Changes - run: | - git config --local user.email "github-actions[bot]@users.noreply.github.com" - git config --local user.name "github-actions[bot]" - - git checkout ${{ github.event.repository.default_branch }} - - for i in 1 2 3; do - if git pull origin ${{ github.event.repository.default_branch }} --ff-only; then - break - fi - if [ $i -eq 3 ]; then - echo "ERROR: Failed to fast-forward after 3 attempts" - exit 1 - fi - sleep $((2**i)) - done - - git add docs/ - - if git diff --cached --quiet; then - echo "No documentation changes to commit." - exit 0 - fi - - git commit -m "docs: auto-scaffold flat documentation for ${{ steps.version_range.outputs.toTag }} [skip ci]" - git push origin HEAD:${{ github.event.repository.default_branch }} diff --git a/.gitignore b/.gitignore index 879a285f..7ed5344c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ coverage/ .vscode/ __pycache__/ *.pyc -*.pyo \ No newline at end of file +*.pyo +.pytest_cache/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 74c059eb..3a866c48 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -77,4 +77,4 @@ For a list of valid contribution types, see the [Emoji Key](https://allcontribut ## Coding Style -Please ensure your code is idiomatic and follows the existing TypeScript conventions in the codebase. +Please ensure your code is idiomatic and follows the existing TypeScript conventions in the codebase as well as the [Coding Style Guide](docs/guides/coding-style.md). diff --git a/README.md b/README.md index 285fc1dd..f8c14b0d 100644 --- a/README.md +++ b/README.md @@ -43,20 +43,21 @@ Unlike tools that focus on a single runtime ([k9s] for Kubernetes or [lazydocker | Feature | Description | |---------|-------------| -| **Auto-Detection** | Automatically discovers Docker containers and Kubernetes pods in your environment | -| **Unified Runner View** | Combined table of Docker and Kubernetes workloads in a single command | -| **Health Monitoring** | Color-coded health status with watch mode for continuous polling | -| **Live Dashboard** | Real-time Ink-based TUI that refreshes every 3 seconds | -| **Log Fetching** | Retrieve logs from containers or pods with automatic Docker-to-Kubernetes fallback | -| **Alert Notifications** | Send alerts via Discord webhook or email SMTP on failures | -| **Minikube Support** | Check Minikube profile status and connectivity | -| **Multi-Agent SRE** | Autonomous multi-agent council with local Ollama models, live Ink TUI, and closed-loop remediation (`kdm analyze --multi-agent`) | -| **AI Diagnostics** | Scan cluster workload configuration errors (`kdm analyze`) with AI explanations (`--explain`) | -| **AI Providers** | Multi-backend credentials (`kdm auth`) supporting OpenAI, Anthropic, Gemini, WatsonX, Ollama, etc. | -| **Local Cache** | Cache system (`kdm cache`) to save AI explanation API tokens and reduce network latency | -| **Dynamic Filters** | Customize default active analyzers list (`kdm filters`) | -| **Custom Analyzers** | Run scripts or webhooks (`kdm custom-analyzer`) to analyze CustomResourceDefinitions (e.g. KEDA, Kyverno) | -| **MCP & HTTP Server** | Run KDM as a REST API or Model Context Protocol (MCP) server (`kdm serve`) for agent integrations | +| **Auto-Detection** | Automatically discovers Docker containers, Kubernetes pods, and Minikube status | +| **Interactive AI Copilot** | 4-tab terminal shell (`kdm copilot`) with live `/pods`, `/containers`, `/analyze`, `/investigate`, `/diff`, and `/health` slash commands | +| **Unified Runner View** | Combined table of Docker and Kubernetes workloads (`kdm show runners`) | +| **Health Monitoring** | Color-coded CPU, memory, pod, and container health with continuous watch mode (`kdm health`) | +| **Live Watch Dashboard** | Real-time Ink TUI (`kdm watch`) with log snapshots (`l`), Copilot launch (`t`), AI diagnosis (`a`), and safe restarts (`r`) | +| **Log Fetching** | Searchable, live-streaming logs (`kdm logs`) with Docker-to-Kubernetes fallback | +| **Multi-Agent SRE** | Autonomous Python + Ollama council (`kdm analyze --multi-agent`) backed by a 4-tier Graceful Degradation Ladder | +| **AI Diagnostics** | Scan **22** built-in Kubernetes analyzers plus `KEDA`, `Kyverno`, and `Prometheus` integrations (`kdm analyze`) | +| **Codebase Investigator** | Correlates live cluster symptoms (OOMs, JVM `-Xmx` heap limits, missing `secretKeyRef` keys) with local YAML manifests and code | +| **Safe Remediation** | Declarative Zod-validated K8s/Docker tools guarded by explicit `[y/N]` confirmation (`SafetyPrompt`) and parameterized `execFile` auditing | +| **AI Providers** | Configure **14** AI backends (`kdm auth`) including OpenAI, Anthropic, Google Gemini (`@google/genai`), Vertex, Ollama, Bedrock, Groq, WatsonX, and OCI GenAI | +| **Local Cache** | SHA-256 local explanation cache (`kdm cache`) to save API tokens and reduce latency | +| **Dynamic Filters & CRDs** | Customize active filters (`kdm filters`) or add external script/webhook analyzers (`kdm custom-analyzer`) | +| **MCP & HTTP Server** | Localhost REST API or JSON-RPC 2.0 Model Context Protocol (MCP) stdio server (`kdm serve`) | +| **Alert Notifications** | Send failure alerts via Discord webhook or email SMTP (`kdm config setup`) | --- @@ -146,30 +147,43 @@ kdm health pods -w -i 2 # Watch mode: refresh every 2 seconds Monitor all workloads in real time with an interactive terminal dashboard. ```bash -kdm watch # Opens Ink-based live dashboard (Ctrl+C to exit) +kdm watch # Opens Ink-based live dashboard (t: Copilot, a: AI diagnose, l: logs, r: restart, Ctrl+C: exit) ``` +### Interactive AI Copilot + +Launch a 4-tab terminal AI Copilot (`Monitor`, `Agents`, `Diffs`, `Settings`) wired directly to live cluster tools and codebase investigation. + +```bash +kdm copilot # Opens Copilot shell on tab 1 (Monitor) +kdm copilot checkout-api -t 2 # Pre-scope to a workload and open Agents tab +``` + +Inside Copilot, run live slash commands including `/pods [-n ]`, `/containers`, `/watch`, `/analyze [pod-name]`, `/investigate`, `/diff`, `/health`, `/help`, `/clear`, and `/quit`. + ### Log Fetching -Retrieve the last 100 lines of logs from a container or pod. Docker is tried first; Kubernetes is used as a fallback. +Retrieve and search live container or pod logs. Docker is tried first; Kubernetes is used as a fallback. ```bash -kdm logs # Container name/ID prefix or pod name +kdm logs # Interactive searchable resource picker and log viewer +kdm logs # Open logs directly for container or pod name ``` ### Diagnostics & AI Analysis -Scan your Kubernetes cluster for common workload issues (mismatched replicas, crashing containers, invalid Gateway routes, etc.) and get AI-powered troubleshooting advice in plain English. +Scan your Kubernetes cluster across **22 built-in analyzers** (plus `KEDA`, `Kyverno`, `Prometheus`, and custom analyzers) for workload issues and get AI-powered troubleshooting advice or multi-agent root-cause synthesis. ```bash -kdm analyze # Run cluster diagnostics and output color-coded report -kdm analyze -n default # Analyze resources in the 'default' namespace only -kdm analyze --multi-agent # Run autonomous multi-agent SRE council with local Ollama models -kdm analyze deployment/checkout-api --multi-agent # Target specific workload for multi-agent root-cause triaging -kdm analyze --explain # Run diagnostics and fetch single-prompt AI explanation -kdm analyze --explain --backend ollama # Force specific active AI backend provider -kdm analyze --explain --anonymize # Anonymize resource names in prompt to protect sensitive data -kdm analyze --output json # Output results in structured JSON format +kdm analyze # Interactive AnalyzeDashboard in TTY / report in non-TTY +kdm analyze -n default # Analyze resources in the 'default' namespace only +kdm analyze --multi-agent # Run autonomous multi-agent SRE council with local Ollama models +kdm analyze deployment/checkout-api --multi-agent # Target specific workload for multi-agent root-cause triaging +kdm analyze --multi-agent --council-timeout 180000 # Override Python council timeout (ms) +kdm analyze --explain # Run diagnostics and fetch single-prompt AI explanation +kdm analyze --explain --backend google-gemini # Force specific AI backend provider +kdm analyze --explain --anonymize --export-audit report.md # Redacted incident report export +kdm analyze --output json # Output results in structured JSON format ``` ### Multi-Agent SRE Architecture (`--multi-agent`) @@ -246,8 +260,10 @@ Remediation successful. | Option | Method | Description | |--------|--------|-------------| -| `--multi-agent` | CLI Flag | Enables multi-agent council for `kdm analyze` | -| `--model ` | CLI Flag | Selects local Ollama model (e.g. `llama3.1`, `qwen2.5-coder`) | +| `--multi-agent` | CLI Flag | Enables Multi-Agent Council for `kdm analyze` | +| `--model ` | CLI Flag | Overrides model (e.g. `llama3.1`, `qwen2.5-coder`, `gemini-2.5-flash`) | +| `--council-timeout ` | CLI Flag | Overrides Python council timeout in milliseconds (default `120000`) | +| `--export-audit ` | CLI Flag | Exports redacted Markdown or JSON incident audit report | | `multi_agent: true` | Config Store | Persist opt-in via `kdm config set multi_agent true` | | `KDM_MULTI_AGENT=true` | Environment | Set environment variable for CI/CD or non-interactive automation | @@ -256,21 +272,22 @@ Remediation successful. KDM is engineered for mission-critical reliability. If local Ollama, Python, or cloud backends fail or timeout, KDM automatically falls back down the degradation ladder without throwing unhandled exceptions: 1. **Step 1: Multi-Agent Council** — Full parallel multi-agent triage with local Ollama models. -2. **Step 2: Deterministic Rule Engine** — Evaluates rules in `< 5ms` with zero LLM overhead for canonical failure signatures (`OOMKilled`, `CrashLoopBackOff`, `ImagePullBackOff`, `ProbeFailure`, `Scheduling`). -3. **Step 3: Legacy AI Client** — Single-prompt cloud provider completions (OpenAI, Claude, Azure). +2. **Step 2: Deterministic Rule Engine** — Evaluates rules with zero LLM overhead for canonical failure signatures (`OOMKilled`, `CrashLoopBackOff`, `ImagePullBackOff`, `ProbeFailure`, `Scheduling`). +3. **Step 3: Legacy AI Client** — Single-prompt cloud provider completions (OpenAI, Claude, Gemini, Azure). 4. **Step 4: Raw Kubernetes Summary** — Structured, formatted workload error diagnostics with zero AI required. ### AI Providers (Auth) -Manage AI backend settings and credentials. KDM supports **10+** AI providers including OpenAI, Anthropic, Gemini, Vertex, OCI GenAI, HuggingFace, Groq, WatsonX, and Ollama. +Manage AI backend settings and credentials. KDM supports **14** configured AI backends (`openai`, `ollama`, `anthropic`, `noop`, `customrest`, `azure-openai`, `cohere`, `google-gemini`, `google-vertex`, `amazon-bedrock`, `huggingface`, `groq`, `ibm-watsonx`, `oci-genai`), plus modern Google GenAI (`@google/genai`, aliased as `google-genai` / `gemini`). ```bash -kdm auth add -b openai -m gpt-4o -p # Configure OpenAI gpt-4o with API key -kdm auth add -b ollama -m llama3.1 # Configure local Ollama llama3.1 -kdm auth list # List configured AI backends and settings -kdm auth default ollama # Set default active AI backend -kdm auth remove openai # Remove backend credentials from config -kdm auth update ollama -t 0.2 # Update settings (e.g. set temperature to 0.2) +kdm auth add -b openai -m gpt-4o -p # Configure OpenAI gpt-4o with API key +kdm auth add -b google-gemini -m gemini-2.5-flash -p # Configure Gemini +kdm auth add -b ollama -m llama3.1 # Configure local Ollama llama3.1 +kdm auth list # List configured AI backends and settings +kdm auth default ollama # Set default active AI backend +kdm auth remove openai # Remove backend credentials from config +kdm auth update ollama -t 0.2 # Update settings (e.g. set temperature to 0.2) ``` ### Local Explanation Cache @@ -352,7 +369,12 @@ KDM stores configuration locally using the [`conf`](https://github.com/sindresor | Variable | Description | |----------|-------------| -| `KDM_SMTP_PASSWORD` | SMTP password (overrides stored config for security) | +| `GEMINI_API_KEY`, `GOOGLE_GENAI_API_KEY`, `GOOGLE_API_KEY` | API key lookup order for `GoogleGenAIClient` (`@google/genai`) | +| `KDM_MULTI_AGENT` | Set `true` or `1` to enable Multi-Agent Council mode globally | +| `KDM_COUNCIL_TIMEOUT_MS` | Override Python Multi-Agent Council timeout in milliseconds (default `120000`) | +| `KDM_ALLOW_LOCAL_AGENTS` | Set `1` or `true` in local dev to allow CWD `agents/council.py` resolution | +| `KDM_SMTP_PASSWORD` | SMTP password (must be set at runtime; never stored in config) | +| `NODE_ENV` | Setting `development` enables local CWD agent script resolution | ### Notification Services @@ -394,12 +416,14 @@ KDM automatically checks for failure conditions and sends notifications when con | Technology | Purpose | |------------|---------| -| [TypeScript] + [Node.js] | Runtime and language | +| [TypeScript] + [Node.js] | Runtime and language (`Node >= 20`) | | [Commander.js] | CLI framework and argument parsing | +| [@google/genai] | Modern Google Gemini SDK, thought-preserving sessions, and function calling | +| [Zod] | Runtime schema validation and OpenAPI tool schema conversion | | [dockerode] | Docker daemon API client | | [@kubernetes/client-node] | Kubernetes API client | | [Python 3] + [Ollama] | Multi-agent SRE council and local LLM execution | -| [Ink] + [React] | Interactive terminal UI (watch mode & council dashboard) | +| [Ink] + [React] | Interactive terminal UI (`CopilotShell`, dashboards, and `SafetyPrompt`) | | [chalk] | Terminal string coloring | | [cli-table3] | Table rendering | | [conf] | Persistent configuration | @@ -410,6 +434,8 @@ KDM automatically checks for failure conditions and sends notifications when con [TypeScript]: https://www.typescriptlang.org [Node.js]: https://nodejs.org [Commander.js]: https://github.com/tj/commander.js +[@google/genai]: https://github.com/googleapis/js-genai +[Zod]: https://zod.dev [dockerode]: https://github.com/apocas/dockerode [@kubernetes/client-node]: https://github.com/kubernetes-client/javascript [Python 3]: https://www.python.org @@ -427,12 +453,54 @@ KDM automatically checks for failure conditions and sends notifications when con ## Documentation -For detailed command reference, see: - +- [Command Reference](docs/guides/commands.md) — Complete CLI flags, Copilot slash commands, TUI shortcuts, HTTP/MCP endpoints, and env vars +- [Architecture & Internals](docs/guides/architecture.md) — How KDM works end-to-end (analyzers, Multi-Agent Council, 4-tier ladder, declarative tools, and safety gate) +- [What Is KDM?](docs/guides/what-is-kdm.md) — Introduction, core capabilities, setup, and concepts +- [Coding Style Guide](docs/guides/coding-style.md) — Code health thresholds and style rules +- [Documentation Index & Changelog](docs/README.md) +- [Multi-Agent Architecture Plan](docs/plans/multi-agent-implementation-plan.md) - [Contribution Guide](CONTRIBUTING.md) - [Code of Conduct](CODE_OF_CONDUCT.md) - [Security Policy](SECURITY.md) -- [Changelog](docs/README.md) + +## Repository Structure + +```text +. +├── bin/ # CLI executable entrypoint (kdm) +├── config/ # Default specialist model routing (agents.yaml) +├── src/ +│ ├── commands/ # Commander CLI command definitions +│ ├── analyzers/ # 22 built-in Kubernetes & custom resource analyzers +│ ├── integrations/ # Optional KEDA, Kyverno, and Prometheus analyzers +│ ├── analysis/ # Core analysis pipeline, preflight probes, 4-tier fallback +│ ├── rules/ # Deterministic offline Kubernetes rule engine +│ ├── ai/ # 14 AI backends + GoogleGenAIClient / GeminiSession +│ ├── agent/ # AgentRegistry, AgentTool, CodebaseInvestigator, Python bridge +│ ├── tools/ # Declarative Zod-validated Kubernetes & Docker tools +│ ├── remediation/ # SafetyPrompt [y/N] confirmation gate & parameterized executor +│ ├── server/ # Localhost HTTP API & stdio MCP server +│ ├── ui/ # React Ink TUI dashboards & CopilotShell +│ ├── config/ # Persistent store, schema validation, legacy migration +│ ├── cache/ # Local explanation file cache provider +│ ├── monitor/ # Alert delivery (Discord webhooks / SMTP email) +│ └── __tests__/ # Vitest unit, baseline, and e2e test suites +├── agents/ +│ ├── council.py # Python + Ollama multi-agent council entrypoint +│ ├── core/ # Evidence model, event streaming, sanitization +│ ├── specialists/ # Domain specialists (runtime, config, resource) +│ ├── orchestration/ # Multi-agent orchestration and tool loop +│ ├── synthesis/ # Consensus hypothesis formulation & investigation +│ ├── validation/ # Cross-agent validation checks +│ ├── remediation/ # Planner, safety policy, post-fix verification +│ ├── rules/ # Python deterministic rule evaluators +│ ├── tools/ # Python Kubernetes & Docker runtime tools +│ └── tests/ # Pytest / unittest suite for Python agents +└── docs/ + ├── guides/ # Architecture, commands, coding style, intro guides + ├── releases/ # Versioned changelogs (v*.md) + └── plans/ # Architecture plans & design specifications +``` --- diff --git a/agents/test_council.py b/agents/tests/test_council.py similarity index 97% rename from agents/test_council.py rename to agents/tests/test_council.py index 588e165f..ea2b34fd 100644 --- a/agents/test_council.py +++ b/agents/tests/test_council.py @@ -3,7 +3,9 @@ import unittest from unittest.mock import MagicMock -sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) +_AGENTS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) +if _AGENTS_DIR not in sys.path: + sys.path.insert(0, _AGENTS_DIR) from specialists import ( RuntimeLogAgent, diff --git a/commands.md b/commands.md deleted file mode 100644 index 92655988..00000000 --- a/commands.md +++ /dev/null @@ -1,274 +0,0 @@ -# KDM CLI Command Reference - -This document provides a comprehensive command reference for the KDM (Kubernetes Docker Monitor) CLI, detailing every command group, subcommand, option flag, and usage example. - ---- - -## 📌 Global Options - -These options apply when invoking the main `kdm` executable: - -| Flag | Description | -|------|-------------| -| `-v, --version` | Output the current version of KDM CLI | -| `-h, --help` | Display general help and command listings | - ---- - -## 1. `kdm show` -Retrieve and display workload resources or cluster metadata in clean, formatted tables. - -### Subcommands: -- **`kdm show runners`** - Displays a combined table of all active Kubernetes pods and Docker containers. -- **`kdm show pods`** - Displays only Kubernetes pods. -- **`kdm show containers`** - Displays only Docker containers. -- **`kdm show nodes`** - Displays details of Kubernetes cluster nodes. -- **`kdm show minikube`** - Checks and prints local Minikube profile and connection status. - -### Examples: -```bash -kdm show runners -kdm show pods -kdm show minikube -``` - ---- - -## 2. `kdm health` -Checks the health of workloads and outputs color-coded logs and details. - -### Options: -- `-w, --watch`: Run in watch mode to continuously refresh the terminal output. -- `-i, --interval `: Refresh interval in seconds (default: `5`). - -### Subcommands: -- **`kdm health all`** - Unified health check of both containers and pods. -- **`kdm health pods`** - Health checks for pods only. -- **`kdm health containers`** - Health checks for Docker containers only. - -### Examples: -```bash -kdm health all -kdm health pods -w # Continuous watch mode (5s refresh) -kdm health containers -w -i 2 # Continuous watch mode (2s refresh) -``` - ---- - -## 3. `kdm watch` -Launches the interactive real-time terminal user interface (TUI) dashboard. Refreshes status and metrics automatically (Ctrl+C to exit). - -### Example: -```bash -kdm watch -``` - ---- - -## 4. `kdm logs ` -Retrieves logs from a workload. KDM attempts a Docker container prefix match first, falling back to Kubernetes pods if no matching container is found. - -### Parameters: -- ``: The container name/ID prefix or the Kubernetes pod name. - -### Example: -```bash -kdm logs auth-api -``` - ---- - -## 5. `kdm analyze` -Scans Kubernetes resources for configuration errors and operational problems. Supports AI-powered troubleshooting explanations. - -### Options: -- `-n, --namespace `: Limit check to a specific Kubernetes namespace. -- `-L, --selector `: Limit check to resources matching a label selector. -- `-f, --filter `: Run a specific analyzer only (e.g. `Pod`, `Ingress`, `Deployment`). Can be specified multiple times. -- `-o, --output `: Output format choice: `text` (default) or `json`. Text output opens an interactive dashboard by default; use `-o json` for machine-readable output. -- `-m, --max-concurrency `: Max concurrency count for analyzers (default: `10`). -- `-s, --with-stat`: Print execution diagnostics statistics. -- `--with-doc`: Retrieve Kubernetes documentation lookups for detected problems. -- `-e, --explain`: Request AI-powered diagnosis explanations. -- `-b, --backend `: Force a specific AI backend provider to query. Changing backend reruns analysis in the interactive dashboard. -- `-l, --language `: Request AI response in a target language (default: `english`). -- `-a, --anonymize`: Mask resource names and identifiers in prompt payload to protect privacy. -- `-c, --no-cache`: Skip looking up or saving to the local AI cache. -- `--kubeconfig `: Specify an alternative kubeconfig file path. -- `--kubecontext `: Specify target Kubernetes context. - -### Examples: -```bash -kdm analyze -kdm analyze -n default --explain -kdm analyze --explain --backend ollama --language spanish -kdm analyze -o json --anonymize -``` - ---- - -## 6. `kdm auth` -Manage credentials, models, and endpoints for AI backend providers. KDM supports **10+** AI providers (OpenAI, Anthropic, Gemini, Vertex AI, WatsonX, OCI GenAI, Cohere, Bedrock, Groq, HuggingFace, etc.). - -### Subcommands: - -- **`kdm auth add`** - Register credentials for a provider. - - Options: - - `-b, --backend `: Target backend name (e.g. `openai`, `ollama`, `anthropic`, `google-gemini`, `google-vertex`, etc.). - - `-m, --model `: Target model name (e.g. `gpt-4o`, `claude-3-5-sonnet-latest`). - - `-p, --password `: API Key or password credential. - - `-u, --baseurl `: Custom API base URL. - - `-t, --temperature `: Model generation temperature. - - `--topp `: Top-P sampling threshold. - - `--topk `: Top-K sampling threshold. - - `--maxtokens `: Max tokens limit. - - `--custom-header `: Add custom HTTP request header. Can be specified multiple times. - -- **`kdm auth list`** - Lists all configured providers and their parameters (API keys are safely masked). - -- **`kdm auth default `** - Sets the active default AI provider. - -- **`kdm auth remove `** - Removes configured settings for a provider. - -- **`kdm auth update `** - Updates settings on an already registered provider (supports the same options as `add`). - -### Examples: -```bash -kdm auth add -b openai -m gpt-4o -p sk-... -kdm auth add -b ollama -m llama3.1 -kdm auth default ollama -kdm auth update ollama -t 0.2 -kdm auth list -``` - ---- - -## 7. `kdm cache` -Manage locally cached AI explanations to save tokens and avoid duplicate network requests. - -### Subcommands: -- **`kdm cache list`** - Lists all cached explanation keys. -- **`kdm cache get `** - Retrieves and prints cached markdown explanation for a key. -- **`kdm cache remove `** - Deletes a specific cache entry. -- **`kdm cache purge`** - Deletes all locally cached explanations. - -### Examples: -```bash -kdm cache list -kdm cache purge -``` - ---- - -## 8. `kdm filters` -Configure default active analyzers to filter what `kdm analyze` checks. Running `kdm filters` opens an interactive checklist TUI dashboard. - -### Interactive Dashboard: -Run `kdm filters` to open the interactive multi-select checklist: -- **`↑` / `↓`**: Navigate analyzer list -- **`Space`**: Toggle analyzer on/off (instantly auto-saved) -- **`Q`**: Quit - -### Subcommands: -- **`kdm filters list`** - Lists all active default analyzers and available inactive ones. -- **`kdm filters add `** - Adds an analyzer (e.g. `Ingress`, `CronJob`) to default active check list. -- **`kdm filters remove `** - Removes an analyzer from the active list. - -### Examples: -```bash -kdm filters # Open interactive checklist dashboard -kdm filters list -kdm filters add Ingress -``` - ---- - -## 9. `kdm custom-analyzer` -Register custom shell commands or HTTP webhooks to analyze arbitrary custom resources (CRDs). - -Running `kdm custom-analyzer` opens an interactive rule manager. Use the arrow keys to select a -rule, `a` to open the add wizard, `d` or `Delete` to remove the selected rule, and `q` to quit. -The dashboard requires a TTY terminal and exits with code 1 otherwise. Press `Esc` to cancel the -add wizard. - -The legacy subcommands remain available for scripts: - -- **`kdm custom-analyzer add `** - Register a new analyzer. Requires either `--command` or `--url`. - - Options: - - `--command `: External CLI command to execute. - - `--url `: HTTP endpoint URL to call. - -- **`kdm custom-analyzer list`** - Lists all registered custom analyzers. - -- **`kdm custom-analyzer remove `** - Deletes a registered custom analyzer. - -### Examples: -```bash -kdm custom-analyzer add keda --command "kubectl get scaledobjects -A -o json" -kdm custom-analyzer list -kdm custom-analyzer remove keda -``` - ---- - -## 10. `kdm serve` -Starts the KDM server daemon in either REST API mode or JSON-RPC Model Context Protocol (MCP) mode. - -### Options: -- `-p, --port `: HTTP server port (default: `8080`). -- `--metrics-port `: Metrics server port. -- `-b, --backend `: Default active AI backend provider. -- `-f, --filter `: Default analyzer filter limit. -- `--http`: Force HTTP mode (default). -- `--mcp`: Start in MCP mode (JSON-RPC stdio). - -### Examples: -```bash -kdm serve --port 8080 # Starts HTTP REST API on http://localhost:8080 -kdm serve --mcp # Starts JSON-RPC MCP server over stdio -``` - ---- - -## 11. `kdm config` -Interactively set up alerting notifications or directly update config properties. - -### Subcommands: -- **`kdm config setup`** - Launches interactive CLI wizard to configure alerting notifications (Discord Webhook, Email SMTP settings). -- **`kdm config list`** - Lists all general configurations. -- **`kdm config set `** - Manually sets a config parameter. -- **`kdm config clear`** - Resets all configurations to default. - -### Examples: -```bash -kdm config setup -kdm config set alert_cooldown 600 -kdm config list -``` diff --git a/docs/README.md b/docs/README.md index 6edc4a65..89271cdc 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,6 +1,27 @@ -# Documentation (Latest: v2.0.1) +# KDM CLI Documentation -# KDM CLI Documentation - v2.0.1 +## Guides & Reference + +- **[What Is KDM?](guides/what-is-kdm.md)** — Introduction, core capabilities, setup, and concepts +- **[How KDM Works: Architecture & Internals](guides/architecture.md)** — End-to-end architecture, Multi-Agent Council, 4-tier degradation ladder, declarative tools, and safety gating +- **[CLI Command Reference](guides/commands.md)** — Complete commands, flags, Copilot slash commands, interactive TUI shortcuts, HTTP/MCP APIs, and environment variables +- **[Coding Style & CodeScene Compliance](guides/coding-style.md)** — Code health thresholds, complexity rules, and refactoring patterns + +## Planning & Specs + +- **[Multi-Agent Architecture Plan](plans/multi-agent-implementation-plan.md)** — Multi-agent architecture & implementation specification + +## Documentation Layout + +| Path | Purpose | +| :--- | :--- | +| `docs/guides/` | Concept guides, architecture internals, CLI command reference, and coding standards | +| `docs/releases/` | Per-version release notes (`v*.md`) generated and indexed automatically | +| `docs/plans/` | Architectural roadmaps and design specifications | + +--- + +## Latest Release Notes (v2.0.1) Published on: 2026-06-11 ### Commits @@ -24,11 +45,11 @@ Published on: 2026-06-11 --- ## Version History -* [v2.0.1](v2.0.1.md) -* [v2.0.0](v2.0.0.md) -* [v1.2.5](v1.2.5.md) -* [v1.2.3](v1.2.3.md) -* [v1.2.2](v1.2.2.md) -* [v1.2.1](v1.2.1.md) -* [v1.2.0](v1.2.0.md) -* [v1.1.3](v1.1.3.md) +* [v2.0.1](releases/v2.0.1.md) +* [v2.0.0](releases/v2.0.0.md) +* [v1.2.5](releases/v1.2.5.md) +* [v1.2.3](releases/v1.2.3.md) +* [v1.2.2](releases/v1.2.2.md) +* [v1.2.1](releases/v1.2.1.md) +* [v1.2.0](releases/v1.2.0.md) +* [v1.1.3](releases/v1.1.3.md) diff --git a/docs/UPGRADE-v2.md b/docs/UPGRADE-v2.md deleted file mode 100644 index 374e6f23..00000000 --- a/docs/UPGRADE-v2.md +++ /dev/null @@ -1,97 +0,0 @@ -# Upgrade guide (v1.x → v2.0) - -This guide explains how to migrate from any **v1.x** release to **v2.0**. - -## Summary of what changed - -v2.0 introduces a new deployment workflow and stricter configuration validation. - -- The deploy command changed from `deploy` to `deploy run`. -- Legacy config keys are no longer accepted. -- Runtime now requires Node.js 18+. - -## Pre-upgrade checklist - -Before upgrading: - -1. Confirm your environment is running **Node.js 18 or newer**. -2. Commit or back up your current `config.yml`. -3. Capture current deployment settings for comparison. - -## Breaking changes - -### 1) Deploy command format changed - -- **v1.x** - ```bash - mytool deploy --env prod - ``` -- **v2.0** - ```bash - mytool deploy run --environment production - ``` - -Notes: -- `--env` was replaced by `--environment`. -- Use explicit environment names (for example, `production`, `staging`). - -### 2) Configuration schema updated - -The old top-level `deploy` key is replaced by `release`. - -- **v1.x (`config.yml`)** - ```yaml - deploy: - env: prod - timeout: 30 - ``` - -- **v2.0 (`config.yml`)** - ```yaml - release: - environment: production - timeoutSeconds: 30 - ``` - -Key migrations: -- `deploy.env` → `release.environment` -- `deploy.timeout` → `release.timeoutSeconds` - -### 3) Deprecated behavior removed - -- Implicit default environment selection is removed. -- Unknown config keys now fail validation instead of being ignored. - -## Step-by-step migration - -1. Upgrade the CLI/application to v2.0. -2. Update `config.yml` keys to the new `release.*` schema. -3. Replace all deployment scripts from `deploy` to `deploy run`. -4. Rename `--env` arguments to `--environment`. -5. Run a dry run in staging: - ```bash - mytool deploy run --environment staging --dry-run - ``` -6. Deploy to production: - ```bash - mytool deploy run --environment production - ``` - -## Validation after upgrade - -- Run configuration validation and confirm there are no unknown keys. -- Verify the target environment is correct in deployment logs. -- Confirm service health checks pass after deployment. - -## Troubleshooting - -- **Error: unknown key `deploy`** - Update config to use `release`. -- **Error: missing environment** - Pass `--environment ` explicitly. -- **Command not found for old flags** - Replace deprecated v1 flags with v2 equivalents. - -## Rollback guidance - -If deployment fails, restore the previous version and the backed-up v1 config, then re-run the v1 deployment command. Keep the v2 migration changes in a separate branch until validation completes. diff --git a/docs/guides/architecture.md b/docs/guides/architecture.md new file mode 100644 index 00000000..994c26f3 --- /dev/null +++ b/docs/guides/architecture.md @@ -0,0 +1,215 @@ +# How KDM Works: Architecture & Internals + +**KDM (`kdm-cli`)** is a terminal-first Kubernetes and Docker diagnostic tool built as a **TypeScript / Node.js CLI and Ink React TUI** paired with a **Python + Ollama multi-agent council**, **deterministic rule engine**, **declarative cluster tools**, and **JSON-RPC MCP / HTTP automation servers**. + +This document explains how KDM's subsystems fit together from CLI invocation to diagnosis, codebase investigation, and human-gated remediation. For exact CLI flags, slash commands, and keyboard shortcuts, see [commands.md](commands.md). + +--- + +## System Overview + +```mermaid +flowchart TD + Entry["bin/kdm.js -> src/index.ts -> src/commands/root.ts"] + Entry -->|"No args (TTY)"| InitUI["InitialDashboard (React Ink)"] + Entry -->|"No args (Non-TTY / CI)"| ProbeOut["Runtime Probes + Help Output"] + Entry -->|"kdm copilot"| Copilot["CopilotShell (4-Tab TUI + Slash Commands)"] + Entry -->|"kdm analyze"| Analysis["Analysis Engine (src/analysis/analysis.ts)"] + Entry -->|"kdm serve --mcp"| MCP["MCP Server (JSON-RPC 2.0 stdio)"] + Entry -->|"kdm serve --http"| HTTP["HTTP Server (127.0.0.1:8080)"] + Entry -->|"show / health / watch / logs / ..."| Dashboards["TUI & CLI Subcommands"] + + Analysis --> Analyzers["Analyzer Registry (22 Built-in + 3 Integrations + Custom)"] + Analyzers --> K8sAPI["Kubernetes API (@kubernetes/client-node)"] + Analysis --> Cache["FileCacheProvider (src/cache/)"] + + Analysis --> Ladder{"Diagnostic Reasoning"} + Ladder -->|"--multi-agent & probes OK"| Bridge["Python Bridge (src/agent/python-bridge.ts)"] + Bridge -->|"NDJSON stdout"| Council["agents/council.py -> Ollama"] + Ladder -->|"Fallback Tier 2"| Rules["Deterministic Rule Engine (src/rules/)"] + Ladder -->|"Fallback Tier 3 / Single-AI"| AIFactory["AI Client Factory (src/ai/factory.ts)"] + Ladder -->|"Fallback Tier 4"| RawSummary["Raw K8s Event/Log Summary"] + + AIFactory --> Gemini["GoogleGenAIClient + GeminiSession (@google/genai)"] + AIFactory --> Cloud["14 Configured AI Providers (OpenAI, Anthropic, Ollama, Bedrock, Vertex, ...)"] + + Copilot --> ToolReg["Declarative ToolRegistry (13 K8s & Docker Tools)"] + ToolReg -->|"read_only"| K8sAPI + ToolReg -->|"read_only"| Docker["Docker API (dockerode / docker CLI)"] + ToolReg -->|"mutating"| Safety["SafetyPrompt Gate ([y/N] Confirmation)"] + Safety --> Exec["RemediationExecutor (execFile + Audit Trail)"] + + AgentReg["AgentRegistry + AgentTool (delegate_to_agent)"] --> Investigator["CodebaseInvestigatorAgent (YAML Diffs, -Xmx, Secret Keys)"] +``` + +--- + +## Entrypoint & Runtime Dispatch + +1. **CLI Initialization (`bin/kdm.js` -> `src/index.ts` -> `src/commands/root.ts`)**: + - Registers 12 top-level commands (`show`, `health`, `watch`, `logs`, `config`, `analyze`, `filters`, `auth`, `cache`, `serve`, `custom-analyzer`, `copilot`) along with integration analyzer registration (`registerIntegrations()`). +2. **Zero-Argument Behavior**: + - **Interactive TTY (`process.stdout.isTTY`)**: Launches `InitialDashboard` (`src/ui/InitialDashboard.tsx`), providing direct navigation into `show`, `health`, `watch`, `logs`, `copilot`, `filters`, `auth`, `cache`, and `custom-analyzer`. + - **Non-TTY / Piped / CI**: Probes local Docker, Kubernetes (`@kubernetes/client-node`), and Minikube availability, prints status badges + summary, displays help, and exits `0` (if at least one runtime is reachable) or `1`. +3. **Interactive Copilot Guard (`src/commands/copilot.tsx`)**: + - `kdm copilot [resource]` (aliases `agent`, `chat`) explicitly rejects non-TTY terminals or CI environments (`CI` truthy or `GITHUB_ACTIONS=true`) so terminal input hooks are never hung in automated pipelines. + +--- + +## Diagnostic Analysis Pipeline (`src/analysis/analysis.ts`) + +When you run `kdm analyze` (either directly in CLI/JSON mode or via interactive Ink `AnalyzeDashboard`), KDM executes the following pipeline: + +### 1. Filter & Target Resolution +- **Filters**: Resolved in order of priority: `-f / --filter` CLI flags → saved `activeFilters` in config → `DEFAULT_FILTERS` (`Pod`, `Deployment`, `Service`, `PersistentVolumeClaim`, `Node`). +- **Target Resource (`[resource]`)**: Passing `kind/name` narrows analysis to that resource type and filters results to `name`; passing `name` matches resources by name across active filters. +- **Namespace & Selector**: `-n / --namespace`, `-L / --selector`, `--kubeconfig`, and `--kubecontext` scope Kubernetes API calls. + +### 2. Concurrent Analyzer Execution +- Resolved analyzers from `src/analyzers/index.ts` execute in bounded concurrency batches controlled by `-m / --max-concurrency` (default `10`, clamped to `1..100`). +- **Built-in Kubernetes Analyzers (22)**: + - Core (5): `Pod`, `Deployment`, `Service`, `PersistentVolumeClaim`, `Node` + - Expanded (17): `ReplicaSet`, `StatefulSet`, `DaemonSet`, `Job`, `CronJob`, `Ingress`, `ConfigMap`, `HorizontalPodAutoscaler`, `PodDisruptionBudget`, `NetworkPolicy`, `Events`, `Logs`, `Security`, `Storage`, `GatewayClass`, `Gateway`, `HTTPRoute` +- **Integration Analyzers (3)** (`src/integrations/integrations.ts`): `KEDA` (`scaledobjects`), `Kyverno` (`clusterpolicies`), and `Prometheus` (`servicemonitors`). +- **Custom Analyzers**: User-registered external commands (`--command`) or HTTP endpoints (`--url`) managed via `kdm custom-analyzer`. + +### 3. Caching & Anonymization +- Unless `--no-cache` is passed, `FileCacheProvider` (`src/cache/file-cache.ts`) caches AI explanations keyed by anonymized problem signatures so repeated failures do not incur redundant model calls. +- Passing `-a / --anonymize` redacts cluster-identifying names (`anonymize` / `deanonymize` in `src/utils/text.ts`) before prompts leave the local machine. + +### 4. Graceful Degradation Ladder (`--multi-agent`) +When multi-agent analysis is enabled (`--multi-agent` flag, `KDM_MULTI_AGENT=true`, or `kdm config set multi_agent true`), KDM first runs parallel preflight probes (`src/analysis/probes.ts`): +- `isPythonAgentAvailable()` checks `python3` and packaged `agents/council.py` availability. +- `isOllamaAvailable()` sends an HTTP probe to the configured Ollama `baseUrl` (default `http://localhost:11434`, 1000ms timeout). + +Results flow through a **4-tier Graceful Degradation Ladder** (`src/analysis/fallback.ts`): + +```mermaid +flowchart LR + T1["1. Python Multi-Agent Council
(Ollama + specialist roles)"] -->|Timeout / Crash / Probe Fail| T2["2. Deterministic Rule Engine
(src/rules/k8s-rules.ts)"] + T2 -->|No deterministic match| T3["3. Legacy Cloud AI Client
(--backend / default / openai)"] + T3 -->|Unconfigured / API Error| T4["4. Raw Kubernetes Event/Log Summary
(Always succeeds)"] +``` + +| Tier | Component | Requirements | Behavior | +| :--- | :--- | :--- | :--- | +| **1** | **Python Multi-Agent Council** (`agents/council.py`) | `python3`, `ollama>=0.4.0`, running Ollama server | Spawns Python subprocess (`DEFAULT_TIMEOUT_MS = 120000`, override with `--council-timeout` or `KDM_COUNCIL_TIMEOUT_MS`); streams NDJSON progress events to UI; synthesizes root cause, confidence score, and proposed patch. | +| **2** | **Deterministic Rule Engine** (`src/rules/engine.ts`, `k8s-rules.ts`) | None (offline) | Evaluates `OOMKilledRule`, `ImagePullRule`, `CrashLoopRule`, `ProbeFailureRule`, and `SchedulingRule` against failure evidence (`confidence >= 0.9`). Handles common Kubernetes outages instantaneously without any LLM. | +| **3** | **Legacy Cloud AI Client** (`src/ai/factory.ts`) | Configured provider or `-b` backend (skips local `ollama`) | Explains failure and suggests remediation using the selected LLM backend (falls back to `openai` provider lookup if configured). | +| **4** | **Raw Kubernetes Summary** (`formatRawKubernetesSummary`) | None (offline) | Emits structured Kubernetes failure text, container state, events, and logs directly so telemetry is never lost even when all AI services are offline. | + +### 5. Audit Export (`--export-audit `) +When `--export-audit ` is passed to `kdm analyze`, `exportIncidentAudit` (`src/utils/audit-exporter.ts`) writes a structured Markdown or JSON report while recursively redacting sensitive object keys matching `password`, `secret`, `key`, `token`, `auth`, or `bearer` as well as inline Bearer tokens, PEM blocks, and `key=value` secrets (`[REDACTED]`). + +--- + +## Multi-Agent Council & Bridge (`agents/` & `src/agent/`) + +### Specialist Roles & Model Routing +`config/agents.yaml` configures per-role local Ollama models resolved in priority order: +1. CLI flag `--model ` +2. Role mapping in `config/agents.yaml` (`runtime`, `config`, `resource`, `synthesizer`) +3. `models.default` in `config/agents.yaml` (`llama3.1`) + +Default role specializations in `config/agents.yaml`: +- `runtime`: `llama3.1` — Pod/container lifecycle, crash loops, exit codes, and runtime events. +- `config`: `qwen2.5-coder` — Manifest syntax, env/Secret/ConfigMap wiring, and patch generation. +- `resource`: `llama3.1` — CPU/memory limits, scheduling constraints, and storage/PVC health. +- `synthesizer`: `llama3.1` — Correlates specialist findings into a unified `ConsensusDiagnosis`. + +### Real-Time NDJSON Event Translation (`src/agent/event-translator.ts`) +While `agents/council.py` executes, it streams newline-delimited JSON (`agent_start`, `agent_progress`, `agent_tool_call`, `agent_tool_result`, `agent_complete`, `agent_failed`, `diagnosis_ready`, `error`) over stdout. `event-translator.ts` buffers chunked streams safely across packet boundaries, drops malformed lines silently, and translates both v4 and legacy event shapes into `SubagentProgress` and `AgentEvent` updates for the TUI. + +### Subprocess Binary Trust Policy (CWE-426 Protection) +To prevent arbitrary script execution from untrusted working directories (CWE-426), `src/agent/python-bridge.ts` always resolves `agents/council.py` from the packaged KDM installation directory unless `KDM_ALLOW_LOCAL_AGENTS=1` / `true` or `NODE_ENV=development` is set. + +--- + +## Agent Delegation & `CodebaseInvestigatorAgent` (`src/agent/`) + +Beyond Python council subprocesses, KDM provides an in-process TypeScript agent subsystem (`src/agent/`): +- **`AgentRegistry` (`src/agent/registry.ts`)**: Stores typed `AgentDefinition` entries validated for non-empty names, Zod `inputSchema` / `outputSchema`, and executable `run()`. +- **`AgentTool` (`src/agent/agent-tool.ts`)**: Exposes `delegate_to_agent` as a declarative tool (`safetyLevel: 'read_only'`, `riskLevel: 'low'`) with recursion protection (`default maxDepth = 2`) and workspace `cwd` propagation via `AgentContext`. +- **`CodebaseInvestigatorAgent` (`src/agent/investigator.ts`)**: Correlates live cluster symptoms with local repository code and Kubernetes YAML manifests: + - Walks local workspace files safely (skipping `node_modules`, `.git`, `dist`, `build`, `.next`, `.cache`, `.agents`, symlinks, and binary extensions) or accepts virtual `workspaceFiles` maps. + - Detects **JVM heap vs. Kubernetes memory limit drift** by extracting `-Xmx` flags and comparing against Kubernetes SI (`M`, `G`) and IEC binary (`Mi`, `Gi`) limits via `parseMemoryBytes`. + - Validates Deployment/Pod `secretKeyRef` references against observed live `secretKeys` in the namespace. + - Generates unified diff patches (`diff` / `createTwoFilesPatch`) and structured `RelevantLocations` + `findings`. + +--- + +## AI Provider Layer & Modern Gemini Support (`src/ai/`) + +`src/ai/factory.ts` maps 14 configured provider identifiers (and convenience aliases) onto 16 runtime client implementations: + +| Provider Family | `kdm auth` Name | Runtime Aliases Supported by Factory | Notes | +| :--- | :--- | :--- | :--- | +| **Google GenAI (v4)** | `google-gemini` | `google-genai`, `googlegenai`, `gemini` -> `GoogleGenAIClient` | `@google/genai` SDK with `GeminiSession`, streaming thoughts, function declarations, and Zod structured JSON output | +| **Google Gemini (Legacy)** | `google-gemini` | `google-gemini`, `googlegemini` | Legacy `@google/generative-ai` wrapper | +| **Google Vertex AI** | `google-vertex` | `google-vertex`, `googlevertex`, `vertex` | Vertex AI client | +| **OpenAI** | `openai` | `openai` | Default fallback backend (`gpt-4o-mini` / user model) | +| **Anthropic** | `anthropic` | `anthropic` | Claude models | +| **Ollama** | `ollama` | `ollama` | Local HTTP model server (default `http://localhost:11434`) | +| **Azure OpenAI** | `azure-openai` | `azure-openai`, `azureopenai` | Azure OpenAI endpoints | +| **Amazon Bedrock** | `amazon-bedrock` | `amazon-bedrock`, `amazonbedrock`, `bedrock` | AWS Bedrock models | +| **Cohere** | `cohere` | `cohere` | Cohere Command models | +| **Hugging Face** | `huggingface` | `huggingface`, `hugging-face` | Hugging Face Inference | +| **Groq** | `groq` | `groq` | Groq LPU inference | +| **IBM watsonx** | `ibm-watsonx` | `ibm-watsonx`, `ibmwatsonx`, `watsonx` | watsonx.ai client | +| **OCI GenAI** | `oci-genai` | `oci-genai`, `ocigenai` | Oracle Cloud Generative AI | +| **Custom REST** | `customrest` | `customrest`, `custom-rest` | Custom OpenAI-compatible or custom HTTP endpoint | +| **No-op** | `noop` | `noop` | Disables LLM calls for offline/testing workflows | + +`GoogleGenAIClient` resolves credentials from `config.password` or environment variables (`GEMINI_API_KEY`, `GOOGLE_GENAI_API_KEY`, `GOOGLE_API_KEY`). `GeminiSession` (`src/ai/gemini-session.ts`) preserves full candidate `Part` arrays across multi-turn function calls—including model `thought` parts and signatures required by thinking-enabled Gemini models—and converts Zod tool schemas automatically via `normalizeTools`. + +--- + +## Declarative Tools & Safe Remediation (`src/tools/` & `src/remediation/`) + +### Declarative Tool Registry (`src/tools/tool-registry.ts`, `src/tools/declarative-tool.ts`) +`defaultToolRegistry` registers 13 cluster tools validated by `zod`: + +- **Kubernetes Tools (9, `src/tools/k8s-tools.ts`)**: + - Read-only: `k8s_get_resources`, `k8s_describe_resource`, `k8s_get_logs`, `k8s_get_events`, `k8s_get_pod`, `k8s_list_pods` + - Mutating (`high` risk / gated): `k8s_apply_manifest`, `k8s_delete_resource`, `k8s_apply_patch` +- **Docker Tools (4, `src/tools/docker-tools.ts`)**: + - Read-only: `docker_list_containers`, `docker_inspect_container`, `docker_get_logs` + - Mutating (`medium` risk / gated): `docker_restart_container` + +Each tool defines a `zod` schema converted to a `@google/genai` `FunctionDeclaration` (`zodToGenAiSchema`), along with `safetyLevel` (`'read_only'` | `'mutating'`) and `riskLevel` (`'low'` | `'medium'` | `'high'`). + +### Human-in-the-Loop Remediation Safety +1. **Mutation Gate**: Before running any `mutating` tool, `ToolRegistry.execute` invokes `context.approveMutation` (unless `dryRun` is enabled) and fails closed if no approval callback authorizes the action. +2. **Interactive Safety Prompt (`src/remediation/safety-prompt.ts`)**: Renders an Ink `SafetyPrompt` displaying the proposed action, color-coded risk badge, exact command, and unified `+`/`-` diff preview. Execution requires an explicit `y` / `Y` keypress; `Enter`, `n`, or any other key aborts. +3. **Parameterized Execution (`src/remediation/executor.ts`)**: Runs approved commands via `child_process.execFile` (no shell interpolation), enforces a 30-second default timeout, suggests rollback commands, and appends immutable `RemediationAuditRecord` entries to `RemediationAuditTrail`. + +--- + +## Copilot Shell & Interactive TUIs (`src/ui/`) + +- **`CopilotShell` (`src/ui/CopilotShell.tsx`)**: Launched by `kdm copilot [resource]` or by pressing `t` on a workload in `WatchDashboard`: + - **4 Tabs**: `Monitor` (`Alt+1`), `Agents` (`Alt+2`), `Diffs` (`Alt+3`), `Settings` (`Alt+4`). + - **Slash Commands**: `/pods`, `/containers`, `/watch`, `/analyze`, `/investigate`, `/diff`, `/health`, `/help`, `/clear`, `/quit`, `/exit` call live Kubernetes/Docker collectors and codebase analysis directly in the conversation stream. + - **Draft Preservation**: Unsent composer input survives switching across tabs. +- **Standalone Dashboards**: Dedicated Ink workspaces for `show`, `health`, `watch`, `logs`, `analyze`, `filters`, `auth`, `cache`, `custom-analyzer`, and `serve`. + +--- + +## HTTP & MCP Servers (`src/server/`) + +`kdm serve` exposes diagnostics for scripts, CI, webhooks, and external AI coding agents: + +1. **HTTP API (`src/server/server.ts`, `--http`)**: + - Binds strictly to `127.0.0.1` (default port `8080`). + - Enforces a 1 MB JSON request body limit on `POST /analyze`. + - Masks provider credentials/passwords as `****` in `GET /config`. +2. **MCP Server (`src/server/mcp.ts`, `--mcp`)**: + - Speaks JSON-RPC 2.0 over `stdin`/`stdout` (`protocolVersion: "2024-11-05"`). + - Advertises 4 read-only cluster diagnostic tools: `analyze_cluster`, `list_filters`, `get_cluster_health`, and `get_resource_issues`. + +--- + +## Configuration, Notifications, & Storage + +- **Persistent Store (`src/config/store.ts`, `src/config/schema.ts`, `src/config/migration.ts`)**: Managed by `conf` (`projectName: 'kdm-cli'`). Stores AI providers (`ai`), active filters (`activeFilters`), Kubernetes context settings (`kubernetes`), cache config (`cache`), output defaults (`output`), alert settings (`notifications`), integration toggles (`integrations`), `customAnalyzers`, and `multi_agent`. +- **Notifications (`src/utils/config.ts` & alert dispatcher)**: Supports `discord` webhooks and `email` SMTP (`nodemailer`) with configurable `alert_cooldown` (seconds). **SMTP passwords are never persisted to config** and must be passed at runtime via `KDM_SMTP_PASSWORD`. diff --git a/coding_style.md b/docs/guides/coding-style.md similarity index 100% rename from coding_style.md rename to docs/guides/coding-style.md diff --git a/docs/guides/commands.md b/docs/guides/commands.md new file mode 100644 index 00000000..5aa6dcb3 --- /dev/null +++ b/docs/guides/commands.md @@ -0,0 +1,395 @@ +# KDM CLI Command Reference + +`kdm` (`kdm-cli`) provides interactive terminal dashboards (React Ink), an AI diagnostic Copilot shell, batch/CI analyzers, local & cloud LLM authentication, notification alerts, HTTP & MCP automation servers, and multi-agent root-cause analysis. + +For internal data flows, tool schemas, and the 4-tier graceful degradation ladder, see [Architecture & Internals](architecture.md). + +--- + +## Global Behavior & Options + +Running `kdm` with no arguments adapts to your environment: + +- **Interactive TTY**: Launches `InitialDashboard` with unified menu navigation across `show`, `health`, `watch`, `logs`, `copilot`, `filters`, `auth`, `cache`, and `custom-analyzer`. +- **Non-TTY / Piped / CI**: Probes local Docker, Kubernetes (`@kubernetes/client-node`), and Minikube availability, prints live runtime connection badges, outputs CLI help, and exits `0` (if at least one runtime is reachable) or `1`. + +```bash +kdm [options] [command] +``` + +| Global Option | Description | +| :--- | :--- | +| `-V, --version` | Output the CLI version number | +| `-h, --help` | Display help for command or run `kdm help [command]` | + +--- + +## Commands Overview + +| Command | Aliases | Purpose | +| :--- | :--- | :--- | +| `copilot [resource]` | `agent`, `chat` | Launch interactive AI Copilot terminal shell with live cluster & codebase slash commands | +| `analyze [resource]` | `analyse` | Analyze Kubernetes cluster failures (or target `kind/name` / `name`) with optional AI or `--multi-agent` | +| `show [target]` | — | Inspect Containers, Pods, Runners, Nodes, or Minikube in an interactive TUI or static CLI tables | +| `health ` | — | Monitor live CPU/RAM host health alongside Kubernetes pod & Docker container health | +| `watch` | — | Live-watch running Kubernetes pods and Docker containers with logs, restart, and Copilot handoff | +| `logs [name]` | — | Search, stream, and search-highlight pod or container logs | +| `filters` | `filter` | Manage active Kubernetes resource analyzers (`list`, `add`, `remove`, or interactive TUI) | +| `custom-analyzer` | — | Register and manage custom external shell-command or webhook analyzers | +| `auth` | — | Configure, update, list, or switch AI provider credentials (14 backends) | +| `cache` | — | Inspect, inspect by key, remove, or purge cached AI diagnosis entries | +| `config` | — | Configure alerts (Discord / Email SMTP), `multi_agent`, and CLI settings | +| `serve` | — | Launch the interactive HTTP dashboard, headless `--http` API, or `--mcp` stdio server | + +--- + +## `kdm copilot [resource]` + +Launch the 4-tab interactive Copilot workspace wired to live cluster collectors, declarative K8s/Docker tools, and `CodebaseInvestigatorAgent`. + +```bash +kdm copilot [resource] +kdm agent [resource] +kdm chat [resource] +``` + +**Arguments & Options:** + +| Argument / Option | Description | Default | +| :--- | :--- | :--- | +| `[resource]` | Optional initial workload or question to scope or seed the Copilot session | — | +| `-t, --tab ` | Initial tab index (`1`: Monitor, `2`: Agents, `3`: Diffs, `4`: Settings) | `1` | + +> **TTY Requirement:** `kdm copilot` requires an interactive terminal and refuses to start in CI or non-TTY environments (`CI` truthy or `GITHUB_ACTIONS=true`). + +### Copilot Slash Commands + +| Slash Command | Description | +| :--- | :--- | +| `/pods [-n ]` | Inspect live Kubernetes pods (optionally filtered to `-n `) | +| `/containers` | List and inspect local Docker containers | +| `/watch` | View live workload monitoring snapshot | +| `/analyze [pod-name]` | Run live failure diagnostics across cluster resources or `[pod-name]` | +| `/investigate` | Run `CodebaseInvestigatorAgent` to correlate cluster failures with local manifests/code | +| `/diff` | Review proposed remediation YAML diffs and patch previews | +| `/health` | Display Docker, Kubernetes, and system health snapshot | +| `/help` | Show available slash commands | +| `/clear` | Clear conversation history in the Copilot view | +| `/quit`, `/exit` | Exit Copilot shell | + +### Copilot Keyboard Shortcuts + +- `Alt+1` / `Alt+2` / `Alt+3` / `Alt+4`: Switch between **Monitor**, **Agents**, **Diffs**, and **Settings** tabs (draft input in the composer is preserved across tab switches). +- `1`–`4` / `q`: When viewing tabs `2`–`4`, bare `1`–`4` also switches tabs and `q` exits; on tab `1`, printable characters go into the composer. +- `Ctrl+C`: Exit Copilot from any tab. + +--- + +## `kdm analyze [resource]` + +Analyze cluster resources for failures and misconfigurations. In an interactive TTY with `-o text`, opens the interactive `AnalyzeDashboard`; with `-o json` or in non-TTY/CI environments, prints the structured report and exits. + +```bash +kdm analyze [resource] [options] +kdm analyse [resource] [options] +``` + +**Arguments:** +- `[resource]` *(optional)*: Target a single resource (`kind/name`, e.g., `pod/api-0`, or `name`). Supplying `kind/name` automatically scopes the analyzer filter to `kind`. + +**Options:** + +| Flag | Description | Default | +| :--- | :--- | :--- | +| `-n, --namespace ` | Scope Kubernetes analysis to a single namespace | All namespaces / kubeconfig default | +| `-L, --selector ` | Kubernetes label selector (e.g., `app=nginx,env=prod`) | — | +| `-f, --filter ` | Analyzer name(s) to run (repeatable) | Saved `activeFilters` or 5 core filters | +| `-o, --output ` | Output format: `text` or `json` | `text` | +| `-m, --max-concurrency ` | Max parallel analyzers (`1`–`100`) | `10` | +| `-s, --with-stat` | Include execution timing and analyzer statistics | `false` | +| `--with-doc` | Include resource remediation documentation references | `false` | +| `--kubeconfig ` | Path to custom kubeconfig file | System default kubeconfig | +| `--kubecontext ` | Kubernetes context name | Current context | +| `-e, --explain` | Use configured AI provider to explain issues and suggest fixes | `false` | +| `-b, --backend ` | AI backend override (`openai`, `ollama`, `anthropic`, `google-gemini`, etc.) | Default configured backend | +| `--model ` | Specific model override (applied to `--multi-agent` / AI analysis) | Config / `agents.yaml` default | +| `-l, --language ` | Output language for AI explanations | `english` | +| `-a, --anonymize` | Anonymize resource and namespace identifiers sent to AI backends | `false` | +| `-r, --custom-headers ` | Extra HTTP headers formatted as `Key:Value,Key2:Value2` | — | +| `-c, --no-cache` | Bypass local diagnosis cache | Cache enabled | +| `--export-audit ` | Write redacted incident report (`.json` or Markdown) to file | — | +| `--multi-agent` | Run Python + Ollama Multi-Agent Council with 4-tier Fallback Ladder | Config `multi_agent` (`false`) | +| `--council-timeout ` | Python Multi-Agent Council timeout in milliseconds | `120000` (or `KDM_COUNCIL_TIMEOUT_MS`) | + +**Built-in Analyzer Filters (22):** +- **Default 5 Core Filters**: `Pod`, `Deployment`, `Service`, `PersistentVolumeClaim`, `Node` +- **Additional 17 Expanded Filters**: `ReplicaSet`, `StatefulSet`, `DaemonSet`, `Job`, `CronJob`, `Ingress`, `ConfigMap`, `HorizontalPodAutoscaler`, `PodDisruptionBudget`, `NetworkPolicy`, `Events`, `Logs`, `Security`, `Storage`, `GatewayClass`, `Gateway`, `HTTPRoute` +- **Integration Analyzers (3)**: `KEDA` (`scaledobjects`), `Kyverno` (`clusterpolicies`), `Prometheus` (`servicemonitors`) + +**Examples:** + +```bash +kdm analyze +kdm analyze -n production -e +kdm analyze pod/payments-api --multi-agent --council-timeout 180000 +kdm analyze --filter Pod --filter NetworkPolicy -o json +kdm analyze -e -a --export-audit ./incident-report.md +``` + +--- + +## `kdm show [target]` + +Inspect cluster and container inventories either in an interactive tabbed TUI or via static terminal tables. + +```bash +kdm show [target] +``` + +**Targets:** +- *(none)* — Launch interactive `ShowDashboard` (Containers, Pods, Runners, Nodes, Minikube) +- `containers` — Docker containers +- `pods` — Kubernetes pods +- `runners` — GitHub Actions self-hosted runner processes +- `nodes` — Kubernetes cluster nodes +- `minikube` — Minikube VM/cluster instances + +**Interactive Shortcuts (`ShowDashboard`):** +- `Tab` / `Left` / `Right` / `Shift+Tab`: Switch resource tabs +- `Up` / `Down`: Move row selection +- `/`: Toggle search filter (`Enter` or `Esc` exits search input; `Esc` with active filter clears it) +- `Enter`: Open detail overlay for selected row (`Esc` or `b` closes) +- `Esc` / `b`: Go back • `q` / `Ctrl+C`: Exit + +--- + +## `kdm health ` + +Monitor host CPU/memory utilization alongside Kubernetes pods and Docker containers. + +```bash +kdm health [options] +``` + +| Target / Option | Description | Default | +| :--- | :--- | :--- | +| `` | Required target: `all`, `containers`, or `pods` | — | +| `-w, --watch` | Continuously refresh health metrics | `false` | +| `-i, --interval ` | Refresh interval in seconds | `5` | + +**Interactive Shortcuts (`HealthDashboard`):** +- `Tab`: Cycle target (`all` → `containers` → `pods`) +- `w`: Toggle continuous watch mode +- `+` / `-`: Adjust refresh interval (`1`–`60` seconds) +- `Space`: Expand or collapse Kubernetes/Docker groups +- `Up` / `Down`: Navigate rows • `Enter`: Toggle item details +- `Esc` / `b`: Close details or go back • `q` / `Ctrl+C`: Exit + +--- + +## `kdm watch` + +Open the real-time workload monitoring dashboard across Kubernetes pods and Docker containers. + +```bash +kdm watch +``` + +**Interactive Shortcuts (`WatchDashboard`):** +- `Tab` / `Left` / `Right`: Switch between Kubernetes Pods and Docker Containers panes +- `Up` / `Down`: Select workload +- `l`: View live log snapshot +- `t`: Launch `CopilotShell` pre-scoped to the selected workload +- `a`: Run quick AI diagnosis for the selected workload +- `r`: Request workload restart (`y` confirms, `n` / `Esc` cancels) +- `Esc` / `b`: Back • `q` / `Ctrl+C`: Exit + +--- + +## `kdm logs [name]` + +Search and stream container or pod logs. Passing `[name]` jumps straight into log streaming; omitting opens an interactive searchable selector. + +```bash +kdm logs [name] +``` + +**Interactive Shortcuts (`LogsDashboard`):** +- **Selector Mode**: Type to search resource names, `Up` / `Down` to pick, `Enter` to open logs, `Esc` (or `b` when search empty) to go back, `q` / `Ctrl+C` to exit. +- **Log Viewer Mode**: + - `Space`: Pause or resume log streaming + - `t`: Toggle timestamps + - `/`: Search within log output (`Enter` or `Esc` finishes query input) + - `n` / `Shift+n`: Next / previous search match + - `Esc` / `b`: Return to resource selector (or go back if opened with `[name]`) + - `q` / `Ctrl+C`: Exit + +--- + +## `kdm filters` + +Manage default active analyzers used by `kdm analyze` when `-f` is omitted. Alias: `filter`. + +```bash +kdm filters # Interactive FiltersDashboard (TTY) +kdm filters list +kdm filters add +kdm filters remove +``` + +Built-in default active filters are `Pod`, `Deployment`, `Service`, `PersistentVolumeClaim`, and `Node`. + +--- + +## `kdm custom-analyzer` + +Add, list, or remove custom analyzers backed by shell commands or HTTP webhooks. + +```bash +kdm custom-analyzer # Interactive CustomAnalyzerDashboard (TTY) +kdm custom-analyzer add --command +kdm custom-analyzer add --url +kdm custom-analyzer list +kdm custom-analyzer remove +``` + +**Interactive Shortcuts (`CustomAnalyzerDashboard`):** +- `Up` / `Down`: Select custom analyzer • `a`: Launch 3-step add wizard • `d` / `Delete`: Remove selected rule (`y`/`n` confirm) • `Esc` / `b`: Cancel wizard / back • `q` / `Ctrl+C`: Exit. + +--- + +## `kdm auth` + +Configure, list, update, remove, or set the default AI provider backend. + +```bash +kdm auth # Interactive AuthDashboard (TTY) +kdm auth list +kdm auth add [options] +kdm auth update [options] +kdm auth default +kdm auth remove +``` + +**Supported Configured Backend Names (`VALID_BACKENDS`):** +`openai`, `ollama`, `anthropic`, `noop`, `customrest`, `azure-openai`, `cohere`, `google-gemini`, `google-vertex`, `amazon-bedrock`, `huggingface`, `groq`, `ibm-watsonx`, `oci-genai` + +> **Runtime Aliases:** In addition to configured names above, `kdm analyze -b ` and internal factory calls recognize `google-genai`, `googlegenai`, `gemini` (routes to modern `@google/genai` client), `googlegemini`, `custom-rest`, `azureopenai`, `googlevertex`, `vertex`, `amazonbedrock`, `bedrock`, `hugging-face`, `ibmwatsonx`, `watsonx`, `ocigenai`. + +**Options for `add` and `update`:** + +| Flag | Applies To | Description | Default | +| :--- | :--- | :--- | :--- | +| `-b, --backend ` | `add` only | Backend name (positional `` on `update`) | `openai` | +| `-m, --model ` | `add`, `update` | Model ID (e.g., `gpt-4o`, `gemini-2.5-flash`, `llama3.1`) | Provider default | +| `-p, --password ` | `add`, `update` | API key or credential | — | +| `-u, --baseurl ` | `add`, `update` | Custom base URL (e.g., `http://localhost:11434` for Ollama) | Provider default | +| `-t, --temperature ` | `add`, `update` | Sampling temperature | `0.7` | +| `--topp ` | `add`, `update` | Nucleus sampling `topP` | `1` | +| `--topk ` | `add`, `update` | `topK` sampling | `50` | +| `--maxtokens ` | `add`, `update` | Max response tokens | `2048` | +| `--custom-header ` | `add`, `update` | Custom HTTP header (repeatable) | — | + +**Interactive Shortcuts (`AuthDashboard`):** +- `Up` / `Down`: Navigate providers • `a`: Add provider wizard • `e`: Edit selected provider • `d`: Set default provider • `r`: Remove confirmation (`y`/`n`) • `Esc` / `b`: Cancel / back • `q` / `Ctrl+C`: Exit. + +**Examples:** + +```bash +kdm auth add -b google-gemini -m gemini-2.5-flash -p "$GEMINI_API_KEY" +kdm auth add -b ollama -m llama3.1 -u http://localhost:11434 +kdm auth default google-gemini +``` + +--- + +## `kdm cache` + +Inspect and manage locally cached diagnostic results (`FileCacheProvider`). + +```bash +kdm cache # Interactive CacheDashboard (TTY) +kdm cache list +kdm cache get +kdm cache remove +kdm cache purge +``` + +**Interactive Shortcuts (`CacheDashboard`):** +- `Up` / `Down`: Select entry • `Enter`: Preview entry • `d` / `Delete` / `Backspace`: Remove entry • `p`: Purge confirmation (`y`/`n`) • `Esc`: Close preview or back • `q`: Exit. + +--- + +## `kdm config` + +Manage KDM persistent configuration (`conf` project `kdm-cli`) and alert integrations. + +```bash +kdm config setup +kdm config list +kdm config set +kdm config clear +``` + +| Config Key (`kdm config set`) | Value Type | Notes | +| :--- | :--- | :--- | +| `multi_agent` | Boolean (`true`/`1`/`false`/`0`) | Enables Multi-Agent Council mode by default for `kdm analyze` | +| `alert_cooldown` | Integer (seconds) | Minimum seconds between repeated alert dispatches | +| `notification_service` | `discord` \| `email` \| `none` | Deprecated via `set`; use `kdm config setup` | +| `discord_webhook`, `email_host`, `email_port`, `email_user`, `email_to` | String (or integer for `email_port`) | Deprecated via `set`; use `kdm config setup` | +| `email_password` | *Rejected* | Cannot be stored in config; must use `KDM_SMTP_PASSWORD` | + +--- + +## `kdm serve` + +Run KDM as an interactive HTTP dashboard, a headless localhost HTTP API, or a Model Context Protocol (MCP) stdio server. + +```bash +kdm serve [options] +``` + +| Flag | Description | Default | +| :--- | :--- | :--- | +| `-p, --port ` | Port for the HTTP server | `8080` | +| `--metrics-port ` | Metrics port option parsed by `serve` | — | +| `-b, --backend ` | Default AI backend for `/analyze` | Configured default | +| `--http` | Run headless HTTP API server without TUI dashboard | `false` | +| `-f, --filter ` | Default analyzer filters for HTTP `/analyze` (repeatable) | Default active filters | +| `--mcp` | Run JSON-RPC 2.0 MCP server over `stdin`/`stdout` | `false` | + +### Interactive Serve Dashboard Controls + +When invoked in interactive mode without `--http` or `--mcp`: +`s` start/stop HTTP server • `r` restart • `p` change port • `c` clear request logs • `Q` quit. + +### HTTP Endpoints (`--http`, bound to `127.0.0.1`) + +| Method & Path | Description | +| :--- | :--- | +| `GET /health` | Service status, uptime, and version (`{ status, uptime, version }`) | +| `GET /filters` | List active and available analyzers (`{ active, available }`) | +| `GET /config` | Current configuration with sensitive tokens/passwords masked as `****` | +| `POST /analyze` | Trigger cluster analysis (max 1 MB JSON body: `{ namespace, filters, explain, anonymize, backend }`) | + +### MCP Tools (`--mcp`, Protocol `2024-11-05`) + +- `analyze_cluster`: Run Kubernetes analyzers with optional `namespace`, `filters`, `explain`, `backend`, `anonymize`. +- `list_filters`: Return configured `active` and `available` analyzer names. +- `get_cluster_health`: Check live Kubernetes API connectivity (`clusterConnected`) plus active/available filter summary. +- `get_resource_issues`: Filter analysis findings to a specific Kubernetes `kind` (`name` and `namespace` optional). + +--- + +## Environment Variables + +| Variable | Subsystem | Description | +| :--- | :--- | :--- | +| `GEMINI_API_KEY` | Gemini (`@google/genai`) | API key for `GoogleGenAIClient` | +| `GOOGLE_GENAI_API_KEY` | Gemini (`@google/genai`) | Alternate API key for `GoogleGenAIClient` | +| `GOOGLE_API_KEY` | Gemini / Google AI | Fallback API key env var for Google clients | +| `KDM_MULTI_AGENT` | `kdm analyze` | Set `true` or `1` to enable Multi-Agent Council mode globally | +| `KDM_COUNCIL_TIMEOUT_MS` | Python Bridge | Override Multi-Agent Council timeout in milliseconds (default `120000`) | +| `KDM_ALLOW_LOCAL_AGENTS` | Python Bridge | Set `1` or `true` in dev to resolve `agents/council.py` from CWD instead of packaged installation | +| `KDM_SMTP_PASSWORD` | Notifications | Required runtime SMTP password for Email alert delivery | +| `NODE_ENV` | Runtime | Setting `development` allows local CWD agent script resolution | diff --git a/docs/what-is-kdm.md b/docs/guides/what-is-kdm.md similarity index 65% rename from docs/what-is-kdm.md rename to docs/guides/what-is-kdm.md index 7567751e..dcb49ed9 100644 --- a/docs/what-is-kdm.md +++ b/docs/guides/what-is-kdm.md @@ -21,20 +21,16 @@ Tools such as `docker ps`, `kubectl get pods`, and `kubectl logs` are powerful, KDM currently supports: -- Auto-detecting Docker and Kubernetes availability. -- Showing Docker containers and Kubernetes pods. -- Showing a unified runner view of containers and pods. -- Showing Minikube profile status. -- Checking workload health with watch and refresh modes. -- Opening a live terminal dashboard. -- Fetching logs from containers or pods with auto fallback. -- Cluster configuration diagnostics (`kdm analyze`) with AI-powered explanations. -- Managing 10+ AI backend providers (OpenAI, Anthropic, Gemini, Vertex, Ollama, etc.) via `kdm auth`. -- Local explanation caching (`kdm cache`) to minimize token cost and latency. -- Custom CRD analyzers configuration (`kdm custom-analyzer`) for Kyverno, KEDA, Prometheus, etc. -- HTTP REST API and JSON-RPC Model Context Protocol (MCP) server daemon modes (`kdm serve`). -- Sending alert notifications through Discord or email SMTP. -- Storing local configuration for notification and runtime behavior. +- Auto-detecting Docker, Kubernetes, and Minikube availability. +- Launching an interactive 4-tab AI **Copilot Shell** (`kdm copilot`) with live cluster/codebase slash commands (`/pods`, `/containers`, `/analyze`, `/investigate`, `/diff`, `/health`, `/watch`). +- Showing Docker containers, Kubernetes pods, unified runners, nodes, and Minikube profiles (`kdm show`). +- Checking host CPU/RAM and workload health with watch mode (`kdm health`), live workload monitoring with Copilot/AI/restart actions (`kdm watch`), and searchable log streaming (`kdm logs`). +- Cluster diagnostics across **22 built-in Kubernetes analyzers** plus `KEDA`, `Kyverno`, and `Prometheus` integrations (`kdm analyze`), with single-prompt AI explanations (`--explain`), redacted incident exports (`--export-audit`), and an autonomous **Multi-Agent SRE Council** (`--multi-agent`) backed by a 4-tier Graceful Degradation Ladder. +- Managing **14 AI backend providers** (OpenAI, Anthropic, Google Gemini / `@google/genai`, Google Vertex AI, Ollama, Azure OpenAI, Amazon Bedrock, Cohere, Hugging Face, Groq, IBM watsonx, OCI GenAI, Custom REST, No-op) via `kdm auth`. +- Local explanation caching (`kdm cache`) and custom external command/webhook analyzers (`kdm custom-analyzer`). +- Declarative Kubernetes and Docker tools with human-in-the-loop `SafetyPrompt` confirmation (`[y/N]`) and parameterized `execFile` remediation auditing. +- HTTP REST API (`127.0.0.1`) and JSON-RPC 2.0 Model Context Protocol (MCP) stdio server modes (`kdm serve`). +- Alert notifications through Discord webhook or email SMTP (`kdm config setup`). ## Core Commands @@ -100,15 +96,25 @@ kdm logs KDM attempts to fetch logs for the given workload name. Docker is tried first, and Kubernetes can be used as a fallback depending on the workload. +### Interactive AI Copilot (`kdm copilot`) + +```bash +kdm copilot +kdm copilot checkout-api -t 2 +``` + +Opens the 4-tab terminal Copilot shell (**Monitor**, **Agents**, **Diffs**, **Settings**) with live slash commands such as `/pods`, `/containers`, `/analyze`, `/investigate`, `/diff`, and `/health`. + ### Diagnostics & AI Analysis ```bash kdm analyze kdm analyze --explain --backend ollama -kdm analyze -n default --explain --anonymize +kdm analyze deployment/checkout-api --multi-agent --council-timeout 180000 +kdm analyze -n default --explain --anonymize --export-audit ./incident.md ``` -Runs dynamic configuration diagnostics on cluster workloads and requests AI-powered troubleshooting advice (optionally anonymizing resource names). +Runs 22 built-in Kubernetes analyzers (plus `KEDA`, `Kyverno`, `Prometheus`, and custom analyzers) and optionally escalates to single-provider AI explanations or the local Python + Ollama Multi-Agent Council with automatic deterministic fallback. ### AI Providers (Auth) @@ -169,39 +175,36 @@ Use `kdm config setup` to configure notifications interactively. ## How KDM Works -KDM is a Node.js and TypeScript CLI. It uses a command-based architecture where each top-level feature is registered as a Commander command. +KDM combines a **TypeScript / Node.js CLI and React Ink TUI** with a **Python + Ollama multi-agent council**, **deterministic rule engine**, **declarative cluster tools**, and **local codebase investigation**. + +For full architecture diagrams, subsystem contracts, and data flows, see [How KDM Works: Architecture & Internals](architecture.md) and the [CLI Command Reference](commands.md). At a high level: ```text -User runs kdm command +User runs `kdm` (TTY dashboard, `copilot`, `analyze`, `watch`, `serve`, ...) | v -Commander parses command and flags +Commander parses command & flags -> Probes Docker / Kubernetes / Minikube | - v -KDM calls Docker, Kubernetes, or Minikube modules - | - v -Data is normalized into tables, health output, logs, or dashboard state + +--> Live TUI Dashboards / CopilotShell (Ink + React) + | - Slash commands, log streaming, declarative tools & SafetyPrompt [y/N] | - v -Alerts may be triggered for failure states + +--> Analysis Engine (22 K8s Analyzers + Integrations + Custom Analyzers) + | + v + 4-Tier Degradation Ladder (`--multi-agent`) or Single AI Provider + 1. Python Multi-Agent Council (agents/council.py -> Ollama) + 2. Deterministic Rule Engine (OOMKilled, ImagePull, CrashLoop, Probe, Scheduling) + 3. Configured Cloud AI Client (OpenAI, Gemini @google/genai, Anthropic, ...) + 4. Raw Kubernetes Event/Log Summary -> Optional Audit Export & Alerts ``` ## Main Internal Components ### CLI Layer -KDM uses Commander.js to define commands such as: - -- `show` -- `health` -- `watch` -- `logs` -- `config` - -The CLI entrypoint registers these commands and handles the default no-argument dashboard. +KDM uses Commander.js to register 12 command groups: `show`, `health`, `watch`, `logs`, `config`, `analyze`, `filters`, `auth`, `cache`, `serve`, `custom-analyzer`, and `copilot`. In an interactive TTY without arguments, `kdm` opens `InitialDashboard`; in non-TTY/CI environments, it prints runtime connection probes and CLI help. ### Docker Layer @@ -260,9 +263,12 @@ Config locations: | Linux | `~/.config/kdm-cli` | | Windows | `%APPDATA%\kdm-cli` | -### AI Integration & Client Layer +### AI Integration & Agent Subsystem -Supports dynamic instantiation of AI clients (`openai`, `anthropic`, `google-gemini`, `google-vertex`, `ollama`, `ibm-watsonx`, `oci-genai`, etc.) based on registered credentials and settings. Handles prompts construction, system guidelines, and localization/language requests. +- **AI Client Factory (`src/ai/factory.ts`)**: Instantiates 14 configured backends (including `GoogleGenAIClient` powered by `@google/genai` and `GeminiSession` multi-turn thought-signature preservation, plus OpenAI, Anthropic, Ollama, Vertex AI, Bedrock, Azure OpenAI, Groq, WatsonX, OCI GenAI, Cohere, Hugging Face, Custom REST, and `noop`). +- **Declarative Tool Registry & Safety Gate (`src/tools/`, `src/remediation/`)**: Exposes 13 Zod-validated Kubernetes & Docker tools categorized as `read_only` or `mutating`. Any cluster mutation passes through an explicit `[y/N]` `SafetyPrompt` and executes via parameterized `execFile` with an immutable audit trail. +- **Agent Delegation & Investigation (`src/agent/`)**: `AgentRegistry` and `AgentTool` (`delegate_to_agent`, `maxDepth = 2`) support subagent workflows such as `CodebaseInvestigatorAgent`, which cross-checks local Kubernetes YAML manifests and JVM `-Xmx` heap flags against live cluster memory limits and Secret keys. +- **Python Council Bridge (`src/agent/python-bridge.ts`)**: Runs packaged `agents/council.py` with role-specific Ollama models (`config/agents.yaml`), streaming real-time NDJSON progress into the TUI with a 4-tier deterministic/cloud fallback ladder. ### Cache Layer @@ -301,10 +307,11 @@ This avoids storing plaintext SMTP credentials in local config. KDM requires: -- Node.js 18 or newer. +- Node.js 20 or newer (`>= 20`). - npm or another compatible Node package manager. - Docker Desktop or Docker Engine for Docker features. -- Kubernetes access for Kubernetes features. +- Kubernetes cluster access (`~/.kube/config`) for Kubernetes features. +- Optional: Python 3 + Ollama (`ollama>=0.4.0`) for local `--multi-agent` council execution. - Optional: Minikube for local Kubernetes status checks. Check Node: @@ -708,9 +715,9 @@ kdm logs ## Future Direction -With the implementation of the Kubernetes diagnostic analyzer engine, multi-backend AI explanations, configuration caching, custom resource analyzers, and MCP server integrations, KDM has evolved into a fully-featured AI-assisted Kubernetes and Docker troubleshooting toolkit. +With 22 built-in Kubernetes analyzers, the interactive AI Copilot shell, `CodebaseInvestigatorAgent`, multi-backend AI explanations, declarative cluster tools with human-gated remediation, explanation caching, custom analyzers, and HTTP/MCP server modes, KDM covers end-to-end terminal troubleshooting for Kubernetes and Docker. -Future enhancements include: -- Interactive TUI-based troubleshooting triggers directly inside watch mode. -- Autopilot cluster remediation suggestions. -- Multi-cluster remote monitoring dashboard aggregation. +Planned areas of exploration include: +- Deeper automated post-remediation rollout verification loops. +- Expanded integration analyzers for additional ecosystem CRDs. +- Multi-cluster context aggregation views. diff --git a/kdm_multi_agent_detailed_implementation_plan (1).md b/docs/plans/multi-agent-implementation-plan.md similarity index 100% rename from kdm_multi_agent_detailed_implementation_plan (1).md rename to docs/plans/multi-agent-implementation-plan.md diff --git a/docs/v1.1.3.md b/docs/releases/v1.1.3.md similarity index 100% rename from docs/v1.1.3.md rename to docs/releases/v1.1.3.md diff --git a/docs/v1.2.0.md b/docs/releases/v1.2.0.md similarity index 100% rename from docs/v1.2.0.md rename to docs/releases/v1.2.0.md diff --git a/docs/v1.2.1.md b/docs/releases/v1.2.1.md similarity index 100% rename from docs/v1.2.1.md rename to docs/releases/v1.2.1.md diff --git a/docs/v1.2.2.md b/docs/releases/v1.2.2.md similarity index 100% rename from docs/v1.2.2.md rename to docs/releases/v1.2.2.md diff --git a/docs/v1.2.3.md b/docs/releases/v1.2.3.md similarity index 100% rename from docs/v1.2.3.md rename to docs/releases/v1.2.3.md diff --git a/docs/v1.2.5.md b/docs/releases/v1.2.5.md similarity index 100% rename from docs/v1.2.5.md rename to docs/releases/v1.2.5.md diff --git a/docs/v2.0.0.md b/docs/releases/v2.0.0.md similarity index 100% rename from docs/v2.0.0.md rename to docs/releases/v2.0.0.md diff --git a/docs/v2.0.1.md b/docs/releases/v2.0.1.md similarity index 100% rename from docs/v2.0.1.md rename to docs/releases/v2.0.1.md diff --git a/src/analysis/__tests__/analysis-baseline.test.ts b/src/__tests__/analysis/analysis-baseline.test.ts similarity index 98% rename from src/analysis/__tests__/analysis-baseline.test.ts rename to src/__tests__/analysis/analysis-baseline.test.ts index 35462691..e75a0a37 100644 --- a/src/analysis/__tests__/analysis-baseline.test.ts +++ b/src/__tests__/analysis/analysis-baseline.test.ts @@ -10,10 +10,10 @@ * @module analysis-baseline.test */ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; -import { runAnalysis, explainSingleResult, resolveBackend } from '../analysis'; +import { runAnalysis, explainSingleResult, resolveBackend } from '../../analysis/analysis'; import { clearConfig, setAIConfig } from '../../config/store'; import { isPythonAgentAvailable, runAgentCouncilProcess, runPythonAgentCouncil } from '../../agent/python-bridge'; -import * as probesModule from '../probes'; +import * as probesModule from '../../analysis/probes'; import { registry, PodAnalyzer, @@ -22,7 +22,7 @@ import { PersistentVolumeClaimAnalyzer, NodeAnalyzer, } from '../../analyzers'; -import type { AnalysisOptions } from '../types'; +import type { AnalysisOptions } from '../../analysis/types'; import type { Analyzer, AnalyzerResult } from '../../analyzers/types'; import { OOM_KILLED_FIXTURE, diff --git a/src/analysis/__tests__/baseline-fixtures.ts b/src/__tests__/analysis/baseline-fixtures.ts similarity index 99% rename from src/analysis/__tests__/baseline-fixtures.ts rename to src/__tests__/analysis/baseline-fixtures.ts index 9f53584b..13722f7a 100644 --- a/src/analysis/__tests__/baseline-fixtures.ts +++ b/src/__tests__/analysis/baseline-fixtures.ts @@ -8,7 +8,7 @@ * @module baseline-fixtures */ import type { AnalyzerResult } from '../../analyzers/types'; -import type { LegacyAnalysisResult } from '../types'; +import type { LegacyAnalysisResult } from '../../analysis/types'; // ─── OOMKilled (exit code 137) ────────────────────────────────────────────── diff --git a/src/__tests__/phase8-analyzers.test.ts b/src/__tests__/phase8-analyzers.test.ts index 0719456a..4e27d9ec 100644 --- a/src/__tests__/phase8-analyzers.test.ts +++ b/src/__tests__/phase8-analyzers.test.ts @@ -3,7 +3,7 @@ * Covers failure detection, healthy-resource green paths, API failure propagation, * result metadata (kind/name/namespace), and edge cases for every analyzer. * - * Follows coding_style.md rules: + * Follows docs/guides/coding-style.md rules: * - it.each parameterized testing to avoid structural duplication * - JSDoc coverage on test utilities and describe blocks */