forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 0
238 lines (228 loc) · 9.69 KB
/
autoformat.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
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
# See .github/workflows/autoformat.md for instructions on how to test this workflow.
name: Autoformat
on:
issue_comment:
types: [created, edited]
defaults:
run:
shell: bash --noprofile --norc -exo pipefail {0}
jobs:
check-comment:
runs-on: ubuntu-latest
timeout-minutes: 10
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'autoformat') }}
permissions:
statuses: write # autoformat.createStatus
pull-requests: write # autoformat.createReaction on PRs
outputs:
should_autoformat: ${{ fromJSON(steps.judge.outputs.result).shouldAutoformat }}
repository: ${{ fromJSON(steps.judge.outputs.result).repository }}
head_ref: ${{ fromJSON(steps.judge.outputs.result).head_ref }}
head_sha: ${{ fromJSON(steps.judge.outputs.result).head_sha }}
base_ref: ${{ fromJSON(steps.judge.outputs.result).base_ref }}
base_sha: ${{ fromJSON(steps.judge.outputs.result).base_sha }}
base_repo: ${{ fromJSON(steps.judge.outputs.result).base_repo }}
pull_number: ${{ fromJSON(steps.judge.outputs.result).pull_number }}
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
.github
- uses: ./.github/actions/validate-author
- name: judge
id: judge
uses: actions/github-script@v7
with:
script: |
core.debug(JSON.stringify(context, null, 2));
const autoformat = require('./.github/workflows/autoformat.js');
const { comment } = context.payload;
const shouldAutoformat = autoformat.shouldAutoformat(comment);
if (shouldAutoformat) {
await autoformat.createReaction(context, github);
await autoformat.createStatus(context, github, core);
}
const pullInfo = await autoformat.getPullInfo(context, github);
return { ...pullInfo, shouldAutoformat };
format:
runs-on: ubuntu-latest
timeout-minutes: 30
needs: check-comment
if: ${{ needs.check-comment.outputs.should_autoformat == 'true' }}
permissions:
pull-requests: read # view files modified in PR
outputs:
reformatted: ${{ steps.patch.outputs.reformatted }}
steps:
- uses: actions/checkout@v4
with:
repository: ${{ needs.check-comment.outputs.repository }}
ref: ${{ needs.check-comment.outputs.head_ref }}
# Set fetch-depth to merge the base branch
fetch-depth: 300
- name: Check diff
id: diff
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
changed_files="$(gh pr view --repo ${{ github.repository }} ${{ needs.check-comment.outputs.pull_number }} --json files --jq '.files.[].path')"
protos=$([[ -z $(echo "$changed_files" | grep '^\(mlflow/protos\|tests/protos\)') ]] && echo "false" || echo "true")
js=$([[ -z $(echo "$changed_files" | grep '^mlflow/server/js') ]] && echo "false" || echo "true")
r=$([[ -z $(echo "$changed_files" | grep '^mlflow/R/mlflow') ]] && echo "false" || echo "true")
echo "protos=$protos" >> $GITHUB_OUTPUT
echo "js=$js" >> $GITHUB_OUTPUT
echo "r=$r" >> $GITHUB_OUTPUT
# Merge the base branch (which is usually master) to apply formatting using the latest configurations.
- name: Merge base branch
run: |
git config user.name 'mlflow-automation'
git config user.email '[email protected]'
git remote add base https://github.com/${{ needs.check-comment.outputs.base_repo }}.git
git fetch base ${{ needs.check-comment.outputs.base_ref }}
git merge base/${{ needs.check-comment.outputs.base_ref }}
- uses: ./.github/actions/setup-python
# ************************************************************************
# pre-commit
# ************************************************************************
- run: |
dev/install-taplo.sh
- run: |
pip install -r requirements/lint-requirements.txt
pre-commit install
pre-commit run --all-files --color=always || true
# ************************************************************************
# protos
# ************************************************************************
- if: steps.diff.outputs.protos == 'true'
env:
DOCKER_BUILDKIT: 1
run: |
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-linux-x86_64.zip -O /tmp/protoc.zip
sudo unzip /tmp/protoc.zip -d /tmp/protoc
sudo chmod -R 777 /tmp/protoc
echo "/tmp/protoc/bin" >> $GITHUB_PATH
pip install --upgrade pip
pip install -e .
- if: steps.diff.outputs.protos == 'true'
run: |
python ./dev/generate_protos.py
# ************************************************************************
# js
# ************************************************************************
- if: steps.diff.outputs.js == 'true'
uses: ./.github/actions/setup-node
- if: steps.diff.outputs.js == 'true'
working-directory: mlflow/server/js
run: |
yarn install
- if: steps.diff.outputs.js == 'true'
working-directory: mlflow/server/js
run: |
yarn lint:fix
yarn prettier:fix
- if: steps.diff.outputs.js == 'true'
working-directory: mlflow/server/js
run: |
yarn i18n
# ************************************************************************
# R
# ************************************************************************
- if: steps.diff.outputs.r == 'true'
working-directory: docs
run: |
./build-rdoc.sh
# ************************************************************************
# Upload patch
# ************************************************************************
- name: Create patch
id: patch
run: |
git diff > ${{ github.run_id }}.diff
reformatted=$([[ -s ${{ github.run_id }}.diff ]] && echo "true" || echo "false")
echo "reformatted=$reformatted" >> $GITHUB_OUTPUT
- name: Upload patch
uses: actions/upload-artifact@v4
with:
name: ${{ github.run_id }}.diff
path: ${{ github.run_id }}.diff
push:
runs-on: ubuntu-latest
timeout-minutes: 5
needs: [check-comment, format]
if: ${{ needs.format.outputs.reformatted == 'true' }}
permissions: {}
outputs:
head_sha: ${{ steps.push.outputs.head_sha }}
steps:
- uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.APP_ID }}
# See https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/managing-private-keys-for-github-apps
# for how to rotate the private key
private-key: ${{ secrets.APP_PRIVATE_KEY }}
- uses: actions/checkout@v4
with:
repository: ${{ needs.check-comment.outputs.repository }}
ref: ${{ needs.check-comment.outputs.head_ref }}
# Set fetch-depth to merge the base branch
fetch-depth: 300
# As reported in https://github.com/orgs/community/discussions/25702, if an action pushes
# code using `GITHUB_TOKEN`, that won't trigger new workflow runs on the PR.
# A personal access token is required to trigger new workflow runs.
token: ${{ steps.app-token.outputs.token }}
- name: Merge base branch
env:
BASE_REPO: ${{ needs.check-comment.outputs.base_repo }}
BASE_REF: ${{ needs.check-comment.outputs.base_ref }}
run: |
git config user.name 'mlflow-automation'
git config user.email '[email protected]'
git remote add base https://github.com/${BASE_REPO}.git
git fetch base $BASE_REF
git merge base/${BASE_REF}
- name: Download patch
uses: actions/download-artifact@v4
with:
name: ${{ github.run_id }}.diff
path: /tmp
- name: Apply patch and push
id: push
env:
RUN_ID: ${{ github.run_id }}
REPOSITORY: ${{ github.repository }}
run: |
git apply /tmp/${RUN_ID}.diff
git commit -sam "Autoformat: https://github.com/${REPOSITORY}/actions/runs/${RUN_ID}"
echo "head_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
git push
update-status:
runs-on: ubuntu-latest
timeout-minutes: 10
needs: [check-comment, format, push]
if: always() && needs.check-comment.outputs.should_autoformat == 'true'
permissions:
statuses: write # To update check statuses
actions: write # To approve workflow runs
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
.github
- name: Update status
uses: actions/github-script@v7
with:
script: |
const needs = ${{ toJson(needs) }};
const head_sha = '${{ needs.check-comment.outputs.head_sha }}'
const autoformat = require('./.github/workflows/autoformat.js');
// TODO: Remove try-catch block once we are confident that the code works fine.
try {
const push_head_sha = '${{ needs.push.outputs.head_sha }}';
if (push_head_sha) {
await autoformat.approveWorkflowRuns(context, github, push_head_sha);
}
} catch (error) {
core.warning(`Failed to approve workflow runs: ${error}`);
}
await autoformat.updateStatus(context, github, head_sha, needs);