|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Author: @ralfhandl |
| 4 | + |
| 5 | +# Run this script from the root of the repo. It is designed to be run manually in a development branch. |
| 6 | + |
| 7 | +branch=$(git branch --show-current) |
| 8 | + |
| 9 | +if [[ ! $branch =~ ^v[0-9]+\.[0-9]+-dev$ ]]; then |
| 10 | + echo "This script is intended to be run from a development branch, e.g. v3.2-dev" |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +vVersion=$(basename "$branch" "-dev") |
| 15 | +version=${vVersion:1} |
| 16 | + |
| 17 | +# Find last published spec version for this minor version |
| 18 | +lastSpec=$(git ls-tree origin/main versions/ --name-only | grep -E "/$version\.[0-9].md" | tail -1) |
| 19 | + |
| 20 | +if [ -z "$lastSpec" ]; then |
| 21 | + # Find last published spec version |
| 22 | + lastSpec=$(git ls-tree origin/main versions/ --name-only | grep -E "/.+\.[0-9].md" | tail -1) |
| 23 | + nextPatch=0 |
| 24 | + releaseType="Release" |
| 25 | +else |
| 26 | + lastPatch=$(basename "$lastSpec" ".md" | cut --delimiter=. --fields=3) |
| 27 | + nextPatch=$((lastPatch + 1)) |
| 28 | + releaseType="Patch release" |
| 29 | +fi |
| 30 | + |
| 31 | +nextVersion="$version.$nextPatch" |
| 32 | + |
| 33 | +if [ -z "$lastSpec" ]; then |
| 34 | + echo "Could not find any published specification version in origin/main" |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +lastVersion=$(basename "$lastSpec" ".md") |
| 39 | +echo === Initialize src/oas.md for $nextVersion from $lastVersion |
| 40 | + |
| 41 | +# Create PR branch from development branch |
| 42 | +prBranch="$branch-start-$nextVersion" |
| 43 | +if git ls-remote --exit-code --heads origin "$prBranch"; then |
| 44 | + echo "=== Failed: PR branch $prBranch already exists on the remote, please delete it and try again" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | +if ! git checkout -b "$prBranch"; then |
| 48 | + echo "=== Failed: PR branch $prBranch already exists locally, please delete it and try again" |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +# Create empty orphan branch and add src/oas.md with last spec's content and no history |
| 53 | +orphan="v$version-orphan" |
| 54 | +if ! git switch --orphan "$orphan"; then |
| 55 | + git switch "$branch" |
| 56 | + git branch -d "$prBranch" |
| 57 | + echo "=== Failed: please delete branch $orphan and try again" |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | +mkdir src |
| 61 | +git show "main:$lastSpec" > src/oas.md |
| 62 | +git add src/oas.md |
| 63 | +git commit -m "copy from $lastVersion" |
| 64 | + |
| 65 | +# Merge orphan branch into PR branch, favoring orphan's version of src/oas.md |
| 66 | +git switch "$prBranch" |
| 67 | +git merge "$orphan" -X theirs --allow-unrelated-histories -m "reset src/oas.md history" |
| 68 | +git branch -D "$orphan" |
| 69 | + |
| 70 | +# Bump version headline, add line to history table |
| 71 | +historyTableHeader="\n| Version | Date | Notes |\n| ---- | ---- | ---- |\n" |
| 72 | +sed -z -e "s/\n## Version $lastVersion\n/\n## Version $nextVersion\n/" \ |
| 73 | + -z -e "s/$historyTableHeader/$historyTableHeader| $nextVersion | TBD | $releaseType of the OpenAPI Specification $nextVersion |\n/" \ |
| 74 | + src/oas.md > temp.md |
| 75 | +mv -f temp.md src/oas.md |
| 76 | + |
| 77 | +git add src/oas.md |
| 78 | +git commit -m "bump version" |
| 79 | + |
| 80 | +echo === Initialized src/oas.md |
| 81 | + |
| 82 | +# adjust tests when starting a new major or minor version |
| 83 | +if [ "$nextPatch" == "0" ]; then |
| 84 | + echo === Adjust tests for new version $version |
| 85 | + |
| 86 | + #TODO adjust test script |
| 87 | + |
| 88 | + #TODO adjust test data |
| 89 | + |
| 90 | + # git commit -m "adjust test script and test data" |
| 91 | + |
| 92 | + echo === Adjusted tests |
| 93 | +fi |
| 94 | + |
| 95 | +# Push PR branch to remote |
| 96 | +git push -u origin $prBranch |
| 97 | + |
| 98 | +# Clean up |
| 99 | +git switch "$branch" |
| 100 | +echo === Done |
0 commit comments