---
name: pre-merge-check
description: Verify all gates pass before merging a PR. Checks code review, user stories, QA, lint, build, and security audit.
argument-hint: "<PR-number>"
---

# /pre-merge-check

Verify all required gates pass before merging a PR to Dev-branch. This is the final checkpoint before merge.

Takes an argument: PR number. E.g. `/pre-merge-check #456`

---

## Pre-Merge Checklist

Run through each gate in order. All must pass before merging.

### Gate 1: Code Review Completed

```bash
gh pr view <PR-number> --json reviews
```

**Check:**
- [ ] PR has been reviewed (via `/review-pr` or manual review)
- [ ] No unresolved P0 or P1 issues
- [ ] Review verdict is APPROVED (not CHANGES REQUESTED)

**If not completed:**
> "Code review is required before merge. Run `/review-pr #<PR-number>` first."

---

### Gate 2: User Story Impact Checked

**Check:**
- [ ] `/check-user-story-impact` has been run
- [ ] Any affected user stories have been updated
- [ ] Updates are committed to the branch

**If not completed:**
> "User story impact check is required. Run `/check-user-story-impact #<issue-number>` first."

---

### Gate 3: QA Status

```bash
gh issue view <issue-number> --json comments
```

**Check:**
- [ ] QA has been run (for UI features)
- [ ] QA result is PASS
- [ ] OR user explicitly skipped QA (for non-UI issues)

**If QA failed:**
> "QA reported failures. Dev Agent must fix issues before merge."

**If QA not run (UI feature):**
> "QA is required for UI features. Run `/run-qa` first."

---

### Gate 4: Lint Passes

```bash
cd app && npm run lint
```

**Check:**
- [ ] No lint errors (warnings are acceptable)

**If lint fails:**
> "Lint errors must be fixed before merge. Fix errors and push to the branch."

---

### Gate 5: Build Passes

```bash
cd app && npm run build
```

**Check:**
- [ ] Build completes successfully
- [ ] No TypeScript errors

**If build fails:**
> "Build failed with TypeScript errors. Fix errors and push to the branch."

---

### Gate 6: Security Audit (npm audit)

```bash
cd app && npm audit --audit-level=high
```

**Check:**
- [ ] No high or critical vulnerabilities

**If audit fails:**
```bash
cd app && npm audit fix
```

Then commit the updated `package-lock.json` to the branch.

**If `npm audit fix` doesn't resolve all issues:**
> "Manual intervention required. Run `npm audit` to see details and update dependencies manually."

---

## Merge Decision

### All Gates Pass → READY TO MERGE

```bash
gh pr merge <PR-number> --merge --delete-branch
```

**Post-merge:**
```bash
# Update issue labels
gh issue edit <issue-number> --remove-label "in-dev" --add-label "ready-for-qa"

# Return to Dev-branch
git checkout Dev-branch && git pull
```

### Any Gate Fails → BLOCKED

Report which gates failed:

```
## Pre-Merge Check: BLOCKED

| Gate | Status |
|------|--------|
| Code Review | ✓ Passed |
| User Story Impact | ✓ Passed |
| QA | ✗ FAILED — Needs fixes |
| Lint | ✓ Passed |
| Build | ✓ Passed |
| Security Audit | ✓ Passed |

**Blocking issue:** QA reported 2 failures. See issue comments for details.

**Next step:** Dev Agent fixes QA issues, then re-run `/pre-merge-check #<PR-number>`
```

---

## Quick Check Mode

For fast verification when you're confident gates have passed:

```bash
# Run lint, build, and security audit in sequence
cd app && npm run lint && npm run build && npm audit --audit-level=high
```

If all pass and you've confirmed code review + QA, proceed to merge.

---

## Branch Flow Reminder

```
feature/* → Dev-branch → staging → main
```

- **This skill merges to Dev-branch only**
- **To promote Dev-branch → staging:** Separate decision when features are ready
- **To promote staging → main:** Requires explicit user approval + `/check-production-requirements`

---

## Report Format

```
## Pre-Merge Check: #<PR-number>

| Gate | Status |
|------|--------|
| Code Review | ✓ / ✗ |
| User Story Impact | ✓ / ✗ |
| QA | ✓ / ✗ / Skipped (non-UI) |
| Lint | ✓ / ✗ |
| Build | ✓ / ✗ |
| Security Audit | ✓ / ✗ |

**Result:** READY TO MERGE / BLOCKED

**Action taken:** [Merged PR / Reported blockers]
```
