-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcronjob.py
More file actions
283 lines (241 loc) · 9.29 KB
/
cronjob.py
File metadata and controls
283 lines (241 loc) · 9.29 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
276
277
278
279
280
281
282
283
"""Automated NuGet dependency updater.
Discovers every `*.csproj` under the repo root (excluding `bin/`, `obj/`,
`node_modules/` and any `EXCLUDED_PATH_PREFIXES`), reads each one's inline
`<PackageReference ... Version="..."/>` entries, and atomically bumps every
reference to the latest stable (non-pre-release) NuGet version.
Pre-release versions (any version containing `-`, per SemVer) are never
installed. References currently pinned to a pre-release are left alone with a
printed warning — fixing those is a manual decision.
On success: one GitHub issue summarizing every bump, one commit staging every
modified csproj (never `git add .`), one patch-version tag, one push.
On `dotnet restore` failure: every modified csproj is rolled back via
`git checkout` and the script exits non-zero without creating an issue,
commit, or tag.
"""
import os
import re
import subprocess
import sys
from pathlib import Path
from xml.etree import ElementTree as ET
import requests
GITHUB_REPO_OWNER = "microting"
GITHUB_REPO_NAME = "eform-debian-service"
EXCLUDED_PATH_PREFIXES = ["Plugins"]
EXCLUDED_DIR_NAMES = {"bin", "obj", "node_modules"}
REPO_ROOT = Path(__file__).resolve().parent
GITHUB_ACCESS_TOKEN = os.getenv("CHANGELOG_GITHUB_TOKEN")
def discover_csprojs():
excluded_prefixes = tuple(p.rstrip("/") + "/" for p in EXCLUDED_PATH_PREFIXES)
results = []
for path in REPO_ROOT.rglob("*.csproj"):
rel = path.relative_to(REPO_ROOT)
if any(part in EXCLUDED_DIR_NAMES for part in rel.parts):
continue
rel_str = rel.as_posix()
if rel_str.startswith(excluded_prefixes):
continue
results.append(path)
return sorted(results)
def read_package_references(csproj_path):
tree = ET.parse(csproj_path)
refs = []
for pr in tree.getroot().iter("PackageReference"):
name = pr.attrib.get("Include")
version = pr.attrib.get("Version")
if name and version:
refs.append((name, version))
return refs
def get_latest_stable_version(package_name):
# Use the NuGet search API instead of the flat-container API: it returns
# only *listed* packages in proper semver order, which filters out the
# junk old versions (e.g. `2010.2.11.1`) that the flat container still
# includes in its raw `versions` array.
url = (
"https://azuresearch-usnc.nuget.org/query"
f"?q=packageid:{package_name}"
"&prerelease=false"
"&take=1"
)
response = requests.get(url, timeout=30)
if response.status_code != 200:
return None
data = response.json().get("data", [])
if not data:
return None
version = data[0].get("version")
if version and "-" in version:
return None
return version
def update_csproj_versions(csproj_path, bumps):
content = csproj_path.read_text(encoding="utf-8")
for name, _old, new in bumps:
pattern = re.compile(
r'(<PackageReference\s+Include="'
+ re.escape(name)
+ r'"\s+Version=")[^"]+(")'
)
content, n = pattern.subn(r"\g<1>" + new + r"\g<2>", content)
if n == 0:
raise RuntimeError(
f"No PackageReference with inline Version attribute found for "
f"{name} in {csproj_path}"
)
csproj_path.write_text(content, encoding="utf-8")
def rollback(csproj_paths):
if not csproj_paths:
return
rel_paths = [str(p.relative_to(REPO_ROOT)) for p in csproj_paths]
subprocess.run(["git", "checkout", "--", *rel_paths], check=True)
def run_restore():
slns = sorted(REPO_ROOT.glob("*.sln"))
if slns:
return subprocess.run(
["dotnet", "restore", str(slns[0])],
capture_output=True,
text=True,
)
last_failure = None
for csproj in discover_csprojs():
result = subprocess.run(
["dotnet", "restore", str(csproj)],
capture_output=True,
text=True,
)
if result.returncode != 0:
last_failure = result
if last_failure is not None:
return last_failure
return subprocess.CompletedProcess(args=[], returncode=0, stdout="", stderr="")
def create_github_issue(bumps_by_csproj):
total = sum(len(bs) for bs in bumps_by_csproj.values())
plural = "s" if total != 1 else ""
title = f"Bump {total} NuGet package{plural}"
lines = ["The following packages were updated:", ""]
for csproj_rel, bumps in bumps_by_csproj.items():
lines.append(f"### `{csproj_rel}`")
for name, old, new in bumps:
lines.append(f"- `{name}`: {old} -> {new}")
lines.append("")
body = "\n".join(lines)
headers = {
"Authorization": f"Bearer {GITHUB_ACCESS_TOKEN}",
"Accept": "application/vnd.github.v3+json",
}
response = requests.post(
f"https://api.github.com/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/issues",
headers=headers,
json={"title": title, "body": body},
)
if response.status_code != 201:
raise RuntimeError(f"Failed to create GitHub issue: {response.text}")
issue_number = response.json()["number"]
print(f"GitHub issue '{title}' created. Issue Number: {issue_number}")
for label in (".Net", "backend", "enhancement"):
label_response = requests.post(
f"https://api.github.com/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/issues/{issue_number}/labels",
headers=headers,
json={"labels": [label]},
)
if label_response.status_code == 200:
print(f"Label '{label}' added to the issue.")
else:
print(f"Failed to add label '{label}' to the issue.")
return issue_number
def commit_modified_csprojs(csproj_paths, issue_number):
rel_paths = [str(p.relative_to(REPO_ROOT)) for p in csproj_paths]
subprocess.run(["git", "add", *rel_paths], check=True)
subprocess.run(["git", "commit", "-m", f"closes #{issue_number}"], check=True)
def push_new_version_tag():
subprocess.run(["git", "fetch", "--tags", "--force"], check=True)
tags_output = (
subprocess.check_output(["git", "tag", "--sort=-v:refname"])
.decode("utf-8")
.strip()
)
if not tags_output:
print("No tags found in the repository.")
return
major = minor = build = None
for line in tags_output.splitlines():
parts = line.lstrip("v").split(".")
if len(parts) == 3 and all(p.isdigit() for p in parts):
major, minor, build = map(int, parts)
break
if major is None:
print("No semver-formatted tags found.")
return
new_tag = f"v{major}.{minor}.{build + 1}"
print(f"Highest tag: v{major}.{minor}.{build}. Creating new tag {new_tag}.")
subprocess.run(["git", "tag", new_tag], check=True)
subprocess.run(["git", "push", "--tags"], check=True)
subprocess.run(["git", "push"], check=True)
def main():
commits_before = len(
subprocess.check_output(["git", "log", "--oneline"])
.decode("utf-8")
.splitlines()
)
print("Current number of commits:", commits_before)
csprojs = discover_csprojs()
if not csprojs:
print("No csproj files found in scope.")
return
print(f"Discovered {len(csprojs)} csproj(s) in scope:")
for p in csprojs:
print(f" {p.relative_to(REPO_ROOT)}")
latest_cache = {}
pre_release_pins = []
planned = {}
for csproj in csprojs:
for name, current in read_package_references(csproj):
if "-" in current:
pre_release_pins.append((csproj, name, current))
continue
if name not in latest_cache:
print(f"Checking {name}")
latest_cache[name] = get_latest_stable_version(name)
latest = latest_cache[name]
if latest is None:
print(f"Failed to retrieve package information for {name}.")
continue
if latest == current:
continue
planned.setdefault(csproj, []).append((name, current, latest))
for csproj, name, version in pre_release_pins:
rel = csproj.relative_to(REPO_ROOT)
print(f"Skipping {name} in {rel}: pinned to pre-release ({version}).")
if not planned:
print("Nothing to do, everything is up to date.")
return
print()
print("Planned bumps:")
for csproj, bumps in planned.items():
rel = csproj.relative_to(REPO_ROOT)
print(f" {rel}")
for name, old, new in bumps:
print(f" {name}: {old} -> {new}")
modified = []
try:
for csproj, bumps in planned.items():
update_csproj_versions(csproj, bumps)
modified.append(csproj)
except Exception:
rollback(modified)
raise
restore = run_restore()
if restore.returncode != 0:
print("dotnet restore failed after applying bumps. Rolling back.")
print(restore.stdout)
print(restore.stderr, file=sys.stderr)
rollback(modified)
sys.exit(1)
bumps_by_csproj_rel = {
str(csproj.relative_to(REPO_ROOT)): bumps
for csproj, bumps in planned.items()
}
issue_number = create_github_issue(bumps_by_csproj_rel)
commit_modified_csprojs(modified, issue_number)
push_new_version_tag()
if __name__ == "__main__":
main()