-
Notifications
You must be signed in to change notification settings - Fork 31
221 lines (200 loc) · 6.59 KB
/
pull_request_ci.yml
File metadata and controls
221 lines (200 loc) · 6.59 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
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
name: Pull Request CI
on:
pull_request:
permissions:
contents: read
jobs:
stargazer-gate:
runs-on: ubuntu-latest
steps:
- name: Validate PR author has starred this repository
env:
ACTOR: ${{ github.actor }}
REPO: ${{ github.repository }}
TOKEN: ${{ secrets.TOKEN }}
run: |
python - <<'PY'
import json
import os
import sys
import urllib.request
actor = os.environ["ACTOR"]
repo = os.environ["REPO"].lower()
token = os.environ.get("TOKEN", "")
headers = {
"Accept": "application/vnd.github+json",
"User-Agent": "stargazer-gate",
}
if token:
headers["Authorization"] = f"Bearer {token}"
has_starred = False
for page in range(1, 31):
url = f"https://api.github.com/users/{actor}/starred?per_page=100&page={page}"
request = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(request) as response:
payload = json.loads(response.read().decode("utf-8"))
if not payload:
break
if any(item.get("full_name", "").lower() == repo for item in payload):
has_starred = True
break
if has_starred:
print(f"Check passed: @{actor} has starred {repo}.")
sys.exit(0)
print(
f"Check failed: @{actor} must star {repo} before raising a PR "
"or their star must be publicly visible."
)
sys.exit(1)
PY
changelog-check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Enforce changelog update for non-doc PRs
shell: bash
run: |
set -euo pipefail
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.sha }}"
CHANGED_FILES="$(git diff --name-only "$BASE_SHA" "$HEAD_SHA")"
if [ -z "$CHANGED_FILES" ]; then
echo "No changed files detected; skipping."
exit 0
fi
DOCS_ONLY=true
while IFS= read -r file; do
case "$file" in
*.md|docs/*|.github/ISSUE_TEMPLATE/*|.github/pull_request_template.md)
;;
*)
DOCS_ONLY=false
break
;;
esac
done <<< "$CHANGED_FILES"
if [ "$DOCS_ONLY" = true ]; then
echo "Docs-only PR detected; CHANGELOG.md update is not required."
exit 0
fi
if echo "$CHANGED_FILES" | grep -qx "CHANGELOG.md"; then
echo "CHANGELOG.md update detected."
exit 0
fi
echo "CHANGELOG.md must be updated for non-doc PRs."
exit 1
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install ruff
run: python -m pip install --upgrade pip ruff
- name: Run lint on changed Python files
shell: bash
run: |
set -euo pipefail
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.sha }}"
CHANGED_PY="$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep -E '\.py$' || true)"
if [ -z "$CHANGED_PY" ]; then
echo "No Python files changed; skipping lint."
exit 0
fi
echo "$CHANGED_PY" | tr '\n' '\0' | xargs -0 ruff check
format:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install ruff
run: python -m pip install --upgrade pip ruff
- name: Check formatting on changed Python files
shell: bash
run: |
set -euo pipefail
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.sha }}"
CHANGED_PY="$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep -E '\.py$' || true)"
if [ -z "$CHANGED_PY" ]; then
echo "No Python files changed; skipping format check."
exit 0
fi
echo "$CHANGED_PY" | tr '\n' '\0' | xargs -0 ruff format --check
typecheck:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install mypy
run: python -m pip install --upgrade pip mypy
- name: Run typecheck on changed Python files
shell: bash
run: |
set -euo pipefail
BASE_SHA="${{ github.event.pull_request.base.sha }}"
HEAD_SHA="${{ github.sha }}"
CHANGED_PY="$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" | grep -E '\.py$' || true)"
if [ -z "$CHANGED_PY" ]; then
echo "No Python files changed; skipping typecheck."
exit 0
fi
echo "$CHANGED_PY" | tr '\n' '\0' | xargs -0 mypy --ignore-missing-imports --follow-imports=silent
validate-architecture:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Validate repository architecture
shell: bash
run: |
set -euo pipefail
REQUIRED_FILES=("README.md" "CONTRIBUTING.md" "LICENSE")
for file in "${REQUIRED_FILES[@]}"; do
if [ ! -f "$file" ]; then
echo "Missing required repository file: $file"
exit 1
fi
done
echo "Architecture validation passed."
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install pytest
run: python -m pip install --upgrade pip pytest
- name: Run tests when present
shell: bash
run: |
set -euo pipefail
if find . -type f \( -name "test_*.py" -o -name "*_test.py" \) | grep -q .; then
pytest -q
else
echo "No test files found; skipping tests."
fi