forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 9
194 lines (186 loc) · 7.33 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
# A workflow to apply autoformatting when a PR is commented with 'autoformat'.
name: Autoformat
on:
issue_comment:
types: [created, edited]
defaults:
run:
shell: bash --noprofile --norc -exo pipefail {0}
jobs:
check-comment:
runs-on: ubuntu-latest
if: ${{ github.event.issue.pull_request && contains(github.event.comment.body, 'autoformat') }}
outputs:
matched: ${{ steps.check-comment.outputs.result }}
repository: ${{ fromJSON(steps.create-status.outputs.result).repository }}
ref: ${{ fromJSON(steps.create-status.outputs.result).ref }}
sha: ${{ fromJSON(steps.create-status.outputs.result).sha }}
pull_number: ${{ fromJSON(steps.create-status.outputs.result).pull_number }}
steps:
- uses: actions/checkout@v3
- name: Check comment
id: check-comment
uses: actions/github-script@v4
with:
result-encoding: string
script: |
return context.payload.comment.body.trim() === 'autoformat';
- name: Create status
id: create-status
if: ${{ steps.check-comment.outputs.result == 'true' }}
uses: actions/github-script@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const autoformat = require('./.github/workflows/autoformat.js');
return await autoformat.createStatus(context, github, core);
check-diff:
runs-on: ubuntu-latest
needs: check-comment
if: ${{ needs.check-comment.outputs.matched == 'true' }}
outputs:
py_changed: ${{ steps.check-diff.outputs.py_changed }}
ui_changed: ${{ steps.check-diff.outputs.ui_changed }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: "3.7"
- run: |
pip install requests
- name: Check diff
id: check-diff
run: |
repository="${{ github.repository }}"
pull_number="${{ needs.check-comment.outputs.pull_number }}"
changed_files="$(python dev/list_changed_files.py --repository $repository --pr-num $pull_number)"
py_changed=$([[ -z $(echo "$changed_files" | grep '\.py$') ]] && echo "false" || echo "true")
ui_changed=$([[ -z $(echo "$changed_files" | grep '^mlflow/server/js') ]] && echo "false" || echo "true")
echo "::set-output name=py_changed::$py_changed"
echo "::set-output name=ui_changed::$ui_changed"
python:
# Generate a patch to format python files.
runs-on: ubuntu-latest
needs: [check-comment, check-diff]
if: ${{ needs.check-diff.outputs.py_changed == 'true' }}
outputs:
has_diff: ${{ steps.check-diff.outputs.has_diff }}
steps:
- uses: actions/checkout@v3
with:
repository: ${{ needs.check-comment.outputs.repository }}
ref: ${{ needs.check-comment.outputs.ref }}
- uses: actions/setup-python@v3
with:
python-version: "3.7"
- name: Install dependencies
run: |
pip install -r requirements/lint-requirements.txt
- name: Run black
run: |
black .
- name: Check diff
id: check-diff
run: |
git diff --output=python.diff
has_diff=$([[ -z "$(cat python.diff)" ]] && echo "false" || echo "true")
echo "::set-output name=has_diff::$has_diff"
- uses: actions/upload-artifact@v3
if: ${{ steps.check-diff.outputs.has_diff == 'true' }}
with:
name: python.${{ github.run_id }}.diff
path: python.diff
if-no-files-found: error
retention-days: 1
ui:
# Generate a patch to format files for MLflow UI.
runs-on: ubuntu-latest
needs: [check-comment, check-diff]
if: ${{ needs.check-diff.outputs.ui_changed == 'true' }}
outputs:
has_diff: ${{ steps.check-diff.outputs.has_diff }}
defaults:
run:
working-directory: mlflow/server/js
steps:
- uses: actions/checkout@v3
with:
repository: ${{ needs.check-comment.outputs.repository }}
ref: ${{ needs.check-comment.outputs.ref }}
- uses: actions/setup-node@v1
with:
node-version: "16"
- name: Install dependencies
run: |
yarn install
- run: |
yarn run lint:fix
- run: |
yarn run extract-i18n
- name: Check diff
id: check-diff
run: |
git diff --output=ui.diff
has_diff=$([[ -z "$(cat ui.diff)" ]] && echo "false" || echo "true")
echo "::set-output name=has_diff::$has_diff"
- uses: actions/upload-artifact@v3
if: ${{ steps.check-diff.outputs.has_diff == 'true' }}
with:
name: ui.${{ github.run_id }}.diff
path: mlflow/server/js/ui.diff
if-no-files-found: error
retention-days: 1
apply-patches:
# Apply the patches and commit changes to the PR branch.
runs-on: ubuntu-latest
needs: [check-comment, check-diff, python, ui]
if: |
always() &&
(needs.python.result == 'success' && needs.python.outputs.has_diff == 'true') ||
(needs.ui.result == 'success' && needs.ui.outputs.has_diff == 'true')
steps:
- uses: actions/checkout@v3
with:
repository: ${{ needs.check-comment.outputs.repository }}
ref: ${{ needs.check-comment.outputs.ref }}
# As described in the doc below, if we use `secrets.GITHUB_TOKEN`, a commit created by
# this workflow will not trigger other workflows:
# https://docs.github.com/en/actions/security-guides/automatic-token-authentication#using-the-github_token-in-a-workflow
# To make it work, commit changes using the mlflow-automation bot (https://github.com/mlflow-automation).
token: ${{ secrets.MLFLOW_AUTOMATION_TOKEN }}
- uses: actions/download-artifact@v3
if: ${{ needs.python.result == 'success' && needs.python.outputs.has_diff == 'true' }}
with:
name: python.${{ github.run_id }}.diff
path: /tmp/patches
- uses: actions/download-artifact@v3
if: ${{ needs.ui.result == 'success' && needs.ui.outputs.has_diff == 'true' }}
with:
name: ui.${{ github.run_id }}.diff
path: /tmp/patches
- name: Apply patches
run: |
find /tmp/patches -maxdepth 1 -type f -name '*.diff' | xargs git apply --verbose
git diff
- name: Commit changes
run: |
git config --global user.name 'mlflow-automation'
git config --global user.email '[email protected]'
run_url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
git commit -sam "Autoformat: $run_url"
git push
update-status:
runs-on: ubuntu-latest
needs: [check-comment, check-diff, python, ui, apply-patches]
if: ${{ always() && needs.check-comment.outputs.matched == 'true' }}
steps:
- uses: actions/checkout@v3
- name: Update status
uses: actions/github-script@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const needs = ${{ toJson(needs) }};
const sha = '${{ needs.check-comment.outputs.sha }}'
const autoformat = require('./.github/workflows/autoformat.js');
await autoformat.updateStatus(context, github, sha, needs);