# MR Churn Analysis

- **Command:** `lore churn [--since <date>]`
- **Confidence:** 72%
- **Tier:** 3
- **Status:** proposed
- **Effort:** medium — multi-table aggregation with composite scoring

## What

For merged MRs, compute a "contentiousness score" based on: number of review
discussions, number of DiffNotes, resolution cycles, file count. Flag high-churn
MRs as candidates for architectural review.

## Why

High-churn MRs often indicate architectural disagreements, unclear requirements,
or code that's hard to review. Surfacing them post-merge enables retrospectives
and identifies areas that need better design upfront.

## Data Required

All exists today:
- `merge_requests` (state='merged')
- `discussions` (merge_request_id, resolved, resolvable)
- `notes` (note_type='DiffNote', discussion_id)
- `mr_file_changes` (file count per MR)

## Implementation Sketch

```sql
SELECT
    mr.iid,
    mr.title,
    mr.author_username,
    p.path_with_namespace,
    COUNT(DISTINCT d.id) as discussion_count,
    COUNT(DISTINCT CASE WHEN n.note_type = 'DiffNote' THEN n.id END) as diffnote_count,
    COUNT(DISTINCT CASE WHEN d.resolvable = 1 AND d.resolved = 1 THEN d.id END) as resolved_threads,
    COUNT(DISTINCT mfc.id) as files_changed,
    -- Composite score: normalize each metric and weight
    (COUNT(DISTINCT d.id) * 2 + COUNT(DISTINCT n.id) + COUNT(DISTINCT mfc.id)) as churn_score
FROM merge_requests mr
JOIN projects p ON mr.project_id = p.id
LEFT JOIN discussions d ON d.merge_request_id = mr.id AND d.noteable_type = 'MergeRequest'
LEFT JOIN notes n ON n.discussion_id = d.id AND n.is_system = 0
LEFT JOIN mr_file_changes mfc ON mfc.merge_request_id = mr.id
WHERE mr.state = 'merged'
  AND mr.merged_at >= ?1
GROUP BY mr.id
ORDER BY churn_score DESC
LIMIT ?2;
```

## Human Output

```
High-Churn MRs (last 90 days)

  MR       Discussions  DiffNotes  Files  Score  Title
  !234          12          28       8     60    Refactor auth middleware
  !225           8          19       5     39    API versioning v2
  !218           6          15      12     39    Database schema migration
  !210           5           8       3     21    Update logging framework
```

## Downsides

- High discussion count could mean thorough review, not contention
- Composite scoring weights are arbitrary; needs calibration per team
- Large MRs naturally score higher regardless of contention

## Extensions

- Normalize by file count (discussions per file changed)
- Compare against team averages (flag outliers, not absolute values)
- `lore churn --author alice` — which of alice's MRs generate the most discussion?
