-
Notifications
You must be signed in to change notification settings - Fork 4
267 lines (208 loc) · 6.96 KB
/
auto-pr-cargo-fixes.yaml
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
name: Auto-pull-request for cargo fixes
on:
# Run on push to main
push:
branches:
- main
paths:
- .github/workflows/auto-pr-cargo-fixes.yaml
- '*.rs'
- '**/*.rs'
# Run weekly
schedule:
- cron: '0 0 * * Sun'
# Needed so we can run it manually
workflow_dispatch:
permissions:
contents: read
defaults:
run:
shell: bash
env:
# So cargo doesn't complain about unstable features
RUSTC_BOOTSTRAP: 1
# Which branch to use to make the pull request
PR_BRANCH: auto/cargo_fix
# Whether or not to enable auto-merge
PR_AUTO_MERGE: false
jobs:
check-pull-request:
name: Check if the pull request exists
runs-on: ubuntu-latest
if: github.repository_owner == 'XaF'
permissions:
contents: read
pull-requests: read
outputs:
pull_request_state: ${{ env.PR_STATE }}
should_run: ${{ github.event_name != 'push' || env.PR_STATE == 'OPEN' }}
steps:
- name: Checkout commit
uses: actions/checkout@v4
- name: Check if pull request already exists
env:
GH_TOKEN: ${{ github.token }}
run: |
STATE=$(gh pr view "${{ env.PR_BRANCH }}" \
--repo "${{ github.repository }}" \
--json state --jq '.state' || \
echo "NOT FOUND")
echo "PR_STATE=${STATE}" | tee -a "$GITHUB_ENV"
run-cargo-fix:
name: Run different cargo fixes
runs-on: ubuntu-latest
needs:
- check-pull-request
if: needs.check-pull-request.outputs.should_run == 'true'
outputs:
any_update: ${{ env.UPDATES }}
steps:
- name: Checkout commit
uses: actions/checkout@v4
- name: Get rust stable
uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt
- name: Set up cargo cache
uses: Swatinem/rust-cache@v2
with:
shared-key: cargo-fixes
- name: Run cargo fix
env:
__CARGO_FIX_YOLO: 1 # So that we offer all the fixes
run: |
cargo fix --all-features --color always --allow-dirty
- name: Run clippy --fix
env:
__CARGO_FIX_YOLO: 1 # So that we offer all the fixes
run: |
cargo clippy --fix --all-features --color always --allow-dirty
- name: Run rustfmt
run: |
cargo fmt --verbose
- name: Check if there is any update
run : |
git diff --name-only
git diff --quiet && \
UPDATES=false || \
UPDATES=true
echo "UPDATES=${UPDATES}" | tee -a "$GITHUB_ENV"
- name: Create archive of modified files
if: ${{ env.UPDATES == 'true' }}
run: |
git stash
git diff 'stash@{0}' -z --name-only | \
xargs -0 git archive 'stash@{0}' -o cargo-fix.zip --
- name: upload cargo-fix.zip artifact for use in PR
if: ${{ env.UPDATES == 'true' }}
uses: actions/upload-artifact@v4
with:
name: cargo-fix
path: cargo-fix.zip
retention-days: 1
create-or-update-pr:
name: Create or update pull-request
runs-on: ubuntu-latest
needs:
- check-pull-request
- run-cargo-fix
if: needs.check-pull-request.outputs.should_run == 'true' && needs.run-cargo-fix.outputs.any_update == 'true'
env:
PR_TITLE: "chore(lints): 💅 cargo fixes"
steps:
- name: Create application token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.OMNICLI_APP_ID }}
private-key: ${{ secrets.OMNICLI_PRIVATE_KEY }}
- name: Checkout commit
uses: actions/checkout@v4
with:
token: ${{ steps.app-token.outputs.token }}
- name: Download cargo-fix.zip from update job
uses: actions/download-artifact@v4
with:
name: cargo-fix
- name: Unzip cargo-fix.zip
run: |
unzip -o cargo-fix.zip -d .
rm cargo-fix.zip
- name: Craft commit message
run: |
echo "${{ env.PR_TITLE }}" | tee commit.txt
- name: Craft pull-request body
run: |
echo 'Fixing lint for rust files' | tee body.md
echo | tee -a body.md
echo "The following files have been updated:" | tee -a body.md
echo | tee -a body.md
git diff --name-only | while read file; do echo "- \`${file}\`"; done | tee -a body.md
- name: git commit
run: |
git config user.name github-actions
git config user.email [email protected]
git switch --force-create "${{ env.PR_BRANCH }}"
git commit --all --no-verify --file=commit.txt
- name: git push
run: |
git push --no-verify --force --set-upstream origin "${{ env.PR_BRANCH }}"
- name: Create or update pull-request
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
PR_STATE: ${{ needs.check-pull-request.outputs.pull_request_state }}
run: |
if [[ "$PR_STATE" != "OPEN" ]]; then
gh pr create \
--title "${{ env.PR_TITLE }}" \
--body-file body.md \
--repo "${{ github.repository }}" \
--label rust
else
gh pr edit "${{ env.PR_BRANCH }}" \
--title "${{ env.PR_TITLE }}" \
--body-file body.md \
--repo "${{ github.repository }}" \
--add-label rust
fi
- name: Enable auto-merge
if: ${{ env.PR_AUTO_MERGE == 'true' }}
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
NUM=$(gh pr view "${{ env.PR_BRANCH }}" \
--repo "${{ github.repository }}" \
--json number --jq '.number' || \
echo "")
if [[ -z "$NUM" ]]; then
echo "Failed to get PR number"
exit 1
fi
gh pr merge --squash --auto --body "" "$NUM"
close-pr:
name: Close pull-request
runs-on: ubuntu-latest
needs:
- check-pull-request
- run-cargo-fix
if: needs.check-pull-request.outputs.should_run == 'true' && needs.run-cargo-fix.outputs.any_update == 'false' && needs.check-pull-request.outputs.pull_request_state == 'OPEN'
env:
PR_CLOSING_COMMENT: "Superseded. No more lints to be fixed for now."
steps:
- name: Create application token
uses: actions/create-github-app-token@v1
id: app-token
env:
APP_ID: ${{ secrets.OMNICLI_APP_ID }}
PRIVATE_KEY: ${{ secrets.OMNICLI_PRIVATE_KEY }}
with:
app-id: ${{ env.APP_ID }}
private-key: ${{ env.PRIVATE_KEY }}
- name: Close pull-request
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
gh pr close \
--title "${{ env.PR_TITLE }}" \
--comment "${{ env.PR_CLOSING_COMMENT }}" \
--delete-branch