-
-
Save QtRoS/bfe4d2e1a5a751b93259166da1be17c7 to your computer and use it in GitHub Desktop.
Requested in https://habr.com/ru/company/nsd/blog/523732/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import argparse | |
from time import sleep | |
import sys | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--branch_name', required=True) | |
parser.add_argument('--login', required=True) | |
parser.add_argument('--password', required=True) | |
args = parser.parse_args() | |
header = {"Accept": "application/json", "Content-Type": "application/json"} | |
login = args.login | |
password = args.password | |
auth = (login, password) | |
TEAMCITY_URL = 'http://your.teamcity.host' | |
TEAMCITY_TASK = 'SonarQubeTask' | |
SONARQUBE_URL = 'http://your.sonarqube.host' | |
SONARQUBE_PROJECT_KEY = 'PROJECT_KEY' | |
def start_scan_branch(branch): | |
url = f'{TEAMCITY_URL}/action.html?add2Queue={TEAMCITY_TASK}&name=custom_branch&value={branch}&moveToTop=true' | |
data = requests.post(url, headers=header, auth=auth) | |
if data.ok: | |
print('Start scan branch {}'.format(branch)) | |
else: | |
print('Something went wrong') | |
def wait_status_change(id): | |
print('Wait while status change') | |
wait_seconds(200) | |
url = f'{TEAMCITY_URL}/app/rest/builds/id:{id}' | |
for i in range(8): | |
data = requests.get(url=url, headers=header, auth=auth).json() | |
if data['state'] != 'running': | |
if data['status'] == 'SUCCESS': | |
break | |
else: | |
print('Something went wrong while scan branch') | |
sys.exit(2) | |
sleep(30) | |
def get_id(): | |
url = f'{TEAMCITY_TASK}/app/rest/builds?locator=running:true' | |
for i in range(8): | |
if i == 5: | |
sys.exit(2) | |
data_json = requests.get(url, headers=header, auth=auth).json() | |
for j in data_json['build']: | |
if j['buildTypeId'] == TEAMCITY_TASK: | |
print('Id found: {}'.format(j['id'])) | |
return j['id'] | |
sleep(20) | |
def wait_seconds(sec): | |
print('Start wait: {}'.format(sec)) | |
sleep(sec) | |
print('Stop wait') | |
def get_qualitygate_status(): | |
url = f'{SONARQUBE_URL}/api/qualitygates/project_status?projectKey={SONARQUBE_PROJECT_KEY}' | |
data = requests.get(url, auth=('admin', 'admin')).json() | |
return data | |
if __name__ == '__main__': | |
# Start scan branch | |
start_scan_branch(args.branch_name) | |
wait_status_change(get_id()) | |
wait_seconds(30) | |
status_quality_gate = get_qualitygate_status() | |
if status_quality_gate['projectStatus']['status'] == 'OK': | |
print('Branch passed verification') | |
print('Bugs in branch: {}'.format(status_quality_gate['projectStatus']['conditions'][0]['actualValue'])) | |
exit(0) | |
if status_quality_gate['projectStatus']['status'] != 'OK': | |
print('Branch failed verification') | |
print('Too many bugs') | |
print('Bugs in branch: {}'.format(status_quality_gate['projectStatus']['conditions'][0]['actualValue'])) | |
exit(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment