Skip to content

Commit

Permalink
Merge pull request #7 from DCMLab/automatic_workflow_release
Browse files Browse the repository at this point in the history
Automatic release workflow completed
  • Loading branch information
johentsch authored Sep 20, 2023
2 parents 02f27f4 + d5e1e87 commit 7e49c25
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 70 deletions.
76 changes: 43 additions & 33 deletions .github/workflows/helper.py
Original file line number Diff line number Diff line change
@@ -1,72 +1,82 @@
import json
import argparse
import re
import os
def create_new_tag(tag):
if not (re.match(r'^v\d', 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')

# Notice that this will make tag version of three digits become two digits
# Notice that this could make a tag version of three digits become two digits
# e.i 3.2.1 -> 3.3
digits_tags = (re.match(r'^v\d+.\d+', tag)).group()[1::].split('.')
digits_tags = (re.match(r'^v\d+\.\d+', tag)).group()[1::].split('.')
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:
print(f'new_tag={tag}', file=fh)

def update_files_with_tag(old_tag, new_tag):
# This function needs to take care of updating
# .zenodo.json and CITATION.cff
# TO-DO: make zenodo.json robust to search for version tags
if os.path.isfile(".zenodo.json"):
try:
with open(".zenodo.json", "r") as f:
data = json.load(f)

data["version"] = data["version"].replace(old_tag,new_tag)
data["title"] = data["title"].replace(old_tag,new_tag)

with open(".zenodo.json", "w") as f:
json.dump(data, f)

except Exception as e:
print(e)

if os.path.isfile("CITATION.cff"):
def update_file_with_tag(f_name, old_tag, new_tag):
if os.path.isfile(f_name):
try:
with open("CITATION.cff", "r", encoding="utf-8") as file:
citation = file.read()
modified_citation = citation.replace(old_tag, new_tag)
with open("CITATION.cff", "w", encoding="utf-8") as file:
file.write(modified_citation)
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:
print(e)
else:
print(f"Warning: {f_name} doest exist at the current path {os.getcwd()}")

def main(args):
tag = args.tag
new_tag = "v2.0"
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_files_with_tag(tag,new_tag)
update_file_with_tag(".zenodo.json", tag, new_tag)
update_file_with_tag("CITATION.cff", tag, new_tag)
update_file_with_tag("README.md", tag, new_tag)
store_tag(new_tag)

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()
73 changes: 36 additions & 37 deletions .github/workflows/version_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,56 @@ jobs:
- name: Checkout corpus repository
uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.MS3_BOT_TOKEN }}

# this step could be replaced with dcml_docker action
- name: "Get ms3 package & transform"
ref: "${{ github.event.pull_request.base.ref }}"

- 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 }}

# this step could be replaced with dcml_docker action
- name: Setup Github credentials
run: |
git config --global user.name "ms3-bot"
git config --global user.email [email protected]
- name: "Generate new tag version"
- name: "Generate a new tag version"
id: generate_tag
run: |
ls -a
python .github/workflows/helper.py --tag "${{ steps.tag.outputs.tag_version }}"
ls -a
cat .zenodo.json
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"
# this step could be replaced with dcml_docker action
- name: "Get ms3 package & transform"
- name: Setup Github credentials & push zenodo, citation and README changes
continue-on-error: true
run: |
git config --global user.name "ms3-bot"
git config --global user.email [email protected]
if [[ -f .zenodo.json ]]; then
git add .zenodo.json
fi
if [[ -f CITATION.cff ]]; then
git add CITATION.cff
fi
if [[ -f README.md ]]; then
git add README.md
fi
git commit -m 'chore: files updated with tag: ${{ steps.generate_tag.outputs.new_tag }}'
git push
- name: "Get ms3 package & apply transform"
continue-on-error: true
run: |
pip install --upgrade pip
pip install ms3
ls -a
ms3 transform -M -N -X -F -D
ls -a
- name: "Create release"
uses: "marvinpinto/action-automatic-releases@latest"
- uses: ncipollo/release-action@v1
with:
repo_token: "${{ secrets.MS3_BOT_TOKEN }}"
automatic_release_tag: "${{ steps.generate_tag.outputs.new_tag }}"
prerelease: false
title: "${{ github.event.pull_request.title }}"
files: |
.zenodo.json
CITATION.cff
${{ github.event.repository.name }}.zip
${{ github.event.repository.name }}.datapackage.json
abc.datapackage.json.errors
- name: "Update release body"
run: |
gh release edit "${{ steps.generate_tag.outputs.new_tag }}" --notes "${{ github.event.pull_request.body }}" --repo "${{ github.event.repository.full_name }}"
env:
GITHUB_TOKEN: ${{ secrets.MS3_BOT_TOKEN }}
artifacts: "${{ github.event.repository.name }}.zip,\
${{ github.event.repository.name }}.datapackage.json,\
${{ github.event.repository.name }}.datapackage.errors"
body: "${{ github.event.pull_request.body }}"
name: "${{ github.event.pull_request.title }}"
tag: "${{ steps.generate_tag.outputs.new_tag }}"
makeLatest: "latest"
commit: "${{ github.event.pull_request.base.ref }}"

0 comments on commit 7e49c25

Please sign in to comment.