def __init__(self, organization, url, username, password, token, app, branch, edition): self.organization = organization self.url = url self.username = username self.password = password self.token = token self.app = app self.branch = branch self.edition = edition if not self.token and not (self.username and self.password): print("Failed!") print("Either token or username and password must be provided.") sys.exit() if not self.url: print("Failed!") print("Sonarqube URL is required.") sys.exit() if not self.app: print("Failed!") print("Sonarqube app is required.") sys.exit() if not self.branch: print("Failed!") print("Branch is required.") sys.exit() if self.username and self.password: if self.edition == 'cloud': print("Failed!") print("Sonar Cloud only accepts token based authentication.") sys.exit() if self.edition == "community": self.client = SonarQubeClient(sonarqube_url=self.url, username=self.username, password=self.password) elif self.edition == "enterprise": self.client = SonarEnterpriseClient(sonarqube_url=self.url, username=self.username, password=self.password) else: self.client = SonarQubeClient(sonarqube_url=self.url, username=self.username, password=self.password) if token: if self.edition == "community": self.client = SonarQubeClient(sonarqube_url=self.url, token=self.token) elif self.edition == "enterprise": self.client = SonarEnterpriseClient(sonarqube_url=self.url, token=self.token) elif self.edition == "cloud": if not self.organization: print("Failed!") print("Organization is required.") sys.exit() self.client = SonarCloudClient(sonarcloud_url=self.url, token=self.token) else: self.client = SonarQubeClient(sonarqube_url=self.url, token=self.token)
def Sonar_Analyse_project(ProjID, SRCFolder, RuleID): #change folder to proejct one retval = os.getcwd() #save current path os.chdir(SRCFolder) #create target analysis dir for ghdl/yosys try: os.mkdir(SONAR_VHDLRC_WORKDIR) except: pass #do sonarqube analysis sonar_scan = SONAR_SCANNER_EXEC + ' -Dsonar.host.url=' + SONAR_SERVER_ADDR + ' -Dsonar.projectKey=' + ProjID + ' -Dsonar.login='******' -Dsonar.vhdlrc.ghdlscript=build.sh -Dsonar.vhdlrc.topEntities=top' os.system(sonar_scan) #connect sonarqube python API to the server sonar = SonarQubeClient(sonarqube_url=SONAR_SERVER_ADDR, token=SONAR_TOKEN) #check authentification result = sonar.auth.check_credentials() if result == False: raise RuntimeError("Could not authentificate to Sonarqube server") #list ongoing task and wait until end of scan tasks = sonar.ce.search_tasks(status="FAILED,CANCELED,PENDING,IN_PROGRESS") while tasks['tasks'] != []: tasks = sonar.ce.search_tasks( status="FAILED,CANCELED,PENDING,IN_PROGRESS") time.sleep(1) #for debug #print("scan ongoing. Please Wait...") #pprint.pprint(tasks) #check project existence projects = list(sonar.projects.search_projects()) for projs in projects: if projs['key'] == ProjID: CurretProject = projs break else: CurretProject = None #verify existence if CurretProject == None: raise RuntimeError("Sonarqube project doesn't exist on server") #check issue from rule issues = list(sonar.issues.search_issues(componentKeys=ProjID)) rules_issues = [] for issue in issues: if issue['rule'] == "vhdlrc-repository:" + RuleID: rules_issues.append(issue) #for debug purpose pprint.pprint(rules_issues) #return to oritginal path os.chdir(retval) #return number of issue raised return len(rules_issues)
def main(): branch = os.getenv('CF_BRANCH', None) pr = os.getenv('CF_PULL_REQUEST_NUMBER', None) sonar_project = os.getenv('SONAR_PROJECT_KEY') sonarcloud_token = os.getenv('SONAR_TOKEN') sonarqube_password = os.getenv('SONAR_PASSWORD') sonarqube_username = os.getenv('SONAR_USERNAME') sonar_url = os.getenv('SONAR_HOST_URL', 'https://sonarcloud.io') if sonarcloud_token: sonar = SonarCloudClient(sonarcloud_url=sonar_url, token=sonarcloud_token) else: sonar = SonarQubeClient(sonarqube_url=sonar_url, username=sonarqube_username, password=sonarqube_password) # Code: https://github.com/shijl0925/python-sonarqube-api/blob/376cf1d6ef231ee084694c77dadf551733395d4f/sonarqube/community/qualitygates.py#L182 # Docs: https://python-sonarqube-api.readthedocs.io/en/1.2.1/examples/qualitygates.html# if pr is not None: print(f"PR Quality Gate for PR #{pr}") branch = None # since the QG check accepts only branch or PR qualitygates_status = sonar.qualitygates.get_project_qualitygates_status(projectKey=sonar_project, branch=branch, pullRequest=pr) exportResults(qualitygates_status) actBasedOnStatus(qualitygates_status)
def main(): branch = os.getenv('CF_BRANCH', 'main') sonar_project = os.getenv('SONAR_PROJECT_KEY') sonarcloud_token = os.getenv('SONAR_TOKEN') sonarqube_password = os.getenv('SONAR_PASSWORD') sonarqube_username = os.getenv('SONAR_USERNAME') sonar_url = os.getenv('SONAR_HOST_URL', 'https://sonarcloud.io') if sonarcloud_token: sonar = SonarCloudClient(sonarcloud_url=sonar_url, token=sonarcloud_token) else: sonar = SonarQubeClient(sonarqube_url=sonar_url, username=sonarqube_username, password=sonarqube_password) # Code: https://github.com/shijl0925/python-sonarqube-api/blob/376cf1d6ef231ee084694c77dadf551733395d4f/sonarqube/community/qualitygates.py#L182 # Docs: https://python-sonarqube-api.readthedocs.io/en/1.2.1/examples/qualitygates.html# qualitygates_status = sonar.qualitygates.get_project_qualitygates_status( projectKey=sonar_project, branch=branch) exportResults(qualitygates_status) actBasedOnStatus(qualitygates_status)
from sonarqube import SonarQubeClient from sonarqube.utils.exceptions import * from requests.exceptions import ConnectionError from tqdm import tqdm url = os.getenv("SONAR_URL", "http://sonarqube:9000") username = "******" default_password = "******" password = os.getenv("SONAR_PASSWORD", "password") project = os.getenv("SONAR_PROJECT_KEY", "generic-project") started = False pbar = tqdm(desc=f"Sonar started: {started}") while not started: try: sonar = SonarQubeClient(sonarqube_url=url, username=username, password=password) try: sonar.auth.authenticate_user(login=username, password=password) except AuthError as e: sonar = SonarQubeClient(sonarqube_url=url, username=username, password=default_password) sonar.auth.authenticate_user(login=username, password=default_password) sonar.auth.logout_user() sonar.users.change_user_password(username, password, default_password) sonar.auth.authenticate_user(login=username, password=password) sonar = SonarQubeClient(sonarqube_url=url, username=username,
def _create_client(self): """ Create a SonarQube client """ return SonarQubeClient(sonarqube_url=self.sonarqube_url, token=self._token)
time.sleep(20) elif state == 'exited': infra.start_server() time.sleep(15) container = infra.client.containers.get(server_name) while True: if infra.server_status() == 'running': logs = str(container.logs(since=start_time)) if 'SonarQube is up' in logs: break else: time.sleep(3) #auth to server s = SonarQubeClient(sonarqube_url=f'http://{host_ip}:{host_port}', username=server_user, password=server_pass) print(f'SonarQube server is available at http://{host_ip}:{host_port}') #Create a token sonar_tokens = s.user_tokens.search_user_tokens(user_login=server_user) for i in sonar_tokens: if i['name'] == project_name: s.user_tokens.revoke_user_token(project_name, user_login=server_user) sonar_token = s.user_tokens.generate_user_token( project_name, user_login=server_user).json()['token'] #check if project exists in sonar, create if not project = list(s.projects.search_projects(projects=project_name)) if len(project) < 1: print(f'Creating a new SonarQube project named {project_name}')