From ec2a014bd1c5aee0d2e02e42e8165f269198e5a3 Mon Sep 17 00:00:00 2001 From: "sevastian.zhukov" Date: Tue, 17 Jan 2023 21:10:44 +0700 Subject: [PATCH] Refactoring trigger-mobile-metrics script for updated notifications --- scripts/trigger-mobile-metrics.py | 86 ++++++++++++++++--------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/scripts/trigger-mobile-metrics.py b/scripts/trigger-mobile-metrics.py index c62d8ac85d8..f2cc352d4fa 100644 --- a/scripts/trigger-mobile-metrics.py +++ b/scripts/trigger-mobile-metrics.py @@ -2,12 +2,14 @@ # Implements https://circleci.com/docs/2.0/api-job-trigger/ -import os import json -import requests +import os import sys -def TriggerWorkflow(token, commit, publish): +import requests + + +def trigger_workflow(token, commit, publish): url = "https://circleci.com/api/v2/project/github/mapbox/mobile-metrics/pipeline" headers = { @@ -17,9 +19,9 @@ def TriggerWorkflow(token, commit, publish): data = { "parameters": { - "run_android_navigation_benchmark": publish, - "mapbox_slug": "mapbox/mapbox-navigation-android", - "mapbox_hash": commit + "run_android_navigation_benchmark": publish, + "mapbox_slug": "mapbox/mapbox-navigation-android", + "mapbox_hash": commit } } @@ -29,13 +31,14 @@ def TriggerWorkflow(token, commit, publish): response = requests.post(url, auth=(token, ""), headers=headers, json=data) if response.status_code != 201 and response.status_code != 200: - print("Error triggering the CircleCI: %s." % response.json()["message"]) - sys.exit(1) + print("Error triggering the CircleCI: %s." % response.json()["message"]) + sys.exit(1) else: - response_dict = json.loads(response.text) - print("Started run_android_navigation_benchmark: %s" % response_dict) + response_dict = json.loads(response.text) + print("Started run_android_navigation_benchmark: %s" % response_dict) -def TriggerJob(token, commit, job): + +def trigger_job(token, commit, job): url = "https://circleci.com/api/v1.1/project/github/mapbox/mobile-metrics/tree/master" headers = { @@ -45,43 +48,42 @@ def TriggerJob(token, commit, job): data = { "build_parameters": { - "CIRCLE_JOB": job, - "BENCHMARK_COMMIT": commit + "CIRCLE_JOB": job, + "BENCHMARK_COMMIT": commit } } response = requests.post(url, auth=(token, ""), headers=headers, json=data) if response.status_code != 201 and response.status_code != 200: - print("Error triggering the CircleCI: %s." % response.json()["message"]) - sys.exit(1) + print("Error triggering the CircleCI: %s." % response.json()["message"]) + sys.exit(1) else: - response_dict = json.loads(response.text) - build_url = response_dict['build_url'] - print("Started %s: %s" % (job, build_url)) - -def Main(): - token = os.getenv("MOBILE_METRICS_TOKEN") - commit = os.getenv("CIRCLE_SHA1") - - if token is None: - print("Error triggering because MOBILE_METRICS_TOKEN is not set") - sys.exit(1) - - # Publish results that have been committed to the main branch. - # Development runs can be found in CircleCI after manually triggered. - publishResults = os.getenv("CIRCLE_BRANCH") == "main" - TriggerWorkflow(token, commit, publishResults) - - if publishResults: - TriggerJob(token, commit, "android-navigation-benchmark") - TriggerJob(token, commit, "android-navigation-code-coverage") - TriggerJob(token, commit, "android-navigation-binary-size") - else: - TriggerJob(token, commit, "android-navigation-code-coverage-ci") - TriggerJob(token, commit, "android-navigation-binary-size-ci") - - return 0 + response_dict = json.loads(response.text) + build_url = response_dict['build_url'] + print("Started %s: %s" % (job, build_url)) + + +def main(): + token = os.getenv("MOBILE_METRICS_TOKEN") + commit = os.getenv("CIRCLE_SHA1") + + if token is None: + print("Error triggering because MOBILE_METRICS_TOKEN is not set") + sys.exit(1) + + # Publish results that have been committed to the main branch. + # Development runs can be found in CircleCI after manually triggered. + publish_results = os.getenv("CIRCLE_BRANCH") == "main" + + trigger_workflow(token, commit, publish_results) + + if not publish_results: + trigger_job(token, commit, "android-navigation-code-coverage-ci") + trigger_job(token, commit, "android-navigation-binary-size-ci") + + return 0 + if __name__ == "__main__": - Main() + main()