-
Notifications
You must be signed in to change notification settings - Fork 4
260 lines (204 loc) · 7.02 KB
/
auto-pr-cargo-dependencies.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
name: Auto-pull-request for cargo dependencies
on:
# Run on push to main
push:
branches:
- main
paths:
- .github/workflows/auto-pr-cargo-dependencies.yaml
- Cargo.lock
# 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_update
# Whether or not to enable auto-merge
PR_AUTO_MERGE: true
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:
GITHUB_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-update:
name: Update cargo dependencies
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
- name: cargo update
run: |
cargo update --color=never 2>&1 \
| tee -a cargo_update.log
- name: Check if there is any update
run : |
[[ $(cat cargo_update.log | wc -l) -gt 1 ]] && \
UPDATES=true || \
UPDATES=false
echo "UPDATES=${UPDATES}" | tee -a "$GITHUB_ENV"
- name: upload Cargo.lock artifact for use in PR
if: ${{ env.UPDATES == 'true' }}
uses: actions/upload-artifact@v4
with:
name: Cargo-lock
path: Cargo.lock
retention-days: 1
- name: upload cargo-update log artifact for use in PR
if: ${{ env.UPDATES == 'true' }}
uses: actions/upload-artifact@v4
with:
name: cargo-updates
path: cargo_update.log
retention-days: 1
create-or-update-pr:
name: Create or update pull-request
runs-on: ubuntu-latest
needs:
- check-pull-request
- run-cargo-update
if: needs.check-pull-request.outputs.should_run == 'true' && needs.run-cargo-update.outputs.any_update == 'true'
env:
PR_TITLE: "chore(deps): 📦 cargo update"
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.lock from update job
uses: actions/download-artifact@v4
with:
name: Cargo-lock
- name: Download cargo-update log from update job
uses: actions/download-artifact@v4
with:
name: cargo-updates
- name: Craft commit message
run: |
echo "${{ env.PR_TITLE }}" | tee commit.txt
echo | tee -a commit.txt
cat cargo_update.log | tee -a commit.txt
- name: Craft pull-request body
run: |
echo 'Updating `Cargo.lock` with `cargo update`' | tee body.md
echo | tee -a body.md
echo "The following crate dependencies are to be updated:" | tee -a body.md
echo | tee -a body.md
awk 'NR > 1 && /Updating/ { \
sub(/^v/, "", $3); \
sub(/^v/, "", $5); \
printf "- [`%s`](https://crates.io/crates/%s) "\
"([`v%s`](https://docs.rs/%s/%s/) -> "\
"[`v%s`](https://docs.rs/%s/%s/))\n",
$2, $2, $3, $2, $3, $5, $2, $5
}' cargo_update.log | 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 add ./Cargo.lock
git commit --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 dependencies \
--label rust
else
gh pr edit "${{ env.PR_BRANCH }}" \
--title "${{ env.PR_TITLE }}" \
--body-file body.md \
--repo "${{ github.repository }}" \
--add-label dependencies \
--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-update
if: needs.check-pull-request.outputs.should_run == 'true' && needs.run-cargo-update.outputs.any_update == 'false' && needs.check-pull-request.outputs.pull_request_state == 'OPEN'
env:
PR_CLOSING_COMMENT: "Superseded. No more dependencies to be updated 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