refactor(content-guards): simplify enforce-issue-limits (391 → 200 lines)#125
refactor(content-guards): simplify enforce-issue-limits (391 → 200 lines)#125JacobPEvans merged 6 commits intomainfrom
Conversation
…00 lines Remove over-engineered two-tier rate limiting (trusted/default user config, external config file, gh api user call) in favor of simple hardcoded limits. Consolidate duplicate functions into generic resource-based equivalents: - Merge get_issue_counts/get_pr_counts into _get_counts(resource) - Merge check_duplicate_issue/check_duplicate_pr into _check_duplicate() - Merge block_rate_limit/block_hard_limit/_block_duplicate into _block() - Replace 6 separate subprocess blocks with single _gh_json() wrapper - Use regex for command detection instead of string padding - Unify rate limit to 10/24h for both issues and PRs (was 5 default/10 trusted) - Remove unused os import and _DEFAULT_CONFIG/_load_rate_config/_get_current_user_id Closes #122 (label-based filtering superseded - labels are user-settable). (claude)
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly refactors the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a significant and well-executed refactoring of enforce-issue-limits.py. The simplification of the rate-limiting logic and removal of external dependencies greatly improves the script's clarity and maintainability. My review includes a couple of suggestions to further enhance the code structure by reducing duplication and consolidating related logic blocks, in line with the goals of this refactoring. Overall, this is an excellent improvement.
There was a problem hiding this comment.
Pull request overview
Refactors the content-guards PreToolUse hook that rate-limits GitHub issue/PR creation by simplifying the implementation (removing tiered config logic) and updating integration tests to match the new unified limits and message formats.
Changes:
- Simplified
enforce-issue-limits.pyby removing external config/tier logic and consolidating helpers into a smaller set of functions. - Unified 24h rate limits to a single constant (10/24h) for both issues and PRs, while keeping hard limits and duplicate-title blocking.
- Updated bats integration tests to reflect the new 10/24h threshold and updated block message strings.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| content-guards/scripts/enforce-issue-limits.py | Refactors the hook: unified limits, consolidated gh JSON wrapper, simplified duplicate/hard/rate-limit checks, and updated block messaging. |
| tests/content-guards/enforce-issue-limits/enforce-issue-limits.bats | Updates integration tests for the new 10/24h rate limit and revised block-message text/casing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
The _CMD_RE regex matches `gh issue edit` but no checks apply to that combination. Add explicit early return to avoid unnecessary processing. (claude)
The _block() helper only indented the first line of multi-line details strings. Now each non-empty line gets two-space indentation for consistent formatting in stderr block messages. (claude)
…miter Consolidate two consecutive `if action == "create":` blocks into a single block, grouping duplicate detection and hard limit checks together. (claude)
…sing Extract duplicated title normalization regex into _normalize_title() helper. Remove unnecessary .lower() on label in block messages — "PRs" and "Issues" read better than "prs" and "issues" in user-facing output. (claude)
…t messages TC5 and TC6 assertions updated to match "Issues"/"PRs" (from label variable) instead of lowercase "issues"/"prs" (from resource variable). (claude)
Summary
enforce-issue-limits.pyfrom 391 to 200 lines by removing over-engineered two-tier rate limiting and consolidating duplicate functionsrate-limit-config.json),gh api usercall, and trusted/default user tiers — replaced with simple hardcoded constants_gh_json()wrapper,_get_counts(),_count_recent(),_check_duplicate(),_block(),main()--author @meidentity-based filtering (unforgeable, unlike label-based approach in closed PR fix(content-guards): scope rate limit to ai-created label #122)Test plan
python3 content-guards/scripts/test_enforce_issue_limits.py)bats tests/content-guards/enforce-issue-limits/)gh issue create🤖 Generated with Claude Code
Greptile Summary
This PR performs a well-executed refactor of the
enforce-issue-limits.pyhook, cutting it nearly in half (391 → 200 lines) by eliminating the two-tier rate limiting system, removing external config file dependencies, and consolidating 14 functions into 7. The core security model is preserved —--author @meidentity-based filtering remains, hard limits (50 total / 25 AI-created) are unchanged, and the fail-open behavior is maintained throughout.get_issue_counts()+get_pr_counts()with a generic_get_counts(resource)— clean DRY wincheck_duplicate_pr()+check_duplicate_issue()+extract_flag_value()+normalize_title()into a single_check_duplicate()functiongh api usercall + config file attack surface_CMD_RE) replaces manual string checks; slightly broader (now matchesgh issue editas a no-op)"prs"instead of"PRs"due to using the raw regex capture group instead of the formattedlabelvariableConfidence Score: 4/5
gh issue edit. All 17 bats integration tests and 9 unit tests pass per the PR description, and the test updates correctly reflect the new behavior.enforce-issue-limits.pyhas minor output formatting nits but no functional issues.Important Files Changed
gh issue edit(harmless no-op)."prs"lowercase,"Duplicate Issue"). The shared mock approach (singleGH_RESPONSEfor all gh calls) works because the script's functions gracefully ignore unexpected/missing fields.Flowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[stdin: JSON hook input] --> B{Parse JSON} B -- invalid --> Z0[exit 0: allow] B -- valid --> C{Regex match\ngh issue/pr create/edit?} C -- no match --> Z0 C -- match --> D[Extract resource + action] D --> E{action == create?} E -- yes --> F[_check_duplicate:\ngh list --state open\ncompare title words] F --> G{Duplicate found?} G -- yes --> BLOCK1[exit 2: Duplicate detected] G -- no --> H[_get_counts:\ngh list --state open\ncount total + ai-created] H --> I{total >= 50 or\nai-created >= 25?} I -- yes --> BLOCK2[exit 2: Hard limit exceeded] I -- no --> J{Rate limit check?} E -- no --> J J -- "create, or pr+edit" --> K[_count_recent:\ngh list --author @me\ncount last 24h] K --> L{recent >= 10?} L -- yes --> BLOCK3[exit 2: Rate limit exceeded] L -- no --> Z0 J -- "issue+edit" --> Z0Prompt To Fix All With AI
Last reviewed commit: 0510e5f