-
Couldn't load subscription status.
- Fork 1.1k
Move typo checks to after_insert
#17764
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| Text, | ||
| UniqueConstraint, | ||
| cast, | ||
| event, | ||
| func, | ||
| or_, | ||
| orm, | ||
|
|
@@ -67,6 +68,7 @@ | |
| from warehouse.classifiers.models import Classifier | ||
| from warehouse.events.models import HasEvents | ||
| from warehouse.forklift import metadata | ||
| from warehouse.helpdesk.interfaces import IAdminNotificationService | ||
| from warehouse.integrations.vulnerabilities.models import VulnerabilityRecord | ||
| from warehouse.observations.models import HasObservations | ||
| from warehouse.organizations.models import ( | ||
|
|
@@ -77,6 +79,10 @@ | |
| Team, | ||
| TeamProjectRole, | ||
| ) | ||
| from warehouse.packaging.interfaces import ( | ||
| IProjectService, | ||
| ProjectNameUnavailableTypoSquattingError, | ||
| ) | ||
| from warehouse.sitemap.models import SitemapMixin | ||
| from warehouse.utils import dotted_navigator, wheel | ||
| from warehouse.utils.attrs import make_repr | ||
|
|
@@ -496,6 +502,92 @@ def yanked_releases(self): | |
| ) | ||
|
|
||
|
|
||
| @event.listens_for(Project, "after_insert") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the first time |
||
| def receive_after_insert(mapper, connection, project): | ||
| request = get_current_request() | ||
| if not request: | ||
| # Can't do anything if there isn't a request | ||
| return | ||
| project_service = request.find_service(IProjectService) | ||
| name = project.name | ||
| try: | ||
| project_service.check_project_name_after_insert(name) | ||
| except ProjectNameUnavailableTypoSquattingError as exc: | ||
| request.log.warning( | ||
| "ProjectNameUnavailableTypoSquattingError", | ||
| check_name=exc.check_name, | ||
| existing_project_name=exc.existing_project_name, | ||
| ) | ||
| # Send notification to Admins for review | ||
| notification_service = request.find_service(IAdminNotificationService) | ||
|
|
||
| warehouse_domain = request.registry.settings.get("warehouse.domain") | ||
| new_project_page = request.route_url( | ||
| "packaging.project", | ||
| name=name, | ||
| _host=warehouse_domain, | ||
| ) | ||
| new_project_text = ( | ||
| f"During `file_upload`, Project Create for " | ||
| f"*<{new_project_page}|{name}>* was detected as a potential " | ||
| f"typo by the `{exc.check_name!r}` check." | ||
| ) | ||
| existing_project_page = request.route_url( | ||
| "packaging.project", | ||
| name=exc.existing_project_name, | ||
| _host=warehouse_domain, | ||
| ) | ||
| existing_project_text = ( | ||
| f"<{existing_project_page}|Existing project: " | ||
| f"{exc.existing_project_name}>" | ||
| ) | ||
|
|
||
| webhook_payload = { | ||
| "blocks": [ | ||
| { | ||
| "type": "header", | ||
| "text": { | ||
| "type": "plain_text", | ||
| "text": "TypoSnyper :warning:", | ||
| "emoji": True, | ||
| }, | ||
| }, | ||
| { | ||
| "type": "section", | ||
| "text": { | ||
| "type": "mrkdwn", | ||
| "text": new_project_text, | ||
| }, | ||
| }, | ||
| { | ||
| "type": "section", | ||
| "text": { | ||
| "type": "mrkdwn", | ||
| "text": existing_project_text, | ||
| }, | ||
| }, | ||
| {"type": "divider"}, | ||
| { | ||
| "type": "context", | ||
| "elements": [ | ||
| { | ||
| "type": "plain_text", | ||
| "text": "Once reviewed/confirmed, " | ||
| "react to this message with :white_check_mark:", | ||
| "emoji": True, | ||
| } | ||
| ], | ||
| }, | ||
| ] | ||
| } | ||
| notification_service.send_notification(payload=webhook_payload) | ||
|
|
||
| request.metrics.increment( | ||
| "warehouse.packaging.services.create_project.typo_squatting", | ||
| tags=[f"check_name:{exc.check_name!r}"], | ||
| ) | ||
|
|
||
|
|
||
| class DependencyKind(enum.IntEnum): | ||
| requires = 1 | ||
| provides = 2 | ||
|
|
||
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.
In other places we use our own
@db.listens_for(db.Session, ...- any reason to not use that?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.
My understanding is because that's a session event and not a instance event, it would require that we add a hook for every commit, and then iterate over every object created in the session to determine if any of them are a Project. I think that would work but it feels like overkill, although it does seem like we would be more likely to avoid unnecessary notifications as a result of
flush().Uh oh!
There was an error while loading. Please reload this page.
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.
The other problem withafter_commitis that the typo check emits SQL, which can't happen in theafter_commithook. See the test failures here: #17772There 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.
I spoke too soon, the failures in #17772 were unrelated.