-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add upgrade crates and openapi versions devbook
- Loading branch information
Showing
3 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
docs/devbook/upgrade-crates-and-openapi-versions/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Upgrade the crates and openapi versions before merging a pull request | ||
|
||
## Introduction | ||
|
||
This devbook provides a script that allow to automatically upgrade the crates and openapi versions in the project. | ||
|
||
Only crates and openapi with changes on the branch compared to `origin/main` are upgraded. | ||
|
||
## Prerequisites | ||
|
||
It requires having `cargo-get` installed, which can be done with the following command: | ||
|
||
``` | ||
cargo install cargo-get | ||
``` | ||
|
||
## Usage | ||
|
||
> [!NOTE] | ||
> All commands are executed from the root of the repository. | ||
### Dry-run | ||
|
||
Just run the script without argument, by default no changes are made to the project. | ||
|
||
```shell | ||
. ./docs/devbook/upgrade-crates-and-openapi-versions/upgrade_crates_and_openapi_versions.sh | ||
``` | ||
|
||
### Run | ||
|
||
Run the script with the `--run` argument to bump the crates and openapi versions. | ||
|
||
The script will finish with a preformatted commit message that can be used to create a commit. | ||
|
||
```shell | ||
. ./docs/devbook/upgrade-crates-and-openapi-versions/upgrade_crates_and_openapi_versions.sh --run | ||
``` | ||
|
||
> [!IMPORTANT] | ||
> The version bump is not based on the version on `origin/main`, but on the actual version in the branch. | ||
> | ||
> This means that running the script more than once will bump the versions again. | ||
101 changes: 101 additions & 0 deletions
101
docs/devbook/upgrade-crates-and-openapi-versions/upgrade_crates_and_openapi_versions.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/env bash | ||
set +a -u -o pipefail | ||
#set -vx | ||
|
||
# Check crates modify against `origin/main` and update their version. | ||
# The openapi.yam is also verified and updated if necessary. | ||
# At the end of the script, the commit message to used is displayed. | ||
|
||
# Usage: | ||
# update_crate_versions.sh --run: to execute the changes (default is dry-run) | ||
# update_crate_versions.sh: to display the changes without executing them (dry-run mode) | ||
|
||
# Prerequisites: | ||
# `cargo-get` needs to be installed (`cargo install cargo-get`). | ||
|
||
# NOTE | ||
# `cargo get workspace.members` display the list of path to crates in the workspace. | ||
# for the `cargo set-version` command, we need the name of the module (last element of the path). | ||
readonly ORANGE="\e[1;33m" | ||
readonly GREEN="\e[1;32m" | ||
readonly RESET="\e[0m" | ||
|
||
readonly OPEN_API_FILE=openapi.yaml | ||
declare OPEN_API_UPDATE="" | ||
declare OPEN_API_UPDATE_MESSAGE="" | ||
|
||
update_crate_versions() { | ||
local -r dry_run=$1 | ||
local -r -n files_modify=$2 | ||
local -r -a members="$(cargo get workspace.members --delimiter " ")" | ||
local -i nb_files_modify=0 | ||
local package_name | ||
|
||
local cargo_options="" | ||
if [ true = "$dry_run" ] | ||
then | ||
cargo_options=--dry-run | ||
fi | ||
|
||
for member in $members | ||
do | ||
nb_files_modify=$(echo "$files_modify" | grep -c "^$member/") | ||
if [[ $nb_files_modify -gt 0 ]] | ||
then | ||
package_name=${member##*/} | ||
cargo set-version $cargo_options --package "${package_name##*/}" --bump patch | ||
fi | ||
|
||
done | ||
} | ||
|
||
update_openapi_version() { | ||
local -r dry_run=$1 | ||
local -r version_line=$(grep -E "version: [0-9]+\.[0-9]+\.[0-9]+" $OPEN_API_FILE) | ||
local -r patch_number=$(echo "$version_line" | cut -d . -f 3) | ||
local -r next_patch_number=$((patch_number + 1)) | ||
local -r new_version=$(echo "$version_line" | cut -d . -f 1-2).$next_patch_number | ||
|
||
echo -e " ${GREEN}Upgrading${RESET} $OPEN_API_FILE from ${version_line##*version: } to ${new_version##*version: }" | ||
if [ true = "$dry_run" ] | ||
then | ||
echo -e "${ORANGE}warning${RESET}: aborting $OPEN_API_FILE update due to dry run" | ||
else | ||
sed -i "s/$version_line/version: $new_version/g" $OPEN_API_FILE | ||
fi | ||
OPEN_API_UPDATE="\n* $OPEN_API_FILE from \`${version_line##*version: }\` to \`${new_version##*version: }\`" | ||
OPEN_API_UPDATE_MESSAGE=" and \`$OPEN_API_FILE\` version" | ||
} | ||
|
||
################ | ||
readonly COMMAND="${1:-""}" | ||
readonly COMMIT_REF="HEAD" | ||
FILES_MODIFY="$(git diff "$COMMIT_REF" --name-only origin/main)" | ||
readonly -a FILES_MODIFY | ||
|
||
declare DRY_RUN=true | ||
if [[ "$COMMAND" == "--run" ]] | ||
then | ||
DRY_RUN=false | ||
fi | ||
|
||
update_crate_versions $DRY_RUN FILES_MODIFY | ||
|
||
if [ "$(echo "${FILES_MODIFY[@]}" | grep -xc "$OPEN_API_FILE")" -gt 0 ] | ||
then | ||
update_openapi_version $DRY_RUN | ||
fi | ||
|
||
if [ true = $DRY_RUN ] | ||
then | ||
echo -e "${ORANGE}warning${RESET}: script is run in dry mode. To execute the changes, run ${GREEN}./update_crate_versions.sh --run${RESET}" | ||
else | ||
UPDATED_CRATES="\n$(find . -name Cargo.toml -exec git diff "$COMMIT_REF" {} + | grep -E "^[\+\-]version = \"[0-9\.]+\"|name = " | tr '\n' ' ' | sed -r "s/ name = \"([a-z\-]+)\" -version = \"([0-9\.]+)\" \+version = \"([0-9\.]+)\" ?/* \1 from \`\2\` to \`\3\`\n/g")" | ||
COMMIT_MESSAGE=$(echo -e "chore: upgrade crate versions${OPEN_API_UPDATE_MESSAGE}\n${UPDATED_CRATES}${OPEN_API_UPDATE}") | ||
|
||
echo -e "$COMMIT_MESSAGE" | ||
|
||
# Do we want to commit the changes ? | ||
#git add $OPEN_API_FILE Cargo.lock ./*/Cargo.toml ./internal/*/Cargo.toml ./mithril-test-lab/*/Cargo.toml && git commit -m "$COMMIT_MESSAGE" | ||
fi | ||
|