-
Notifications
You must be signed in to change notification settings - Fork 2
198 lines (174 loc) · 6.22 KB
/
main.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
name: Quality Checks
on:
pull_request:
branches:
- main
push:
branches:
- '*'
jobs:
build:
name: pylint and pytest scores
runs-on: ubuntu-latest
steps:
# Checkout the code
- name: Checkout
uses: actions/checkout@v4
# Install Python
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
# Install dependencies
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install pytest pylint
for FILE in $(find . -name 'requirements.txt'); do
pip install -r $FILE
done
# Run pytest
- name: Run tests
run: |
pytest
# Write Pytest overview results to a file
- name: Record test results
if: '!cancelled()'
run: |
[ ! -f .util/pytest_scores.txt ] && touch '.util/pytest_scores.txt'
pytest -r N --tb=no > .util/pytest_scores.txt || true
- name: Create pylint_scores
if: '!cancelled()'
run: |
:> .util/pylint_scores.txt
# Run Pylint and Get output in file
- name: Run Pylint
if: '!cancelled()'
run: |
find . \( -name '*.py' ! -name 'test*.py' \) \
-exec pylint {} \; >> .util/pylint_scores.txt
# Convert Pylint scores to JSON
- name: Convert Pylint scores to JSON
if: '!cancelled()'
run: python .util/parse_raw_python.py --input .util/pylint_scores.txt --output .util/pylint_scores.json --type pylint
# Convert Pytest scores to JSON
- name: Convert Pytest scores to JSON
if: '!cancelled()'
run: python .util/parse_raw_python.py --input .util/pytest_scores.txt --output .util/pytest_scores.json --type pytest
# Get the passed_percentage from pytest_scores.json
- name: Obtain passed_percentage from JSON
if: '!cancelled()'
run: |
echo "PASSED_PERCENTAGE=$(jq '.passed_percentage' .util/pytest_scores.json)" >> $GITHUB_ENV
env:
CODE_QUALITY: ${{ env.PASSED_PERCENTAGE }}
BADGE_PATH: .github/badges/passed_percentage.svg
# Get the number_of_tests from pytest_scores.json
- name: Obtain total_tests from JSON
if: '!cancelled()'
run: |
echo "TOTAL_TESTS=$(jq '.total' .util/pytest_scores.json)" >> $GITHUB_ENV
env:
CODE_QUALITY: ${{ env.TOTAL_TESTS }}
BADGE_PATH: .github/badges/number_of_tests.svg
# Get the passing_tests from pytest_scores.json
- name: Obtain passing_tests from JSON
if: '!cancelled()'
run: |
echo "PASSING_TESTS=$(jq '.passed' .util/pytest_scores.json)" >> $GITHUB_ENV
env:
CODE_QUALITY: ${{ env.PASSING_TESTS }}
BADGE_PATH: .github/badges/number_of_tests.svg
# Get the avg_score from pylint_scores.json
- name: Obtain avg_score from JSON
if: '!cancelled()'
run: |
echo "AVG_SCORE=$(jq '.avg_score' .util/pylint_scores.json)" >> $GITHUB_ENV
env:
CODE_QUALITY: ${{ env.AVG_SCORE }}
BADGE_PATH: .github/badges/avg_score.svg
- name: Fail pylint if score below threshold
if: '!cancelled()'
run: |
result=$(echo "$AVG_SCORE < 9.0" | bc -l)
if [ "$result" -eq 1 ]; then
echo "[FAILED] Code quality score: $AVG_SCORE."
exit 1
fi
echo "[PASS] Code quality score: $AVG_SCORE."
exit 0
# Generate the badge for passed_percentage
- name: Generate the badge SVG image for passed_percentage
if: '!cancelled()'
uses: emibcn/badge-action@v1
id: passed_percentage_badge
with:
label: 'Passing Tests: '
status: ${{ format('{0}%', env.PASSED_PERCENTAGE) }}
color: 'blue'
path: .github/badges/passed_percentage.svg
scale: 1.5
- name: Upload badge as artefact
if: '!cancelled()'
uses: actions/upload-artifact@v2
with:
name: badge
path: .github/badges/passed_percentage.svg
if-no-files-found: ignore
# Generate the badge for number_of_tests
- name: Generate the badge SVG image for number_of_tests
if: '!cancelled()'
uses: emibcn/badge-action@v1
id: number_of_tests_badge
with:
label: 'Passing/Total Tests: '
status: ${{ format('{0}/{1}', env.PASSING_TESTS , env.TOTAL_TESTS) }}
color: 'blue'
path: .github/badges/number_of_tests.svg
scale: 1.5
- name: Upload badge as artefact
if: '!cancelled()'
uses: actions/upload-artifact@v2
with:
name: badge
path: .github/badges/number_of_tests.svg
if-no-files-found: ignore
# Generate the badge for avg_score
- name: Generate the badge SVG image for avg_score
if: '!cancelled()'
uses: emibcn/badge-action@v1
id: avg_score_badge
with:
label: 'Pylint Score: '
status: ${{ format('{0}/10', env.AVG_SCORE) }}
color: 'blue'
path: .github/badges/avg_score.svg
scale: 1.5
- name: Upload badge as artefact
if: '!cancelled()'
uses: actions/upload-artifact@v2
with:
name: badge
path: .github/badges/avg_score.svg
if-no-files-found: ignore
# Commit results
- name: Push results
if: '!cancelled()'
run: |
git config --local user.email "${{ secrets.WORKFLOW_EMAIL }}"
git config --local user.name "Testing bot"
git add .util/pytest_scores.txt
git add .util/pytest_scores.json
git add .util/pylint_scores.txt
git add .util/pylint_scores.json
git add .github/badges/passed_percentage.svg
git add .github/badges/avg_score.svg
git add .github/badges/number_of_tests.svg
git commit -m "Update scores" || true
echo "Event Type: ${{ github.event_name }}"
if [[ "${{ github.event_name }}" == "push" ]]; then
echo "Pushing"
git push
else
echo "No Push"
fi