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