Skip to content

feat(open-pr-comments): get sentry projects and filenames #59952

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 4 commits into from
Nov 15, 2023
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
32 changes: 30 additions & 2 deletions src/sentry/tasks/integrations/github/open_pr_comment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from __future__ import annotations

import logging
from typing import List
from typing import List, Set, Tuple

from django.db.models import Value
from django.db.models.functions import StrIndex

from sentry.integrations.github.client import GitHubAppsClient
from sentry.models.integrations.repository_project_path_config import RepositoryProjectPathConfig
from sentry.models.project import Project
from sentry.models.pullrequest import PullRequest
from sentry.models.repository import Repository
from sentry.shared_integrations.exceptions.base import ApiError
Expand Down Expand Up @@ -44,7 +49,7 @@ def safe_for_comment(
OPEN_PR_METRIC_BASE.format(key="api_error"),
tags={"type": GithubAPIErrorType.UNKNOWN.value, "code": e.code},
)
logger.exception("github.open_pr_comment.unknown_api_error")
logger.exception("github.open_pr_comment.unknown_api_error", extra={"error": str(e)})
return False

safe_to_comment = True
Expand Down Expand Up @@ -74,3 +79,26 @@ def get_pr_filenames(
# new files will not have sentry issues associated with them
pr_filenames: List[str] = [file["filename"] for file in pr_files if file["status"] != "added"]
return pr_filenames


def get_projects_and_filenames_from_source_file(
org_id: int,
pr_filename: str,
) -> Tuple[Set[Project], Set[str]]:
# fetch the code mappings in which the source_root is a substring at the start of pr_filename
code_mappings = (
RepositoryProjectPathConfig.objects.filter(organization_id=org_id)
.annotate(substring_match=StrIndex(Value(pr_filename), "source_root"))
.filter(substring_match=1)
Comment on lines +90 to +92
Copy link
Member Author

Choose a reason for hiding this comment

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

StrIndex returns the index in which there is a match, but uses 1-indexing. it returns 0 if there is no match

Copy link
Contributor

Choose a reason for hiding this comment

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

Q: is this case-sensitive?

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'm assuming it works like regular string indexing, but i can check

Copy link
Contributor

Choose a reason for hiding this comment

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

Apparently it depends on the DB backend used. In our case it's PostgreSQL, where it's rendered as STRPOS(), which is indeed case-sensitive.

)

project_list: Set[Project] = set()
sentry_filenames = set()

if len(code_mappings):
for code_mapping in code_mappings:
project_list.add(code_mapping.project)
sentry_filenames.add(
pr_filename.replace(code_mapping.source_root, code_mapping.stack_root)
)
return project_list, sentry_filenames
63 changes: 61 additions & 2 deletions tests/sentry/tasks/integrations/github/test_open_pr_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import responses

from sentry.tasks.integrations.github.open_pr_comment import get_pr_filenames, safe_for_comment
from sentry.tasks.integrations.github.open_pr_comment import (
get_pr_filenames,
get_projects_and_filenames_from_source_file,
safe_for_comment,
)
from sentry.testutils.silo import region_silo_test
from sentry.testutils.skips import requires_snuba
from tests.sentry.tasks.integrations.github.test_pr_comment import GithubCommentTestCase
Expand Down Expand Up @@ -144,7 +148,7 @@ def setUp(self):
self.gh_client = installation.get_client()

@responses.activate
def test_simple(self):
def test_get_pr_filenames(self):
responses.add(
responses.GET,
self.gh_path.format(pull_number=self.pr.key),
Expand All @@ -157,3 +161,58 @@ def test_simple(self):
)

assert set(get_pr_filenames(self.gh_client, self.gh_repo, self.pr)) == {"bar.py", "baz.py"}

def test_get_projects_and_filenames_from_source_file(self):
projects = [self.create_project() for _ in range(4)]

source_stack_pairs = [
("", "./"),
("src/sentry", "sentry/"),
("src/", ""),
("src/sentry/", "sentry/"),
]
for i, pair in enumerate(source_stack_pairs):
source_root, stack_root = pair
self.create_code_mapping(
project=projects[i],
repo=self.gh_repo,
source_root=source_root,
stack_root=stack_root,
default_branch="master",
)

# matching code mapping from a different org
other_org_code_mapping = self.create_code_mapping(
project=self.another_org_project,
repo=self.another_org_repo,
source_root="",
stack_root="./",
)
other_org_code_mapping.organization_id = self.another_organization.id
other_org_code_mapping.save()

source_stack_nonmatches = [
("/src/sentry", "sentry"),
("tests/", "tests/"),
("app/", "static/app"),
]
for source_root, stack_root in source_stack_nonmatches:
self.create_code_mapping(
project=self.create_project(),
repo=self.gh_repo,
source_root=source_root,
stack_root=stack_root,
default_branch="master",
)

filename = "src/sentry/tasks/integrations/github/open_pr_comment.py"
correct_filenames = [
filename.replace(source_root, stack_root)
for source_root, stack_root in source_stack_pairs
]

project_list, sentry_filenames = get_projects_and_filenames_from_source_file(
self.organization.id, filename
)
assert project_list == set(projects)
assert sentry_filenames == set(correct_filenames)