From d5e1e87fcc07a91522fbbc6b44609383890fcbbe Mon Sep 17 00:00:00 2001 From: marcelmmc Date: Mon, 18 Sep 2023 19:21:08 -0500 Subject: [PATCH] feat: get latest tag and allow to update major version by attaching a label --- .github/workflows/helper.py | 33 +++++++++++++++++++++++---- .github/workflows/version_release.yml | 12 ++++++---- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/.github/workflows/helper.py b/.github/workflows/helper.py index 88f84b0..3698fd6 100644 --- a/.github/workflows/helper.py +++ b/.github/workflows/helper.py @@ -1,7 +1,7 @@ import argparse import re import os -def create_new_tag(tag): +def create_new_tag(tag, update_major): if not (re.match(r'^v\d+\.\d+$', tag)): raise Exception(f'tag: {tag} is not giving in the correct format e.i v0.0') @@ -11,8 +11,15 @@ def create_new_tag(tag): if len(digits_tags) != 2: raise Exception(f'tag: {tag} must contain two version digits') - new_digit = int(digits_tags[1]) + 1 - return "v" + str(digits_tags[0]) + "." + str(new_digit) + major_num = int(digits_tags[0]) + minor_num = int(digits_tags[1]) + if update_major: + print(f"Label detected to update major version") + major_num += 1 + minor_num = 0 + else: + minor_num += 1 + return f"v{major_num}.{minor_num}" def store_tag(tag): with open(os.environ['GITHUB_OUTPUT'], 'a') as fh: @@ -24,7 +31,6 @@ def update_file_with_tag(f_name, old_tag, new_tag): with open(f_name, "r",encoding="utf-8") as f: data = f.read() data = data.replace(old_tag, new_tag) - with open(f_name, "w",encoding="utf-8") as f: f.write(data) except Exception as e: @@ -38,7 +44,7 @@ def main(args): if not tag: print(f"Warning: a latest release with a tag does not exist in current repository, starting from {new_tag}") else: - new_tag = create_new_tag(tag) + new_tag = create_new_tag(tag,args.update_major_ver) print(f"Repository with tag: {tag}, creating a new tag with: {new_tag}") update_file_with_tag(".zenodo.json", tag, new_tag) update_file_with_tag("CITATION.cff", tag, new_tag) @@ -49,6 +55,18 @@ def run(): args = parser.parse_args() main(args) + +def str_to_bool(value): + if value.lower() == "true": + return True + elif value.lower() == "false": + return False + else: + raise Exception( + f"Error: value {value} as argument is not accepted\n" + f"retry with true or false" + ) + if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( @@ -56,4 +74,9 @@ def run(): help="Require: latest tag", required=True ) + parser.add_argument( + "--update_major_ver", type=str_to_bool, + help="Require: boolean to update the major tag number", + required=True + ) run() \ No newline at end of file diff --git a/.github/workflows/version_release.yml b/.github/workflows/version_release.yml index b7fb00b..28b27ff 100644 --- a/.github/workflows/version_release.yml +++ b/.github/workflows/version_release.yml @@ -12,21 +12,24 @@ jobs: - name: Checkout corpus repository uses: actions/checkout@v3 with: + fetch-depth: 0 token: ${{ secrets.MS3_BOT_TOKEN }} ref: "${{ github.event.pull_request.base.ref }}" - - name: "Get ms3 package & transform" + + - name: "Get latest tag version" id: tag continue-on-error: true run: | - res=$(gh api -H "Accept: application/vnd.github+json" repos/${{ github.event.repository.full_name }}/releases/latest --jq '.tag_name') + res=$(git tag -l --sort=-v:refname | grep --invert-match '\^' | head -n 1) echo "tag_version=${res}" >> $GITHUB_OUTPUT env: GITHUB_TOKEN: ${{ secrets.MS3_BOT_TOKEN }} - - name: "Generate new tag version" + - name: "Generate a new tag version" id: generate_tag run: | - python .github/workflows/helper.py --tag "${{ steps.tag.outputs.tag_version }}" + major_in_PR="${{ contains(github.event.pull_request.labels.*.name, 'major_version')}}" + python .github/workflows/helper.py --tag "${{ steps.tag.outputs.tag_version }}" --update_major_ver "$major_in_PR" - name: Setup Github credentials & push zenodo, citation and README changes continue-on-error: true @@ -46,6 +49,7 @@ jobs: git push - name: "Get ms3 package & apply transform" + continue-on-error: true run: | pip install --upgrade pip pip install ms3