Skip to content
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

feat(issue-views): Update endpoints to send and receive page filters #83937

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

MichaelSun48
Copy link
Member

@MichaelSun48 MichaelSun48 commented Jan 23, 2025

This PR updates the two Groupsearch view endpoints:

  • PUT ../group-search-view: updated to optionally accept the four new page filter parameters. The frontend will be updated shortly to send over these parameters.
  • GET ../group-search-view: updated to return the four new page filter parameters. The frontend will not do anything with these parameters until later.
    • validating projects parameter in this endpoint will come in a later PR

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 23, 2025
Comment on lines +195 to +215
gsv = GroupSearchView.objects.get(id=view["id"], user_id=user_id)
gsv.name = view["name"]
gsv.query = view["query"]
gsv.query_sort = view["querySort"]
gsv.position = position
gsv.is_all_projects = view.get("isAllProjects", False)

if "projects" in view:
gsv.projects.set(view["projects"])

if "environments" in view:
gsv.environments = view["environments"]

if "timeFilters" in view:
gsv.time_filters = view["timeFilters"]

gsv.save()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't set projects directly in the .update() since it's an M2M, so we have to do this shenanigans

Copy link

codecov bot commented Jan 23, 2025

❌ 1 Tests Failed:

Tests completed Failed Passed Skipped
23661 1 23660 289
View the top 1 failed tests by shortest run time
tests.sentry.issues.endpoints.test_organization_group_search_views.OrganizationGroupSearchViewsProjectsTransactionTest::test_invalid_project_ids
Stack Traces | 15.3s run time
#x1B[1m#x1B[.../issues/endpoints/test_organization_group_search_views.py#x1B[0m:584: in test_invalid_project_ids
    assert response.content == {"detail": "One or more projects do not exist"}
#x1B[1m#x1B[31mE   assert b'{"detail":"...o not exist"}' == {'detail': 'O...do not exist'}#x1B[0m
#x1B[1m#x1B[31mE     #x1B[0m
#x1B[1m#x1B[31mE     Full diff:#x1B[0m
#x1B[1m#x1B[31mE     - {#x1B[0m
#x1B[1m#x1B[31mE     -     'detail': 'One or more projects do not exist',#x1B[0m
#x1B[1m#x1B[31mE     ? ^^^^       ^ ^^                                  ^#x1B[0m
#x1B[1m#x1B[31mE     + (b'{"detail":"One or more projects do not exist"}')#x1B[0m
#x1B[1m#x1B[31mE     ? ^^ ++      ^ ^                                 ++ ^#x1B[0m
#x1B[1m#x1B[31mE     - }#x1B[0m

To view more test analytics, go to the Test Analytics Dashboard
📢 Thoughts on this report? Let us know!

@MichaelSun48 MichaelSun48 marked this pull request as ready for review January 23, 2025 21:17
@MichaelSun48 MichaelSun48 requested a review from a team as a code owner January 23, 2025 21:17
Copy link
Member

@malwilley malwilley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelSun48 do you think there is value in feature flagging any of this logic? I'm just a bit worried about adding so much code that we can't easily roll back

try:
validate_projects(organization, request.user, view)
except ValidationError as e:
sentry_sdk.capture_message(e.args[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally we wouldn't want to capture the error since this is expected. Did you add this just to be certain nothing is wrong during rollout?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's right, the frontend should never be sending invalid projects anyways, so capturing this is mostly just to verify that.

def validate_projects(
org: Organization, user: User | AnonymousUser, view: GroupSearchViewValidatorResponse
) -> None:
if "projects" in view and view["projects"] is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there are quite a few if statements here. IMO it's more readable if you add early returns instead, but this is just a style preference so feel free to ignore

view["isAllProjects"] = True
view["projects"] = []
else:
projects = Project.objects.filter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a bit slow if you have many views since it does each in sequence. Do you think it's necessary to check this? The db transaction should fail if the project ID doesn't exist right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't consider that, great point! Yeah this definitely would have added a lot of unnecessary database queries

@MichaelSun48
Copy link
Member Author

do you think there is value in feature flagging any of this logic? I'm just a bit worried about adding so much code that we can't easily roll back

@malwilley Yeah I think I was initially adverse because it seemed pretty complicated ot split some of the serializer logic, but looking at this again, I don't think it should be too bad. I'll add that in in the next PR, good suggestion! fwiw, I think these changes should be pretty safe, assuming I correctly wrote it in a way that doesn't interfere with how the frontend works right now.

@malwilley
Copy link
Member

@MichaelSun48 no need to split if the changes are indeed trivial. I'd just make sure to run this locally in the UI and make sure that things are working well (for global views permissions on/off) before merging since the unit tests can't catch everything

response = self.client.put(
url, data={"views": views}, format="json", content_type="application/json"
)
assert response.status_code == 500
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should respond with a 4xx code since invalid project IDs are a bad request. Anytime I see a 500 I think it's an unexpected error

Copy link
Member Author

@MichaelSun48 MichaelSun48 Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not sure if this is possible since this check occurs when the transaction is committed and not when the view is actually updated, so it might be impossible to tell what caused the integrity violation (though I will dig deeper into this)

We could query the Projects table again and check, for each view, if it's projects exist in the QuerySet, but then the only purpose of that data query would be to make the error message more accurate...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not worth making the extra request, but I hope there is a way to discern the type of violation because I'm sure we'll get some Sentry issues at some point and will want to know if it's expected behavior or not

@@ -129,6 +157,20 @@ def bulk_update_views(
_update_existing_view(org, user_id, view, position=idx)


def pick_default_project(org: Organization, user: User | AnonymousUser) -> int:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't used anymore right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope! But it will eventually be used for the get endpoint 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants