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

GitHub Enterprise feature parity #52951

Merged
merged 11 commits into from
Oct 17, 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
1 change: 1 addition & 0 deletions src/sentry/api/bases/external_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

AVAILABLE_PROVIDERS = {
ExternalProviders.GITHUB,
ExternalProviders.GITHUB_ENTERPRISE,
ExternalProviders.GITLAB,
ExternalProviders.SLACK,
ExternalProviders.MSTEAMS,
Expand Down
6 changes: 5 additions & 1 deletion src/sentry/api/validators/project_codeowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def validate_codeowners_associations(
external_actors = ExternalActor.objects.filter(
external_name__in=usernames + team_names,
organization_id=project.organization_id,
provider__in=[ExternalProviders.GITHUB.value, ExternalProviders.GITLAB.value],
provider__in=[
ExternalProviders.GITHUB.value,
ExternalProviders.GITHUB_ENTERPRISE.value,
ExternalProviders.GITLAB.value,
],
)

# Convert CODEOWNERS into IssueOwner syntax
Expand Down
10 changes: 9 additions & 1 deletion src/sentry/integrations/github_enterprise/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ class GitHubEnterpriseAppsClient(GitHubClientMixin):
integration_name = "github_enterprise"

def __init__(self, base_url, integration, app_id, private_key, verify_ssl):
self.base_url = f"https://{base_url}/api/v3"
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)

def build_url(self, path: str) -> str:
if path.startswith("/"):
if path == "/graphql":
path = "/api/graphql"
else:
path = "/api/v3/{}".format(path.lstrip("/"))
return super().build_url(path)

def _get_installation_id(self) -> str:
return self.integration.metadata["installation_id"]

Expand Down
86 changes: 83 additions & 3 deletions src/sentry/integrations/github_enterprise/integration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

from typing import Any
from datetime import timezone
from typing import Any, Mapping, Sequence
from urllib.parse import urlparse

from django import forms
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
from isodate import parse_datetime
from rest_framework.request import Request

from sentry import http
Expand All @@ -21,7 +23,9 @@
from sentry.integrations.github.issues import GitHubIssueBasic
from sentry.integrations.github.utils import get_jwt
from sentry.integrations.mixins import RepositoryMixin
from sentry.integrations.mixins.commit_context import CommitContextMixin
from sentry.models.integrations.integration import Integration
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
Expand Down Expand Up @@ -58,6 +62,19 @@
""",
IntegrationFeatures.ISSUE_BASIC,
),
FeatureDescription(
"""
Link your Sentry stack traces back to your GitHub source code with stack
trace linking.
""",
IntegrationFeatures.STACKTRACE_LINK,
),
FeatureDescription(
"""
Import your GitHub [CODEOWNERS file](https://docs.sentry.io/product/integrations/source-code-mgmt/github/#code-owners) and use it alongside your ownership rules to assign Sentry issues.
""",
IntegrationFeatures.CODEOWNERS,
),
]


Expand Down Expand Up @@ -109,8 +126,11 @@
}


class GitHubEnterpriseIntegration(IntegrationInstallation, GitHubIssueBasic, RepositoryMixin):
class GitHubEnterpriseIntegration(
IntegrationInstallation, GitHubIssueBasic, RepositoryMixin, CommitContextMixin
):
repo_search = True
codeowners_locations = ["CODEOWNERS", ".github/CODEOWNERS", "docs/CODEOWNERS"]

def get_client(self):
base_url = self.model.metadata["domain_name"].split("/")[0]
Expand Down Expand Up @@ -163,6 +183,59 @@ def message_from_error(self, exc):
else:
return ERR_INTERNAL

def format_source_url(self, repo: Repository, filepath: str, branch: str) -> str:
# Must format the url ourselves since `check_file` is a head request
# "https://github.example.org/octokit/octokit.rb/blob/master/README.md"
return f"{repo.url}/blob/{branch}/{filepath}"

def get_commit_context(
self, repo: Repository, filepath: str, ref: str, event_frame: Mapping[str, Any]
) -> Mapping[str, str] | None:
lineno = event_frame.get("lineno", 0)
if not lineno:
return None
try:
blame_range: Sequence[Mapping[str, Any]] | None = self.get_blame_for_file(
repo, filepath, ref, lineno
)

if blame_range is None:
return None
except ApiError as e:
raise e

try:
commit: Mapping[str, Any] = max(
(
blame
for blame in blame_range
if blame.get("startingLine", 0) <= lineno <= blame.get("endingLine", 0)
and blame.get("commit", {}).get("committedDate")
),
key=lambda blame: parse_datetime(blame.get("commit", {}).get("committedDate")),
default={},
)
if not commit:
return None
except (ValueError, IndexError):
return None

commitInfo = commit.get("commit")
if not commitInfo:
return None
else:
committed_date = parse_datetime(commitInfo.get("committedDate")).astimezone(
timezone.utc
)

return {
"commitId": commitInfo.get("oid"),
"committedDate": committed_date,
"commitMessage": commitInfo.get("message"),
"commitAuthorName": commitInfo.get("author", {}).get("name"),
"commitAuthorEmail": commitInfo.get("author", {}).get("email"),
}


class InstallationForm(forms.Form):
url = forms.CharField(
Expand Down Expand Up @@ -273,7 +346,14 @@ class GitHubEnterpriseIntegrationProvider(GitHubIntegrationProvider):
name = "GitHub Enterprise"
metadata = metadata
integration_cls = GitHubEnterpriseIntegration
features = frozenset([IntegrationFeatures.COMMITS, IntegrationFeatures.ISSUE_BASIC])
features = frozenset(
[
IntegrationFeatures.COMMITS,
IntegrationFeatures.ISSUE_BASIC,
IntegrationFeatures.STACKTRACE_LINK,
IntegrationFeatures.CODEOWNERS,
]
)

def _make_identity_pipeline_view(self):
"""
Expand Down
1 change: 1 addition & 0 deletions src/sentry/models/integrations/external_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ExternalActor(DefaultFieldsModel):
(ExternalProviders.MSTEAMS, "msteams"),
(ExternalProviders.PAGERDUTY, "pagerduty"),
(ExternalProviders.GITHUB, "github"),
(ExternalProviders.GITHUB_ENTERPRISE, "github_enterprise"),
(ExternalProviders.GITLAB, "gitlab"),
# TODO: do migration to delete this from database
(ExternalProviders.CUSTOM, "custom_scm"),
Expand Down
4 changes: 4 additions & 0 deletions src/sentry/types/integrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ExternalProviders(ValueEqualityEnum):
DISCORD = 140
OPSGENIE = 150
GITHUB = 200
GITHUB_ENTERPRISE = 201
GITLAB = 210

# TODO: do migration to delete this from database
Expand All @@ -33,6 +34,7 @@ class ExternalProviderEnum(Enum):
DISCORD = "discord"
OPSGENIE = "opsgenie"
GITHUB = "github"
GITHUB_ENTERPRISE = "github_enterprise"
GITLAB = "gitlab"
CUSTOM = "custom_scm"

Expand All @@ -45,6 +47,7 @@ class ExternalProviderEnum(Enum):
ExternalProviderEnum.DISCORD: ExternalProviders.DISCORD,
ExternalProviderEnum.OPSGENIE: ExternalProviders.OPSGENIE,
ExternalProviderEnum.GITHUB: ExternalProviders.GITHUB,
ExternalProviderEnum.GITHUB_ENTERPRISE: ExternalProviders.GITHUB_ENTERPRISE,
ExternalProviderEnum.GITLAB: ExternalProviders.GITLAB,
ExternalProviderEnum.CUSTOM: ExternalProviders.CUSTOM,
}
Expand All @@ -57,6 +60,7 @@ class ExternalProviderEnum(Enum):
ExternalProviders.DISCORD: ExternalProviderEnum.DISCORD.value,
ExternalProviders.OPSGENIE: ExternalProviderEnum.OPSGENIE.value,
ExternalProviders.GITHUB: ExternalProviderEnum.GITHUB.value,
ExternalProviders.GITHUB_ENTERPRISE: ExternalProviderEnum.GITHUB_ENTERPRISE.value,
ExternalProviders.GITLAB: ExternalProviderEnum.GITLAB.value,
ExternalProviders.CUSTOM: ExternalProviderEnum.CUSTOM.value,
}
Expand Down
152 changes: 152 additions & 0 deletions tests/sentry/integrations/github_enterprise/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import base64
from unittest import mock

import pytest
import responses

from sentry.integrations.github_enterprise.integration import GitHubEnterpriseIntegration
from sentry.models.repository import Repository
from sentry.shared_integrations.exceptions import ApiError
from sentry.testutils.cases import TestCase

GITHUB_CODEOWNERS = {
"filepath": "CODEOWNERS",
"html_url": "https://github.example.org/Test-Organization/foo/blob/master/CODEOWNERS",
"raw": "docs/* @jianyuan @getsentry/ecosystem\n* @jianyuan\n",
}


class GitHubAppsClientTest(TestCase):
base_url = "https://github.example.org/api/v3"

def setUp(self):
super().setUp()

patcher_1 = mock.patch(
"sentry.integrations.github_enterprise.client.get_jwt", return_value="jwt_token_1"
)
patcher_1.start()
self.addCleanup(patcher_1.stop)

patcher_2 = mock.patch(
"sentry.integrations.github_enterprise.integration.get_jwt", return_value="jwt_token_1"
)
patcher_2.start()
self.addCleanup(patcher_2.stop)

integration = self.create_integration(
organization=self.organization,
provider="github_enterprise",
name="Github Test Org",
external_id="1",
metadata={
"access_token": None,
"expires_at": None,
"icon": "https://github.example.org/avatar.png",
"domain_name": "github.example.org/Test-Organization",
"account_type": "Organization",
"installation_id": "install_id_1",
"installation": {
"client_id": "client_id",
"client_secret": "client_secret",
"id": "2",
"name": "test-app",
"private_key": "private_key",
"url": "github.example.org",
"webhook_secret": "webhook_secret",
"verify_ssl": True,
},
},
)
self.repo = Repository.objects.create(
organization_id=self.organization.id,
name="Test-Organization/foo",
url="https://github.example.org/Test-Organization/foo",
provider="integrations:github_enterprise",
external_id=123,
integration_id=integration.id,
)
install = integration.get_installation(organization_id=self.organization.id)
assert isinstance(install, GitHubEnterpriseIntegration)
self.install = install
self.gh_client = self.install.get_client()
responses.add(
method=responses.POST,
url=f"{self.base_url}/app/installations/install_id_1/access_tokens",
body='{"token": "12345token", "expires_at": "2030-01-01T00:00:00Z"}',
status=200,
content_type="application/json",
)

@responses.activate
def test_check_file(self):
path = "src/sentry/integrations/github/client.py"
version = "master"
url = f"{self.base_url}/repos/{self.repo.name}/contents/{path}?ref={version}"

responses.add(
method=responses.HEAD,
url=url,
json={"text": 200},
)

resp = self.gh_client.check_file(self.repo, path, version)
assert resp.status_code == 200

@responses.activate
def test_check_no_file(self):
path = "src/santry/integrations/github/client.py"
version = "master"
url = f"{self.base_url}/repos/{self.repo.name}/contents/{path}?ref={version}"

responses.add(method=responses.HEAD, url=url, status=404)

with pytest.raises(ApiError):
self.gh_client.check_file(self.repo, path, version)
assert responses.calls[1].response.status_code == 404

@responses.activate
def test_get_stacktrace_link(self):
path = "/src/sentry/integrations/github/client.py"
version = "master"
url = f"{self.base_url}/repos/{self.repo.name}/contents/{path.lstrip('/')}?ref={version}"

responses.add(
method=responses.HEAD,
url=url,
json={"text": 200},
)

source_url = self.install.get_stacktrace_link(self.repo, path, "master", version)
assert (
source_url
== "https://github.example.org/Test-Organization/foo/blob/master/src/sentry/integrations/github/client.py"
)

@mock.patch(
"sentry.integrations.github.integration.GitHubIntegration.check_file",
return_value=GITHUB_CODEOWNERS["html_url"],
)
@responses.activate
def test_get_codeowner_file(self, mock_check_file):
self.config = self.create_code_mapping(
repo=self.repo,
project=self.project,
)
url = f"{self.base_url}/repos/{self.repo.name}/contents/CODEOWNERS?ref=master"

responses.add(
method=responses.HEAD,
url=url,
json={"text": 200},
)
responses.add(
method=responses.GET,
url=url,
json={"content": base64.b64encode(GITHUB_CODEOWNERS["raw"].encode()).decode("ascii")},
)
result = self.install.get_codeowner_file(
self.config.repository, ref=self.config.default_branch
)

assert result == GITHUB_CODEOWNERS
Loading
Loading