# Workflow: rcode-export-to-github

<purpose>
Push rcode state to GitHub as issues, milestones, and (optionally) a Project v2 board. Thin slash-command wrapper around the existing `rcode github-sync` CLI, plus a `--decisions` mode that publishes the project's recent decision log as a single tracking issue.

Use this to externalize a phase into GitHub for stakeholders who live in issues, to seed a milestone before starting work, or to publish decisions for team visibility.
</purpose>

<output_format>
Open with banner:

```
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 rcode ► EXPORT TO GITHUB
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```

Always default to dry-run. Show a preview, then require `--execute` on a follow-up run to mutate GitHub.
</output_format>

<required_reading>
@.rcode/references/output-format.md
@.rcode/references/command-redirect-format.md
</required_reading>

<process>
## Step 0 — Usage check

If `$ARGUMENTS` is empty or contains `--help` / `-h`:

```
/rcode-export-to-github [target] [flags]

Targets (mutually exclusive):
  (none)                    Sync all phases → milestones, epics/stories → issues
  --phase <id>              One phase
  --sprint <id>             One sprint's stories
  --epic <id>               One epic + its child stories
  --story <id>              One story only
  --decisions [--since ISO] Publish recent decisions as a single tracking issue

Flags (passed through to rcode github-sync):
  --repo owner/name         Target repo (default: auto-detect from gh)
  --with-labels             Create rcode label taxonomy
  --project                 Also create a GitHub Project v2 board
  --no-update / --no-close / --no-update-body / --no-update-labels
  --execute                 Actually mutate (default is dry-run)
  --yes                     Skip confirmation (requires --force-yolo in yolo mode)
```

STOP — do not proceed.

## Step 1 — Preflight

```bash
# gh CLI must be installed and authenticated
command -v gh >/dev/null 2>&1 || { echo "gh CLI not installed. Install: https://cli.github.com/"; exit 0; }
gh auth status >/dev/null 2>&1 || { echo "gh not authenticated. Run: gh auth login"; exit 0; }
```

## Step 2 — Branch on target

### Standard targets (phase/sprint/epic/story/default)

Delegate to the CLI:

```bash
node "$(command -v rcode || echo ./cli/index.js)" github-sync $ARGUMENTS
```

If `rcode` is not on PATH, fall back to `node ./cli/index.js github-sync $ARGUMENTS`. The CLI handles:

- Auto-detecting the repo via `gh repo view`
- Idempotent sync of phases → milestones, stories → issues
- Dry-run by default (mutations require `--execute`)
- Guided/yolo confirmation gates per `AGENTS.md`

Pass through all arguments unchanged. Print the CLI output verbatim — do not re-wrap it.

### Decisions target

When `$ARGUMENTS` starts with `--decisions`, follow this branch instead:

```bash
SINCE=""
if echo "$ARGUMENTS" | grep -q -- "--since"; then
  SINCE=$(echo "$ARGUMENTS" | grep -oE -- "--since [^ ]+" | awk '{print $2}')
fi

# Pull this project's decisions from the cross-project log
RESULT=$(node .rcode/bin/rcode-tools.cjs state decisions-global --limit 100 ${SINCE:+--since "$SINCE"} --project "$(node .rcode/bin/rcode-tools.cjs state read | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try{process.stdout.write(JSON.parse(s).project||"")}catch{}}')" 2>/dev/null)
```

Render the decisions as a markdown table in the issue body:

```markdown
# Decision Log — {project_name}

_Generated by `/rcode-export-to-github --decisions` on {ISO date}. Source: `~/.rcode/decisions.jsonl` (this project only)._

| Date | Phase | Plan | Decision |
|------|-------|------|----------|
| {YYYY-MM-DD} | {phase} | {plan} | {summary} |
| ... |
```

**Dry-run (default):**

Print the rendered markdown to stdout with a header "The following issue would be created:" and STOP. Do not call `gh`.

**With `--execute`:**

```bash
REPO_FLAG=""
if echo "$ARGUMENTS" | grep -q -- "--repo"; then
  REPO_FLAG=$(echo "$ARGUMENTS" | grep -oE -- "--repo [^ ]+")
fi

TITLE="Decision log — ${PROJECT_NAME} (${ISO_DATE})"

# Confirmation gate — mandatory before mutating GitHub
echo "About to create issue: ${TITLE} in repo $(gh repo view --json nameWithOwner -q .nameWithOwner)"
read -p "Proceed? [y/N] " CONFIRM
[ "$CONFIRM" = "y" ] || { echo "Aborted."; exit 0; }

gh issue create $REPO_FLAG \
  --title "$TITLE" \
  --label "rcode,decisions" \
  --body-file /tmp/rcode-decisions-export.md
```

On success, echo the issue URL returned by `gh issue create`.

## Step 3 — Report

For standard targets: rely on the CLI's own summary.

For `--decisions`:

```
✓ Created issue: {url}
  Contents: {N} decisions{since_suffix}
```

Where `since_suffix` is `, since {ISO}` if `--since` was passed.

## Step 4 — Record in state

After a successful mutation (either CLI or --decisions), append a decision for traceability:

```bash
node .rcode/bin/rcode-tools.cjs state add-decision \
  "Exported ${TARGET_DESCRIPTION} to GitHub: ${ISSUE_OR_SUMMARY_URL}" \
  2>/dev/null || true
```

This also mirrors to `~/.rcode/decisions.jsonl` so `/rcode-decisions` surfaces the export.
</process>

## Success Criteria

- Dry-run is the default for every target — mutations require `--execute`
- `gh` preflight aborts cleanly if the CLI is missing or unauthenticated
- Standard targets fully delegate to `rcode github-sync` (no duplicated logic)
- `--decisions` target reads from `~/.rcode/decisions.jsonl` filtered to this project
- Confirmation gate is shown before `gh issue create` — yolo mode does NOT bypass this for GitHub mutations
- Successful export is recorded via `state add-decision` for audit trail

## On Error

- `gh auth status` fails → tell the user to run `gh auth login`, exit
- `gh repo view` fails (no remote) → tell the user to pass `--repo owner/name`, exit
- `rcode` missing from PATH → fall back to `node ./cli/index.js`; if that also fails, instruct the user to `pnpm install` from the repo root
- Empty decision log under `--decisions` → print "No decisions to export{since_suffix}. Run /rcode-council or complete a sprint to create some." and exit cleanly

## Next Up

- `/rcode-inbox` — triage the GitHub issues that were created
- `/rcode-status` — check project state after the export
