Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion scripts/initiative-planner/apply-plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,26 @@ epic_body="$(jq -r '.epic.body' "$PLAN_PATH")"
src="$(jq -r '.source_discussion // empty' "$PLAN_PATH")"
[ -n "$src" ] || src="$DISCUSSION_NUMBER"
if [ -n "$src" ]; then
epic_body="${epic_body}"$'\n\n'"---"$'\n'"Planned from idea discussion #${src} by the BMAD Scrum Master initiative-planner. **Inert until a maintainer adds \`initiative:auto\`.**"
# idem_key is the idempotency back-reference embedded in every epic body and
# reused verbatim as the search key by find_existing_epic.
idem_key="Planned from idea discussion #${src}"
epic_body="${epic_body}"$'\n\n'"---"$'\n'"${idem_key} by the BMAD Scrum Master initiative-planner. **Inert until a maintainer adds \`initiative:auto\`.**"

# Idempotency guard: a second dispatch/auto-trigger for the same idea must not
# create a duplicate epic + DAG. If an open initiative epic already carries the
# back-reference, this is a benign re-run — skip creation and exit 0.
existing_epic="$(find_existing_epic "$REPO" "$idem_key")"
if [ -n "$existing_epic" ]; then
echo "already planned (epic #${existing_epic}) for idea discussion #${src} — skipping creation"
if [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
{
echo "## Initiative plan skipped$(if [ "${DRY_RUN:-0}" = "1" ]; then echo " (DRY_RUN)"; fi)"
echo "- Already planned: epic #${existing_epic} for idea discussion #${src}"
echo "- No issues created (idempotency guard)."
} >>"$GITHUB_STEP_SUMMARY"
fi
exit 0
fi
fi

epic_out="$(create_issue "$REPO" "$epic_title" "$epic_body" "initiative")"
Expand Down
22 changes: 22 additions & 0 deletions scripts/initiative-planner/lib/mutations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ issue_id() {
gh api "repos/${repo}/issues/${number}" --jq '.id'
}

# find_existing_epic <repo> <back_reference>
# Idempotency guard: searches open `initiative` issues for an epic whose body
# already embeds <back_reference> (the "Planned from idea discussion #N" marker
# apply-plan.sh writes into every epic). Prints the matching epic number on
# stdout, or nothing when no epic exists.
#
# DRY_RUN: returns the DRY_RUN_EXISTING_EPIC stub env (empty if unset) so the
# offline bats suite can simulate both the "already planned" and "first run"
# paths without touching the network.
find_existing_epic() {
local repo="$1" backref="$2"
if _is_dry_run; then
printf '%s' "${DRY_RUN_EXISTING_EPIC:-}"
return 0
fi
# --search is GitHub's tokenized full-text search; re-filter with an exact
# substring match so a tokenized hit on a different discussion can't match.
gh issue list --repo "$repo" --label initiative --state open \
--search "\"$backref\"" --json number,body \
| jq -r --arg ref "$backref" 'first(.[] | select(.body | contains($ref)) | .number) // empty'
Comment thread
don-petry marked this conversation as resolved.
Resolved
}

# link_sub_issue <repo> <epic_number> <child_id>
link_sub_issue() {
local repo="$1" epic="$2" child_id="$3"
Expand Down
23 changes: 23 additions & 0 deletions tests/test_initiative_planner.bats
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,29 @@ teardown() { rm -rf "$TMP"; }
[ "$(grep -c '"op":"create_issue"' "$LOG")" -eq 4 ]
}

@test "apply-plan skips creation when an epic already exists for the discussion" {
# DRY_RUN_EXISTING_EPIC simulates find_existing_epic finding a prior epic
# offline (the idempotency guard); no issues must be created.
DRY_RUN=1 DRY_RUN_LOG="$LOG" DRY_RUN_EXISTING_EPIC=777 \
REPO="petry-projects/.github-private" \
DISCUSSION_NUMBER=413 DISCUSSION_NODE_ID="D_test" PLAN_PATH="$PLAN" \
run bash "$PLANNER_DIR/apply-plan.sh"
[ "$status" -eq 0 ]
[[ "$output" == *"already planned (epic #777)"* ]]
# zero mutations of any kind
[ ! -s "$LOG" ] || [ "$(grep -c '"op":"create_issue"' "$LOG")" -eq 0 ]
[ ! -s "$LOG" ] || [ "$(grep -c '"op":"add_sub_issue"' "$LOG")" -eq 0 ]
}

@test "apply-plan still creates the full DAG when no existing epic is found" {
# No DRY_RUN_EXISTING_EPIC => find_existing_epic returns empty => normal path.
DRY_RUN=1 DRY_RUN_LOG="$LOG" REPO="petry-projects/.github-private" \
DISCUSSION_NUMBER=413 DISCUSSION_NODE_ID="D_test" PLAN_PATH="$PLAN" \
run bash "$PLANNER_DIR/apply-plan.sh"
[ "$status" -eq 0 ]
[ "$(grep -c '"op":"create_issue"' "$LOG")" -eq 4 ]
}

@test "story bodies are rendered in the BMAD create-story template" {
DRY_RUN=1 DRY_RUN_LOG="$LOG" REPO="petry-projects/.github-private" \
DISCUSSION_NUMBER=413 DISCUSSION_NODE_ID="D_test" PLAN_PATH="$PLAN" \
Expand Down
Loading