-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add conditional rule logic via <code>when:</code> block (issue #73, PR 1/3) #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
codesensei-tushar
wants to merge
9
commits into
warestack:main
Choose a base branch
from
codesensei-tushar:feat/first-time-contributor-rules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e701776
feat(rules): add when: block to gate rule applicability
codesensei-tushar 56f9d5d
feat(enricher): fetch contributor PR history for when: predicates
codesensei-tushar 5ccf6a7
feat(engine): skip rules whose when: predicates do not hold
codesensei-tushar 526f23c
test: cover when: predicate evaluation and contributor context
codesensei-tushar 8fc4a24
docs: changelog entry for conditional when: rules
codesensei-tushar c859d5b
fix(rules): fail-open contributor predicate when merged_pr_count is u…
codesensei-tushar 2f40743
test: edge cases for when: evaluator and contributor-context failures
codesensei-tushar d58ae8f
test: broaden when: coverage and search_merged_pr_count HTTP branches
codesensei-tushar 8251dc8
chore(rules): TODO to swap fnmatch for pathspec gitwildmatch
codesensei-tushar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import fnmatch | ||
| import logging | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| if TYPE_CHECKING: | ||
| from src.rules.models import RuleWhen | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def should_apply_rule(when: RuleWhen | None, event_data: dict[str, Any]) -> tuple[bool, str]: | ||
| """ | ||
| Return whether the rule should be evaluated for this event, and a reason if skipped. | ||
|
|
||
| Args: | ||
| when: Parsed RuleWhen block (or None when the rule has no predicates). | ||
| event_data: Enriched event data, expected to include `contributor_context` | ||
| and `changed_files` when the predicates reference them. | ||
|
|
||
| Returns: | ||
| A tuple of (applies, reason). ``applies`` is True when all named | ||
| predicates in ``when`` hold (or ``when`` is empty/None). ``reason`` | ||
| is a human-readable explanation when the rule is skipped, or an | ||
| empty string when the rule applies. If a predicate is present but | ||
| its required context is missing, the rule is applied (fail-open) | ||
| and a warning is logged — skipping silently on missing data would | ||
| hide misconfiguration. | ||
| """ | ||
| if when is None: | ||
| return True, "" | ||
|
|
||
| contributor_ctx = event_data.get("contributor_context") or {} | ||
|
|
||
| if when.contributor is not None: | ||
| if not contributor_ctx: | ||
| logger.warning("when.contributor set but contributor_context missing — applying rule") | ||
| elif contributor_ctx.get("merged_pr_count") is None: | ||
| # API failure: we cannot tell whether the author is first-time or trusted. | ||
| # Fail-open (apply the rule) so a transient Search API outage does not | ||
| # silently disable stricter checks for newcomers. | ||
| logger.warning(f"when.contributor='{when.contributor}' set but merged_pr_count is unknown — applying rule") | ||
| else: | ||
| predicate = when.contributor.strip().lower() | ||
| if predicate == "first_time": | ||
| if not contributor_ctx.get("is_first_time", False): | ||
| return False, "contributor is not first-time" | ||
| elif predicate == "trusted": | ||
| if not contributor_ctx.get("trusted", False): | ||
| return False, "contributor is not trusted" | ||
| else: | ||
| logger.warning(f"Unknown contributor predicate '{when.contributor}' — ignoring") | ||
|
|
||
| if when.pr_count_below is not None: | ||
| if not contributor_ctx: | ||
| logger.warning("when.pr_count_below set but contributor_context missing — applying rule") | ||
| else: | ||
| merged_count = contributor_ctx.get("merged_pr_count") | ||
| if merged_count is None: | ||
| logger.warning("when.pr_count_below set but merged_pr_count is None — applying rule") | ||
| elif merged_count >= when.pr_count_below: | ||
| return False, f"contributor has {merged_count} merged PRs (threshold: {when.pr_count_below})" | ||
|
|
||
| if when.files_match is not None: | ||
| patterns: list[str] = [when.files_match] if isinstance(when.files_match, str) else list(when.files_match) | ||
| changed_files = event_data.get("changed_files") or [] | ||
| filenames = [f.get("filename", "") for f in changed_files if isinstance(f, dict) and f.get("filename")] | ||
| if not any(fnmatch.fnmatch(name, pat) for name in filenames for pat in patterns): | ||
| return False, f"no changed files match pattern {patterns}" | ||
|
|
||
| return True, "" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.