Skip to content

feat(repo-stats): count review cycles per PR and show them on the dashboard - #4

Merged
g-husam merged 3 commits into
mainfrom
repo-stats-review-cycles
Oct 28, 2026
Merged

g-husam merged 3 commits into
mainfrom
repo-stats-review-cycles

Conversation

@g-husam

@g-husam g-husam commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Counts how many review cycles each PR goes through and shows them on the dashboard.

A review cycle is one round of review on one version of the PR, by anyone other than the author and bots, before the merge:

Scenario Cycles
Merged with no review 0
Approved on first look 1
Changes requested → fix pushed → approved 2
Several reviewers on the same version, then approval after one push 2
Push after approval, merged without re-review unchanged (an unreviewed push adds none)

Data

  • GitHub: each review names the commit it was made against (reviews{commit{oid}}), so a review of a new oid starts a new cycle. Force-pushes and rebases are handled correctly, and no extra API calls are needed. The page size is raised to 100 reviews. Above that, the count is marked as a lower bound.
  • GitLab: cycles come from the MR notes. added N commits and force-pushed system notes are pushes, and comments and approvals by other people are reviews. All notes are read, up to 50 cycles; past that, the value is capped and marked as a lower bound. This uses no new endpoint, so it still works with a CI job token.
  • Git mode: cycles are empty (None), never guessed.
  • Records, CSV and JSON get three new fields: review_cycles, changes_requested (GitHub only) and reviewers.

Behaviour change (GitLab): an approval with no comment now counts as a review. It therefore sets first_review and reviewed.

Dashboard

  • New KPIs:
    • Median review cycles: the median for reviewed PRs only, with the p90 and the median with unreviewed PRs counted as 0.
    • Merged without review: the count and its share of merged PRs.
  • New "Review cycles" section, between Time and DORA:
    • statistics shown both without unreviewed PRs and with them counted as 0
    • PRs by number of cycles (0 to 5+)
    • reviewed vs unreviewed PRs by month
    • monthly median, p90 and mean
    • cycles by PR size
    • the ten PRs with the most cycles
  • Other changes: a "Median review cycles" column in Statistics by type, and a Methodology entry. README Heuristics is updated.

Tests

  • test_repo_stats.H1_ReviewCycles (15 new tests): the counting rule, the cap, every scenario above, force-pushes, ignored reviews (author, bot, pending, post-merge), truncation, GitLab pushes and approvals, --no-reviews, git mode and the CSV columns.
  • test_ui (2 new tests, 40 in total): the KPIs and statistics tables are checked against Python's numbers with and without unreviewed PRs, and git mode is checked for the "needs API" message. The synthetic API dataset now has multi-cycle reviews.
  • All new tests and all browser tests pass locally. Three existing B3/B4 tests fail the same way on main in my sandbox; this is an environment issue (a real gh on PATH, and sleep mocking), not this change.

…hboard

A review cycle is one round of review on one version of the PR. GitHub reviews name the commit they were made against; GitLab cycles come from the MR notes (pushes split cycles, comments and approvals by others are reviews), read in full up to 50 cycles. Git mode leaves cycles empty.

Records, CSV and JSON gain review_cycles, changes_requested and reviewers. The dashboard adds Median review cycles and Merged without review KPIs, a Review cycles section (statistics with and without unreviewed PRs, distribution, monthly trend, by size, top ten), a by-type column and a Methodology entry.

GitLab approvals without a comment now count as a review.
…view covered

On GitHub a reply to an older review thread is a new review of the older commit, so comparing each review with the previous one counted A, B, A, B as four cycles. Found by running on googleapis/librarian (#7469 was counted 6, has 2).
@g-husam
g-husam merged commit 3c7f6d8 into main Oct 28, 2026
@g-husam
g-husam deleted the repo-stats-review-cycles branch October 28, 2026 19:43
g-husam added a commit that referenced this pull request Oct 28, 2026
…ned CI, 404 probe) (#5)

Follow-ups from the #3 review. They fix GitHub Enterprise logins, run
the CLI from a private directory, pin the CI examples, and stop a single
404 from turning off GitLab commit lookups for every MR. The tests no
longer reach a real `gh` or depend on timing.

### What changed

```diff
 Cli(tool, host)
-  hint: "in CI set GH_TOKEN"                    # wrong for GitHub Enterprise Server
+  hint: "in CI set " + ci_login_hint(tool, host) # GH_TOKEN for github.com / *.ghe.com, else GH_ENTERPRISE_TOKEN
-  _run: cwd = tempfile.gettempdir()             # shared /tmp: another user can plant a .git
+  _run: cwd = private TemporaryDirectory        # new, empty, 0700, one per run, removed after it
+  close() / with-block
 collect
-  cli = Cli(...)
+  with Cli(...) as cli
 fetch_gitlab
-  probe refused on 401 / 403 / 404              # one missing MR turned off commits for all MRs
+  probe refused on 401 / 403                    # a 404 only affects that MR
 retries
-  time.sleep(...)
+  _backoff(...)                                 # tests patch this instead of time.sleep for everyone
```

### Docs (`README.adoc`)

```diff
 Logging in
+  NOTE: GitHub Enterprise Server uses GH_ENTERPRISE_TOKEN, not GH_TOKEN
 GitHub Actions
+  ref: <full commit SHA>                        # pin the tool: it runs next to the token
+  GHES: set GH_ENTERPRISE_TOKEN instead
 GitLab CI
-  git clone --depth 1 …scripts.git              # unpinned HEAD
+  git fetch --depth 1 …scripts.git "$REPO_STATS_SHA" && git checkout FETCH_HEAD
 Security
-  runs from the system temp directory
+  runs from a new, empty, private temp directory
```

### Tests

```text
tests/test_repo_stats.py
├── B3  login hint names GH_ENTERPRISE_TOKEN for a GHES host (and GH_TOKEN alone is not enough)
├── B3  glab with no login → anonymous 401 → git fallback naming `glab auth login`
├── B3  "gh missing" test puts only git on PATH (/usr/bin also holds a real gh, which it used to run)
├── B4/F2/…  patch rs._backoff instead of time.sleep (subprocess's own sleeps made the retry tests flaky)
├── G1  every CLI call runs in one private repo-stats-cli-* dir, mode 0700, removed after the run
├── G2  a 404 on the probed MR's commits does not stop lookups for the other MRs
└── review cycles (#4)  close the Cli the GitLab helper creates
```

### Not changed
- The review also said the "no PRs in the selected date range" warning
ignores `--until`. That was wrong: the API filters only by `--since`, so
an empty result with only `--until` means the repository has no PRs. The
message is correct as it is.
- No code left over from #3 needed deleting: no unused imports,
functions or constants were found.

### Testing
- `python3 -m unittest test_repo_stats`: 222 tests pass on Python 3.13
(rebased on main with #4), with no `ResourceWarning`. Run with
`GIT_CONFIG_GLOBAL=/dev/null`, because a global `tag.gpgSign` breaks the
fixtures' annotated tags.
- Not run: the Playwright UI tests (no node here) and Python 3.8.
`template.html` is unchanged.
- The real `glab` no-login behaviour and the job-token permissions are
still checked only against the fakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant