From 62c7d645b73dd9e5fc9a4aec9e41248ad033c1cd Mon Sep 17 00:00:00 2001 From: yevh-berdnyk Date: Sat, 24 Nov 2018 17:18:31 +0100 Subject: [PATCH] Commit status change depending on e2e tests results Signed-off-by: Anton Danchenko --- test/appium/support/api/jenkins_api.py | 11 +++++++++++ test/appium/support/testrail_report.py | 10 ++++++++++ test/appium/tests/conftest.py | 20 ++++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 test/appium/support/api/jenkins_api.py diff --git a/test/appium/support/api/jenkins_api.py b/test/appium/support/api/jenkins_api.py new file mode 100644 index 0000000000..6ecd6ee63d --- /dev/null +++ b/test/appium/support/api/jenkins_api.py @@ -0,0 +1,11 @@ +import requests + + +def get_jenkins_build_url(pr_number: str) -> str: + jenkins_api_url = 'https://ci.status.im/job/end-to-end-tests/job/status-app-end-to-end-tests/' + builds_list = requests.get(jenkins_api_url + 'api/json?tree=builds[id,url]').json()[ + 'builds'] + for build in builds_list: + data = requests.get('%s/%s/api/json' % (jenkins_api_url, build['id'])).json() + if data['displayName'] == 'PR-' + pr_number: + return build['url'] + '/console' diff --git a/test/appium/support/testrail_report.py b/test/appium/support/testrail_report.py index 69e531d26e..cfa01042e6 100644 --- a/test/appium/support/testrail_report.py +++ b/test/appium/support/testrail_report.py @@ -102,6 +102,16 @@ class TestrailReport(BaseTestReport): else devices + test_steps} self.post(method, data=data) + def get_run_results(self): + return self.get('get_results_for_run/%s' % self.run_id) + + def is_run_successful(self): + for test in self.get_run_results(): + if test['status_id'] != 1: + return False + else: + return True + def get_test_result_link(self, test_run_id, test_case_id): try: test_id = self.get('get_results_for_case/%s/%s' % (test_run_id, test_case_id))[0]['test_id'] diff --git a/test/appium/tests/conftest.py b/test/appium/tests/conftest.py index b8072f7a16..25035849c2 100644 --- a/test/appium/tests/conftest.py +++ b/test/appium/tests/conftest.py @@ -10,6 +10,7 @@ from datetime import datetime from os import environ from io import BytesIO from sauceclient import SauceClient +from support.api.jenkins_api import get_jenkins_build_url from support.api.network_api import NetworkApi from support.github_report import GithubHtmlReport from support.testrail_report import TestrailReport @@ -143,14 +144,21 @@ def pytest_configure(config): test_suite_data.apk_name = ([i for i in [i for i in config.getoption('apk').split('/') if '.apk' in i]])[0] if is_master(config): + pr_number = config.getoption('pr_number') if config.getoption('testrail_report'): - pr_number = config.getoption('pr_number') if pr_number: run_number = len(testrail_report.get_runs(pr_number)) + 1 run_name = 'PR-%s run #%s' % (pr_number, run_number) else: run_name = test_suite_data.apk_name testrail_report.add_run(run_name) + if pr_number: + from github import Github + repo = Github(github_token).get_user('status-im').get_repo('status-react') + pull = repo.get_pull(int(pr_number)) + pull.get_commits()[0].create_status(state='pending', context='Mobile e2e tests', + description='e2e tests are running', + target_url=get_jenkins_build_url(pr_number)) if config.getoption('env') == 'sauce': if not is_uploaded(): if 'http' in config.getoption('apk'): @@ -175,7 +183,15 @@ def pytest_unconfigure(config): from github import Github repo = Github(github_token).get_user('status-im').get_repo('status-react') pull = repo.get_pull(int(config.getoption('pr_number'))) - pull.create_issue_comment(github_report.build_html_report(testrail_report.run_id)) + comment = pull.create_issue_comment(github_report.build_html_report(testrail_report.run_id)) + if not testrail_report.is_run_successful(): + pull.get_commits()[0].create_status(state='failure', context='Mobile e2e tests', + description='Failure - e2e tests are failed', + target_url=comment.html_url) + else: + pull.get_commits()[0].create_status(state='success', context='Mobile e2e tests', + description='Success - e2e tests are passed', + target_url=comment.html_url) def should_save_device_stats(config):