Skip to content

Commit

Permalink
ci: ensure Cargo.toml and CHANGELOG of published crates are updated
Browse files Browse the repository at this point in the history
Resolves: #4367.

Pull-Request: #4620.
  • Loading branch information
thomaseizinger authored Oct 14, 2023
1 parent 74c087d commit 0446068
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
CRATE: ${{ matrix.crate }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: r7kamura/rust-problem-matchers@2c2f1016021a7455a6b5b4bbae31145f3b3cd83a #v1.4.0

Expand Down Expand Up @@ -70,6 +72,15 @@ jobs:
test "$PACKAGE_VERSION" = "$SPECIFIED_VERSION"
- name: Ensure manifest and CHANGELOG are properly updated
if: github.event_name == 'pull_request'
run: |
git fetch origin master:master
./scripts/ensure-version-bump-and-changelog.sh
env:
HEAD_SHA: ${{ github.event.pull_request.head.sha }}


wasm_tests:
name: Run all WASM tests
runs-on: ubuntu-latest
Expand Down
37 changes: 37 additions & 0 deletions scripts/ensure-version-bump-and-changelog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

set -ex;

MANIFEST_PATH=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "'"$CRATE"'") | .manifest_path')
DIR_TO_CRATE=$(dirname "$MANIFEST_PATH")

MERGE_BASE=$(git merge-base "$HEAD_SHA" master) # Find the merge base. This ensures we only diff what was actually added in the PR.

DIFF_TO_MASTER=$(git diff "$HEAD_SHA".."$MERGE_BASE" --name-status -- "$DIR_TO_CRATE")
CHANGELOG_DIFF=$(git diff "$HEAD_SHA".."$MERGE_BASE" --name-only -- "$DIR_TO_CRATE/CHANGELOG.md")

VERSION_IN_CHANGELOG=$(awk -F' ' '/^## [0-9]+\.[0-9]+\.[0-9]+/{print $2; exit}' "$DIR_TO_CRATE/CHANGELOG.md")
VERSION_IN_MANIFEST=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "'"$CRATE"'") | .version')

# First, ensure version in Cargo.toml and CHANGELOG are in sync. This should always hold, regardless of whether the code in the crate was modified.
if [[ "$VERSION_IN_CHANGELOG" != "$VERSION_IN_MANIFEST" ]]; then
echo "Version in Cargo.toml ($VERSION_IN_MANIFEST) does not match version in CHANGELOG ($VERSION_IN_CHANGELOG)"
exit 1
fi

# If the crate wasn't touched in this PR, exit early.
if [ -z "$DIFF_TO_MASTER" ]; then
exit 0;
fi

# Code was touched, ensure changelog is updated too.
if [ -z "$CHANGELOG_DIFF" ]; then
echo "Files in $DIR_TO_CRATE have changed, please write a changelog entry in $DIR_TO_CRATE/CHANGELOG.md"
exit 1
fi

# Code was touched, ensure the version used in the manifest hasn't been released yet.
if git tag | grep -q "^$CRATE-v${VERSION_IN_MANIFEST}$"; then
echo "v$VERSION_IN_MANIFEST of '$CRATE' has already been released, please bump the version."
exit 1
fi

0 comments on commit 0446068

Please sign in to comment.