From 348a95262c15a2c712aaf8b201d16309d376fa2b Mon Sep 17 00:00:00 2001 From: donpetry-bot <281750570+donpetry-bot@users.noreply.github.com> Date: Sat, 13 Jun 2026 13:15:50 +0000 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20implement=20issue=20#638=20?= =?UTF-8?q?=E2=80=94=20Feature=20Ideation=20doesn't=20enhance=20human=20id?= =?UTF-8?q?eas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/idea-enhancer.yml | 149 ++++++++++++++++++ .github/workflows/lint.yml | 1 + .../idea-to-initiative-pipeline.md | 16 ++ scripts/idea-enhancer/gather-candidates.sh | 84 ++++++++++ scripts/idea-enhancer/post-enhancement.sh | 64 ++++++++ tests/test_idea_enhancer.bats | 142 +++++++++++++++++ 6 files changed, 456 insertions(+) create mode 100644 .github/workflows/idea-enhancer.yml create mode 100755 scripts/idea-enhancer/gather-candidates.sh create mode 100755 scripts/idea-enhancer/post-enhancement.sh create mode 100644 tests/test_idea_enhancer.bats diff --git a/.github/workflows/idea-enhancer.yml b/.github/workflows/idea-enhancer.yml new file mode 100644 index 000000000..dc4265169 --- /dev/null +++ b/.github/workflows/idea-enhancer.yml @@ -0,0 +1,149 @@ +# ───────────────────────────────────────────────────────────────────────────── +# Idea Enhancer — enriches human-authored Ideas Discussions +# +# When a human opens an Ideas Discussion (or periodically on a schedule), an +# analyst pass reads the idea, researches it, and posts ONE enhancement comment: +# clarifying framing, market/competitive context, a sharper problem statement, +# impact/effort, and concrete acceptance criteria. A hidden marker is appended so +# the same idea is never enhanced twice (idempotent across runs). +# +# This complements feature-ideation.yml (which generates NEW idea Discussions): +# that pipeline never enriches human-added items, which is what this closes. +# +# Pipeline position: +# ★ human opens an Ideas Discussion ──> THIS (enhance comment) ──┐ +# feature-ideation (new Ideas) ─────────────────────────────┤ +# -> idea-triage (shortlist) -> ★ human adds idea:approved │ +# -> initiative-planner -> ★ human adds initiative:auto │ +# -> initiative-driver -> dev-lead <───────────────────────────────┘ +# +# Org-private automation (like idea-triage.yml / initiative-planner.yml) — NOT a +# thin caller stub. The tooling lives in scripts/idea-enhancer/ and is unit-tested +# (tests/test_idea_enhancer.bats). +# ───────────────────────────────────────────────────────────────────────────── +name: Idea Enhancer + +on: + discussion: + types: [created] + schedule: + - cron: '0 7 * * 3' # Wednesday 07:00 UTC — safety net between ideation (Fri) and triage (Mon) + workflow_dispatch: + inputs: + dry_run: + description: 'Log intended enhancement comments to an artifact instead of posting them' + required: false + default: false + type: boolean + model: + description: 'Anthropic model id for the enhancement pass' + required: false + default: 'claude-sonnet-4-6' + type: string + +permissions: {} + +concurrency: + group: idea-enhancer + cancel-in-progress: false + +jobs: + enhance: + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read # checkout + discussions: write # read the Ideas backlog + post enhancement comments + id-token: write # claude-code-action OIDC + env: + REPO: ${{ github.repository }} + DRY_RUN: ${{ inputs.dry_run && '1' || '0' }} + # Empty for schedule/dispatch (scan all); the created discussion's number + # for the `discussion` trigger (enhance just that one, immediately). + DISCUSSION_NUMBER: ${{ github.event.discussion.number }} + steps: + - name: Checkout agent repo + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 + with: + persist-credentials: false + + - name: Set temp paths + run: | + echo "DRY_RUN_LOG=${{ runner.temp }}/dry-run.jsonl" >> "$GITHUB_ENV" + echo "CONTEXT_PATH=${{ runner.temp }}/candidates.json" >> "$GITHUB_ENV" + + - name: Gather enhancement candidates + id: gather + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + bash scripts/idea-enhancer/gather-candidates.sh + echo "count=$(jq '.candidates | length' "$CONTEXT_PATH")" >> "$GITHUB_OUTPUT" + + - name: Enhance each candidate idea + if: ${{ steps.gather.outputs.count != '0' }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + uses: anthropics/claude-code-action@787c5a0ce96a9a6cfb050ea0c8f4c05f2447c251 # v1.0.133 + with: + claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }} + github_token: ${{ secrets.GITHUB_TOKEN }} + claude_args: | + --model ${{ inputs.model || 'claude-sonnet-4-6' }} + --allowedTools Bash,Read,Glob,Grep + prompt: | + You are the **idea-enhancer analyst** for the ${{ github.repository }} repo. + Your job: for each candidate Idea Discussion, post exactly ONE comment that + makes the idea more actionable. You do NOT approve, label, or promote + anything — that stays with a human and the triage/planner pipeline. + + ## Input + + The candidate set is at `$CONTEXT_PATH` (JSON): `repo`, the idempotency + `marker`, and `candidates[]` (each with number, title, url, author, + timestamps, comment count, labels, and an excerpt of the idea body). Read it: + + ```bash + cat "$CONTEXT_PATH" | jq . + ``` + + Every candidate is an open, human-authored Ideas Discussion that has not + yet been enhanced. If `candidates` is empty, do nothing and stop. + + Use Read/Grep/Glob on the checkout (`AGENTS.md`, `docs/`, `scripts/`, + `.github/workflows/`) and the feature-ideator profile + (`agents/feature-ideator.md`) to ground each enhancement in what this repo + actually does. Favor ideas that leverage existing infrastructure. + + ## For each candidate + + Draft a concise enhancement comment (GitHub Markdown) to a temp file, then + post it. Structure the comment as: + - **Sharpened problem & goal** — restate the idea crisply; note any + ambiguity worth the author clarifying. + - **Context** — relevant prior art in this repo, related initiatives, or + market/competitive signal (cite sources if you researched the web). + - **Impact / Effort** — HIGH/MEDIUM/LOW and S/M/L/XL, with one line of why. + - **Suggested acceptance criteria** — 2–5 specific, testable bullets. + - Keep it constructive and brief; you are augmenting a human's idea, not + replacing it. + + Then post it (the script appends the idempotency marker for you; honors + `$DRY_RUN`): + + ```bash + REPO="${{ github.repository }}" DISCUSSION_NUMBER="" \ + BODY_PATH="" \ + bash scripts/idea-enhancer/post-enhancement.sh + ``` + + Process every candidate. Finish with a 2–3 line summary of how many ideas + you enhanced. + + - name: Upload dry-run enhancement log + if: ${{ env.DRY_RUN == '1' }} + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: idea-enhancer-dry-run + path: ${{ runner.temp }}/dry-run.jsonl + if-no-files-found: warn diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1e2b982a5..2b4f86d0d 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -53,6 +53,7 @@ jobs: tests/test_gitleaks_config.bats \ tests/test_initiative_planner.bats \ tests/test_idea_triage.bats \ + tests/test_idea_enhancer.bats \ tests/test_oversized_pr.bats \ tests/test_apply_repo_settings.bats diff --git a/docs/initiatives/idea-to-initiative-pipeline.md b/docs/initiatives/idea-to-initiative-pipeline.md index 81e2ce9f3..54f143e06 100644 --- a/docs/initiatives/idea-to-initiative-pipeline.md +++ b/docs/initiatives/idea-to-initiative-pipeline.md @@ -45,6 +45,22 @@ Both are **org-private automation** (like `initiative-driver.yml`), not thin caller stubs. Tooling and the vendored BMAD personas live in this repo and are unit-tested. +### `idea-enhancer.yml` — enrich human-authored ideas +- Trigger: a Discussion is **created** (enhance that one immediately), a weekly + **schedule** (safety-net sweep of the backlog), or manual `workflow_dispatch` + (`dry_run` logs intended comments to an artifact). +- Closes the gap where `feature-ideation` only ever generates *new* idea + Discussions and never enriches the ones a human adds. For each open, + human-authored, not-yet-enhanced **Ideas** Discussion it posts **one** comment + (sharpened problem/goal, repo + market context, impact/effort, suggested + acceptance criteria). Bot-authored ideas are skipped — they are already + AI-generated. +- Idempotent: each enhancement comment carries a hidden marker + (``); a discussion that already has one is never + re-enhanced. It never approves, labels, or promotes — that stays with triage + and the human gates. +- Tooling: `scripts/idea-enhancer/{gather-candidates,post-enhancement}.sh`. + ### `idea-triage.yml` — weekly ripeness shortlist - Scans every open **Ideas** Discussion, scores each **Ripe / Soon / Not yet** on evidence, prerequisites, alignment, coverage, and freshness. diff --git a/scripts/idea-enhancer/gather-candidates.sh b/scripts/idea-enhancer/gather-candidates.sh new file mode 100755 index 000000000..dde3639eb --- /dev/null +++ b/scripts/idea-enhancer/gather-candidates.sh @@ -0,0 +1,84 @@ +#!/usr/bin/env bash +# gather-candidates.sh — find open "Ideas" Discussions that are worth an +# automated enhancement pass: human-authored, not closed, and not yet enhanced +# (i.e. no prior comment carrying the idempotency marker). +# +# Emits $CONTEXT_PATH (JSON): { repo, marker, candidates: [ {number, title, url, +# author, createdAt, updatedAt, comments, labels, excerpt}, ... ] }. The enhancer +# agent reads it and posts one enhancement comment per candidate. +# +# Bot-authored ideas are skipped: those come from the feature-ideation run and +# are already AI-generated, so the human-added items are the ones that benefit. +# +# Env: +# REPO owner/repo (required) +# CONTEXT_PATH output JSON path (required) +# DISCUSSION_NUMBER restrict to a single discussion (optional; used by the +# `discussion: created` trigger so a new human idea is +# enhanced immediately) +# ENHANCER_MARKER idempotency marker (default below; must match post-enhancement.sh) +# GH_TOKEN token for gh +set -euo pipefail + +REPO="${REPO:?REPO required}" +CONTEXT_PATH="${CONTEXT_PATH:?CONTEXT_PATH required}" +MARKER="${ENHANCER_MARKER:-}" +ONLY="${DISCUSSION_NUMBER:-}" + +owner="${REPO%/*}" +name="${REPO#*/}" + +all="[]" +cursor="" +has_next="true" + +while [[ "$has_next" = "true" ]]; do + extra_args=() + [[ -n "$cursor" ]] && extra_args+=(-F after="$cursor") + + page="$(gh api graphql \ + -f query=' + query($owner:String!,$name:String!,$after:String){ + repository(owner:$owner,name:$name){ + discussions(first:50, after:$after, orderBy:{field:UPDATED_AT, direction:DESC}){ + pageInfo{ hasNextPage endCursor } + nodes{ + number title url createdAt updatedAt closed + category{ name } + author{ login __typename } + labels(first:20){ nodes{ name } } + comments(first:100){ totalCount nodes{ body author{ login } } } + bodyText + } + } + } + }' \ + -F owner="$owner" -F name="$name" "${extra_args[@]}")" + + nodes="$(printf '%s' "$page" | jq --arg marker "$MARKER" ' + [.data.repository.discussions.nodes[] + | select(.category.name == "Ideas") + | select(.closed == false) + | select((.author.__typename // "User") != "Bot") + | select(((.author.login) // "") | endswith("[bot]") | not) + | select([.comments.nodes[]? | select((.body // "") | contains($marker))] | length == 0) + | {number, title, url, + author: ((.author.login) // "unknown"), + createdAt, updatedAt, + comments: .comments.totalCount, + labels: [.labels.nodes[].name], + excerpt: (.bodyText[0:600])}]')" + all="$(jq -n --argjson a "$all" --argjson b "$nodes" '$a + $b')" + + has_next="$(printf '%s' "$page" | jq -r '.data.repository.discussions.pageInfo.hasNextPage')" + cursor="$(printf '%s' "$page" | jq -r '.data.repository.discussions.pageInfo.endCursor // ""')" +done + +if [[ -n "$ONLY" ]]; then + all="$(jq --argjson n "$ONLY" '[.[] | select(.number == $n)]' <<<"$all")" +fi + +jq -n --arg repo "$REPO" --arg marker "$MARKER" --argjson candidates "$all" \ + '{repo: $repo, marker: $marker, candidates: $candidates}' >"$CONTEXT_PATH" + +echo "context written to ${CONTEXT_PATH} (candidates=$(printf '%s' "$all" | jq 'length'))" diff --git a/scripts/idea-enhancer/post-enhancement.sh b/scripts/idea-enhancer/post-enhancement.sh new file mode 100755 index 000000000..39e2dfadd --- /dev/null +++ b/scripts/idea-enhancer/post-enhancement.sh @@ -0,0 +1,64 @@ +#!/usr/bin/env bash +# post-enhancement.sh — post a single enhancement comment to an Ideas Discussion. +# Appends an idempotency marker so a later gather-candidates pass skips the idea +# (one enhancement per idea, re-run safe). Honors DRY_RUN. +# +# Env: +# REPO owner/repo (required) +# DISCUSSION_NUMBER discussion to comment on (required) +# BODY_PATH file holding the enhancement markdown (required) +# ENHANCER_MARKER idempotency marker (default below; must match gather-candidates.sh) +# DRY_RUN / DRY_RUN_LOG "1" => log the intended comment instead of posting +# GH_TOKEN token for gh +set -euo pipefail + +REPO="${REPO:?REPO required}" +DISCUSSION_NUMBER="${DISCUSSION_NUMBER:?DISCUSSION_NUMBER required}" +BODY_PATH="${BODY_PATH:?BODY_PATH required}" +MARKER="${ENHANCER_MARKER:-}" + +[[ -s "$BODY_PATH" ]] || { echo "::error::enhancement body '$BODY_PATH' missing or empty" >&2; exit 1; } + +owner="${REPO%/*}" +name="${REPO#*/}" + +# Final comment = the drafted enhancement + the marker on its own line. +body="$(printf '%s\n\n%s\n' "$(cat "$BODY_PATH")" "$MARKER")" + +_is_dry_run() { [[ "${DRY_RUN:-0}" = "1" ]]; } + +if _is_dry_run; then + jq -nc --arg op post_enhancement --argjson number "$DISCUSSION_NUMBER" \ + --arg body "$body" '{op:$op, number:$number, body_len:($body|length)}' \ + >>"${DRY_RUN_LOG:-./dry-run.jsonl}" + echo "[dry-run] would comment on discussion #${DISCUSSION_NUMBER}" + exit 0 +fi + +# Resolve the discussion's node id, then add the comment via GraphQL. +discussion_id="$(gh api graphql \ + -f query=' + query($owner:String!,$name:String!,$number:Int!){ + repository(owner:$owner,name:$name){ + discussion(number:$number){ id } + } + }' \ + -F owner="$owner" -F name="$name" -F number="$DISCUSSION_NUMBER" \ + --jq '.data.repository.discussion.id')" + +if [[ -z "$discussion_id" || "$discussion_id" = "null" ]]; then + echo "::error::could not resolve discussion #${DISCUSSION_NUMBER} in ${REPO}" >&2 + exit 1 +fi + +gh api graphql \ + -f query=' + mutation($discussionId:ID!,$body:String!){ + addDiscussionComment(input:{discussionId:$discussionId, body:$body}){ + comment{ url } + } + }' \ + -F discussionId="$discussion_id" -F body="$body" \ + --jq '.data.addDiscussionComment.comment.url' + +echo "enhanced discussion #${DISCUSSION_NUMBER}" diff --git a/tests/test_idea_enhancer.bats b/tests/test_idea_enhancer.bats new file mode 100644 index 000000000..5d97c28c4 --- /dev/null +++ b/tests/test_idea_enhancer.bats @@ -0,0 +1,142 @@ +#!/usr/bin/env bats +# Tests for the idea-enhancer tooling: +# - scripts/idea-enhancer/gather-candidates.sh (mocks `gh` GraphQL — candidate filtering) +# - scripts/idea-enhancer/post-enhancement.sh (DRY_RUN path only — no network) +# +# Run with: bats tests/test_idea_enhancer.bats + +setup() { + ROOT="$(cd "$BATS_TEST_DIRNAME/.." && pwd)" + ENH_DIR="$ROOT/scripts/idea-enhancer" + TMP="$(mktemp -d)" + MOCK_BIN="$TMP/bin" + mkdir -p "$MOCK_BIN" + CONTEXT="$TMP/context.json" + LOG="$TMP/dry.jsonl" + export PATH="$MOCK_BIN:$PATH" + export GH_TOKEN="tok" +} + +teardown() { rm -rf "$TMP"; } + +# A `gh` mock that returns a single page of Ideas discussions covering every +# filter branch the gather script must apply. Args are ignored (one page only). +_install_gh_mock() { + cat > "$MOCK_BIN/gh" <<'EOF' +#!/usr/bin/env bash +cat <<'JSON' +{ + "data": { "repository": { "discussions": { + "pageInfo": { "hasNextPage": false, "endCursor": "" }, + "nodes": [ + { "number": 101, "title": "Human idea A", "url": "u/101", + "createdAt": "2026-06-01T00:00:00Z", "updatedAt": "2026-06-01T00:00:00Z", + "closed": false, "category": { "name": "Ideas" }, + "author": { "login": "alice", "__typename": "User" }, + "labels": { "nodes": [] }, + "comments": { "totalCount": 0, "nodes": [] }, + "bodyText": "A plain human idea." }, + { "number": 102, "title": "Bot idea", "url": "u/102", + "createdAt": "2026-06-01T00:00:00Z", "updatedAt": "2026-06-01T00:00:00Z", + "closed": false, "category": { "name": "Ideas" }, + "author": { "login": "ideation-bot", "__typename": "Bot" }, + "labels": { "nodes": [] }, + "comments": { "totalCount": 0, "nodes": [] }, + "bodyText": "Generated by the ideation bot." }, + { "number": 103, "title": "Already enhanced idea", "url": "u/103", + "createdAt": "2026-06-01T00:00:00Z", "updatedAt": "2026-06-01T00:00:00Z", + "closed": false, "category": { "name": "Ideas" }, + "author": { "login": "bob", "__typename": "User" }, + "labels": { "nodes": [] }, + "comments": { "totalCount": 1, "nodes": [ + { "body": "Here is an enhancement.\n\n", + "author": { "login": "github-actions" } } ] }, + "bodyText": "An idea that already has an enhancement comment." }, + { "number": 104, "title": "Not an idea", "url": "u/104", + "createdAt": "2026-06-01T00:00:00Z", "updatedAt": "2026-06-01T00:00:00Z", + "closed": false, "category": { "name": "General" }, + "author": { "login": "carol", "__typename": "User" }, + "labels": { "nodes": [] }, + "comments": { "totalCount": 0, "nodes": [] }, + "bodyText": "A general discussion, not an idea." }, + { "number": 105, "title": "Closed idea", "url": "u/105", + "createdAt": "2026-06-01T00:00:00Z", "updatedAt": "2026-06-01T00:00:00Z", + "closed": true, "category": { "name": "Ideas" }, + "author": { "login": "dave", "__typename": "User" }, + "labels": { "nodes": [] }, + "comments": { "totalCount": 0, "nodes": [] }, + "bodyText": "A closed idea." }, + { "number": 106, "title": "Human idea B", "url": "u/106", + "createdAt": "2026-06-02T00:00:00Z", "updatedAt": "2026-06-02T00:00:00Z", + "closed": false, "category": { "name": "Ideas" }, + "author": { "login": "erin", "__typename": "User" }, + "labels": { "nodes": [] }, + "comments": { "totalCount": 0, "nodes": [] }, + "bodyText": "Another plain human idea." } + ] + } } } +} +JSON +EOF + chmod +x "$MOCK_BIN/gh" +} + +# ── gather-candidates: filtering ───────────────────────────────────────────── + +@test "gather-candidates keeps only fresh, open, human-authored Ideas" { + _install_gh_mock + REPO="owner/repo" CONTEXT_PATH="$CONTEXT" run bash "$ENH_DIR/gather-candidates.sh" + [ "$status" -eq 0 ] + # Only #101 and #106 survive (bot/enhanced/non-Ideas/closed are dropped). + [ "$(jq '.candidates | length' "$CONTEXT")" -eq 2 ] + nums="$(jq -c '[.candidates[].number] | sort' "$CONTEXT")" + [ "$nums" = "[101,106]" ] +} + +@test "gather-candidates records repo and marker in the context" { + _install_gh_mock + REPO="owner/repo" CONTEXT_PATH="$CONTEXT" run bash "$ENH_DIR/gather-candidates.sh" + [ "$status" -eq 0 ] + [ "$(jq -r '.repo' "$CONTEXT")" = "owner/repo" ] + [ "$(jq -r '.marker' "$CONTEXT")" = "" ] +} + +@test "gather-candidates scopes to DISCUSSION_NUMBER when set" { + _install_gh_mock + REPO="owner/repo" CONTEXT_PATH="$CONTEXT" DISCUSSION_NUMBER="106" \ + run bash "$ENH_DIR/gather-candidates.sh" + [ "$status" -eq 0 ] + [ "$(jq '.candidates | length' "$CONTEXT")" -eq 1 ] + [ "$(jq -r '.candidates[0].number' "$CONTEXT")" = "106" ] +} + +@test "gather-candidates fails when REPO is missing" { + _install_gh_mock + # REPO can leak in from the surrounding environment; unset it explicitly so + # the required-var guard is what's under test. + CONTEXT_PATH="$CONTEXT" run env -u REPO bash "$ENH_DIR/gather-candidates.sh" + [ "$status" -ne 0 ] +} + +# ── post-enhancement: DRY_RUN path ─────────────────────────────────────────── + +@test "post-enhancement (DRY_RUN) logs the comment with the marker appended" { + BODY="$TMP/body.md" + printf 'A concrete enhancement to your idea.' > "$BODY" + body_len=$(wc -c < "$BODY") + DRY_RUN=1 DRY_RUN_LOG="$LOG" REPO="owner/repo" DISCUSSION_NUMBER="101" \ + BODY_PATH="$BODY" run bash "$ENH_DIR/post-enhancement.sh" + [ "$status" -eq 0 ] + grep -q '"op":"post_enhancement"' "$LOG" + [ "$(jq -r 'select(.op=="post_enhancement") | .number' "$LOG")" = "101" ] + # The posted body includes the original text plus the appended marker, so it + # must be strictly longer than the raw body file. + [ "$(jq -r 'select(.op=="post_enhancement") | .body_len' "$LOG")" -gt "$body_len" ] +} + +@test "post-enhancement fails on a missing/empty body" { + DRY_RUN=1 DRY_RUN_LOG="$LOG" REPO="owner/repo" DISCUSSION_NUMBER="101" \ + BODY_PATH="$TMP/nope.md" run bash "$ENH_DIR/post-enhancement.sh" + [ "$status" -ne 0 ] + [[ "$output" == *"missing or empty"* ]] +} From 5dd58937a09badffcfd11ed849fa3b9e9959c0dc Mon Sep 17 00:00:00 2001 From: donpetry-bot <281750570+donpetry-bot@users.noreply.github.com> Date: Sat, 13 Jun 2026 13:25:09 +0000 Subject: [PATCH 2/2] fix(reviews): address review comments [skip ci-relay] --- scripts/idea-enhancer/gather-candidates.sh | 86 +++++++++++++++------- scripts/idea-enhancer/post-enhancement.sh | 4 + tests/test_idea_enhancer.bats | 59 ++++++++++++++- 3 files changed, 120 insertions(+), 29 deletions(-) diff --git a/scripts/idea-enhancer/gather-candidates.sh b/scripts/idea-enhancer/gather-candidates.sh index dde3639eb..6b4093e6b 100755 --- a/scripts/idea-enhancer/gather-candidates.sh +++ b/scripts/idea-enhancer/gather-candidates.sh @@ -21,6 +21,10 @@ set -euo pipefail REPO="${REPO:?REPO required}" +if [[ "$REPO" != */* ]]; then + echo "::error::REPO must be in 'owner/repo' format" >&2 + exit 1 +fi CONTEXT_PATH="${CONTEXT_PATH:?CONTEXT_PATH required}" MARKER="${ENHANCER_MARKER:-}" ONLY="${DISCUSSION_NUMBER:-}" @@ -29,34 +33,27 @@ owner="${REPO%/*}" name="${REPO#*/}" all="[]" -cursor="" -has_next="true" - -while [[ "$has_next" = "true" ]]; do - extra_args=() - [[ -n "$cursor" ]] && extra_args+=(-F after="$cursor") +if [[ -n "$ONLY" ]]; then page="$(gh api graphql \ -f query=' - query($owner:String!,$name:String!,$after:String){ + query($owner:String!,$name:String!,$number:Int!){ repository(owner:$owner,name:$name){ - discussions(first:50, after:$after, orderBy:{field:UPDATED_AT, direction:DESC}){ - pageInfo{ hasNextPage endCursor } - nodes{ - number title url createdAt updatedAt closed - category{ name } - author{ login __typename } - labels(first:20){ nodes{ name } } - comments(first:100){ totalCount nodes{ body author{ login } } } - bodyText - } + discussion(number:$number){ + number title url createdAt updatedAt closed + category{ name } + author{ login __typename } + labels(first:20){ nodes{ name } } + comments(first:100){ totalCount nodes{ body author{ login } } } + bodyText } } }' \ - -F owner="$owner" -F name="$name" "${extra_args[@]}")" + -F owner="$owner" -F name="$name" -F number="$ONLY")" - nodes="$(printf '%s' "$page" | jq --arg marker "$MARKER" ' - [.data.repository.discussions.nodes[] + all="$(printf '%s' "$page" | jq --arg marker "$MARKER" ' + [.data.repository.discussion + | select(. != null) | select(.category.name == "Ideas") | select(.closed == false) | select((.author.__typename // "User") != "Bot") @@ -68,14 +65,51 @@ while [[ "$has_next" = "true" ]]; do comments: .comments.totalCount, labels: [.labels.nodes[].name], excerpt: (.bodyText[0:600])}]')" - all="$(jq -n --argjson a "$all" --argjson b "$nodes" '$a + $b')" +else + cursor="" + has_next="true" - has_next="$(printf '%s' "$page" | jq -r '.data.repository.discussions.pageInfo.hasNextPage')" - cursor="$(printf '%s' "$page" | jq -r '.data.repository.discussions.pageInfo.endCursor // ""')" -done + while [[ "$has_next" = "true" ]]; do + extra_args=() + [[ -n "$cursor" ]] && extra_args+=(-F after="$cursor") -if [[ -n "$ONLY" ]]; then - all="$(jq --argjson n "$ONLY" '[.[] | select(.number == $n)]' <<<"$all")" + page="$(gh api graphql \ + -f query=' + query($owner:String!,$name:String!,$after:String){ + repository(owner:$owner,name:$name){ + discussions(first:50, after:$after, orderBy:{field:UPDATED_AT, direction:DESC}){ + pageInfo{ hasNextPage endCursor } + nodes{ + number title url createdAt updatedAt closed + category{ name } + author{ login __typename } + labels(first:20){ nodes{ name } } + comments(first:100){ totalCount nodes{ body author{ login } } } + bodyText + } + } + } + }' \ + -F owner="$owner" -F name="$name" "${extra_args[@]}")" + + nodes="$(printf '%s' "$page" | jq --arg marker "$MARKER" ' + [.data.repository.discussions.nodes[] + | select(.category.name == "Ideas") + | select(.closed == false) + | select((.author.__typename // "User") != "Bot") + | select(((.author.login) // "") | endswith("[bot]") | not) + | select([.comments.nodes[]? | select((.body // "") | contains($marker))] | length == 0) + | {number, title, url, + author: ((.author.login) // "unknown"), + createdAt, updatedAt, + comments: .comments.totalCount, + labels: [.labels.nodes[].name], + excerpt: (.bodyText[0:600])}]')" + all="$(jq -n --argjson a "$all" --argjson b "$nodes" '$a + $b')" + + has_next="$(printf '%s' "$page" | jq -r '.data.repository.discussions.pageInfo.hasNextPage')" + cursor="$(printf '%s' "$page" | jq -r '.data.repository.discussions.pageInfo.endCursor // ""')" + done fi jq -n --arg repo "$REPO" --arg marker "$MARKER" --argjson candidates "$all" \ diff --git a/scripts/idea-enhancer/post-enhancement.sh b/scripts/idea-enhancer/post-enhancement.sh index 39e2dfadd..a5c0765de 100755 --- a/scripts/idea-enhancer/post-enhancement.sh +++ b/scripts/idea-enhancer/post-enhancement.sh @@ -13,6 +13,10 @@ set -euo pipefail REPO="${REPO:?REPO required}" +if [[ "$REPO" != */* ]]; then + echo "::error::REPO must be in 'owner/repo' format" >&2 + exit 1 +fi DISCUSSION_NUMBER="${DISCUSSION_NUMBER:?DISCUSSION_NUMBER required}" BODY_PATH="${BODY_PATH:?BODY_PATH required}" MARKER="${ENHANCER_MARKER:-}" diff --git a/tests/test_idea_enhancer.bats b/tests/test_idea_enhancer.bats index 5d97c28c4..28f9e192a 100644 --- a/tests/test_idea_enhancer.bats +++ b/tests/test_idea_enhancer.bats @@ -19,12 +19,48 @@ setup() { teardown() { rm -rf "$TMP"; } -# A `gh` mock that returns a single page of Ideas discussions covering every -# filter branch the gather script must apply. Args are ignored (one page only). +# A `gh` mock that handles both the single-discussion query (when DISCUSSION_NUMBER +# is set) and the paginated discussions query (bulk scan path). _install_gh_mock() { cat > "$MOCK_BIN/gh" <<'EOF' #!/usr/bin/env bash -cat <<'JSON' +if [[ "$*" == *"discussion(number:"* ]]; then + num="106" + if [[ "$*" == *"number=101"* || "$*" == *"-F number=101"* ]]; then + num="101" + fi + + if [ "$num" = "106" ]; then + cat <<'JSON' +{ + "data": { "repository": { "discussion": { + "number": 106, "title": "Human idea B", "url": "u/106", + "createdAt": "2026-06-02T00:00:00Z", "updatedAt": "2026-06-02T00:00:00Z", + "closed": false, "category": { "name": "Ideas" }, + "author": { "login": "erin", "__typename": "User" }, + "labels": { "nodes": [] }, + "comments": { "totalCount": 0, "nodes": [] }, + "bodyText": "Another plain human idea." + } } } +} +JSON + else + cat <<'JSON' +{ + "data": { "repository": { "discussion": { + "number": 101, "title": "Human idea A", "url": "u/101", + "createdAt": "2026-06-01T00:00:00Z", "updatedAt": "2026-06-01T00:00:00Z", + "closed": false, "category": { "name": "Ideas" }, + "author": { "login": "alice", "__typename": "User" }, + "labels": { "nodes": [] }, + "comments": { "totalCount": 0, "nodes": [] }, + "bodyText": "A plain human idea." + } } } +} +JSON + fi +else + cat <<'JSON' { "data": { "repository": { "discussions": { "pageInfo": { "hasNextPage": false, "endCursor": "" }, @@ -77,6 +113,7 @@ cat <<'JSON' } } } } JSON +fi EOF chmod +x "$MOCK_BIN/gh" } @@ -118,6 +155,13 @@ EOF [ "$status" -ne 0 ] } +@test "gather-candidates fails when REPO lacks a slash" { + _install_gh_mock + REPO="noslash" CONTEXT_PATH="$CONTEXT" run bash "$ENH_DIR/gather-candidates.sh" + [ "$status" -ne 0 ] + [[ "$output" == *"owner/repo"* ]] +} + # ── post-enhancement: DRY_RUN path ─────────────────────────────────────────── @test "post-enhancement (DRY_RUN) logs the comment with the marker appended" { @@ -140,3 +184,12 @@ EOF [ "$status" -ne 0 ] [[ "$output" == *"missing or empty"* ]] } + +@test "post-enhancement fails when REPO lacks a slash" { + BODY="$TMP/body.md" + printf 'A concrete enhancement.' > "$BODY" + DRY_RUN=1 DRY_RUN_LOG="$LOG" REPO="noslash" DISCUSSION_NUMBER="101" \ + BODY_PATH="$BODY" run bash "$ENH_DIR/post-enhancement.sh" + [ "$status" -ne 0 ] + [[ "$output" == *"owner/repo"* ]] +}