From 70e8905ac767da24fb89213c08fdc309e7e263c9 Mon Sep 17 00:00:00 2001 From: Jamie MacDonald Date: Wed, 20 Dec 2023 11:52:47 +0000 Subject: [PATCH] Add branch parsing script --- scripts/get_tags_from_branch.py | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 scripts/get_tags_from_branch.py diff --git a/scripts/get_tags_from_branch.py b/scripts/get_tags_from_branch.py new file mode 100644 index 000000000..3c3651e97 --- /dev/null +++ b/scripts/get_tags_from_branch.py @@ -0,0 +1,39 @@ +import ast +import os +import subprocess +import sys + +def get_tags_from_branches(branches): + tags = [] + + for branch in branches: + # Strip ".x" suffix from branch name. + branch_name = branch.rstrip('.x') + + # Try to fetch all git tags for the given branch. + # `output` will be sorted in the descending order. + try: + output = subprocess.check_output( + f'git tag | sort -r -V | grep -E "^{branch_name}.[0-9]+$"', + shell=True, + universal_newlines=True + ) + except subprocess.CalledProcessError: + print(f'Failed to retrieve tags for `{branch_name}` branch.') + continue + + branch_tags = output.strip().split('\n') + tags.extend(branch_tags) + + return tags + +input_branches = sys.argv[1] + +# Convert stringified list to a Python list. +branches = ast.literal_eval(input_branches) +tags = get_tags_from_branches(branches) + +print(f'{tags}') + +with open(os.environ['GITHUB_OUTPUT'], 'a') as github_output_file: + print(f'tags={tags}', file=github_output_file) \ No newline at end of file