3636from sentry .integrations .models .integration import Integration
3737from sentry .integrations .models .organization_integration import OrganizationIntegration
3838from sentry .integrations .pipeline import IntegrationPipeline
39- from sentry .integrations .referrer_ids import GITHUB_OPEN_PR_BOT_REFERRER , GITHUB_PR_BOT_REFERRER
39+ from sentry .integrations .referrer_ids import GITHUB_PR_BOT_REFERRER
4040from sentry .integrations .services .integration import integration_service
4141from sentry .integrations .services .repository import RpcRepository , repository_service
4242from sentry .integrations .source_code_management .commit_context import (
43- OPEN_PR_MAX_FILES_CHANGED ,
44- OPEN_PR_MAX_LINES_CHANGED ,
4543 CommitContextIntegration ,
46- OpenPRCommentWorkflow ,
4744 PRCommentWorkflow ,
48- PullRequestFile ,
49- PullRequestIssue ,
50- )
51- from sentry .integrations .source_code_management .language_parsers import (
52- get_patch_parsers_for_organization ,
5345)
5446from sentry .integrations .source_code_management .repo_trees import RepoTreesIntegration
5547from sentry .integrations .source_code_management .repository import RepositoryIntegration
7365from sentry .shared_integrations .constants import ERR_INTERNAL , ERR_UNAUTHORIZED
7466from sentry .shared_integrations .exceptions import ApiError , ApiInvalidRequestError , IntegrationError
7567from sentry .snuba .referrer import Referrer
76- from sentry .templatetags .sentry_helpers import small_count
7768from sentry .users .models .user import User
7869from sentry .users .services .user import RpcUser
7970from sentry .users .services .user .serial import serialize_rpc_user
@@ -585,9 +576,6 @@ def update_organization_config(self, data: MutableMapping[str, Any]) -> None:
585576 def get_pr_comment_workflow (self ) -> PRCommentWorkflow :
586577 return GitHubPRCommentWorkflow (integration = self )
587578
588- def get_open_pr_comment_workflow (self ) -> OpenPRCommentWorkflow :
589- return GitHubOpenPRCommentWorkflow (integration = self )
590-
591579 def create_comment_attribution (self , user_id , comment_text ):
592580 user = user_service .get_user (user_id )
593581 username = "Unknown User" if user is None else user .name
@@ -678,31 +666,6 @@ def get_comment_data(
678666 return comment_data
679667
680668
681- OPEN_PR_COMMENT_BODY_TEMPLATE = """\
682- ## 🔍 Existing Issues For Review
683- Your pull request is modifying functions with the following pre-existing issues:
684-
685- {issue_tables}""" .rstrip ()
686-
687- OPEN_PR_ISSUE_TABLE_TEMPLATE = """\
688- 📄 File: **{filename}**
689-
690- | Function | Unhandled Issue |
691- | :------- | :----- |
692- {issue_rows}"""
693-
694- OPEN_PR_ISSUE_TABLE_TOGGLE_TEMPLATE = """\
695- <details>
696- <summary><b>📄 File: {filename} (Click to Expand)</b></summary>
697-
698- | Function | Unhandled Issue |
699- | :------- | :----- |
700- {issue_rows}
701- </details>"""
702-
703- OPEN_PR_ISSUE_DESCRIPTION_LENGTH = 52
704-
705-
706669def process_api_error (e : ApiError ) -> list [dict [str , Any ]] | None :
707670 if e .json :
708671 message = e .json .get ("message" , "" )
@@ -717,127 +680,6 @@ def process_api_error(e: ApiError) -> list[dict[str, Any]] | None:
717680 return None
718681
719682
720- class GitHubOpenPRCommentWorkflow (OpenPRCommentWorkflow ):
721- integration : GitHubIntegration
722- organization_option_key = "sentry:github_open_pr_bot"
723- referrer = Referrer .GITHUB_PR_COMMENT_BOT
724- referrer_id = GITHUB_OPEN_PR_BOT_REFERRER
725-
726- def safe_for_comment (self , repo : Repository , pr : PullRequest ) -> list [dict [str , Any ]]:
727- client = self .integration .get_client ()
728- try :
729- pr_files = client .get_pullrequest_files (repo = repo .name , pull_number = pr .key )
730- except ApiError as e :
731- api_error_resp = process_api_error (e )
732- if api_error_resp is not None :
733- return api_error_resp
734- else :
735- raise
736-
737- changed_file_count = 0
738- changed_lines_count = 0
739- filtered_pr_files = []
740-
741- organization = Organization .objects .get_from_cache (id = repo .organization_id )
742- patch_parsers = get_patch_parsers_for_organization (organization )
743-
744- for file in pr_files :
745- filename = file ["filename" ]
746- # we only count the file if it's modified and if the file extension is in the list of supported file extensions
747- # we cannot look at deleted or newly added files because we cannot extract functions from the diffs
748- if file ["status" ] != "modified" or filename .split ("." )[- 1 ] not in patch_parsers :
749- continue
750-
751- changed_file_count += 1
752- changed_lines_count += file ["changes" ]
753- filtered_pr_files .append (file )
754-
755- if (
756- changed_file_count > OPEN_PR_MAX_FILES_CHANGED
757- or changed_lines_count > OPEN_PR_MAX_LINES_CHANGED
758- ):
759- return []
760-
761- return filtered_pr_files
762-
763- def get_pr_files (self , pr_files : list [dict [str , Any ]]) -> list [PullRequestFile ]:
764- # new files will not have sentry issues associated with them
765- # only fetch Python files
766- pullrequest_files = [
767- PullRequestFile (filename = file ["filename" ], patch = file ["patch" ])
768- for file in pr_files
769- if "patch" in file
770- ]
771-
772- return pullrequest_files
773-
774- def get_pr_files_safe_for_comment (
775- self , repo : Repository , pr : PullRequest
776- ) -> list [PullRequestFile ]:
777- pr_files = self .safe_for_comment (repo = repo , pr = pr )
778-
779- if len (pr_files ) == 0 :
780- return []
781-
782- return self .get_pr_files (pr_files )
783-
784- def get_comment_data (self , comment_body : str ) -> dict [str , Any ]:
785- return {
786- "body" : comment_body ,
787- }
788-
789- @staticmethod
790- def format_comment_url (url : str , referrer : str ) -> str :
791- return url + "?referrer=" + referrer
792-
793- @staticmethod
794- def format_open_pr_comment (issue_tables : list [str ]) -> str :
795- return OPEN_PR_COMMENT_BODY_TEMPLATE .format (issue_tables = "\n " .join (issue_tables ))
796-
797- @staticmethod
798- def format_open_pr_comment_subtitle (title_length , subtitle ):
799- # the title length + " " + subtitle should be <= 52
800- subtitle_length = OPEN_PR_ISSUE_DESCRIPTION_LENGTH - title_length - 1
801- return (
802- subtitle [: subtitle_length - 3 ] + "..." if len (subtitle ) > subtitle_length else subtitle
803- )
804-
805- def format_issue_table (
806- self ,
807- diff_filename : str ,
808- issues : list [PullRequestIssue ],
809- patch_parsers : dict [str , Any ],
810- toggle : bool ,
811- ) -> str :
812- language_parser = patch_parsers .get (diff_filename .split ("." )[- 1 ], None )
813-
814- if not language_parser :
815- return ""
816-
817- issue_row_template = language_parser .issue_row_template
818-
819- issue_rows = "\n " .join (
820- [
821- issue_row_template .format (
822- title = issue .title ,
823- subtitle = self .format_open_pr_comment_subtitle (len (issue .title ), issue .subtitle ),
824- url = self .format_comment_url (issue .url , GITHUB_OPEN_PR_BOT_REFERRER ),
825- event_count = small_count (issue .event_count ),
826- function_name = issue .function_name ,
827- affected_users = small_count (issue .affected_users ),
828- )
829- for issue in issues
830- ]
831- )
832-
833- if toggle :
834- return OPEN_PR_ISSUE_TABLE_TOGGLE_TEMPLATE .format (
835- filename = diff_filename , issue_rows = issue_rows
836- )
837-
838- return OPEN_PR_ISSUE_TABLE_TEMPLATE .format (filename = diff_filename , issue_rows = issue_rows )
839-
840-
841683class GitHubIntegrationProvider (IntegrationProvider ):
842684 key = IntegrationProviderSlug .GITHUB .value
843685 name = "GitHub"
0 commit comments