testrail_utils.py (3196B)
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 script contains utility functions designed to support the integration of automated 8 testing processes with TestRail, a test case management tool. The primary focus is on 9 creating and managing milestones in TestRail based on automated smoke tests for product 10 releases. It includes functions for building milestone names and descriptions, determining 11 release types, and loading TestRail credentials. 12 13 Functions: 14 - build_milestone_name(product_type, release_type, version_number): Constructs a formatted 15 milestone name based on the product type, release type, and version number. 16 - build_milestone_description(milestone_name): Generates a detailed description for the 17 milestone, including the release date and placeholders for testing status and QA recommendations. 18 - get_release_version(): Reads and returns the release version number from a 'version.txt' file. 19 - get_release_type(version): Determines the release type (e.g., Alpha, Beta, RC) based on 20 the version string. 21 - load_testrail_credentials(json_file_path): Loads TestRail credentials from a JSON file 22 and handles potential errors during the loading process. 23 """ 24 25 import json 26 import os 27 import textwrap 28 from datetime import datetime 29 30 31 def build_milestone_name(product_type, release_type, version_number): 32 return f"Build Validation sign-off - {product_type} {release_type} {version_number}" 33 34 35 def build_milestone_description(milestone_name): 36 current_date = datetime.now() 37 formatted_date = current_date = current_date.strftime("%B %d, %Y") 38 return textwrap.dedent( 39 f""" 40 RELEASE: {milestone_name}\n\n\ 41 RELEASE_TAG_URL: https://archive.mozilla.org/pub/fenix/releases/\n\n\ 42 RELEASE_DATE: {formatted_date}\n\n\ 43 TESTING_STATUS: [ TBD ]\n\n\ 44 QA_RECOMMENDATION:[ TBD ]\n\n\ 45 QA_RECOMENTATION_VERBOSE: \n\n\ 46 TESTING_SUMMARY\n\n\ 47 Known issues: n/a\n\ 48 New issue: n/a\n\ 49 Verified issue: 50 """ 51 ) 52 53 54 def get_release_version(): 55 # Check if version.txt was found 56 version_file_path = os.path.join( 57 os.environ.get("GECKO_PATH", "."), "mobile", "android", "version.txt" 58 ) 59 if not os.path.isfile(version_file_path): 60 raise FileNotFoundError(f"{version_file_path} not found.") 61 62 # Read the version from the file 63 with open(version_file_path) as file: 64 version = file.readline().strip() 65 66 return version 67 68 69 def get_release_type(version): 70 release_map = {"a": "Alpha", "b": "Beta"} 71 # use generator expression to check each char for key else default to 'RC' 72 product_type = next( 73 (release_map[char] for char in version if char in release_map), "RC" 74 ) 75 return product_type 76 77 78 def load_testrail_credentials(json_file_path): 79 try: 80 with open(json_file_path) as file: 81 credentials = json.load(file) 82 return credentials 83 except json.JSONDecodeError as e: 84 raise ValueError(f"Failed to load TestRail credentials: {e}")