-
-
Notifications
You must be signed in to change notification settings - Fork 14
135 lines (114 loc) · 4.37 KB
/
auto-fix-linting.yml
File metadata and controls
135 lines (114 loc) · 4.37 KB
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
name: Auto-fix linting issues
on:
issue_comment:
types: [created]
jobs:
auto-fix-linting:
# Only run on pull request comments with the trigger phrase
if: github.event.issue.pull_request && contains(github.event.comment.body, '/fix-linting')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
env:
COMPOSE_PROGRESS: plain
steps:
- name: Check user permissions
id: permission
uses: actions/github-script@v9
with:
script: |
const { data: collaboration } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: context.payload.comment.user.login,
});
const hasPermission = ['admin', 'write'].includes(collaboration.permission);
if (!hasPermission) {
core.setFailed('User does not have write permissions to trigger linting fixes');
}
return hasPermission;
- name: Get PR details
id: pr
uses: actions/github-script@v9
with:
script: |
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});
return {
head_ref: pr.data.head.ref,
head_sha: pr.data.head.sha,
base_ref: pr.data.base.ref
};
- name: Checkout PR branch
uses: actions/checkout@v6
with:
ref: ${{ fromJson(steps.pr.outputs.result).head_ref }}
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install pre-commit and linting tools
run: |
pip install pre-commit black flake8 isort mypy
- name: Set up Docker environment
run: |
echo "" > .envrc
echo CONFIG_PATH=data/config/openfoodfacts.yml >> .envrc
echo OFF_API_URL=https://world.openfoodfacts.org >> .envrc
echo ALLOWED_ORIGINS='http://localhost,http://127.0.0.1' >> .envrc
echo USER_UID=$(id -u) >> .envrc
echo USER_GID=$(id -g) >> .envrc
- name: Build Docker containers for frontend linting
run: make build
continue-on-error: true
- name: Run backend linting fixes
run: |
# Run black to fix Python formatting
black app/ tests/ --line-length=88 || true
# Run isort to fix import ordering
isort app/ tests/ --profile black || true
continue-on-error: true
- name: Run frontend linting fixes
run: |
# Run frontend linting if Docker is available
make lint_front || echo "Frontend linting skipped (Docker not available)"
continue-on-error: true
- name: Check for changes
id: changes
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
if [[ $(git status --porcelain) ]]; then
echo "changes=true" >> $GITHUB_OUTPUT
echo "Found changes to commit"
else
echo "changes=false" >> $GITHUB_OUTPUT
echo "No changes found"
fi
- name: Commit and push changes
if: steps.changes.outputs.changes == 'true'
run: |
git add .
git commit -m "Auto-fix linting issues
Triggered by comment from @${{ github.event.comment.user.login }}
Co-authored-by: ${{ github.event.comment.user.login }} <${{ github.event.comment.user.login }}@users.noreply.github.com>"
git push
- name: Comment on PR
uses: actions/github-script@v9
with:
script: |
const changes = '${{ steps.changes.outputs.changes }}';
const message = changes === 'true'
? '✅ Linting issues have been automatically fixed and committed to this PR!'
: '✅ No linting issues found that could be automatically fixed.';
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
});