Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance actions support multimodules #37

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/actions/gitlog/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ inputs:
output-file:
description: File path where to place the content of the changed commits
required: true
crate:
description: Name of the crate to get git log for
required: true
outputs:
last_release:
description: Last release commit or first commit of history
value: ${{ steps.gitlog.outputs.last_release }}
runs:
using: composite
steps:
- shell: bash
id: gitlog
run: |
${{ github.action_path }}/gitlog.sh --output-file ${{ inputs.output-file }}
${{ github.action_path }}/gitlog.sh --output-file ${{ inputs.output-file }} --crate ${{ inputs.crate }}
58 changes: 53 additions & 5 deletions .github/actions/gitlog/gitlog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,41 @@

# This mangles git log entries for change lop purposes

from_commit=HEAD
last_release=$(git tag | sort -r | head -1) # get last tag

output_file=""
crate=""
while true; do
case $1 in
"--output-file")
shift
output_file="$1"
shift
;;
"--crate")
shift
crate="$1"
shift
;;
*)
break
;;
esac
done

if [[ "$output_file" == "" ]]; then
echo "Missing --output-file <file> option argument, define path to file or - for stdout" && exit 1
fi
if [[ "$crate" == "" ]]; then
echo "Missing --crate <crate> option argument, need an explisit crate to get git log for" && exit 1
fi

from_commit=HEAD
last_release=$(git tag | grep -E "$crate-[0-9]*\.[0-9]*\.[0-9]*" | sort -r | head -1)
echo "Found tag: $last_release"
if [[ "$last_release" == "" ]]; then
last_release=$(git tag | sort -r | head -1) # get last tag
echo "Using latest tag: $last_release"
fi

commit_range=""
if [[ $last_release != "" ]]; then
commit_range="$from_commit...$last_release"
Expand All @@ -33,10 +51,40 @@ fi

mapfile -t log_lines < <(git log --pretty=format:'(%h) %s' $ancestry_path $commit_range)

function is_crate_related {
commit="$1"
changes="$(git diff --name-only "$commit"~ "$commit" | awk -F / '{print $1}' | xargs)"

is_related=false
if [[ "$crate" != "utoipa" ]] && [[ "$changes" == *"$crate"* ]]; then
is_related=true
fi
if [[ "$crate" == "utoipa" ]] && [[ ! $changes =~ (utoipa-gen|utoipa-swagger-ui) ]]; then
is_related=true
fi

echo $is_related
}

log=""
for line in "${log_lines[@]}"; do
log=$log"* $line\n"
commit=$(echo "$line" | awk -F ' ' '{print $1}')
commit=${commit//[\(\)]/}

if [[ $(is_crate_related "$commit") == true ]]; then
log=$log"* $line\n"
fi
done

if [[ "$output_file" != "" ]]; then
echo -e "$log" > "$output_file"
if [[ "$output_file" == "-" ]]; then
echo -e "$log"
else
echo -e "$log" > "$output_file"
fi
fi

if [[ "$last_release" == "" ]]; then
last_release=$(git rev-list --reverse HEAD | head -1)
fi
echo "::set-output name=last_release::$last_release"
5 changes: 4 additions & 1 deletion .github/actions/publish/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ inputs:
token:
description: Cargo login token to use the publish the crate
required: true
ref:
description: "Github release tag ref"
required: true
runs:
using: composite
steps:
- shell: bash
id: publish_crate
run: |
${{ github.action_path }}/publish.sh --token ${{ inputs.token }}
${{ github.action_path }}/publish.sh --token ${{ inputs.token }} --ref ${{ inputs.ref }}
24 changes: 22 additions & 2 deletions .github/actions/publish/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
# Publishes crate to crates.io

token=""
ref=""
while true; do
case $1 in
"--token")
shift
token="$1"
shift
;;
"--ref")
shift
ref=${1/refs\/tags\//}
shift
;;
*)
break
;;
Expand All @@ -19,6 +25,9 @@ done
if [[ "$token" == "" ]]; then
echo "Missing --token <token> option argument, cannot publish crates without it!" && exit 1
fi
if [[ "$ref" == "" ]]; then
echo "Missing --ref <ref> option argument, need an explisit ref to release!" && exit 1
fi

function publish {
module="$1"
Expand All @@ -35,15 +44,26 @@ fi

echo "$token" | cargo login
while read -r module; do
echo "Publishing module $module..."
# crate=$(echo "$ref" | sed 's|-[0-9]*\.[0-9]*\.[0-9].*||')
crate=${ref/-[0-9]\.[0-9]\.[0-9]*/}
if [[ "$crate" != "$module" ]]; then
echo "Module: $module does not match to release crate: $crate, skipping release for module"
continue
fi

current_version=$(cargo read-manifest --manifest-path "$module"/Cargo.toml | jq -r '.version')
toml="Cargo.toml"
if [[ "$module" != "utoipa" ]]; then
toml="$module/Cargo.toml"
fi

current_version=$(cargo read-manifest --manifest-path "$toml" | jq -r '.version')
last_version=$(curl -sS https://crates.io/api/v1/crates/"$module"/versions | jq -r '.versions[0].num')
if [[ "$last_version" == "$current_version" ]]; then
echo "Module: $module, is already at it's latest release ($last_version), nothing to release"
continue
fi

echo "Publishing module $module..."
max_retries=10
retry=0
while ! publish "$module" && [[ $retry -lt $max_retries ]]; do
Expand Down
16 changes: 7 additions & 9 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ jobs:
strategy:
matrix:
testset:
- default
- actix
- gen
- swagger
- utoipa
- utoipa-gen
- utoipa-swagger-ui
fail-fast: true
runs-on: ubuntu-latest

Expand Down Expand Up @@ -61,14 +60,13 @@ jobs:

- name: Run tests
run: |
if [[ "${{ matrix.testset }}" == "default" ]] && [[ ${{ steps.changes.outputs.root_changed }} == true ]]; then
if [[ "${{ matrix.testset }}" == "utoipa" ]] && [[ ${{ steps.changes.outputs.root_changed }} == true ]]; then
cargo test
cargo test --test path_response_derive_test_no_serde_json --no-default-features
cargo test --test component_derive_no_serde_json --no-default-features
elif [[ "${{ matrix.testset }}" == "actix" ]] && [[ ${{ steps.changes.outputs.root_changed }} == true ]]; then
cargo test --features actix_extras
elif [[ "${{ matrix.testset }}" == "gen" ]] && [[ ${{ steps.changes.outputs.gen_changed }} == true ]]; then
cargo test --test path_derive_actix --test path_parameter_derive_actix --features actix_extras
elif [[ "${{ matrix.testset }}" == "utoipa-gen" ]] && [[ ${{ steps.changes.outputs.gen_changed }} == true ]]; then
cargo test -p utoipa-gen --features actix_extras
elif [[ "${{ matrix.testset }}" == "swagger" ]] && [[ ${{ steps.changes.outputs.swagger_changed }} == true ]]; then
elif [[ "${{ matrix.testset }}" == "utoipa-swagger-ui" ]] && [[ ${{ steps.changes.outputs.swagger_changed }} == true ]]; then
cargo test -p utoipa-swagger-ui --features actix-web
fi
33 changes: 24 additions & 9 deletions .github/workflows/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ env:

jobs:
draft:
strategy:
matrix:
crate:
- utoipa
- utoipa-gen
- utoipa-swagger-ui
fail-fast: true
runs-on: ubuntu-latest

steps:
Expand All @@ -22,45 +29,53 @@ jobs:
id: gitlog
with:
output-file: ./draft-gitlog.md
crate: ${{ matrix.crate }}

- name: Prepare changes
run: |
echo "# Changes in this Release" > ./draft-changes.md
echo "## What's New :gem: :new: :tada:" > ./draft-changes.md
cat < ./draft-gitlog.md >> ./draft-changes.md

cat ./draft-changes.md

- name: Get release info
id: release_info
run: |
version=$(cargo read-manifest | jq -r .version)
version=""
if [[ "${{ matrix.crate }}" == "utoipa" ]]; then
version=$(cargo read-manifest | jq -r .version)
else
version=$(cargo read-manifest --manifest-path "${{ matrix.crate }}/Cargo.toml" | jq -r .version)
fi

prerelease=false
if [[ "$version" =~ .*-.* ]]; then
prerelease=true
fi
echo "::set-output name=is_prerelease::$prerelease"
echo "::set-output name=version::$version"

- name: Add full change log link
run: |
echo -e "#### Full [change log](${{ github.server_url }}/${{ github.repository }}/compare/${{ steps.gitlog.outputs.last_release }}...${{ matrix.crate }}-${{ steps.release_info.outputs.version }})" >> ./draft-changes.md

- name: Check existing release
run: |
if git tag | grep ${{ steps.release_info.outputs.version }} > /dev/null; then
echo "Tag tag with ${{ steps.release_info.outputs.version }} already exists, cannot draft a release for already existing tag!, Consider upgrading versions to Cargo.toml file" && exit 1
if git tag | grep ${{ matrix.crate }}-${{ steps.release_info.outputs.version }} > /dev/null; then
echo "Tag tag with ${{ matrix.crate }}-${{ steps.release_info.outputs.version }} already exists, cannot draft a release for already existing tag!, Consider upgrading versions to Cargo.toml file" && exit 1
fi

- name: Remove previous release
run: |
echo ${{ secrets.GITHUB_TOKEN }} | gh auth login --with-token
gh release delete ${{ steps.release_info.outputs.version }} -y || true
gh release delete ${{ matrix.crate }}-${{ steps.release_info.outputs.version }} -y || true

- name: Create release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.release_info.outputs.version }}
release_name: Release ${{ steps.release_info.outputs.version }}
tag_name: ${{ matrix.crate }}-${{ steps.release_info.outputs.version }}
release_name: ${{ matrix.crate }}-${{ steps.release_info.outputs.version }}
body_path: ./draft-changes.md
draft: true
prerelease: ${{ steps.release_info.outputs.is_prerelease }}
1 change: 1 addition & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ jobs:
name: Cargo publish
with:
token: ${{ secrets.CARGO_LOGIN }}
ref: ${{ github.ref }}