-
Notifications
You must be signed in to change notification settings - Fork 3
275 lines (221 loc) · 8.73 KB
/
Copy pathpropagate.yml
File metadata and controls
275 lines (221 loc) · 8.73 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
name: propagate
on:
push:
branches: [main]
permissions:
contents: write
pull-requests: write
jobs:
propagate:
strategy:
fail-fast: false
matrix:
branch: [modal, hetzner, stripe]
runs-on: ubuntu-latest
steps:
- name: Generate token
id: create_token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ steps.create_token.outputs.token }}
- name: Install uv
uses: astral-sh/setup-uv@v4
- name: Attempt merge and push, or open PR on conflict
env:
GH_TOKEN: ${{ steps.create_token.outputs.token }}
BRANCH: ${{ matrix.branch }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
open_propagation_pr() {
TEMP_BRANCH="auto-merge/main-to-${BRANCH}-$(date +%Y%m%d%H%M%S)"
git checkout -b "$TEMP_BRANCH" origin/main
git push origin "$TEMP_BRANCH"
gh pr create \
--base "$BRANCH" \
--head "$TEMP_BRANCH" \
--title "Propagate main to ${BRANCH}" \
--body "$(cat <<'PREOF'
## Summary
- Auto-generated PR to propagate changes from `main` to the deployment branch
- Opened because an automatic merge had conflicts that need manual resolution
🤖 Generated by the propagate workflow
PREOF
)"
}
unresolved_conflicts() {
git diff --name-only --diff-filter=U | sort
}
conflicts_are_modal_overlay() {
[ "$BRANCH" = "modal" ] || return 1
conflicts="$(unresolved_conflicts)"
[ -n "$conflicts" ] || return 1
while IFS= read -r file; do
case "$file" in
pyproject.toml|uv.lock) ;;
*) return 1 ;;
esac
done <<< "$conflicts"
}
add_modal_dependency() {
python3 <<'PY'
from pathlib import Path
import subprocess
import tomllib
def is_modal_dependency(dependency: str) -> bool:
return dependency.split(";", 1)[0].strip().lower().startswith("modal")
def modal_dependency_from_branch() -> str:
try:
result = subprocess.run(
["git", "show", ":2:pyproject.toml"],
check=True,
capture_output=True,
text=True,
)
except subprocess.CalledProcessError:
return "modal>=0.73.162"
dependencies = tomllib.loads(result.stdout).get("project", {}).get("dependencies", [])
return next((dependency for dependency in dependencies if is_modal_dependency(dependency)), "modal>=0.73.162")
path = Path("pyproject.toml")
text = path.read_text()
data = tomllib.loads(text)
dependencies = data.get("project", {}).get("dependencies", [])
if any(is_modal_dependency(dependency) for dependency in dependencies):
raise SystemExit(0)
lines = text.splitlines()
start = next(
(index for index, line in enumerate(lines) if line.strip() == "dependencies = ["),
None,
)
if start is None:
raise SystemExit("Could not find project dependencies in pyproject.toml")
end = next(
(index for index in range(start + 1, len(lines)) if lines[index].strip() == "]"),
None,
)
if end is None:
raise SystemExit("Could not find end of project dependencies in pyproject.toml")
insert_at = end
for index in range(start + 1, end):
if lines[index].strip().startswith('"psycopg2-binary'):
insert_at = index
break
lines.insert(insert_at, f' "{modal_dependency_from_branch()}",')
path.write_text("\n".join(lines) + "\n")
PY
}
resolve_modal_overlay() {
if unresolved_conflicts | grep -qx "pyproject.toml"; then
git checkout --theirs pyproject.toml
fi
add_modal_dependency
if unresolved_conflicts | grep -qx "uv.lock"; then
git checkout --ours uv.lock
fi
uv lock
git add pyproject.toml uv.lock
remaining_conflicts="$(unresolved_conflicts)"
if [ -n "$remaining_conflicts" ]; then
echo "Conflicts remain after modal overlay resolution:"
echo "$remaining_conflicts"
return 1
fi
git commit --no-edit
git push origin "$BRANCH"
}
conflicts_are_stripe_overlay() {
[ "$BRANCH" = "stripe" ] || return 1
conflicts="$(unresolved_conflicts)"
[ -n "$conflicts" ] || return 1
while IFS= read -r file; do
case "$file" in
pyproject.toml|uv.lock) ;;
*) return 1 ;;
esac
done <<< "$conflicts"
}
add_stripe_dependency() {
python3 <<'PY'
from pathlib import Path
import subprocess
import tomllib
def is_stripe_dependency(dependency: str) -> bool:
return dependency.split(";", 1)[0].strip().lower().startswith("stripe")
def stripe_dependency_from_branch() -> str:
try:
result = subprocess.run(
["git", "show", ":2:pyproject.toml"],
check=True,
capture_output=True,
text=True,
)
except subprocess.CalledProcessError:
return "stripe>=15.3.0"
dependencies = tomllib.loads(result.stdout).get("project", {}).get("dependencies", [])
return next(
(dependency for dependency in dependencies if is_stripe_dependency(dependency)),
"stripe>=15.3.0",
)
path = Path("pyproject.toml")
text = path.read_text()
data = tomllib.loads(text)
dependencies = data.get("project", {}).get("dependencies", [])
if any(is_stripe_dependency(dependency) for dependency in dependencies):
raise SystemExit(0)
lines = text.splitlines()
start = next(
(index for index, line in enumerate(lines) if line.strip() == "dependencies = ["),
None,
)
if start is None:
raise SystemExit("Could not find project dependencies in pyproject.toml")
end = next(
(index for index in range(start + 1, len(lines)) if lines[index].strip() == "]"),
None,
)
if end is None:
raise SystemExit("Could not find end of project dependencies in pyproject.toml")
lines.insert(end, f' "{stripe_dependency_from_branch()}",')
path.write_text("\n".join(lines) + "\n")
PY
}
resolve_stripe_overlay() {
if unresolved_conflicts | grep -qx "pyproject.toml"; then
git checkout --theirs pyproject.toml
fi
add_stripe_dependency
if unresolved_conflicts | grep -qx "uv.lock"; then
git checkout --ours uv.lock
fi
uv lock
git add pyproject.toml uv.lock
remaining_conflicts="$(unresolved_conflicts)"
if [ -n "$remaining_conflicts" ]; then
echo "Conflicts remain after stripe overlay resolution:"
echo "$remaining_conflicts"
return 1
fi
git commit --no-edit
git push origin "$BRANCH"
}
git checkout "$BRANCH"
if git merge origin/main --no-edit; then
echo "Merge succeeded, pushing to $BRANCH"
git push origin "$BRANCH"
elif conflicts_are_modal_overlay; then
echo "Merge conflict only affects the modal dependency overlay; resolving automatically"
resolve_modal_overlay
elif conflicts_are_stripe_overlay; then
echo "Merge conflict only affects the stripe dependency overlay; resolving automatically"
resolve_stripe_overlay
else
echo "Merge conflict detected, opening PR"
git merge --abort
open_propagation_pr
fi