Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
khulnasoft-bot committed Dec 3, 2024
1 parent ed3614c commit e3eba0c
Show file tree
Hide file tree
Showing 223 changed files with 59,874 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .github/actions/gitlog/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Git log
description: Mangles git log for change log
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 }} --crate ${{ inputs.crate }}
126 changes: 126 additions & 0 deletions .github/actions/gitlog/gitlog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/bin/bash

# This mangles git log entries for change lop purposes

output_file=""
crate=""
range=""
auth=""
while true; do
case $1 in
"--output-file")
shift
output_file="$1"
shift
;;
"--crate")
shift
crate="$1"
shift
;;
"--range")
shift
range="$1"
shift
;;
"--auth")
shift
auth="$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

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

if [[ $last_release != "" ]]; then
commit_range="$from_commit...$last_release"
else
commit_range="$from_commit"
fi
else
commit_range="$range"
fi

ancestry_path=""
if [[ "$last_release" != "" ]]; then
ancestry_path="--ancestry-path"
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)"
IFS=" " read -r -a change_dirs <<<"$changes"

is_related=false
for change in "${change_dirs[@]}"; do
if [[ "$change" == "$crate" ]]; then
is_related=true
break
fi
done

echo $is_related
}

get_username() {
commit=$1

args=()
if [ -n "$auth" ]; then
args=("${args[@]}" "-H" "Authorization: Bearer $auth")
fi

curl -sSL \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${args[@]}" \
https://api.github.com/repos/nxpkg/fastapi/commits/"$commit" | jq -r .author.login
}

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

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

if [[ "$output_file" != "" ]]; then
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

if [ -n "$GITHUB_OUTPUT" ]; then
echo "last_release=$last_release" >>"$GITHUB_OUTPUT"
fi
16 changes: 16 additions & 0 deletions .github/actions/publish/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish crate
description: Publishes crate to crates.io
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 }} --ref ${{ inputs.ref }}
70 changes: 70 additions & 0 deletions .github/actions/publish/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

# 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
;;
esac
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"
# echo "publish: $module"
cargo publish -p "$module"
}

if [ ! -f "Cargo.toml" ]; then
echo "Missing Cargo.toml file, not in a Rust project root?" && exit 1
fi

echo "$token" | cargo login
while read -r module; do
# 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 metadata --format-version=1 --no-deps | jq -r '.packages[] | select(.name | test("'"$module"'$")) | .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
await_time=$((retry*2))
echo "Failed to publish, Retrying $retry... after $await_time sec."
sleep $await_time
retry=$((retry+1))
done
if [[ $retry -eq $max_retries ]]; then
echo "Failed to publish crate $module, try to increase await time? Or retries?" && exit 1
fi
done < <(cargo metadata --format-version=1 --no-deps | jq -r '.metadata.publish.order[]')
143 changes: 143 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
name: Fastapi build

on:
push:
paths:
- "**.rs"
- "**Cargo.toml"
pull_request:
branches: [master]
paths:
- "**.rs"
- "**Cargo.toml"
env:
CARGO_TERM_COLOR: always

jobs:
test:
strategy:
matrix:
crate:
- fastapi
- fastapi-gen
- fastapi-swagger-ui-vendored
- fastapi-swagger-ui
- fastapi-redoc
- fastapi-rapidoc
- fastapi-scalar
- fastapi-axum
- fastapi-config
- fastapi-actix-web
fail-fast: true
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2

- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Resolve changed paths
id: changes
run: |
if [[ $GITHUB_EVENT_NAME != "pull_request" ]]; then
echo "changes=true" >> $GITHUB_OUTPUT
exit 0
fi
changes=false
while read -r change; do
if [[ "$change" == "fastapi-gen" && "${{ matrix.crate }}" == "fastapi-gen" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi-swagger-ui-vendored" && "${{ matrix.crate }}" == "fastapi-swagger-ui-vendored" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi-swagger-ui" && "${{ matrix.crate }}" == "fastapi-swagger-ui" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi" && "${{ matrix.crate }}" == "fastapi" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi-redoc" && "${{ matrix.crate }}" == "fastapi-redoc" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi-rapidoc" && "${{ matrix.crate }}" == "fastapi-rapidoc" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi-scalar" && "${{ matrix.crate }}" == "fastapi-scalar" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi-axum" && "${{ matrix.crate }}" == "fastapi-axum" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi-config" && "${{ matrix.crate }}" == "fastapi-config" && $changes == false ]]; then
changes=true
elif [[ "$change" == "fastapi-actix-web" && "${{ matrix.crate }}" == "fastapi-actix-web" && $changes == false ]]; then
changes=true
fi
done < <(git diff --name-only ${{ github.sha }}~ ${{ github.sha }} | grep .rs | awk -F \/ '{print $1}')
echo "${{ matrix.crate }} changes: $changes"
echo "changes=$changes" >> $GITHUB_OUTPUT
- name: Check format
run: |
if [[ ${{ steps.changes.outputs.changes }} == true ]]; then
cargo fmt --check --package ${{ matrix.crate }}
fi
- name: Check clippy
run: |
if [[ ${{ steps.changes.outputs.changes }} == true ]]; then
cargo clippy --quiet --package ${{ matrix.crate }}
fi
- name: Run tests
run: |
if [[ ${{ steps.changes.outputs.changes }} == true ]]; then
./scripts/test.sh ${{ matrix.crate }}
fi
check-typos:
name: typos
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: taiki-e/install-action@v2
with:
tool: typos
- run: typos

test-examples-compile:
name: "test (examples)"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 2

- name: Install stable Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
components: clippy, rustfmt

- name: Install nightly Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
components: clippy, rustfmt

- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
examples/**/target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}_examples

- name: Test that examples compile
run: |
./scripts/validate-examples.sh
Loading

0 comments on commit e3eba0c

Please sign in to comment.