-
Notifications
You must be signed in to change notification settings - Fork 0
137 lines (117 loc) · 5.05 KB
/
pr-checks.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# rust-clippy is a tool that runs a bunch of lints to catch common
# mistakes in your Rust code and help improve your Rust code.
# More details at https://github.com/rust-lang/rust-clippy
# and https://rust-lang.github.io/rust-clippy/
name: PR Checks
on:
pull_request:
branches: [ "main" ]
types: [ready_for_review, opened, synchronize]
concurrency: "PR${{ github.event.pull_request && github.event.number }}"
jobs:
is-changelog-updated:
name: Check if updated changelogs are present
runs-on: ubuntu-latest
outputs:
needs-check: ${{ steps.check_relevant_changes.outputs.code_or_cargo_changed }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- uses: taiki-e/install-action@just
- name: List changed files
run: |
just changed-files ${{ github.event.pull_request.base.sha }}
printf "::debug::Changeset:\n%s" "$(cat changeset.txt)"
- name: Check for code or cargo-related changes
id: check_relevant_changes
run: |
# Initialize the boolean flag
code_or_cargo_changed=false
# Check the file list
while IFS= read -r file; do
if [[ "$file" == *.rs || "$file" == *.toml || "$file" == "Cargo.lock" ]]; then
code_or_cargo_changed=true
break
fi
done < changeset.txt
# Output the result
echo "code_or_cargo_changed=$code_or_cargo_changed" >> $GITHUB_OUTPUT
echo "## Detected changes" >> $GITHUB_STEP_SUMMARY
printf "Changes to code detected: %s" $code_or_cargo_changed >> $GITHUB_STEP_SUMMARY
- uses: swatinem/rust-cache@v2
if: ${{ steps.check_relevant_changes.outputs.code_or_cargo_changed == 'true' }}
- name: Install Rust toolchain
if: ${{ steps.check_relevant_changes.outputs.code_or_cargo_changed == 'true' }}
uses: dtolnay/rust-toolchain@stable
- name: Verify if CHANGELOG is updated
if: ${{ steps.check_relevant_changes.outputs.code_or_cargo_changed == 'true' }}
run: |
# Extract a list of modified workspace packages
workspace_root=$(cargo metadata --format-version 1 --no-deps | jq -r '.workspace_root')
affected_packages=()
# Find packages with changes
while IFS= read -r file; do
dir=$(dirname "$file")
if [[ -f "$workspace_root/$dir/Cargo.toml" ]]; then
package_name=$(grep -m 1 '^name' "$workspace_root/$dir/Cargo.toml" | awk -F\" '{print $2}')
if ! grep -q "CHANGELOG.md" <<< "$(git diff --name-only ${{ github.event.pull_request.base.sha }} HEAD -- $workspace_root/$dir)"; then
affected_packages+=("$package_name")
echo "::error file=$dir/CHANGELOG.md::Package '$package_name' has changes but CHANGELOG.md is not updated."
fi
fi
done < changeset.txt
if [[ ${#affected_packages[@]} -gt 0 ]]; then
printf "## Changelog report\n" >> $GITHUB_STEP_SUMMARY
echo "One or more packages require an updated CHANGELOG.md. See job errors for details." >> $GITHUB_STEP_SUMMARY
exit 1
else
echo "All changed packages have updated CHANGELOG.md."
fi
rust-clippy-analyze:
name: Analyze with clippy
runs-on: ubuntu-latest
needs: is-changelog-updated
if: ${{ needs.is-changelog-updated.outputs.needs-check == 'true' }}
permissions:
contents: read
security-events: write
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: swatinem/rust-cache@v2
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: taiki-e/install-action@cargo-binstall
- name: Install required cargo
run: cargo binstall -y --force clippy-sarif sarif-fmt
- name: Run rust-clippy
run:
cargo clippy --message-format=json --locked | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
continue-on-error: true
- name: Upload analysis results to GitHub
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: rust-clippy-results.sarif
wait-for-processing: true
cargo-test:
needs: [rust-clippy-analyze, is-changelog-updated]
if: ${{ needs.is-changelog-updated.outputs.needs-check == 'true' }}
name: Run unit tests
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4
- uses: swatinem/rust-cache@v2
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- name: Run unit tests
run: cargo test --tests --locked