Coverage for chalicelib/core/issue_tracking/integrations_manager.py: 85%
27 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:56 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 12:56 +0000
1from chalicelib.core.issue_tracking import github, jira_cloud
2from chalicelib.utils import pg_client
4SUPPORTED_TOOLS = [github.PROVIDER, jira_cloud.PROVIDER]
7def get_available_integrations(user_id):
8 with pg_client.PostgresClient() as cur:
9 cur.execute(
10 cur.mogrify(f"""\
11 SELECT EXISTS((SELECT 1
12 FROM public.oauth_authentication
13 WHERE user_id = %(user_id)s
14 AND provider = 'github')) AS github,
15 EXISTS((SELECT 1
16 FROM public.jira_cloud
17 WHERE user_id = %(user_id)s)) AS jira;""",
18 {"user_id": user_id})
19 )
20 current_integrations = cur.fetchone()
21 return dict(current_integrations)
24def __get_default_integration(user_id):
25 current_integrations = get_available_integrations(user_id)
26 return github.PROVIDER if current_integrations["github"] else jira_cloud.PROVIDER if \
27 current_integrations["jira"] else None
30def get_integration(tenant_id, user_id, tool=None, for_delete=False):
31 if tool is None:
32 tool = __get_default_integration(user_id=user_id)
33 if tool is None:
34 return {"errors": [f"no issue tracking tool found"]}, None
35 tool = tool.upper()
36 if tool not in SUPPORTED_TOOLS: 36 ↛ 37line 36 didn't jump to line 37 because the condition on line 36 was never true
37 return {"errors": [f"issue tracking tool not supported yet, available: {SUPPORTED_TOOLS}"]}, None
38 if tool == jira_cloud.PROVIDER:
39 integration = jira_cloud.JIRAIntegration(tenant_id=tenant_id, user_id=user_id)
40 if not for_delete and integration.integration is not None and not integration.integration.get("valid", True): 40 ↛ 41line 40 didn't jump to line 41 because the condition on line 40 was never true
41 return {"errors": ["JIRA: connexion issue/unauthorized"]}, integration
42 return None, integration
43 elif tool == github.PROVIDER: 43 ↛ 45line 43 didn't jump to line 45 because the condition on line 43 was always true
44 return None, github.GitHubIntegration(tenant_id=tenant_id, user_id=user_id)
45 return {"errors": ["lost integration"]}, None