Testing fmt workflow #12
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
name: Test Code with Clippy and Fmt Checks | |
on: | |
pull_request: | |
push: | |
branches: | |
- main | |
jobs: | |
fmt-check: | |
runs-on: ubuntu-latest | |
env: | |
RUSTUP_NIGHTLY_VERSION: nightly-2024-05-30 | |
RUSTFLAGS: "-Dwarnings" | |
outputs: | |
output: ${{ steps.need-commit.outputs.exitcode }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
# - name: Free up Space and Installation | |
# uses: ./.github/actions/shared | |
- name: Remove rust-toolchain.toml | |
# To make sure that the nightly version will be used all throughout | |
run: | | |
rm /home/runner/work/pendulum/pendulum/rust-toolchain.toml | |
- name: Setup nightly Rust toolchain | |
uses: dtolnay/rust-toolchain@nightly | |
with: | |
toolchain: ${{ env.RUSTUP_NIGHTLY_VERSION }} | |
components: rustfmt, clippy | |
target: wasm32-unknown-unknown | |
- name: Setup nightly Rust as default | |
run: rustup default ${{ env.RUSTUP_NIGHTLY_VERSION }} | |
- name: Perform fmt | |
uses: actions-rs/cargo@v1 | |
with: | |
toolchain: ${{ env.RUSTUP_NIGHTLY_VERSION }} | |
command: fmt | |
args: --all | |
- name: Check for Changes | |
continue-on-error: true | |
id: need-commit | |
run: | | |
set +e | |
git diff --exit-code --quiet | |
exitcode="$?" | |
echo "exitcode=$exitcode" >> $GITHUB_OUTPUT | |
if exitcode == 0; then | |
echo "No changes detected" | |
else | |
echo "changes detected. adding all" | |
fi | |
- name: Set github actions username and email | |
if: steps.need-commit.outputs.exitcode == 1 | |
run: | | |
git config user.name 'github-actions[bot]' | |
git config user.email 'github-actions[bot]@users.noreply.github.com' | |
git diff --name-only > changed_files.txt | |
echo "List files amended by formatter" | |
cat changed_files.txt | |
commit_oid=$(git rev-parse HEAD) | |
# Initialize an empty JSON object for the additions | |
files_for_commit='{"additions": []}' | |
# Read the changed files from changed_files.txt | |
while IFS= read -r file; do | |
if [[ -f "$file" ]]; then | |
# Get the content for the file | |
file_content="$(cat "$file")" | |
# Base64 encode the contents of the file | |
base64_content=$(base64 -w 0 <<< "$file_content") | |
# Construct a JSON object for this file and append it to the additions array | |
files_for_commit=$(echo "$files_for_commit" | jq --arg path "$file" --arg content "$base64_content" \ | |
'.additions += [{ "path": $path, "contents": $content }]') | |
fi | |
done < changed_files.txt | |
# Output the final JSON array | |
echo "$files_for_commit" > files_for_commit.json | |
# Error handling for `jq` output | |
echo "Check for valid json output" | |
if ! jq . files_for_commit.json; then | |
echo "Error reading files_for_commit.json" | |
exit 1 | |
fi | |
commit_message="Commit changes made by code formatters" | |
# Prepare the mutation payload | |
mutation_payload=$(jq -n \ | |
--arg branch_name "${{ github.head_ref }}" \ | |
--arg commit_oid "$commit_oid" \ | |
--arg commit_message "$commit_message" \ | |
--arg repo_name "${{ github.repository }}" \ | |
--argjson fileChanges "$(jq -c . < files_for_commit.json)" \ | |
'{ | |
query: "mutation($input: CreateCommitOnBranchInput!) { createCommitOnBranch(input: $input) { commit { oid } } }", | |
variables: { | |
input: { | |
branch: { | |
repositoryNameWithOwner: $repo_name, | |
branchName: $branch_name | |
}, | |
message: { | |
headline: $commit_message | |
}, | |
fileChanges: $fileChanges, | |
expectedHeadOid: $commit_oid | |
} | |
} | |
}') | |
echo "Mutation Payload: $mutation_payload" | |
# Send the mutation request to GitHub's GraphQL API and capture the response | |
RESPONSE=$(curl -X POST -H "Authorization: bearer $GITHUB_TOKEN" \ | |
-H "Content-Type: application/json" \ | |
-d "$mutation_payload" https://api.github.com/graphql) | |
# Parse the commit OID from the response | |
COMMIT_OID=$(echo "$RESPONSE" | jq -r ".data.createCommitOnBranch.commit.oid") | |
# Check if the commit was successfully created | |
if [ "$commit_oid" != "null" ]; then | |
echo "Commit successfully created with OID: $commit_oid" | |
else | |
echo "Error creating commit: $RESPONSE" | |
fi | |
test-code: | |
needs: fmt-check | |
if: needs.fmt-check.outputs.output == 0 | |
runs-on: ubuntu-latest | |
env: | |
# Make sure CI fails on all warnings, including Clippy lints | |
RUSTFLAGS: "-Dwarnings" | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Free up Space and Installation | |
uses: ./.github/actions/shared | |
- name: Install Protoc | |
uses: arduino/setup-protoc@v1 | |
with: | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Test | |
uses: actions-rs/cargo@v1 | |
with: | |
command: test | |
args: --release | |
- name: Test for Runtime Benchmarks | |
uses: actions-rs/cargo@v1 | |
with: | |
command: test | |
args: --release --features=runtime-benchmarks,try-runtime | |
- name: Clippy -- Main | |
uses: actions-rs/cargo@v1 | |
with: | |
command: clippy | |
args: --release --all-features -- -W clippy::all -A clippy::style -A forgetting_copy_types -A forgetting_references | |
- name: Clippy -- All Targets (except integration) | |
uses: actions-rs/cargo@v1 | |
with: | |
command: clippy | |
# We are a bit more forgiving when it comes to the code in tests and only check for correctness | |
args: --release --workspace --all-features --all-targets --exclude runtime-integration-tests -- -A clippy::all -W clippy::correctness -A forgetting_copy_types -A forgetting_references | |
- name: Clippy -- Integration | |
uses: actions-rs/cargo@v1 | |
with: | |
command: clippy | |
# We are a bit more forgiving when it comes to the code in tests and only check for correctness | |
args: --release --package runtime-integration-tests --all-features --all-targets -- -A clippy::all -W clippy::correctness -A forgetting_copy_types -A forgetting_references |