22
33# Implements https://circleci.com/docs/2.0/api-job-trigger/
44
5- import json
65import os
7- import sys
8-
6+ import json
97import requests
8+ import sys
109
11-
12- def trigger_workflow (token , commit , publish ):
10+ def TriggerWorkflow (token , commit , publish ):
1311 url = "https://circleci.com/api/v2/project/github/mapbox/mobile-metrics/pipeline"
1412
1513 headers = {
@@ -19,9 +17,9 @@ def trigger_workflow(token, commit, publish):
1917
2018 data = {
2119 "parameters" : {
22- "run_android_navigation_benchmark" : publish ,
23- "mapbox_slug" : "mapbox/mapbox-navigation-android" ,
24- "mapbox_hash" : commit
20+ "run_android_navigation_benchmark" : publish ,
21+ "mapbox_slug" : "mapbox/mapbox-navigation-android" ,
22+ "mapbox_hash" : commit
2523 }
2624 }
2725
@@ -31,14 +29,13 @@ def trigger_workflow(token, commit, publish):
3129 response = requests .post (url , auth = (token , "" ), headers = headers , json = data )
3230
3331 if response .status_code != 201 and response .status_code != 200 :
34- print ("Error triggering the CircleCI: %s." % response .json ()["message" ])
35- sys .exit (1 )
32+ print ("Error triggering the CircleCI: %s." % response .json ()["message" ])
33+ sys .exit (1 )
3634 else :
37- response_dict = json .loads (response .text )
38- print ("Started run_android_navigation_benchmark: %s" % response_dict )
35+ response_dict = json .loads (response .text )
36+ print ("Started run_android_navigation_benchmark: %s" % response_dict )
3937
40-
41- def trigger_job (token , commit , job ):
38+ def TriggerJob (token , commit , job ):
4239 url = "https://circleci.com/api/v1.1/project/github/mapbox/mobile-metrics/tree/master"
4340
4441 headers = {
@@ -48,42 +45,43 @@ def trigger_job(token, commit, job):
4845
4946 data = {
5047 "build_parameters" : {
51- "CIRCLE_JOB" : job ,
52- "BENCHMARK_COMMIT" : commit
48+ "CIRCLE_JOB" : job ,
49+ "BENCHMARK_COMMIT" : commit
5350 }
5451 }
5552
5653 response = requests .post (url , auth = (token , "" ), headers = headers , json = data )
5754
5855 if response .status_code != 201 and response .status_code != 200 :
59- print ("Error triggering the CircleCI: %s." % response .json ()["message" ])
60- sys .exit (1 )
56+ print ("Error triggering the CircleCI: %s." % response .json ()["message" ])
57+ sys .exit (1 )
6158 else :
62- response_dict = json .loads (response .text )
63- build_url = response_dict ['build_url' ]
64- print ("Started %s: %s" % (job , build_url ))
65-
66-
67- def main ():
68- token = os .getenv ("MOBILE_METRICS_TOKEN" )
69- commit = os .getenv ("CIRCLE_SHA1" )
70-
71- if token is None :
72- print ("Error triggering because MOBILE_METRICS_TOKEN is not set" )
73- sys .exit (1 )
74-
75- # Publish results that have been committed to the main branch.
76- # Development runs can be found in CircleCI after manually triggered.
77- publish_results = os .getenv ("CIRCLE_BRANCH" ) == "main"
78-
79- trigger_workflow (token , commit , publish_results )
80-
81- if not publish_results :
82- trigger_job (token , commit , "android-navigation-code-coverage-ci" )
83- trigger_job (token , commit , "android-navigation-binary-size-ci" )
84-
85- return 0
86-
59+ response_dict = json .loads (response .text )
60+ build_url = response_dict ['build_url' ]
61+ print ("Started %s: %s" % (job , build_url ))
62+
63+ def Main ():
64+ token = os .getenv ("MOBILE_METRICS_TOKEN" )
65+ commit = os .getenv ("CIRCLE_SHA1" )
66+
67+ if token is None :
68+ print ("Error triggering because MOBILE_METRICS_TOKEN is not set" )
69+ sys .exit (1 )
70+
71+ # Publish results that have been committed to the main branch.
72+ # Development runs can be found in CircleCI after manually triggered.
73+ publishResults = os .getenv ("CIRCLE_BRANCH" ) == "main"
74+ TriggerWorkflow (token , commit , publishResults )
75+
76+ if publishResults :
77+ TriggerJob (token , commit , "android-navigation-benchmark" )
78+ TriggerJob (token , commit , "android-navigation-code-coverage" )
79+ TriggerJob (token , commit , "android-navigation-binary-size" )
80+ else :
81+ TriggerJob (token , commit , "android-navigation-code-coverage-ci" )
82+ TriggerJob (token , commit , "android-navigation-binary-size-ci" )
83+
84+ return 0
8785
8886if __name__ == "__main__" :
89- main ()
87+ Main ()
0 commit comments