22
33# Implements https://circleci.com/docs/2.0/api-job-trigger/
44
5- import os
65import json
7- import requests
6+ import os
87import sys
98
10- def TriggerWorkflow (token , commit , publish ):
9+ import requests
10+
11+
12+ def trigger_workflow (token , commit , publish ):
1113 url = "https://circleci.com/api/v2/project/github/mapbox/mobile-metrics/pipeline"
1214
1315 headers = {
@@ -17,9 +19,9 @@ def TriggerWorkflow(token, commit, publish):
1719
1820 data = {
1921 "parameters" : {
20- "run_android_navigation_benchmark" : publish ,
21- "mapbox_slug" : "mapbox/mapbox-navigation-android" ,
22- "mapbox_hash" : commit
22+ "run_android_navigation_benchmark" : publish ,
23+ "mapbox_slug" : "mapbox/mapbox-navigation-android" ,
24+ "mapbox_hash" : commit
2325 }
2426 }
2527
@@ -29,13 +31,14 @@ def TriggerWorkflow(token, commit, publish):
2931 response = requests .post (url , auth = (token , "" ), headers = headers , json = data )
3032
3133 if response .status_code != 201 and response .status_code != 200 :
32- print ("Error triggering the CircleCI: %s." % response .json ()["message" ])
33- sys .exit (1 )
34+ print ("Error triggering the CircleCI: %s." % response .json ()["message" ])
35+ sys .exit (1 )
3436 else :
35- response_dict = json .loads (response .text )
36- print ("Started run_android_navigation_benchmark: %s" % response_dict )
37+ response_dict = json .loads (response .text )
38+ print ("Started run_android_navigation_benchmark: %s" % response_dict )
3739
38- def TriggerJob (token , commit , job ):
40+
41+ def trigger_job (token , commit , job ):
3942 url = "https://circleci.com/api/v1.1/project/github/mapbox/mobile-metrics/tree/master"
4043
4144 headers = {
@@ -45,43 +48,42 @@ def TriggerJob(token, commit, job):
4548
4649 data = {
4750 "build_parameters" : {
48- "CIRCLE_JOB" : job ,
49- "BENCHMARK_COMMIT" : commit
51+ "CIRCLE_JOB" : job ,
52+ "BENCHMARK_COMMIT" : commit
5053 }
5154 }
5255
5356 response = requests .post (url , auth = (token , "" ), headers = headers , json = data )
5457
5558 if response .status_code != 201 and response .status_code != 200 :
56- print ("Error triggering the CircleCI: %s." % response .json ()["message" ])
57- sys .exit (1 )
59+ print ("Error triggering the CircleCI: %s." % response .json ()["message" ])
60+ sys .exit (1 )
5861 else :
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
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+
8587
8688if __name__ == "__main__" :
87- Main ()
89+ main ()
0 commit comments