-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Move typo checks to after_insert
#17764
Open
di
wants to merge
3
commits into
pypi:main
Choose a base branch
from
di:move-typo-checks-to-after-insert
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 |
---|---|---|
|
@@ -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 | ||
|
This file contains 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
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.
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()
.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_commit
is that the typo check emits SQL, which can't happen in theafter_commit
hook. 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.