Skip to content

Commit 9089ece

Browse files
ref: remove nullability of default_identity (#89734)
<!-- Describe your PR here. -->
1 parent ee0e8dd commit 9089ece

File tree

8 files changed

+23
-44
lines changed

8 files changed

+23
-44
lines changed

src/sentry/integrations/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,8 @@ def get_keyring_client(self, keyid: int | str) -> Any:
436436
"""
437437
raise NotImplementedError
438438

439-
def get_default_identity(self) -> RpcIdentity:
439+
@cached_property
440+
def default_identity(self) -> RpcIdentity:
440441
"""For Integrations that rely solely on user auth for authentication."""
441442
try:
442443
org_integration = self.org_integration

src/sentry/integrations/bitbucket_server/integration.py

+8-13
Original file line numberDiff line numberDiff line change
@@ -244,23 +244,18 @@ class BitbucketServerIntegration(RepositoryIntegration):
244244
IntegrationInstallation implementation for Bitbucket Server
245245
"""
246246

247-
default_identity = None
248-
249247
@property
250248
def integration_name(self) -> str:
251249
return "bitbucket_server"
252250

253-
def get_client(self):
254-
if self.default_identity is None:
255-
try:
256-
self.default_identity = self.get_default_identity()
257-
except Identity.DoesNotExist:
258-
raise IntegrationError("Identity not found.")
259-
260-
return BitbucketServerClient(
261-
integration=self.model,
262-
identity=self.default_identity,
263-
)
251+
def get_client(self) -> BitbucketServerClient:
252+
try:
253+
return BitbucketServerClient(
254+
integration=self.model,
255+
identity=self.default_identity,
256+
)
257+
except Identity.DoesNotExist:
258+
raise IntegrationError("Identity not found.")
264259

265260
# IntegrationInstallation methods
266261

src/sentry/integrations/gitlab/client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(self, installation: GitlabIntegration):
8787
def identity(self) -> RpcIdentity:
8888
if self.refreshed_identity:
8989
return self.refreshed_identity
90-
return self.installation.get_default_identity()
90+
return self.installation.default_identity
9191

9292
@property
9393
def metadata(self):

src/sentry/integrations/gitlab/integration.py

+8-12
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,18 @@
9898
class GitlabIntegration(RepositoryIntegration, GitlabIssuesSpec, CommitContextIntegration):
9999
codeowners_locations = ["CODEOWNERS", ".gitlab/CODEOWNERS", "docs/CODEOWNERS"]
100100

101-
def __init__(self, *args, **kwargs):
102-
super().__init__(*args, **kwargs)
103-
self.default_identity = None
104-
105101
@property
106102
def integration_name(self) -> str:
107103
return "gitlab"
108104

109-
def get_client(self):
110-
if self.default_identity is None:
111-
try:
112-
self.default_identity = self.get_default_identity()
113-
except Identity.DoesNotExist:
114-
raise IntegrationError("Identity not found.")
115-
116-
return GitLabApiClient(self)
105+
def get_client(self) -> GitLabApiClient:
106+
try:
107+
# eagerly populate this just for the error message
108+
self.default_identity
109+
except Identity.DoesNotExist:
110+
raise IntegrationError("Identity not found.")
111+
else:
112+
return GitLabApiClient(self)
117113

118114
# IntegrationInstallation methods
119115
def error_message_from_json(self, data):

src/sentry/integrations/jira_server/integration.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,12 @@ class JiraServerIntegration(IssueSyncIntegration):
334334
issues_ignored_fields_key = "issues_ignored_fields"
335335
resolution_strategy_key = "resolution_strategy"
336336

337-
default_identity = None
338-
339337
def get_client(self):
340338
try:
341-
self.default_identity = self.get_default_identity()
339+
return JiraServerClient(integration=self.model, identity=self.default_identity)
342340
except Identity.DoesNotExist:
343341
raise IntegrationError("Identity not found.")
344342

345-
return JiraServerClient(
346-
integration=self.model,
347-
identity=self.default_identity,
348-
)
349-
350343
def get_organization_config(self):
351344
configuration: list[_Config] = [
352345
{

src/sentry/integrations/vsts/integration.py

-6
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,13 @@ class VstsIntegration(RepositoryIntegration, VstsIssuesSpec):
130130
outbound_assignee_key = "sync_forward_assignment"
131131
inbound_assignee_key = "sync_reverse_assignment"
132132

133-
def __init__(self, *args: Any, **kwargs: Any) -> None:
134-
super().__init__(*args, **kwargs)
135-
self.default_identity: RpcIdentity | None = None
136-
137133
@property
138134
def integration_name(self) -> str:
139135
return "vsts"
140136

141137
def get_client(self) -> VstsApiClient:
142138
base_url = self.instance
143139
if SiloMode.get_current_mode() != SiloMode.REGION:
144-
if self.default_identity is None:
145-
self.default_identity = self.get_default_identity()
146140
self._check_domain_name(self.default_identity)
147141

148142
if self.org_integration is None:

tests/sentry/integrations/gitlab/test_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_check_file(self):
178178

179179
resp = self.gitlab_client.check_file(self.repo, path, ref)
180180
assert responses.calls[0].response.status_code == 200
181-
assert resp.status_code == 200
181+
assert resp # this is None on error
182182

183183
@responses.activate
184184
def test_check_no_file(self):

tests/sentry/integrations/test_base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def test_with_context(self):
4040
assert integration.model.id == self.model.id
4141
assert integration.org_integration is not None
4242
assert integration.org_integration.id == self.org_integration.id
43-
assert integration.get_default_identity() == serialize_identity(self.identity)
43+
assert integration.default_identity == serialize_identity(self.identity)
4444

4545
def test_missing_org_integration(self):
4646
with pytest.raises(Identity.DoesNotExist):
47-
ExampleIntegration(self.model, -1).get_default_identity()
47+
ExampleIntegration(self.model, -1).default_identity
4848

4949
def test_model_default_fields(self):
5050
# These fields are added through the DefaultFieldsModel

0 commit comments

Comments
 (0)