|
| 1 | +import requests |
| 2 | +from zipfile import ZipFile |
| 3 | +from io import BytesIO |
| 4 | +import datetime |
| 5 | +import os |
| 6 | +import json |
| 7 | +import re |
| 8 | + |
| 9 | +class JiraConnector: |
| 10 | + |
| 11 | + def download_features(project_id, access_key=None, secret_key=None, output_folder='./features/', jql=None, mode='automated', |
| 12 | + proxy_uri=None, proxy_username=None, proxy_password=None): |
| 13 | + |
| 14 | + if access_key is None: |
| 15 | + if os.environ.get("ASSERTTHAT_ACCESS_KEY") is None: |
| 16 | + print("[ERROR] ASSERTTHAT_ACCESS_KEY is missing, should be provided as environment variable or parameter") |
| 17 | + else: |
| 18 | + access_key = os.environ.get("ASSERTTHAT_ACCESS_KEY") |
| 19 | + |
| 20 | + if secret_key is None: |
| 21 | + if os.environ.get("ASSERTTHAT_SECRET_KEY") is None: |
| 22 | + print("[ERROR] ASSERTTHAT_SECRET_KEY is missing, should be provided as environment variable or parameter") |
| 23 | + else: |
| 24 | + secret_key = os.environ.get("ASSERTTHAT_SECRET_KEY") |
| 25 | + |
| 26 | + path = 'https://bdd.assertthat.com/rest/api/1/project/' + project_id + '/features' |
| 27 | + headers = {} |
| 28 | + payload = {'mode': mode, |
| 29 | + 'jql': jql |
| 30 | + } |
| 31 | + print('Fetching from: ' + path) |
| 32 | + |
| 33 | + if proxy_uri is None: |
| 34 | + proxies = None |
| 35 | + |
| 36 | + elif proxy_uri is not None: |
| 37 | + if proxy_username is None: |
| 38 | + proxy_http = "http://%s" % (proxy_uri) |
| 39 | + proxy_https = "https://%s" % (proxy_uri) |
| 40 | + elif proxy_username is not None: |
| 41 | + proxy_http = "http://%s:%s@%s" % (proxy_username, proxy_password, proxy_uri) |
| 42 | + proxy_https = "https://%s:%s@%s" % (proxy_username, proxy_password, proxy_uri) |
| 43 | + proxies = {'http': proxy_http, |
| 44 | + 'https': proxy_https |
| 45 | + } |
| 46 | + |
| 47 | + try: |
| 48 | + response = requests.get(path, auth=(access_key, secret_key), headers=headers, params=payload, |
| 49 | + proxies=proxies) |
| 50 | + |
| 51 | + if response.status_code == 200: |
| 52 | + print('Fetched.') |
| 53 | + print('Preparing to extract...') |
| 54 | + zip = ZipFile(BytesIO(response.content)) |
| 55 | + infolist = zip.infolist() |
| 56 | + zip.extractall(path=output_folder) |
| 57 | + print('[INFO] Downloaded ' + str(infolist.__len__()) + ' feature files into "' + output_folder + '"') |
| 58 | + |
| 59 | + response.raise_for_status() |
| 60 | + |
| 61 | + except requests.exceptions.HTTPError as errh: |
| 62 | + response_content = response.content |
| 63 | + print("[ERROR] Failed to download features %s" % (response_content)) |
| 64 | + |
| 65 | + except requests.exceptions.RequestException as err: |
| 66 | + print("[ERROR] Failed to download features", err) |
| 67 | + |
| 68 | + def upload_report(project_id, access_key, secret_key, |
| 69 | + run_name='Test run ' + datetime.datetime.now().strftime("%d %b %Y %H:%M:%S"), |
| 70 | + json_report_folder='./reports/', json_report_include_pattern='\.json$', type='cucumber', |
| 71 | + proxy_uri=None, proxy_username=None, proxy_password=None): |
| 72 | + |
| 73 | + path = 'https://bdd.assertthat.com/rest/api/1/project/' + project_id + '/report' |
| 74 | + |
| 75 | + if proxy_uri is None: |
| 76 | + proxies = None |
| 77 | + |
| 78 | + elif proxy_uri is not None: |
| 79 | + if proxy_username is None: |
| 80 | + proxy_http = "http://%s" % proxy_uri |
| 81 | + proxy_https = "https://%s" % proxy_uri |
| 82 | + elif proxy_username is not None: |
| 83 | + proxy_http = "http://%s:%s@%s" % (proxy_username, proxy_password, proxy_uri) |
| 84 | + proxy_https = "https://%s:%s@%s" % (proxy_username, proxy_password, proxy_uri) |
| 85 | + proxies = {'http': proxy_http, |
| 86 | + 'https': proxy_https |
| 87 | + } |
| 88 | + |
| 89 | + runId = '-1' |
| 90 | + for subdir, dirs, files in os.walk(json_report_folder): |
| 91 | + print("[INFO] Uploading json reports to AssertThat for %s " % files) |
| 92 | + for file in files: |
| 93 | + if re.search(r"%s" % json_report_include_pattern, file): |
| 94 | + report_file = os.path.join(subdir, file) |
| 95 | + upload_file = {'file': open(report_file, 'rb')} |
| 96 | + |
| 97 | + headers = {} |
| 98 | + payload = {'runName': run_name, |
| 99 | + 'type': type, |
| 100 | + 'runId': runId |
| 101 | + } |
| 102 | + |
| 103 | + try: |
| 104 | + response = requests.post(path, auth=(access_key, secret_key), headers=headers, params=payload, |
| 105 | + files=upload_file, proxies=proxies) |
| 106 | + response_content = json.loads(response.content) |
| 107 | + |
| 108 | + if response.status_code == 200: |
| 109 | + runId = response_content['runId'] |
| 110 | + print("[INFO] Uploaded report file %s to AssertThat" % file) |
| 111 | + |
| 112 | + response.raise_for_status() |
| 113 | + |
| 114 | + except requests.exceptions.HTTPError as errh: |
| 115 | + print("[ERROR] %s while uploading %s" % (response_content['message'], file)) |
| 116 | + |
| 117 | + except requests.exceptions.RequestException as err: |
| 118 | + print("[ERROR] Failed to send request", err) |
0 commit comments