testrail_main.py (3644B)
1 #!/usr/bin/env python3 2 3 # This Source Code Form is subject to the terms of the Mozilla Public 4 # License, v. 2.0. If a copy of the MPL was not distributed with this 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 """ 7 This Python script automates creating milestones and test runs in TestRail and updating 8 test cases based on the results of automated smoke tests for different product releases. 9 10 Functionality includes: 11 - Reading TestRail credentials and environment variables. 12 - Building milestone names and descriptions. 13 - Interacting with the TestRail API to create milestones, test runs, and update test cases. 14 - Sending notifications to a specified Slack channel. 15 """ 16 17 import os 18 import sys 19 20 from lib.testrail_api import TestRail 21 from lib.testrail_utils import ( 22 build_milestone_description, 23 build_milestone_name, 24 get_release_type, 25 get_release_version, 26 load_testrail_credentials, 27 ) 28 from slack_notifier import ( 29 get_product_icon, 30 get_taskcluster_options, 31 send_error_notification, 32 send_success_notification, 33 ) 34 35 # Constants 36 SUCCESS_CHANNEL_ID = "C07HUFVU2UD" # mobile-testeng-releases 37 ERROR_CHANNEL_ID = "C0134KJ4JHL" # mobile-alerts-android 38 39 40 def main(): 41 # Load TestRail credentials 42 credentials = load_testrail_credentials(".testrail_credentials.json") 43 testrail = TestRail( 44 credentials["host"], credentials["username"], credentials["password"] 45 ) 46 47 # Read task environment variables 48 try: 49 shipping_product = os.environ["SHIPPING_PRODUCT"] 50 testrail_product_type = os.environ["TESTRAIL_PRODUCT_TYPE"] 51 testrail_project_id = os.environ["TESTRAIL_PROJECT_ID"] 52 testrail_test_suite_id = os.environ["TESTRAIL_TEST_SUITE_ID"] 53 except KeyError as e: 54 raise ValueError(f"ERROR: Missing Environment Variable: {e}") 55 56 # Release information 57 release_version = get_release_version() 58 release_type = get_release_type(release_version) 59 60 # Build milestone information 61 milestone_name = build_milestone_name( 62 testrail_product_type, release_type, release_version 63 ) 64 milestone_description = build_milestone_description(milestone_name) 65 66 # Configure Taskcluster API 67 options = get_taskcluster_options() 68 69 try: 70 # Check if milestone exists 71 if testrail.does_milestone_exist(testrail_project_id, milestone_name): 72 print(f"Milestone for {milestone_name} already exists. Exiting script...") 73 sys.exit() 74 75 # Create milestone and test runs 76 devices = ["Google Pixel 3(Android11)", "Google Pixel 2(Android11)"] 77 milestone = testrail.create_milestone( 78 testrail_project_id, milestone_name, milestone_description 79 ) 80 81 for device in devices: 82 test_run = testrail.create_test_run( 83 testrail_project_id, milestone["id"], device, testrail_test_suite_id 84 ) 85 testrail.update_test_run_tests(test_run["id"], 1) # 1 = Passed 86 87 product_icon = get_product_icon(shipping_product) 88 89 # Send success notification 90 success_values = { 91 "RELEASE_TYPE": release_type, 92 "RELEASE_VERSION": release_version, 93 "SHIPPING_PRODUCT": shipping_product, 94 "TESTRAIL_PROJECT_ID": testrail_project_id, 95 "TESTRAIL_PRODUCT_TYPE": testrail_product_type, 96 "PRODUCT_ICON": product_icon, 97 } 98 send_success_notification(success_values, SUCCESS_CHANNEL_ID, options) 99 100 except Exception as error_message: 101 send_error_notification(str(error_message), ERROR_CHANNEL_ID, options) 102 103 104 if __name__ == "__main__": 105 main()