Skip to content

Commit c728e25

Browse files
authored
chore: Add crate release helper scripts (#825)
* chore: add crate release helper scripts * fix typo in error message * fix yaml formatting in pre-commit config * add shellcheck to pre-commit * add verify_crate_versions.sh to pre-commit * add shellcheck to reviewdog workflow * ci: add pre-commit action * fix: run pre-commit with specific rust version for the rust checks * fix: install cargo components for pre-commit
1 parent 045af8d commit c728e25

File tree

5 files changed

+176
-23
lines changed

5 files changed

+176
-23
lines changed

.github/workflows/pr_pre-commit.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: pre-commit
3+
4+
on:
5+
pull_request:
6+
7+
env:
8+
CARGO_TERM_COLOR: always
9+
RUST_TOOLCHAIN_VERSION: "1.79.0"
10+
11+
jobs:
12+
pre-commit:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
16+
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
17+
with:
18+
python-version: '3.12'
19+
- uses: dtolnay/rust-toolchain@master
20+
with:
21+
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
22+
components: rustfmt,clippy
23+
- uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1

.github/workflows/reviewdog.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ jobs:
3434
with:
3535
github_token: ${{ secrets.GITHUB_TOKEN }}
3636

37+
shellcheck:
38+
runs-on: ubuntu-latest
39+
steps:
40+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
41+
- uses: reviewdog/action-shellcheck@52f34f737a16c65b8caa8c51ae1b23036afe5685 # v1.23.0
42+
with:
43+
github_token: ${{ secrets.GITHUB_TOKEN }}
44+
3745
yamllint:
3846
runs-on: ubuntu-latest
3947
steps:

.pre-commit-config.yaml

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,43 @@
22
# See https://pre-commit.com for more information
33
# See https://pre-commit.com/hooks.html for more hooks
44
repos:
5-
- repo: https://github.com/pre-commit/pre-commit-hooks
6-
rev: v4.2.0
7-
hooks:
8-
- id: trailing-whitespace
9-
- id: end-of-file-fixer
10-
- id: detect-aws-credentials
11-
args: ["--allow-missing-credentials"]
12-
- id: detect-private-key
13-
- repo: https://github.com/doublify/pre-commit-rust
14-
rev: v1.0
15-
hooks:
16-
- id: fmt
17-
args: ["--all", "--", "--check"]
18-
- id: clippy
19-
args: ["--all-targets", "--", "-D", "warnings"]
20-
- repo: https://github.com/adrienverge/yamllint
21-
rev: v1.26.3
22-
hooks:
23-
- id: yamllint
24-
- repo: https://github.com/igorshubovych/markdownlint-cli
25-
rev: v0.31.1
26-
hooks:
27-
- id: markdownlint
5+
- repo: https://github.com/pre-commit/pre-commit-hooks
6+
rev: v4.2.0
7+
hooks:
8+
- id: trailing-whitespace
9+
- id: end-of-file-fixer
10+
- id: detect-aws-credentials
11+
args: ["--allow-missing-credentials"]
12+
- id: detect-private-key
13+
14+
- repo: https://github.com/doublify/pre-commit-rust
15+
rev: v1.0
16+
hooks:
17+
- id: fmt
18+
args: ["--all", "--", "--check"]
19+
- id: clippy
20+
args: ["--all-targets", "--", "-D", "warnings"]
21+
- repo: https://github.com/adrienverge/yamllint
22+
rev: v1.26.3
23+
hooks:
24+
- id: yamllint
25+
26+
- repo: https://github.com/igorshubovych/markdownlint-cli
27+
rev: v0.31.1
28+
hooks:
29+
- id: markdownlint
30+
31+
- repo: https://github.com/koalaman/shellcheck-precommit
32+
rev: v0.10.0
33+
hooks:
34+
- id: shellcheck
35+
args: ["--severity=info"]
36+
37+
- repo: local
38+
hooks:
39+
- id: .scripts/verify-crate-versions
40+
name: .scripts/verify-crate-versions
41+
language: system
42+
entry: .scripts/verify_crate_versions.sh
43+
stages: [commit, merge-commit, manual]
44+
pass_filenames: false

.scripts/tag_and_push_release.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
3+
# Tag the latest release based on the versions in the Cargo.toml files.
4+
# Do this after merging a release PR to main.
5+
#
6+
# This script will skip local tags (eg: if a crate release has already been
7+
# done). You will be prompted at the end whether to push the tags or not.
8+
#
9+
# To ensure crate versions match the latest version in the changelogs,
10+
# see verify_crate_versions.sh
11+
12+
set -euo pipefail
13+
14+
# You need fzf to run this
15+
type fzf &> /dev/null || (echo "This script requires fzf" >&2 && exit 1)
16+
17+
# Update from remote
18+
git fetch origin
19+
20+
# Select a commit (usually the first is the right one)
21+
SHORT_COMMIT_HASH=$(git log --color=always --oneline | fzf --ansi --layout=reverse --prompt="Select the commit to tag" | cut -d' ' -f1)
22+
23+
# Get the latest versions
24+
LATEST_TAGS=$(find . -mindepth 2 -name CHANGELOG.md -exec sh -c 'grep -HoE "\[[0-9]+\.[0-9]+\.[0-9]+\]" "$1" | head -1 | sed -e "s|^./crates/\([a-z0-9_\-]\+\).*:\[\(.*\)\]$|\1-\2|"' shell {} \; | sort)
25+
LATEST_TAGS=$(find . -mindepth 2 -name CHANGELOG.md -exec sh -c 'grep -HoE "\[[0-9]+\.[0-9]+\.[0-9]+\]" "$1" | head -1 | sed -e "s|^./crates/\([a-z0-9_\-]\+\).*:\[\(.*\)\]$|\1-\2|"' shell {} \; | sort)
26+
ALL_TAGS=$(git tag | sort)
27+
28+
# Only show tags that don't exist (if you need to overwrite tags, do it manually)
29+
AVAILABLE_TAGS=$(comm -23 <(echo -e "$LATEST_TAGS") <(echo -e "$ALL_TAGS"))
30+
31+
# Select tags
32+
SELECTED_TAGS=$(echo -e "$AVAILABLE_TAGS" | fzf --reverse --multi --bind "ctrl-a:toggle-all" --bind "space:toggle" --prompt "Select multiple to release (use SPACE to select, CTRL+A to invert selection)")
33+
34+
# Tag each selected to the selected commit
35+
echo -e "$SELECTED_TAGS" | xargs -I {} git tag -s -m "release/{}" {} "$SHORT_COMMIT_HASH"
36+
echo
37+
read -r -p "push the tags [y/N]? " PUSH
38+
if [ "${PUSH,,}" == "y" ]; then
39+
echo -e "$SELECTED_TAGS" | xargs -I {} git push origin {}
40+
fi
41+
42+
git log -1 --oneline

.scripts/verify_crate_versions.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# Eg: makes a list like this based on the latest release in the CHANGELOG.md files:
6+
# k8s-version-0.1.1
7+
# stackable-certs-0.3.1
8+
# stackable-operator-0.70.0
9+
# stackable-operator-derive-0.3.1
10+
# stackable-telemetry-0.2.0
11+
# stackable-versioned-0.1.1
12+
# stackable-webhook-0.3.1
13+
14+
for CRATE in $(find . -mindepth 2 -name Cargo.toml | sed -e 's|^./crates/\([a-z0-9_\-]\+\).*|\1|' | sort); do
15+
# Get the version in Cargo.toml
16+
CRATE_VERSION=$(grep 'version' "./crates/$CRATE/Cargo.toml" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
17+
[ -n "$CRATE_VERSION" ] || (
18+
echo "CRATE_VERSION for $CRATE is empty." >&2
19+
echo "Please check ./crates/$CRATE/Cargo.toml" >&2
20+
exit 21
21+
)
22+
23+
# Special treatment of stackable-versioned-macros:
24+
# - It has no changelog
25+
# - The version should be the same as stackable-versioned
26+
if [ "$CRATE" = "stackable-versioned-macros" ]; then
27+
ASSOCIATED_CRATE="stackable-versioned"
28+
29+
# Get the version in Cargo.toml
30+
ASSOCIATED_CRATE_VERSION=$(grep 'version' "./crates/$ASSOCIATED_CRATE/Cargo.toml" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
31+
[ -n "$ASSOCIATED_CRATE_VERSION" ] || (
32+
echo "ASSOCIATED_CRATE_VERSION for $ASSOCIATED_CRATE_VERSION is empty" >&2
33+
echo "Please check ./crates/$ASSOCIATED_CRATE_VERSION/Cargo.toml" >&2
34+
exit 22
35+
)
36+
37+
# Ensure the versions match
38+
[ "$CRATE_VERSION" == "$ASSOCIATED_CRATE_VERSION" ] || (
39+
echo "Versions for $CRATE and $ASSOCIATED_CRATE do not match. $CHANGELOG_VERSION != $ASSOCIATED_CRATE_VERSION" >&2
40+
echo "Ensure the version in ./crates/$CRATE/Cargo.toml matches the version in ./crates/$ASSOCIATED_CRATE/Cargo.toml" >&2
41+
exit 23
42+
)
43+
44+
else
45+
# Get the latest documented version from the CHANGELOG.md
46+
CHANGELOG_VERSION=$(grep -oE '\[[0-9]+\.[0-9]+\.[0-9]+\]' "./crates/$CRATE/CHANGELOG.md" | head -1 | tr -d '[]')
47+
[ -n "$CHANGELOG_VERSION" ] || (
48+
echo "CHANGELOG_VERSION for $CRATE is empty" >&2
49+
echo "Please check the latest release version in ./crates/$CRATE/CHANGELOG.md" >&2
50+
exit 24
51+
)
52+
53+
# Ensure the versions match
54+
[ "$CRATE_VERSION" == "$CHANGELOG_VERSION" ] || (
55+
echo "Versions for $CRATE do not match. $CHANGELOG_VERSION != $CRATE_VERSION." >&2
56+
echo "Ensure the version in ./crates/$CRATE/CHANGELOG.md matches the latest release version in ./crates/$CRATE/Cargo.toml" >&2
57+
exit 25
58+
)
59+
fi
60+
61+
echo "${CRATE}-${CRATE_VERSION}"
62+
63+
done

0 commit comments

Comments
 (0)