Skip to content

fix: detect approvals when approval_state reports no rules - #641

Open
sergey-kalin wants to merge 1 commit into
zereight:mainfrom
sergey-kalin:fix/approval-state-empty-rules
Open

fix: detect approvals when approval_state reports no rules#641
sergey-kalin wants to merge 1 commit into
zereight:mainfrom
sergey-kalin:fix/approval-state-empty-rules

Conversation

@sergey-kalin

Copy link
Copy Markdown

README says:

get_merge_request_approval_state uses approval_state when available, otherwise falls back to approvals

That fallback doesn't work in certain setup.

get_merge_request_approval_state derives approved_by solely from approval_state's rules array, and only falls back to /approvals on a 404.

Approval rules are a paid-tier feature. A project on Premium Gitlab account but without rules configured answers GET /approval_state with HTTP 200 and rules: [], so the flatMap over rules yields no approvers even when someone has approved the MR. The fallback never fires because the request succeeded. Callers are told nobody approved with Approval rules despite of real approvers status.

The same response also leaves approved, user_has_approved and user_can_approve null, since approval_state does not carry them -- only /approvals does.

Two changes:

  • fall back on 402/403 as well as 404, for instances or namespaces that reject the endpoint rather than 404 it;
  • when approval_state returns an empty rules array, read /approvals and prefer its result if it lists approvers. Wrapped in try/catch, so an unavailable /approvals preserves existing behaviour.

Keyed on the rules array rather than on the resulting approver count. A non-empty rules array means rules are in use and approval_state is authoritative, so a project using approval rules makes no extra request.

get_merge_request_approval_state derives approved_by solely from
approval_state's rules array, and only falls back to /approvals on a 404.

Approval rules are a paid-tier feature. A project without them answers
GET /approval_state with HTTP 200 and `rules: []`, so the flatMap over
rules yields no approvers even when someone has approved the MR. The
fallback never fires because the request succeeded. Callers are told
nobody approved.

The same response also leaves `approved`, `user_has_approved` and
`user_can_approve` null, since approval_state does not carry them -- only
/approvals does.

Two changes:

- fall back on 402/403 as well as 404, for instances or namespaces that
  reject the endpoint rather than 404 it;
- when approval_state returns an empty rules array, read /approvals and
  prefer its result if it lists approvers. Wrapped in try/catch so an
  unavailable /approvals keeps the previous behaviour.

Keyed on the rules array rather than on the approver count, so a project
using approval rules makes no extra request even while an MR is still
waiting for its first approval -- a non-empty rules array means rules are
in use and approval_state is authoritative. The added test covers the
empty-rules case; the existing rules test asserts that path still
resolves from approval_state alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 85ababd8-1620-4278-bbaf-d676810567fa

📥 Commits

Reviewing files that changed from the base of the PR and between 926d42c and c2a89ac.

📒 Files selected for processing (2)
  • index.ts
  • test/test-merge-request-approval-state-tools.ts
📜 Recent review details
🔇 Additional comments (2)
index.ts (1)

5809-5816: LGTM!

Also applies to: 5829-5843

test/test-merge-request-approval-state-tools.ts (1)

10-10: LGTM!

Also applies to: 164-200


📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved merge-request approval detection when the approval-state endpoint is unavailable due to licensing, authorization, or access restrictions.
    • Added fallback handling for projects without configured approval rules, ensuring available approvers are still displayed.
    • Preserved accurate approval source information and approver details across fallback scenarios.

Walkthrough

This change updates merge request approval lookup fallback behavior. It now falls back from /approval_state on HTTP 402 and 403, and also checks /approvals when /approval_state returns empty rules. Tests add the new empty-rules scenario.

Changes

Merge request approval fallback flow

Layer / File(s) Summary
Approval endpoint fallback handling
index.ts, test/test-merge-request-approval-state-tools.ts
getMergeRequestApprovalState now uses the /approvals fallback for HTTP 402, 403, and for successful /approval_state responses with empty rules when fallback approvers exist. Tests add a new merge request fixture, mock both endpoints for the empty-rules case, and assert source_endpoint: "approvals" with the expected approver result.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: zereight, raskad, vrajpal-jhala

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: the function now detects approvals when approval_state returns no rules, which is the primary fix addressed in this changeset.
Description check ✅ Passed The description clearly explains the problem, the root cause (empty rules array with HTTP 200), and both solutions implemented in the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
✨ Simplify code
  • Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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