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(azure-devops): CODEOWNERS support #86958

Open
wants to merge 1 commit 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
22 changes: 18 additions & 4 deletions src/sentry/integrations/vsts/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from sentry.constants import ObjectStatus
from sentry.exceptions import InvalidIdentity
from sentry.integrations.base import IntegrationFeatureNotImplementedError
from sentry.integrations.client import ApiClient
from sentry.integrations.services.integration.service import integration_service
from sentry.integrations.source_code_management.repository import RepositoryClient
Expand Down Expand Up @@ -172,12 +171,12 @@ def identity(self):

def request(self, method: str, *args: Any, **kwargs: Any) -> Any:
api_preview = kwargs.pop("api_preview", False)
new_headers = prepare_headers(
base_headers = prepare_headers(
api_version=self.api_version,
method=method,
api_version_preview=self.api_version_preview if api_preview else "",
)
kwargs.setdefault("headers", {}).update(new_headers)
kwargs["headers"] = {**base_headers, **(kwargs.get("headers", {}))}
Copy link
Member

Choose a reason for hiding this comment

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

curious why this change was required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

prepare_headers() returns a dictionary with an Accept key. I need to override the header to Accept: */* in the following method. I thought allowing user-provided headers to take precedence would make sense.

https://github.com/getsentry/sentry/pull/86958/files#diff-2fe5f1304407937661dd4a7be31cc76c586a7dfd672a87dce18195424826c4e6R464


return self._request(method, *args, **kwargs)

Expand Down Expand Up @@ -450,4 +449,19 @@ def check_file(self, repo: Repository, path: str, version: str | None) -> object
def get_file(
self, repo: Repository, path: str, ref: str | None, codeowners: bool = False
) -> str:
raise IntegrationFeatureNotImplementedError
response = self.get_cached(
path=VstsApiPath.items.format(
instance=repo.config["instance"],
project=quote(repo.config["project"]),
repo_id=quote(repo.config["name"]),
),
params={
"path": path,
"api-version": "7.0",
"versionDescriptor.version": ref,
"download": "true",
},
headers={"Accept": "*/*"},
raw_response=True,
)
return response.text
10 changes: 10 additions & 0 deletions src/sentry/integrations/vsts/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
""",
IntegrationFeatures.STACKTRACE_LINK,
),
FeatureDescription(
"""
Import your Azure DevOps codeowners file into Sentry and use it alongside your
ownership rules to assign Sentry issues.
""",
IntegrationFeatures.CODEOWNERS,
),
FeatureDescription(
"""
Automatically create Azure DevOps work items based on Issue Alert conditions.
Expand Down Expand Up @@ -129,6 +136,8 @@ class VstsIntegration(RepositoryIntegration, VstsIssuesSpec):
outbound_assignee_key = "sync_forward_assignment"
inbound_assignee_key = "sync_reverse_assignment"

codeowners_locations = ["CODEOWNERS", ".sentry/CODEOWNERS"]

def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.default_identity: RpcIdentity | None = None
Expand Down Expand Up @@ -406,6 +415,7 @@ class VstsIntegrationProvider(IntegrationProvider):
IntegrationFeatures.ISSUE_BASIC,
IntegrationFeatures.ISSUE_SYNC,
IntegrationFeatures.STACKTRACE_LINK,
IntegrationFeatures.CODEOWNERS,
IntegrationFeatures.TICKET_RULES,
]
)
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/models/commitfilechange.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ def process_resource_change(instance, **kwargs):
from sentry.integrations.bitbucket.integration import BitbucketIntegration
from sentry.integrations.github.integration import GitHubIntegration
from sentry.integrations.gitlab.integration import GitlabIntegration
from sentry.integrations.vsts.integration import VstsIntegration
from sentry.tasks.codeowners import code_owners_auto_sync

def _spawn_task():
filepaths = (
set(GitHubIntegration.codeowners_locations)
| set(GitlabIntegration.codeowners_locations)
| set(BitbucketIntegration.codeowners_locations)
| set(VstsIntegration.codeowners_locations)
)

# CODEOWNERS file added or modified, trigger auto-sync
Expand Down
29 changes: 29 additions & 0 deletions tests/sentry/integrations/vsts/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,35 @@ def test_check_no_file(self):
with pytest.raises(ApiError):
client.check_file(repo, path, version)

@responses.activate
def test_get_file(self):
self.assert_installation()
integration, installation = self._get_integration_and_install()
with assume_test_silo_mode(SiloMode.REGION):
repo = Repository.objects.create(
provider="visualstudio",
name="example",
organization_id=self.organization.id,
config={
"instance": self.vsts_base_url,
"project": "project-name",
"name": "example",
},
integration_id=integration.id,
external_id="albertos-apples",
)

client = installation.get_client()

path = "README.md"
version = "master"
url = f"https://myvstsaccount.visualstudio.com/project-name/_apis/git/repositories/{repo.name}/items?path={path}&api-version=7.0&versionDescriptor.version={version}&download=true"

responses.add(method=responses.GET, url=url, body="Hello, world!")

resp = client.get_file(repo, path, version)
assert resp == "Hello, world!"

@responses.activate
def test_get_stacktrace_link(self):
self.assert_installation()
Expand Down
Loading