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+
27+ if TYPE_CHECKING :
28+ from sonar .projects import Project
29+ from sonar .util import types
30+ from datetime import datetime
31+
2732import json
2833import re
2934from urllib .parse import unquote
3035import requests .utils
3136
3237from sonar import platform
33- from sonar .util import types , cache
38+ from sonar .util import cache
3439import sonar .logging as log
3540from sonar import components , settings , exceptions , tasks
3641from sonar import projects
@@ -57,7 +62,7 @@ class Branch(components.Component):
5762 "get_new_code" : "new_code_periods/list" ,
5863 }
5964
60- def __init__ (self , project : projects . Project , name : str ) -> None :
65+ def __init__ (self , project : Project , name : str ) -> None :
6166 """Don't use this, use class methods to create Branch objects
6267
6368 :raises UnsupportedOperation: When attempting to branches on Community Edition
@@ -76,10 +81,10 @@ def __init__(self, project: projects.Project, name: str) -> None:
7681 log .debug ("Created object %s" , str (self ))
7782
7883 @classmethod
79- def get_object (cls , concerned_object : projects . Project , branch_name : str ) -> Branch :
84+ def get_object (cls , concerned_object : Project , branch_name : str ) -> Branch :
8085 """Gets a SonarQube Branch object
8186
82- :param projects. Project concerned_object: projects. Project concerned by the branch
87+ :param Project concerned_object: Project concerned by the branch
8388 :param str branch_name: The branch name
8489 :raises UnsupportedOperation: If trying to manipulate branches on a community edition
8590 :raises ObjectNotFound: If project key or branch name not found in SonarQube
@@ -97,10 +102,10 @@ def get_object(cls, concerned_object: projects.Project, branch_name: str) -> Bra
97102 return cls .load (concerned_object , branch_name , br )
98103
99104 @classmethod
100- def load (cls , concerned_object : projects . Project , branch_name : str , data : types .ApiPayload ) -> Branch :
105+ def load (cls , concerned_object : Project , branch_name : str , data : types .ApiPayload ) -> Branch :
101106 """Gets a Branch object from JSON data gotten from a list API call
102107
103- :param projects. Project concerned_object: the projects. Project the branch belonsg to
108+ :param Project concerned_object: the Project the branch belonsg to
104109 :param str branch_name: Name of the branch
105110 :param dict data: Data received from API call
106111 :return: The Branch object
@@ -122,7 +127,7 @@ def __hash__(self) -> int:
122127 """Computes a uuid for the branch that can serve as index"""
123128 return hash ((self .concerned_object .key , self .name , self .base_url ()))
124129
125- def project (self ) -> projects . Project :
130+ def project (self ) -> Project :
126131 """Returns the project key"""
127132 return self .concerned_object
128133
@@ -186,7 +191,7 @@ def get(
186191 except exceptions .ObjectNotFound as e :
187192 if re .match (r"Project .+ not found" , e .message ):
188193 log .warning ("Clearing project cache" )
189- projects . Project .CACHE .clear ()
194+ Project .CACHE .clear ()
190195 raise
191196
192197 def post (self , api : str , params : types .ApiParams = None , mute : tuple [HTTPStatus ] = (), ** kwargs : str ) -> requests .Response :
@@ -196,7 +201,7 @@ def post(self, api: str, params: types.ApiParams = None, mute: tuple[HTTPStatus]
196201 except exceptions .ObjectNotFound as e :
197202 if re .match (r"Project .+ not found" , e .message ):
198203 log .warning ("Clearing project cache" )
199- projects . Project .CACHE .clear ()
204+ Project .CACHE .clear ()
200205 raise
201206
202207 def new_code (self ) -> str :
@@ -414,10 +419,10 @@ def last_task(self) -> Optional[tasks.Task]:
414419 return task
415420
416421
417- def get_list (project : projects . Project ) -> dict [str , Branch ]:
422+ def get_list (project : Project ) -> dict [str , Branch ]:
418423 """Retrieves the list of branches of a project
419424
420- :param projects. Project project: projects. Project the branch belongs to
425+ :param Project project: Project the branch belongs to
421426 :raises UnsupportedOperation: Branches not supported in Community Edition
422427 :return: List of project branches
423428 :rtype: dict{branch_name: Branch}
@@ -436,13 +441,13 @@ def exists(endpoint: platform.Platform, branch_name: str, project_key: str) -> b
436441
437442 :param Platform endpoint: Reference to the SonarQube platform
438443 :param str branch_name: Branch name
439- :param str project_key: projects. Project key
444+ :param str project_key: Project key
440445 :raises UnsupportedOperation: Branches not supported in Community Edition
441446 :return: Whether the branch exists in SonarQube
442447 :rtype: bool
443448 """
444449 try :
445- project = projects . Project .get_object (endpoint , project_key )
450+ project = Project .get_object (endpoint , project_key )
446451 except exceptions .ObjectNotFound :
447452 return False
448453 return branch_name in get_list (project )
0 commit comments