|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script: |
| 4 | +# 1. parses the version number from the branch name |
| 5 | +# 2. updates version.py files to match that version |
| 6 | +# 3. iterates through CHANGELOG.md files and updates any files containing |
| 7 | +# unreleased changes |
| 8 | +# 4. sets the output variable 'version_updated' to determine whether |
| 9 | +# the github action to create a pull request should run. this allows |
| 10 | +# maintainers to merge changes back into the release branch without |
| 11 | +# triggering unnecessary pull requests |
| 12 | +# |
| 13 | + |
| 14 | +VERSION=`echo $1 | awk -F "/" '{print $NF}'` |
| 15 | +echo "Using version ${VERSION}" |
| 16 | + |
| 17 | +# check the version matches expected versioning e.g |
| 18 | +# 0.6, 0.6b, 0.6b0, 0.6.0 |
| 19 | +if [[ ! "${VERSION}" =~ ^([0-9])(\.*[0-9]{1,5}[a-b]*){1,3}$ ]]; then |
| 20 | + echo "Version number invalid: $VERSION" |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +function update_version_file() { |
| 25 | + errors=0 |
| 26 | + for f in `find . -name version.py`; do |
| 27 | + # check if version is already in version.py |
| 28 | + grep -q ${VERSION} $f; |
| 29 | + rc=$? |
| 30 | + if [ $rc == 0 ]; then |
| 31 | + errors=1 |
| 32 | + echo "${f} already contains ${VERSION}" |
| 33 | + continue |
| 34 | + fi |
| 35 | + # update version.py |
| 36 | + perl -i -pe "s/__version__.*/__version__ = \"${VERSION}\"/g" ${f}; |
| 37 | + git add ${f}; |
| 38 | + echo "Updating ${f}" |
| 39 | + done |
| 40 | + if [ ${errors} != 0 ]; then |
| 41 | + echo "::set-output name=version_updated::0" |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +function update_changelog() { |
| 47 | + errors=0 |
| 48 | + for f in `find . -name CHANGELOG.md`; do |
| 49 | + # check if version is already in CHANGELOG |
| 50 | + grep -q ${VERSION} $f; |
| 51 | + rc=$? |
| 52 | + if [ $rc == 0 ]; then |
| 53 | + errors=1 |
| 54 | + echo "${f} already contains ${VERSION}" |
| 55 | + continue |
| 56 | + fi |
| 57 | + # check if changelog contains any new details |
| 58 | + changes=`sed -n '/## Unreleased/,/^##/p' ${f} | grep -v '^##' | wc -w | awk '{$1=$1;print}'` |
| 59 | + if [ ${changes} != "0" ]; then |
| 60 | + # update CHANGELOG.md |
| 61 | + perl -i -pe 's/## Unreleased.*/## Unreleased\n\n## '${VERSION}'/' ${f}; |
| 62 | + git add ${f}; |
| 63 | + echo "Updating ${f}" |
| 64 | + else |
| 65 | + echo "Skipping ${f}, no changes detected" |
| 66 | + fi |
| 67 | + done |
| 68 | + if [ ${errors} != 0 ]; then |
| 69 | + echo "::set-output name=version_updated::0" |
| 70 | + exit 0 |
| 71 | + fi |
| 72 | +} |
| 73 | + |
| 74 | +update_version_file |
| 75 | +update_changelog |
| 76 | + |
| 77 | +git config --local user.email "[email protected]" |
| 78 | +git config --local user.name "GitHub Action" |
| 79 | +git commit -m "updating changelogs and version to ${VERSION}" |
| 80 | +echo "::set-output name=version_updated::1" |
0 commit comments