Skip to content

Commit

Permalink
feat: get latest tag and allow to update major version by attaching a…
Browse files Browse the repository at this point in the history
… label
  • Loading branch information
marcelmmc committed Sep 19, 2023
1 parent 1e642ba commit d5e1e87
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
33 changes: 28 additions & 5 deletions .github/workflows/helper.py
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -49,11 +55,28 @@ 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(
"--tag", type=str,
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()
12 changes: 8 additions & 4 deletions .github/workflows/version_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit d5e1e87

Please sign in to comment.