-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
ref: fix typing of jira_server.integration #86388
Open
asottile-sentry
wants to merge
2
commits into
master
Choose a base branch
from
asottile-typing-jira-server-integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import logging | ||
import re | ||
from collections.abc import Mapping, Sequence | ||
from typing import Any | ||
from typing import Any, NotRequired, TypedDict | ||
from urllib.parse import urlparse | ||
|
||
from cryptography.hazmat.backends import default_backend | ||
|
@@ -114,6 +114,49 @@ | |
) | ||
|
||
|
||
class _Project(TypedDict): | ||
value: str | ||
label: str | ||
|
||
|
||
class _AddDropDown(TypedDict): | ||
emptyMessage: str | ||
noResultsMessage: str | ||
items: list[_Project] | ||
|
||
|
||
class _Choices(TypedDict): | ||
choices: list[tuple[str, str]] | ||
placeholder: str | ||
|
||
|
||
class _MappedSelectors(TypedDict): | ||
on_resolve: _Choices | ||
on_unresolve: _Choices | ||
|
||
|
||
class _ColumnLabels(TypedDict): | ||
on_resolve: str | ||
on_unresolve: str | ||
|
||
|
||
class _Config(TypedDict): | ||
name: str | ||
type: str | ||
label: str | ||
help: str | str | ||
placeholder: NotRequired[str] | ||
choices: NotRequired[list[tuple[str, str]]] | ||
addButtonText: NotRequired[str] | ||
addDropdown: NotRequired[_AddDropDown] | ||
mappedSelectors: NotRequired[_MappedSelectors] | ||
columnLabels: NotRequired[_ColumnLabels] | ||
mappedColumnLabel: NotRequired[str] | ||
formatMessageValue: NotRequired[bool] | ||
disabled: NotRequired[bool] | ||
disabledReason: NotRequired[str] | ||
|
||
|
||
class InstallationForm(forms.Form): | ||
url = forms.CharField( | ||
label=_("Jira URL"), | ||
|
@@ -200,6 +243,9 @@ def dispatch(self, request: HttpRequest, pipeline: Pipeline) -> HttpResponseBase | |
return pipeline.next_step() | ||
|
||
config = pipeline.fetch_state("installation_data") | ||
if config is None: | ||
return pipeline.error("Missing installation_data") | ||
|
||
client = JiraServerSetupClient( | ||
config.get("url"), | ||
config.get("consumer_key"), | ||
|
@@ -237,6 +283,9 @@ class OAuthCallbackView(PipelineView): | |
@method_decorator(csrf_exempt) | ||
def dispatch(self, request: HttpRequest, pipeline: Pipeline) -> HttpResponseBase: | ||
config = pipeline.fetch_state("installation_data") | ||
if config is None: | ||
return pipeline.error("Missing installation_data") | ||
|
||
client = JiraServerSetupClient( | ||
config.get("url"), | ||
config.get("consumer_key"), | ||
|
@@ -298,7 +347,7 @@ def get_client(self): | |
) | ||
|
||
def get_organization_config(self): | ||
configuration = [ | ||
configuration: list[_Config] = [ | ||
{ | ||
"name": self.outbound_status_key, | ||
"type": "choice_mapper", | ||
|
@@ -384,7 +433,9 @@ def get_organization_config(self): | |
configuration[0]["mappedSelectors"]["on_resolve"]["choices"] = statuses | ||
configuration[0]["mappedSelectors"]["on_unresolve"]["choices"] = statuses | ||
|
||
projects = [{"value": p["id"], "label": p["name"]} for p in client.get_projects_list()] | ||
projects: list[_Project] = [ | ||
{"value": p["id"], "label": p["name"]} for p in client.get_projects_list() | ||
] | ||
configuration[0]["addDropdown"]["items"] = projects | ||
except ApiError: | ||
configuration[0]["disabled"] = True | ||
|
@@ -395,9 +446,12 @@ def get_organization_config(self): | |
context = organization_service.get_organization_by_id( | ||
id=self.organization_id, include_teams=False, include_projects=False | ||
) | ||
organization = context.organization | ||
if context is not None: | ||
organization = context.organization | ||
has_issue_sync = features.has("organizations:integrations-issue-sync", organization) | ||
else: | ||
has_issue_sync = False | ||
|
||
has_issue_sync = features.has("organizations:integrations-issue-sync", organization) | ||
if not has_issue_sync: | ||
for field in configuration: | ||
field["disabled"] = True | ||
|
@@ -450,10 +504,12 @@ def update_organization_config(self, data): | |
data[self.issues_ignored_fields_key] = ignored_fields_list | ||
|
||
config.update(data) | ||
self.org_integration = integration_service.update_organization_integration( | ||
org_integration = integration_service.update_organization_integration( | ||
org_integration_id=self.org_integration.id, | ||
config=config, | ||
) | ||
if org_integration is not None: | ||
self.org_integration = org_integration | ||
|
||
def get_config_data(self): | ||
config = self.org_integration.config | ||
|
@@ -473,7 +529,7 @@ def get_config_data(self): | |
) | ||
return config | ||
|
||
def sync_metadata(self): | ||
def sync_metadata(self) -> None: | ||
client = self.get_client() | ||
|
||
try: | ||
|
@@ -491,7 +547,11 @@ def sync_metadata(self): | |
avatar = projects[0]["avatarUrls"]["48x48"] | ||
self.model.metadata.update({"icon": avatar}) | ||
|
||
self.model.save() | ||
integration_service.update_integration( | ||
integration_id=self.model.id, | ||
name=self.model.name, | ||
metadata=self.model.metadata, | ||
) | ||
Comment on lines
+550
to
+554
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this change made to address the case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep -- it would otherwise crash with |
||
|
||
def get_link_issue_config(self, group, **kwargs): | ||
fields = super().get_link_issue_config(group, **kwargs) | ||
|
@@ -564,8 +624,9 @@ def create_comment(self, issue_id, user_id, group_note): | |
quoted_comment = self.create_comment_attribution(user_id, comment) | ||
return self.get_client().create_comment(issue_id, quoted_comment) | ||
|
||
def create_comment_attribution(self, user_id, comment_text): | ||
def create_comment_attribution(self, user_id: int, comment_text: str) -> str: | ||
user = user_service.get_user(user_id=user_id) | ||
assert user is not None | ||
attribution = f"{user.name} wrote:\n\n" | ||
return f"{attribution}{{quote}}{comment_text}{{quote}}" | ||
|
||
|
@@ -651,13 +712,15 @@ def build_dynamic_field(self, field_meta, group=None): | |
or schema["type"] == "issuelink" | ||
): | ||
fieldtype = "select" | ||
organization = ( | ||
group.organization | ||
if group | ||
else organization_service.get_organization_by_id( | ||
if group is not None: | ||
organization = group.organization | ||
else: | ||
ctx = organization_service.get_organization_by_id( | ||
id=self.organization_id, include_teams=False, include_projects=False | ||
).organization | ||
) | ||
) | ||
assert ctx is not None | ||
organization = ctx.organization | ||
|
||
fkwargs["url"] = self.search_url(organization.slug) | ||
fkwargs["choices"] = [] | ||
elif schema["type"] in ["timetracking"]: | ||
|
@@ -717,7 +780,7 @@ def get_projects(self, cached=True): | |
return jira_projects | ||
|
||
@all_silo_function | ||
def get_create_issue_config(self, group: Group | None, user: RpcUser | User, **kwargs): | ||
def get_create_issue_config(self, group: Group | None, user: User, **kwargs): | ||
""" | ||
We use the `group` to get three things: organization_slug, project | ||
defaults, and default title and description. In the case where we're | ||
|
@@ -1284,9 +1347,12 @@ def create_webhook(self, external_id, webhook_secret, install, credentials): | |
"jira-server.webhook.failed", | ||
extra={"error": str(err), "external_id": external_id}, | ||
) | ||
try: | ||
details = next(x for x in err.json["messages"][0].values()) | ||
except (KeyError, TypeError, StopIteration): | ||
if err.json is None: | ||
details = "" | ||
else: | ||
try: | ||
details = next(x for x in err.json["messages"][0].values()) | ||
except (KeyError, TypeError, StopIteration): | ||
details = "" | ||
message = f"Could not create issue webhook in Jira. {details}" | ||
raise IntegrationError(message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the "big" change in this changeset that I'm not certain about -- it makes it so that we can't construct an integration with a missing
OrganizationIntegration
I'm not certain that this is correct -- but it seems most of the integrations do not handle the case where this is missing and as far as I can understand it doesn't really make sense to have an integration not associated with an organization -- will need the domain owners to confirm this idea though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I brought this up with my team to double check, and we think this is a fair assumption to make. Most of our integrations operate with the assumption that we have an org integration already, so it should be fine to handle the potentially few cases that don't explicitly with a try/catch.