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(bitbucket-server): CODEOWNERS support and stacktrace linking #86639

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
36 changes: 19 additions & 17 deletions src/sentry/integrations/bitbucket_server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from requests_oauthlib import OAuth1

from sentry.identity.services.identity.model import RpcIdentity
from sentry.integrations.base import IntegrationFeatureNotImplementedError
from sentry.integrations.bitbucket_server.utils import BitbucketServerAPIPath
from sentry.integrations.client import ApiClient
from sentry.integrations.models.integration import Integration
from sentry.integrations.services.integration.model import RpcIntegration
Expand All @@ -17,20 +17,6 @@
logger = logging.getLogger("sentry.integrations.bitbucket_server")


class BitbucketServerAPIPath:
"""
project is the short key of the project
repo is the fully qualified slug
"""

repository = "/rest/api/1.0/projects/{project}/repos/{repo}"
repositories = "/rest/api/1.0/repos"
repository_hook = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks/{id}"
repository_hooks = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks"
repository_commits = "/rest/api/1.0/projects/{project}/repos/{repo}/commits"
commit_changes = "/rest/api/1.0/projects/{project}/repos/{repo}/commits/{commit}/changes"


class BitbucketServerSetupClient(ApiClient):
"""
Client for making requests to Bitbucket Server to follow OAuth1 flow.
Expand Down Expand Up @@ -256,9 +242,25 @@ def _get_values(self, uri, params, max_pages=1000000):
return values

def check_file(self, repo: Repository, path: str, version: str | None) -> object | None:
raise IntegrationFeatureNotImplementedError
return self.head_cached(
path=BitbucketServerAPIPath.build_source(
project=repo.config["project"],
repo=repo.config["repo"],
path=path,
sha=version,
),
)

def get_file(
self, repo: Repository, path: str, ref: str | None, codeowners: bool = False
) -> str:
raise IntegrationFeatureNotImplementedError
response = self.get_cached(
path=BitbucketServerAPIPath.build_raw(
project=repo.config["project"],
repo=repo.config["repo"],
path=path,
sha=ref,
),
raw_response=True,
)
return response.text
58 changes: 51 additions & 7 deletions src/sentry/integrations/bitbucket_server/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from collections.abc import Mapping
from typing import Any
from urllib.parse import urlparse
from urllib.parse import parse_qs, quote, urlencode, urlparse

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import load_pem_private_key
Expand All @@ -19,7 +19,6 @@
FeatureDescription,
IntegrationData,
IntegrationDomain,
IntegrationFeatureNotImplementedError,
IntegrationFeatures,
IntegrationMetadata,
IntegrationProvider,
Expand Down Expand Up @@ -62,6 +61,19 @@
""",
IntegrationFeatures.COMMITS,
),
FeatureDescription(
"""
Link your Sentry stack traces back to your Bitbucket Server source code with stack
trace linking.
""",
IntegrationFeatures.STACKTRACE_LINK,
),
FeatureDescription(
"""
Import your Bitbucket Server [CODEOWNERS file](https://support.atlassian.com/bitbucket-cloud/docs/set-up-and-use-code-owners/) and use it alongside your ownership rules to assign Sentry issues.
""",
IntegrationFeatures.CODEOWNERS,
),
]

setup_alert = {
Expand Down Expand Up @@ -246,6 +258,8 @@ class BitbucketServerIntegration(RepositoryIntegration):

default_identity = None

codeowners_locations = [".bitbucket/CODEOWNERS"]

@property
def integration_name(self) -> str:
return "bitbucket_server"
Expand Down Expand Up @@ -312,16 +326,40 @@ def get_unmigratable_repositories(self):
return list(filter(lambda repo: repo.name not in accessible_repos, repos))

def source_url_matches(self, url: str) -> bool:
raise IntegrationFeatureNotImplementedError
return url.startswith(self.model.metadata["base_url"])

def format_source_url(self, repo: Repository, filepath: str, branch: str | None) -> str:
raise IntegrationFeatureNotImplementedError
project = quote(repo.config["project"])
repo_name = quote(repo.config["repo"])
source_url = f"{self.model.metadata["base_url"]}/projects/{project}/repos/{repo_name}/browse/{filepath}"

if branch:
source_url += "?" + urlencode({"at": branch})

return source_url

def extract_branch_from_source_url(self, repo: Repository, url: str) -> str:
raise IntegrationFeatureNotImplementedError
parsed_url = urlparse(url)
qs = parse_qs(parsed_url.query)

if "at" in qs and len(qs["at"]) == 1:
Copy link
Member

Choose a reason for hiding this comment

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

could you explain what the "at" means here? i am curious how this is different from bitbucket

Copy link
Member

Choose a reason for hiding this comment

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

i see, at refers to the branch i think

branch = qs["at"][0]

# branch name may be prefixed with refs/heads/, so we strip that
refs_prefix = "refs/heads/"
if branch.startswith(refs_prefix):
branch = branch[len(refs_prefix) :]

return branch

return ""

def extract_source_path_from_source_url(self, repo: Repository, url: str) -> str:
raise IntegrationFeatureNotImplementedError
if repo.url is None:
return ""
parsed_repo_url = urlparse(repo.url)
parsed_url = urlparse(url)
return parsed_url.path.replace(parsed_repo_url.path + "/", "")
Comment on lines +358 to +362
Copy link
Member

Choose a reason for hiding this comment

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

i'm also curious how this is different from bitbucket, it kind of looks the same?


# Bitbucket Server only methods

Expand All @@ -336,7 +374,13 @@ class BitbucketServerIntegrationProvider(IntegrationProvider):
metadata = metadata
integration_cls = BitbucketServerIntegration
needs_default_identity = True
features = frozenset([IntegrationFeatures.COMMITS])
features = frozenset(
[
IntegrationFeatures.COMMITS,
IntegrationFeatures.STACKTRACE_LINK,
IntegrationFeatures.CODEOWNERS,
]
)
setup_dialog_config = {"width": 1030, "height": 1000}

def get_pipeline_views(self) -> list[PipelineView]:
Expand Down
37 changes: 37 additions & 0 deletions src/sentry/integrations/bitbucket_server/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from urllib.parse import quote, urlencode


class BitbucketServerAPIPath:
"""
project is the short key of the project
repo is the fully qualified slug
"""

repository = "/rest/api/1.0/projects/{project}/repos/{repo}"
repositories = "/rest/api/1.0/repos"
repository_hook = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks/{id}"
repository_hooks = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks"
repository_commits = "/rest/api/1.0/projects/{project}/repos/{repo}/commits"
commit_changes = "/rest/api/1.0/projects/{project}/repos/{repo}/commits/{commit}/changes"

@staticmethod
def build_raw(project: str, repo: str, path: str, sha: str) -> str:
project = quote(project)
repo = quote(repo)

params = {}
if sha:
params["at"] = sha

return f"/projects/{project}/repos/{repo}/raw/{path}?{urlencode(params)}"

@staticmethod
def build_source(project: str, repo: str, path: str, sha: str) -> str:
project = quote(project)
repo = quote(repo)

params = {}
if sha:
params["at"] = sha

return f"/rest/api/1.0/projects/{project}/repos/{repo}/browse/{path}?{urlencode(params)}"
2 changes: 2 additions & 0 deletions src/sentry/models/commitfilechange.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def is_valid_type(value: str) -> bool:

def process_resource_change(instance, **kwargs):
from sentry.integrations.bitbucket.integration import BitbucketIntegration
from sentry.integrations.bitbucket_server.integration import BitbucketServerIntegration
from sentry.integrations.github.integration import GitHubIntegration
from sentry.integrations.gitlab.integration import GitlabIntegration
from sentry.tasks.codeowners import code_owners_auto_sync
Expand All @@ -58,6 +59,7 @@ def _spawn_task():
set(GitHubIntegration.codeowners_locations)
| set(GitlabIntegration.codeowners_locations)
| set(BitbucketIntegration.codeowners_locations)
| set(BitbucketServerIntegration.codeowners_locations)
)

# CODEOWNERS file added or modified, trigger auto-sync
Expand Down
Loading
Loading