Browse Source

fix(hc): Adds missing integration ID to github enterprise client (#68725)

Gabe Villalobos 11 months ago
parent
commit
f29fcd4252

+ 2 - 2
src/sentry/integrations/github_enterprise/client.py

@@ -5,12 +5,12 @@ from sentry.integrations.github.utils import get_jwt
 class GitHubEnterpriseAppsClient(GitHubClientMixin):
     integration_name = "github_enterprise"
 
-    def __init__(self, base_url, integration, app_id, private_key, verify_ssl):
+    def __init__(self, base_url, integration, app_id, private_key, verify_ssl, org_integration_id):
         self.base_url = f"https://{base_url}"
         self.integration = integration
         self.app_id = app_id
         self.private_key = private_key
-        super().__init__(verify_ssl=verify_ssl)
+        super().__init__(verify_ssl=verify_ssl, org_integration_id=org_integration_id)
 
     def build_url(self, path: str) -> str:
         if path.startswith("/"):

+ 5 - 1
src/sentry/integrations/github_enterprise/integration.py

@@ -27,7 +27,7 @@ from sentry.models.repository import Repository
 from sentry.pipeline import NestedPipelineView, PipelineView
 from sentry.services.hybrid_cloud.organization import RpcOrganizationSummary
 from sentry.shared_integrations.constants import ERR_INTERNAL, ERR_UNAUTHORIZED
-from sentry.shared_integrations.exceptions import ApiError
+from sentry.shared_integrations.exceptions import ApiError, IntegrationError
 from sentry.utils import jwt
 from sentry.utils.http import absolute_uri
 from sentry.web.helpers import render_to_response
@@ -137,6 +137,9 @@ class GitHubEnterpriseIntegration(
     codeowners_locations = ["CODEOWNERS", ".github/CODEOWNERS", "docs/CODEOWNERS"]
 
     def get_client(self):
+        if not self.org_integration:
+            raise IntegrationError("Organization Integration does not exist")
+
         base_url = self.model.metadata["domain_name"].split("/")[0]
         return GitHubEnterpriseAppsClient(
             base_url=base_url,
@@ -144,6 +147,7 @@ class GitHubEnterpriseIntegration(
             private_key=self.model.metadata["installation"]["private_key"],
             app_id=self.model.metadata["installation"]["id"],
             verify_ssl=self.model.metadata["installation"]["verify_ssl"],
+            org_integration_id=self.org_integration.id,
         )
 
     def get_repositories(self, query=None):

+ 12 - 9
tests/sentry/integrations/github_enterprise/test_issues.py

@@ -1,4 +1,5 @@
 from functools import cached_property
+from typing import cast
 from unittest.mock import patch
 
 import responses
@@ -35,12 +36,14 @@ class GitHubEnterpriseIssueBasicTest(TestCase, IntegratedApiTestCase):
                 },
             )
             self.model.add_organization(self.organization, self.user)
-        self.integration = GitHubEnterpriseIntegration(self.model, self.organization.id)
+        install = self.model.get_installation(self.organization.id)
+        self.install = cast(GitHubEnterpriseIntegration, install)
 
     def _check_proxying(self) -> None:
         assert len(responses.calls) == 1
         request = responses.calls[0].request
-        assert request.headers[PROXY_OI_HEADER] == str(None)
+        assert self.install.org_integration is not None
+        assert request.headers[PROXY_OI_HEADER] == str(self.install.org_integration.id)
         assert request.headers[PROXY_BASE_URL_HEADER] == f"https://{self._IP_ADDRESS}"
         assert PROXY_SIGNATURE_HEADER in request.headers
 
@@ -60,7 +63,7 @@ class GitHubEnterpriseIssueBasicTest(TestCase, IntegratedApiTestCase):
         )
 
         repo = "getsentry/sentry"
-        assert self.integration.get_allowed_assignees(repo) == (
+        assert self.install.get_allowed_assignees(repo) == (
             ("", "Unassigned"),
             ("MeredithAnya", "MeredithAnya"),
         )
@@ -93,7 +96,7 @@ class GitHubEnterpriseIssueBasicTest(TestCase, IntegratedApiTestCase):
 
         repo = "getsentry/sentry"
         # results should be sorted alphabetically
-        assert self.integration.get_repo_labels(repo) == (
+        assert self.install.get_repo_labels(repo) == (
             ("bug", "bug"),
             ("duplicate", "duplicate"),
             ("enhancement", "enhancement"),
@@ -136,7 +139,7 @@ class GitHubEnterpriseIssueBasicTest(TestCase, IntegratedApiTestCase):
             "description": "This is the description",
         }
 
-        assert self.integration.create_issue(form_data) == {
+        assert self.install.create_issue(form_data) == {
             "key": 321,
             "description": "This is the description",
             "title": "hello",
@@ -177,7 +180,7 @@ class GitHubEnterpriseIssueBasicTest(TestCase, IntegratedApiTestCase):
             json=[{"number": 321, "title": "hello", "body": "This is the description"}],
         )
         repo = "getsentry/sentry"
-        assert self.integration.get_repo_issues(repo) == ((321, "#321 hello"),)
+        assert self.install.get_repo_issues(repo) == ((321, "#321 hello"),)
 
         if self.should_call_api_without_proxying():
             assert len(responses.calls) == 2
@@ -193,7 +196,7 @@ class GitHubEnterpriseIssueBasicTest(TestCase, IntegratedApiTestCase):
     @responses.activate
     @patch("sentry.integrations.github_enterprise.client.get_jwt", return_value="jwt_token_1")
     def test_link_issue(self, mock_get_jwt):
-        issue_id = 321
+        issue_id = "321"
         responses.add(
             responses.POST,
             f"https://{self._IP_ADDRESS}/api/v3/app/installations/installation_id/access_tokens",
@@ -213,7 +216,7 @@ class GitHubEnterpriseIssueBasicTest(TestCase, IntegratedApiTestCase):
 
         data = {"repo": "getsentry/sentry", "externalIssue": issue_id, "comment": "hello"}
 
-        assert self.integration.get_issue(issue_id, data=data) == {
+        assert self.install.get_issue(issue_id, data=data) == {
             "key": issue_id,
             "description": "This is the description",
             "title": "hello",
@@ -252,7 +255,7 @@ class GitHubEnterpriseIssueBasicTest(TestCase, IntegratedApiTestCase):
             organization_id=self.organization.id, integration_id=self.model.id, key="hello#321"
         )
 
-        self.integration.after_link_issue(external_issue, data=data)
+        self.install.after_link_issue(external_issue, data=data)
 
         if self.should_call_api_without_proxying():
             assert len(responses.calls) == 2