|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -eu -o pipefail |
| 4 | + |
| 5 | +# Check that we're not on the main branch |
| 6 | +current_branch=$(git branch --show-current) |
| 7 | +if [ "$current_branch" = "main" ]; then |
| 8 | + echo "Error: Releases should not be done directly on the main branch." |
| 9 | + echo "Please create a release branch and run this script from there." |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +# Fetch latest changes and check that we're not behind origin/main |
| 14 | +echo "Fetching from origin..." |
| 15 | +git fetch origin |
| 16 | + |
| 17 | +if ! git merge-base --is-ancestor origin/main HEAD; then |
| 18 | + echo "Error: Current branch is behind origin/main." |
| 19 | + echo "Please merge or rebase with origin/main before releasing." |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +changelog=$(cat CHANGELOG.md) |
| 24 | + |
| 25 | +# minfraud-api-node format: "8.2.0 (2025-11-20)" followed by "---" |
| 26 | +regex='([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9]+)?) \(([0-9]{4}-[0-9]{2}-[0-9]{2})\)' |
| 27 | + |
| 28 | +if [[ ! $changelog =~ $regex ]]; then |
| 29 | + echo "Could not find version/date in CHANGELOG.md!" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +version="${BASH_REMATCH[1]}" |
| 34 | +date="${BASH_REMATCH[3]}" |
| 35 | + |
| 36 | +# Extract release notes: everything after the "---" line until the next version header |
| 37 | +notes=$(awk -v ver="$version" ' |
| 38 | + $0 ~ "^" ver " \\(" { found=1; next } |
| 39 | + found && /^-+$/ { in_notes=1; next } |
| 40 | + in_notes && /^[0-9]+\.[0-9]+\.[0-9]+.* \([0-9]{4}-[0-9]{2}-[0-9]{2}\)/ { exit } |
| 41 | + in_notes { print } |
| 42 | +' CHANGELOG.md | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}') |
| 43 | + |
| 44 | +if [[ "$date" != "$(date +"%Y-%m-%d")" ]]; then |
| 45 | + echo "Release date $date is not today ($(date +"%Y-%m-%d"))!" |
| 46 | + exit 1 |
| 47 | +fi |
| 48 | + |
| 49 | +tag="v$version" |
| 50 | + |
| 51 | +if [ -n "$(git status --porcelain)" ]; then |
| 52 | + echo "Working directory is not clean." >&2 |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +# Update version in package.json |
| 57 | +current_version=$(node -p "require('./package.json').version") |
| 58 | +if [ "$current_version" != "$version" ]; then |
| 59 | + echo "Updating version in package.json from $current_version to $version..." |
| 60 | + npm version "$version" --no-git-tag-version |
| 61 | +fi |
| 62 | + |
| 63 | +# Build and test |
| 64 | +echo "Running build and tests..." |
| 65 | +npm ci |
| 66 | +npm run build |
| 67 | +npm test |
| 68 | +npm run lint |
| 69 | + |
| 70 | +echo $'\nDiff:' |
| 71 | +git diff |
| 72 | + |
| 73 | +echo $'\nRelease notes:' |
| 74 | +echo "$notes" |
| 75 | + |
| 76 | +read -e -p "Commit changes and create release? (y/n) " should_continue |
| 77 | + |
| 78 | +if [ "$should_continue" != "y" ]; then |
| 79 | + echo "Aborting" |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +git commit -m "Prepare for $version" -a |
| 84 | + |
| 85 | +git push |
| 86 | + |
| 87 | +gh release create --target "$(git branch --show-current)" -t "$version" -n "$notes" "$tag" |
| 88 | + |
| 89 | +echo "" |
| 90 | +echo "Release $tag created successfully!" |
| 91 | +echo "The GitHub Actions workflow will now publish to npm." |
| 92 | +echo "Monitor the release at: https://github.com/maxmind/minfraud-api-node/actions" |
0 commit comments