11"""Services and functions that can be used to create custom actions"""
2- from typing import TypedDict
2+ import logging
3+ from typing import Dict , List
34
45import bugzilla as rh_bugzilla
56from atlassian import Jira
67
78from src .app import environment
9+ from src .jbi .models import Actions
810
911settings = environment .get_settings ()
1012
13+ logger = logging .getLogger (__name__ )
1114
12- ServiceHealth = TypedDict ("ServiceHealth" , {"up" : bool })
15+
16+ ServiceHealth = Dict [str , bool ]
1317
1418
1519def get_jira ():
@@ -22,31 +26,53 @@ def get_jira():
2226 )
2327
2428
29+ def jira_visible_projects (jira = None ) -> List [Dict ]:
30+ """Return list of projects that are visible with the configured Jira credentials"""
31+ jira = jira or get_jira ()
32+ projects : List [Dict ] = jira .projects (included_archived = None )
33+ return projects
34+
35+
2536def get_bugzilla ():
2637 """Get bugzilla service"""
2738 return rh_bugzilla .Bugzilla (
2839 settings .bugzilla_base_url , api_key = str (settings .bugzilla_api_key )
2940 )
3041
3142
32- def bugzilla_check_health () -> ServiceHealth :
43+ def _bugzilla_check_health () -> ServiceHealth :
3344 """Check health for Bugzilla Service"""
3445 bugzilla = get_bugzilla ()
3546 health : ServiceHealth = {"up" : bugzilla .logged_in }
3647 return health
3748
3849
39- def jira_check_health ( ) -> ServiceHealth :
50+ def _jira_check_health ( actions : Actions ) -> ServiceHealth :
4051 """Check health for Jira Service"""
4152 jira = get_jira ()
4253 server_info = jira .get_server_info (True )
43- health : ServiceHealth = {"up" : server_info is not None }
54+ is_up = server_info is not None
55+ health : ServiceHealth = {
56+ "up" : is_up ,
57+ "all_projects_are_visible" : is_up and _all_jira_projects_visible (jira , actions ),
58+ }
4459 return health
4560
4661
47- def jbi_service_health_map ():
62+ def _all_jira_projects_visible (jira , actions : Actions ) -> bool :
63+ visible_projects = {project ["key" ] for project in jira_visible_projects (jira )}
64+ missing_projects = actions .configured_jira_projects_keys - visible_projects
65+ if missing_projects :
66+ logger .error (
67+ "Jira projects %s are not visible with configured credentials" ,
68+ missing_projects ,
69+ )
70+ return not missing_projects
71+
72+
73+ def jbi_service_health_map (actions : Actions ):
4874 """Returns dictionary of health check's for Bugzilla and Jira Services"""
4975 return {
50- "bugzilla" : bugzilla_check_health (),
51- "jira" : jira_check_health ( ),
76+ "bugzilla" : _bugzilla_check_health (),
77+ "jira" : _jira_check_health ( actions ),
5278 }
0 commit comments