forked from shorthills-ai/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 1
215 lines (204 loc) · 8.89 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
# See .github/workflows/autoformat.md for instructions on how to test this workflow.
name: Autoformat
on:
issue_comment:
types: [created, edited]
permissions:
contents: read
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, '@mlflow-automation') && 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@v3
- name: judge
id: judge
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
core.debug(JSON.stringify(context, null, 2));
const autoformat = require('./.github/workflows/autoformat.js');
const { comment } = context.payload;
const isMaintainer = autoformat.isMlflowMaintainer(comment.author_association);
if (!isMaintainer) {
core.setFailed("Only MLflow maintainers can trigger this workflow.");
}
const shouldAutoformat = autoformat.shouldAutoformat(comment);
if (shouldAutoformat) {
await autoformat.createReaction(context, github);
await autoformat.createStatus(context, github, core);
}
const pullInfo = await autoformat.getPullInformation(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@v3
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') ]] && echo "false" || echo "true")
python=$([[ -z $(echo "$changed_files" | grep '\.py$\|^pyproject\.toml$') ]] && 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 "python=$python" >> $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 }}
# ************************************************************************
# Prettier
# ************************************************************************
- run: |
pre-commit run prettier --all-files --color=always || true
# ************************************************************************
# protos
# ************************************************************************
- if: steps.diff.outputs.protos == 'true'
env:
DOCKER_BUILDKIT: 1
run: |
docker build -t gen-protos -f dev/Dockerfile.protos .
- if: steps.diff.outputs.protos == 'true'
run: |
docker run --rm -w /mlflow -v $(pwd):/mlflow gen-protos ./dev/generate-protos.sh
# ************************************************************************
# python
# ************************************************************************
- if: steps.diff.outputs.python == 'true'
uses: ./.github/actions/setup-python
- if: steps.diff.outputs.python == 'true'
run: |
pip install -r requirements/lint-requirements.txt
- if: steps.diff.outputs.python == 'true'
run: |
ruff --fix .
black .
blacken-docs $(git ls-files '*.py' '*.rst' '*.md') || true
# ************************************************************************
# js
# ************************************************************************
- if: steps.diff.outputs.js == 'true'
uses: actions/setup-node@v3
with:
node-version: "16"
- 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
- 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@v3
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' }}
steps:
- uses: actions/checkout@v3
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
token: ${{ secrets.MLFLOW_AUTOMATION_TOKEN }}
- 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 }}
- name: Download patch
uses: actions/download-artifact@v3
with:
name: ${{ github.run_id }}.diff
path: /tmp
- name: Apply patch and push
run: |
git apply /tmp/${{ github.run_id }}.diff
git commit -sam "Autoformat: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
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 # autoformat.updateStatus
steps:
- uses: actions/checkout@v3
- name: Update status
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const needs = ${{ toJson(needs) }};
const head_sha = '${{ needs.check-comment.outputs.head_sha }}'
const autoformat = require('./.github/workflows/autoformat.js');
await autoformat.updateStatus(context, github, head_sha, needs);