From 12e3f5f3a594a9a497e6634ada40f47b0a0f4c9c Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Thu, 1 Sep 2022 14:34:05 +0100 Subject: [PATCH] feat(chalice): changed jira integration feat(chalice): changed github integration --- api/chalicelib/core/integration_base.py | 7 +++- api/chalicelib/core/integration_github.py | 4 +++ api/chalicelib/core/integration_jira_cloud.py | 33 ++++++++++++++----- api/routers/core.py | 2 ++ api/schemas.py | 2 +- 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/api/chalicelib/core/integration_base.py b/api/chalicelib/core/integration_base.py index f8edaad62..6377599af 100644 --- a/api/chalicelib/core/integration_base.py +++ b/api/chalicelib/core/integration_base.py @@ -7,13 +7,18 @@ class BaseIntegration(ABC): def __init__(self, user_id, ISSUE_CLASS): self._user_id = user_id - self.issue_handler = ISSUE_CLASS(self.integration_token) + self.__issue_handler = ISSUE_CLASS(self.integration_token) @property @abstractmethod def provider(self): pass + @property + @abstractmethod + def issue_handler(self): + pass + @property def integration_token(self): integration = self.get() diff --git a/api/chalicelib/core/integration_github.py b/api/chalicelib/core/integration_github.py index 2de8cc518..31e715f4a 100644 --- a/api/chalicelib/core/integration_github.py +++ b/api/chalicelib/core/integration_github.py @@ -16,6 +16,10 @@ class GitHubIntegration(integration_base.BaseIntegration): def provider(self): return PROVIDER + @property + def issue_handler(self): + return + def get_obfuscated(self): integration = self.get() if integration is None: diff --git a/api/chalicelib/core/integration_jira_cloud.py b/api/chalicelib/core/integration_jira_cloud.py index 469723e4e..656a22b14 100644 --- a/api/chalicelib/core/integration_jira_cloud.py +++ b/api/chalicelib/core/integration_jira_cloud.py @@ -17,21 +17,29 @@ class JIRAIntegration(integration_base.BaseIntegration): # super(JIRAIntegration, self).__init__(jwt, user_id, JIRACloudIntegrationProxy) self._user_id = user_id self.integration = self.get() + if self.integration is None: return self.integration["valid"] = True - try: - self.issue_handler = JIRACloudIntegrationIssue(token=self.integration["token"], - username=self.integration["username"], - url=self.integration["url"]) - except Exception as e: - self.issue_handler = None + if not self.integration["url"].endswith('atlassian.net'): self.integration["valid"] = False @property def provider(self): return PROVIDER + @property + def issue_handler(self): + if self.integration["url"].endswith('atlassian.net') and self.__issue_handler is None: + try: + self.__issue_handler = JIRACloudIntegrationIssue(token=self.integration["token"], + username=self.integration["username"], + url=self.integration["url"]) + except Exception as e: + self.__issue_handler = None + self.integration["valid"] = False + return self.__issue_handler + # TODO: remove this once jira-oauth is done def get(self): with pg_client.PostgresClient() as cur: @@ -42,7 +50,14 @@ class JIRAIntegration(integration_base.BaseIntegration): WHERE user_id=%(user_id)s;""", {"user_id": self._user_id}) ) - return helper.dict_to_camel_case(cur.fetchone()) + data = helper.dict_to_camel_case(cur.fetchone()) + + if data is None: + return + data["valid"] = True + if not data["url"].endswith('atlassian.net'): + data["valid"] = False + return data def get_obfuscated(self): if self.integration is None: @@ -67,7 +82,7 @@ class JIRAIntegration(integration_base.BaseIntegration): w = helper.dict_to_camel_case(cur.fetchone()) if obfuscate: w["token"] = obfuscate_string(w["token"]) - return w + return self.get() # TODO: make this generic for all issue tracking integrations def _add(self, data): @@ -85,7 +100,7 @@ class JIRAIntegration(integration_base.BaseIntegration): "token": token, "url": url}) ) w = helper.dict_to_camel_case(cur.fetchone()) - return w + return self.get() def delete(self): with pg_client.PostgresClient() as cur: diff --git a/api/routers/core.py b/api/routers/core.py index 35f27e248..f9ca28a4d 100644 --- a/api/routers/core.py +++ b/api/routers/core.py @@ -456,6 +456,8 @@ def get_integration_status_github(context: schemas.CurrentContext = Depends(OR_c @app.put('/integrations/jira', tags=["integrations"]) def add_edit_jira_cloud(data: schemas.JiraSchema = Body(...), context: schemas.CurrentContext = Depends(OR_context)): + if not data.url.endswith('atlassian.net'): + return {"errors": ["url must be a valid JIRA URL (example.atlassian.net)"]} error, integration = integrations_manager.get_integration(tool=integration_jira_cloud.PROVIDER, tenant_id=context.tenant_id, user_id=context.user_id) diff --git a/api/schemas.py b/api/schemas.py index 42787c9e5..f6dc8b34b 100644 --- a/api/schemas.py +++ b/api/schemas.py @@ -110,7 +110,7 @@ class JiraSchema(GithubSchema): @validator('url') def transform_url(cls, v: HttpUrl): - return HttpUrl.build(scheme=v.scheme, host=v.host) + return HttpUrl.build(scheme=v.scheme.lower(), host=v.host.lower()) class CreateEditWebhookSchema(BaseModel):