2222
2323from __future__ import annotations
2424from http import HTTPStatus
25- from typing import Optional
26- from datetime import datetime
25+ from typing import Optional , TYPE_CHECKING
26+
2727import json
2828import re
2929from urllib .parse import unquote
3030import requests .utils
3131
3232from sonar import platform
33- from sonar .util import types , cache
33+ from sonar .util import cache
3434import sonar .logging as log
3535from sonar import components , settings , exceptions , tasks
36- from sonar import projects
36+ from sonar import projects as proj
3737import sonar .utilities as util
3838
3939from sonar .audit .problem import Problem
4040from sonar .audit .rules import get_rule , RuleId
4141import sonar .util .constants as c
4242
43+ if TYPE_CHECKING :
44+ from sonar .util import types
45+ from datetime import datetime
4346
4447_UNSUPPORTED_IN_CE = "Branches not available in Community Edition"
4548
@@ -57,7 +60,7 @@ class Branch(components.Component):
5760 "get_new_code" : "new_code_periods/list" ,
5861 }
5962
60- def __init__ (self , project : projects .Project , name : str ) -> None :
63+ def __init__ (self , project : proj .Project , name : str ) -> None :
6164 """Don't use this, use class methods to create Branch objects
6265
6366 :raises UnsupportedOperation: When attempting to branches on Community Edition
@@ -76,10 +79,10 @@ def __init__(self, project: projects.Project, name: str) -> None:
7679 log .debug ("Created object %s" , str (self ))
7780
7881 @classmethod
79- def get_object (cls , concerned_object : projects .Project , branch_name : str ) -> Branch :
82+ def get_object (cls , concerned_object : proj .Project , branch_name : str ) -> Branch :
8083 """Gets a SonarQube Branch object
8184
82- :param projects. Project concerned_object: projects. Project concerned by the branch
85+ :param Project concerned_object: Project concerned by the branch
8386 :param str branch_name: The branch name
8487 :raises UnsupportedOperation: If trying to manipulate branches on a community edition
8588 :raises ObjectNotFound: If project key or branch name not found in SonarQube
@@ -97,10 +100,10 @@ def get_object(cls, concerned_object: projects.Project, branch_name: str) -> Bra
97100 return cls .load (concerned_object , branch_name , br )
98101
99102 @classmethod
100- def load (cls , concerned_object : projects .Project , branch_name : str , data : types .ApiPayload ) -> Branch :
103+ def load (cls , concerned_object : proj .Project , branch_name : str , data : types .ApiPayload ) -> Branch :
101104 """Gets a Branch object from JSON data gotten from a list API call
102105
103- :param projects. Project concerned_object: the projects. Project the branch belonsg to
106+ :param Project concerned_object: the Project the branch belonsg to
104107 :param str branch_name: Name of the branch
105108 :param dict data: Data received from API call
106109 :return: The Branch object
@@ -122,7 +125,7 @@ def __hash__(self) -> int:
122125 """Computes a uuid for the branch that can serve as index"""
123126 return hash ((self .concerned_object .key , self .name , self .base_url ()))
124127
125- def project (self ) -> projects .Project :
128+ def project (self ) -> proj .Project :
126129 """Returns the project key"""
127130 return self .concerned_object
128131
@@ -186,7 +189,7 @@ def get(
186189 except exceptions .ObjectNotFound as e :
187190 if re .match (r"Project .+ not found" , e .message ):
188191 log .warning ("Clearing project cache" )
189- projects .Project .CACHE .clear ()
192+ proj .Project .CACHE .clear ()
190193 raise
191194
192195 def post (self , api : str , params : types .ApiParams = None , mute : tuple [HTTPStatus ] = (), ** kwargs : str ) -> requests .Response :
@@ -196,7 +199,7 @@ def post(self, api: str, params: types.ApiParams = None, mute: tuple[HTTPStatus]
196199 except exceptions .ObjectNotFound as e :
197200 if re .match (r"Project .+ not found" , e .message ):
198201 log .warning ("Clearing project cache" )
199- projects .Project .CACHE .clear ()
202+ proj .Project .CACHE .clear ()
200203 raise
201204
202205 def new_code (self ) -> str :
@@ -396,8 +399,7 @@ def audit(self, audit_settings: types.ConfigSettings) -> list[Problem]:
396399 try :
397400 if audit_settings .get (c .AUDIT_MODE_PARAM , "" ) == "housekeeper" :
398401 return self .__audit_last_analysis (audit_settings )
399- else :
400- return self .__audit_last_analysis (audit_settings ) + self .__audit_never_analyzed () + self ._audit_component (audit_settings )
402+ return self .__audit_last_analysis (audit_settings ) + self .__audit_never_analyzed () + self ._audit_component (audit_settings )
401403 except Exception as e :
402404 log .error ("%s while auditing %s, audit skipped" , util .error_msg (e ), str (self ))
403405 return []
@@ -414,10 +416,10 @@ def last_task(self) -> Optional[tasks.Task]:
414416 return task
415417
416418
417- def get_list (project : projects .Project ) -> dict [str , Branch ]:
419+ def get_list (project : proj .Project ) -> dict [str , Branch ]:
418420 """Retrieves the list of branches of a project
419421
420- :param projects. Project project: projects. Project the branch belongs to
422+ :param Project project: Project the branch belongs to
421423 :raises UnsupportedOperation: Branches not supported in Community Edition
422424 :return: List of project branches
423425 :rtype: dict{branch_name: Branch}
@@ -436,13 +438,13 @@ def exists(endpoint: platform.Platform, branch_name: str, project_key: str) -> b
436438
437439 :param Platform endpoint: Reference to the SonarQube platform
438440 :param str branch_name: Branch name
439- :param str project_key: projects. Project key
441+ :param str project_key: Project key
440442 :raises UnsupportedOperation: Branches not supported in Community Edition
441443 :return: Whether the branch exists in SonarQube
442444 :rtype: bool
443445 """
444446 try :
445- project = projects .Project .get_object (endpoint , project_key )
447+ project = proj .Project .get_object (endpoint , project_key )
446448 except exceptions .ObjectNotFound :
447449 return False
448450 return branch_name in get_list (project )
0 commit comments