Skip to content

surface conflicting prefixes when reviewing organizations #18036

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

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions tests/unit/admin/views/test_organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,30 +701,38 @@ def test_detail_is_approved_false(self, db_request):

@pytest.mark.usefixtures("_enable_organizations")
@pytest.mark.parametrize(
("name", "conflicts"),
("name", "conflicts", "conflicting_prefixes", "not_conflicting"),
[
("pypi", ["PyPI", "pypi"]),
("py-pi", ["Py-PI", "PY-PI"]),
(
"pypi",
["PyPI", "pypi"],
["pypi-common", "PyPi_rocks", "pypi-team-garbage"],
["py-pi"],
),
("py-pi", ["Py-PI", "PY-PI"], ["py", "py-pi_dot-com"], ["pypi"]),
],
)
def test_detail_conflicting_applications(self, db_request, name, conflicts):
def test_detail_conflicting_applications(
self, db_request, name, conflicts, conflicting_prefixes, not_conflicting
):
organization_application = OrganizationApplicationFactory.create(
name=name, status=OrganizationApplicationStatus.Declined
)
conflicting_applications = sorted(
[
OrganizationApplicationFactory.create(name=conflict)
for conflict in conflicts
for conflict in conflicts + conflicting_prefixes
],
key=lambda o: o.submitted,
)
[OrganizationApplicationFactory.create(name=name) for name in not_conflicting]
db_request.matchdict["organization_application_id"] = (
organization_application.id
)
result = views.organization_application_detail(db_request)
assert result["user"] == organization_application.submitted_by
assert result["form"].name.data == organization_application.name
assert result["conflicting_applications"] == conflicting_applications
assert set(result["conflicting_applications"]) == set(conflicting_applications)
assert result["organization_application"] == organization_application

@pytest.mark.usefixtures("_enable_organizations")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,18 @@ <h3 class="card-title">Organization Request{% if information_requests %}{% if ou
{% if conflicting_applications %}
<div class="form-group">
<label class="col-12 control-label">
Conflicting Applications <i class="fa fa-scale-unbalanced text-red"></i>
Conflicting Applications <i class="fa fa-clipboard-question text-orange"></i>
</label>
<div class="col-12">
<table class="table">
<tr><th>Application</th><th>Submitted</th><th>Requestor</th></tr>
<tr><th>Application</th><th>Status</th><th>Submitted</th><th>Requestor</th></tr>
{% for application in conflicting_applications %}
<tr>
<td><a target="_blank" href="{{ request.route_url('admin.organization_application.detail', organization_application_id=application.id) }}">{{ application.name }}</a></td>
<td>
{% if application.normalized_name == organization_application.normalized_name %}<i class="fa fa-equals text-red"></i>{% endif %}
<a target="_blank" href="{{ request.route_url('admin.organization_application.detail', organization_application_id=application.id) }}">{{ application.name }}</a>
</td>
<td>{{ application.status }}</td>
<td>{{ application.submitted|format_date() }}</td>
<td><a target="_blank" href="{{ request.route_url('admin.user.detail', username=application.submitted_by.username) }}">{{ application.submitted_by.username }}</a></td>
</tr>
Expand Down
30 changes: 26 additions & 4 deletions warehouse/admin/views/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from paginate_sqlalchemy import SqlalchemyOrmPage as SQLAlchemyORMPage
from pyramid.httpexceptions import HTTPBadRequest, HTTPNotFound, HTTPSeeOther
from pyramid.view import view_config
from sqlalchemy import or_
from sqlalchemy import desc, func, or_
from sqlalchemy.orm import joinedload

from warehouse.accounts.interfaces import IUserService
Expand Down Expand Up @@ -361,14 +361,36 @@ def organization_application_detail(request):
)
return HTTPSeeOther(location=request.current_route_path())

parts = organization_application.normalized_name.split("-")
conflicting_applications = (
request.db.query(OrganizationApplication)
.filter(
OrganizationApplication.normalized_name
== organization_application.normalized_name
or_(
*(
[
OrganizationApplication.normalized_name == parts[0],
OrganizationApplication.normalized_name.startswith(
parts[0] + "-"
),
]
+ [
OrganizationApplication.normalized_name.startswith(
"-".join(parts[: i + 1])
)
for i in range(1, len(parts))
]
)
)
)
.filter(OrganizationApplication.id != organization_application.id)
.order_by(OrganizationApplication.submitted)
.order_by(
desc(
func.similarity(
OrganizationApplication.normalized_name,
organization_application.normalized_name,
)
)
)
.all()
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
add pg_trgm extension

Revision ID: 13c1c0ac92e9
Revises: c8384ca429fc
Create Date: 2025-04-29 08:37:33.788528
"""

from alembic import op

revision = "13c1c0ac92e9"
down_revision = "c8384ca429fc"


def upgrade():
op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm")


def downgrade():
pass