# Unlinked MR Finder

- **Command:** `lore unlinked [--since <date>]`
- **Confidence:** 83%
- **Tier:** 2
- **Status:** proposed
- **Effort:** low — LEFT JOIN queries

## What

Two reports:
1. Merged MRs with no entity_references at all (no "closes", no "mentioned",
   no "related") — orphan MRs with no issue traceability
2. Closed issues with no MR reference — issues closed manually without code change

## Why

Process compliance metric. Unlinked MRs mean lost traceability — you can't trace
a code change back to a requirement. Manually closed issues might mean work was done
outside the tracked process, or issues were closed prematurely.

## Data Required

All exists today:
- `merge_requests` (state, merged_at)
- `issues` (state, closed/updated_at)
- `entity_references` (for join/anti-join)

## Implementation Sketch

```sql
-- Orphan merged MRs (no references at all)
SELECT mr.iid, mr.title, mr.author_username, mr.merged_at,
       p.path_with_namespace
FROM merge_requests mr
JOIN projects p ON mr.project_id = p.id
LEFT JOIN entity_references er
    ON er.source_entity_type = 'merge_request' AND er.source_entity_id = mr.id
WHERE mr.state = 'merged'
  AND mr.merged_at >= ?1
  AND er.id IS NULL
ORDER BY mr.merged_at DESC;

-- Closed issues with no MR reference
SELECT i.iid, i.title, i.author_username, i.updated_at,
       p.path_with_namespace
FROM issues i
JOIN projects p ON i.project_id = p.id
LEFT JOIN entity_references er
    ON er.target_entity_type = 'issue' AND er.target_entity_id = i.id
    AND er.source_entity_type = 'merge_request'
WHERE i.state = 'closed'
  AND i.updated_at >= ?1
  AND er.id IS NULL
ORDER BY i.updated_at DESC;
```

## Human Output

```
Unlinked MRs (merged with no issue reference, last 30 days)

  !245  Fix typo in README              (alice, merged 2d ago)
  !239  Update CI pipeline              (bob, merged 1w ago)
  !236  Bump dependency versions         (charlie, merged 2w ago)

Orphan Closed Issues (closed without any MR, last 30 days)

  #92   Update documentation for v2      (closed by dave, 3d ago)
  #88   Investigate memory usage          (closed by eve, 2w ago)
```

## Downsides

- Some MRs legitimately don't reference issues (chores, CI fixes, dependency bumps)
- Some issues are legitimately closed without code (questions, duplicates, won't-fix)
- Noise level depends on team discipline

## Extensions

- `lore unlinked --ignore-labels "chore,ci"` — filter out expected orphans
- Compliance score: % of MRs with issue links over time (trend metric)
