-
Notifications
You must be signed in to change notification settings - Fork 25
Add reference-only edit detection and auto-approval #127
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
miraclousharshita
wants to merge
10
commits into
Wikimedia-Suomi:main
Choose a base branch
from
miraclousharshita:miraclousharshita/feat/adding-autoreview-for-ref-links
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
10 commits
Select commit
Hold shift + click to select a range
dc1b418
added ref only autoreview
miraclousharshita 671f86f
fix bugs
miraclousharshita 74cc192
revert changes
miraclousharshita b4a7512
fixing some test cases
miraclousharshita 22fd171
Merge branch 'main' into miraclousharshita/feat/adding-autoreview-for…
miraclousharshita 1e99c80
fix from comments
miraclousharshita 5653e90
Merge branch 'main' into miraclousharshita/feat/adding-autoreview-for…
zache-fi 724bffe
Merge branch 'main' into miraclousharshita/feat/adding-autoreview-for…
zache-fi 650b8bb
fix from comments
miraclousharshita 5be03db
fix from reviews
miraclousharshita 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from ..base import CheckResult | ||
| from ..context import CheckContext | ||
| from ..decision import AutoreviewDecision | ||
| from ..utils.wikitext import ( | ||
| extract_domain_from_url, | ||
| extract_urls_from_references, | ||
| get_parent_wikitext, | ||
| is_reference_only_edit, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def check_reference_only_edit(context: CheckContext) -> CheckResult: | ||
| """Check if revision only adds or modifies references.""" | ||
| pending_wikitext = context.revision.get_wikitext() | ||
| parent_wikitext = get_parent_wikitext(context.revision) | ||
|
|
||
| is_ref_only, has_removals, new_or_modified_refs = is_reference_only_edit( | ||
| parent_wikitext, pending_wikitext | ||
| ) | ||
|
|
||
| if not is_ref_only: | ||
| return CheckResult( | ||
| check_id="reference-only-edit", | ||
| check_title="Reference-only edit detection", | ||
| status="skip", | ||
| message="Edit modifies content beyond references.", | ||
| ) | ||
|
|
||
| if has_removals and not new_or_modified_refs: | ||
| return CheckResult( | ||
| check_id="reference-only-edit", | ||
| check_title="Reference-only edit detection", | ||
| status="not_ok", | ||
| message="Edit only removes references without adding new ones.", | ||
| decision=AutoreviewDecision( | ||
| status="manual", | ||
| label="Requires manual review", | ||
| reason="Reference-only edits that only remove references require manual review.", | ||
| ), | ||
| should_stop=True, | ||
| ) | ||
|
|
||
| if not new_or_modified_refs: | ||
| return CheckResult( | ||
| check_id="reference-only-edit", | ||
| check_title="Reference-only edit detection", | ||
| status="skip", | ||
| message="No new or modified references detected.", | ||
| ) | ||
|
|
||
| urls = extract_urls_from_references(new_or_modified_refs) | ||
|
|
||
| if not urls: | ||
| logger.info( | ||
| "Auto-approving reference-only edit %s (no URLs in new references)", | ||
| context.revision.revid, | ||
| ) | ||
| ref_count = len(new_or_modified_refs) | ||
| return CheckResult( | ||
| check_id="reference-only-edit", | ||
| check_title="Reference-only edit detection", | ||
| status="ok", | ||
| message=f"Edit only modifies references ({ref_count} reference(s) added/modified).", | ||
| decision=AutoreviewDecision( | ||
| status="approve", | ||
| label="Can be auto-approved", | ||
| reason="Edit only adds or modifies references without external URLs.", | ||
| ), | ||
| should_stop=True, | ||
| ) | ||
|
|
||
| domains = [] | ||
| for url in urls: | ||
| domain = extract_domain_from_url(url) | ||
| if domain: | ||
| domains.append(domain) | ||
|
|
||
| new_domains = [] | ||
| checked_domains = set() | ||
|
|
||
| for domain in domains: | ||
| if domain in checked_domains: | ||
| continue | ||
| checked_domains.add(domain) | ||
|
|
||
| has_been_used = context.client.has_domain_been_used(domain) | ||
|
|
||
| if not has_been_used: | ||
| new_domains.append(domain) | ||
| logger.info( | ||
| "Domain %s has not been used before in revision %s", | ||
| domain, | ||
| context.revision.revid, | ||
| ) | ||
|
|
||
| if new_domains: | ||
| domain_list = ", ".join(new_domains[:3]) | ||
| if len(new_domains) > 3: | ||
| domain_list += "..." | ||
| domain_count = len(new_domains) | ||
|
|
||
| return CheckResult( | ||
| check_id="reference-only-edit", | ||
| check_title="Reference-only edit detection", | ||
| status="not_ok", | ||
| message=f"Edit adds references with new domain(s): {domain_list}", | ||
| decision=AutoreviewDecision( | ||
| status="manual", | ||
| label="Requires manual review", | ||
| reason=f"Reference-only edit contains {domain_count} previously unused domain(s).", | ||
| ), | ||
| should_stop=True, | ||
| ) | ||
|
|
||
| logger.info( | ||
| "Auto-approving reference-only edit %s with %s known domain(s)", | ||
| context.revision.revid, | ||
| len(checked_domains), | ||
| ) | ||
| ref_count = len(new_or_modified_refs) | ||
| domain_count = len(checked_domains) | ||
| return CheckResult( | ||
| check_id="reference-only-edit", | ||
| check_title="Reference-only edit detection", | ||
| status="ok", | ||
| message=f"Edit only modifies references ({ref_count} reference(s) with " | ||
| f"{domain_count} known domain(s)).", | ||
| decision=AutoreviewDecision( | ||
| status="approve", | ||
| label="Can be auto-approved", | ||
| reason="Edit only adds or modifies references with known domains.", | ||
| ), | ||
| should_stop=True, | ||
| ) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
total=1then this will match to itself if it still exists in latest revision. You can somewhat mitigate self-references by adding total=2 and check if there is least 2 links.