Repository navigation
Expand file tree
/
Copy pathrelease_notes.py
More file actions
165 lines (142 loc) · 6.72 KB
/
Copy pathrelease_notes.py
File metadata and controls
165 lines (142 loc) · 6.72 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
#!/usr/bin/env python3
"""Compose desktop-first release notes and refresh published download links."""
import json
from html import escape
import os
import re
import subprocess
import sys
from pathlib import Path
ISSUES_QUERY = """query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $number) {
closingIssuesReferences(first: 100) { nodes { title url state } }
}
}
}"""
def gh(*args):
return json.loads(subprocess.check_output(["gh", "api", *args], text=True))
def fixed_issues(notes, repository):
owner, repo = repository.split("/", 1)
pattern = rf"https://github\.com/{re.escape(repository)}/pull/(\d+)"
issues = []
seen = set()
for number in dict.fromkeys(re.findall(pattern, notes)):
try:
result = gh(
"graphql", "-f", f"query={ISSUES_QUERY}", "-f", f"owner={owner}",
"-f", f"repo={repo}", "-F", f"number={number}",
)
linked = result["data"]["repository"]["pullRequest"]["closingIssuesReferences"]["nodes"]
except (subprocess.CalledProcessError, KeyError, TypeError, ValueError) as error:
print(f"::warning::Could not list fixed issues for PR #{number}: {error}", file=sys.stderr)
continue
for issue in linked:
if issue["state"] == "CLOSED" and issue["url"] not in seen:
seen.add(issue["url"])
title = issue["title"].replace("\\", "\\\\").replace("[", "\\[").replace("]", "\\]")
issues.append(
f"- [{title}]({issue['url']}) — fixed by "
f"[#{number}](https://github.com/{repository}/pull/{number})"
)
return issues
DOWNLOAD_START = "<!-- desktop-downloads:start -->"
DOWNLOAD_END = "<!-- desktop-downloads:end -->"
DESKTOP_ASSETS = (
("OpenResearch.dmg", "Download for macOS — Apple silicon and Intel", "macos"),
("OpenResearch-Setup.exe", "Download for Windows", "windows"),
("OpenResearch-x86_64.AppImage", "Download for Linux (x86)", "linux-x86"),
("OpenResearch-aarch64.AppImage", "Download for Linux (ARM64)", "linux-arm64"),
)
def downloads(assets=()):
urls = {asset["name"]: asset["browser_download_url"] for asset in assets}
images = "https://raw.githubusercontent.com/alphaXiv/OpenResearch/main/.github/readme-assets"
buttons = [
f'<a href="{escape(urls[name], quote=True)}"><picture>'
f'<source media="(prefers-color-scheme: dark)" srcset="{images}/download-{platform}-dark.svg">'
f'<img src="{images}/download-{platform}.svg" alt="{label}" width="220" height="44" />'
'</picture></a>'
for name, label, platform in DESKTOP_ASSETS if name in urls
]
links = ["<p>\n" + "\n".join(buttons) + "\n</p>"] if buttons else []
if not links:
links = ["[Download the desktop app](https://openresearch.sh/)"]
return "\n\n".join((
DOWNLOAD_START,
"## Download OpenResearch",
"Install the desktop app to get everything you need. No separate installation or terminal setup required.",
"\n\n".join(links),
"The desktop app updates automatically by default. You can manage updates in Settings.",
DOWNLOAD_END,
))
def refresh_downloads(body, assets):
before, start, rest = body.partition(DOWNLOAD_START)
_, end, after = rest.partition(DOWNLOAD_END)
if not start and DOWNLOAD_END not in body:
return body
if not start or not end:
raise ValueError("Release has no managed desktop download section")
return before + downloads(assets) + after
def remote_install(repository, tag):
url = f"https://github.com/{repository}/releases/download/{tag}/openresearch-cli-installer.sh"
return (
"<details>\n<summary>Install ORX on a remote machine</summary>\n\n"
"For remote servers and compute clusters, install ORX directly from a terminal:\n\n"
"```sh\n"
f"curl --proto '=https' --tlsv1.2 -LsSf {url} | sh\n"
"```\n\n"
"To update an existing terminal installation, run `orx update`.\n\n"
"</details>"
)
def compose(notes, issues, remote):
notes = notes.strip()
if issues:
section = "## Fixed issues\n\n" + "\n".join(issues)
marker = "**Full Changelog**:"
before, found, after = notes.partition(marker)
notes = (before.rstrip() + "\n\n" + section + "\n\n" + found + after) if found else notes + "\n\n" + section
return "\n\n".join(part for part in (downloads(), remote.strip(), notes) if part) + "\n"
def previous_stable_release(repository, tag, commit):
version = re.fullmatch(r"v(\d+)\.(\d+)\.(\d+)(?:[-+][0-9A-Za-z.+-]+)?", tag)
if not version:
raise ValueError(f"Invalid release tag: {tag}")
target = tuple(map(int, version.groups()))
pages = gh(f"repos/{repository}/releases?per_page=100", "--paginate", "--slurp")
releases = [release for page in pages for release in page if not release["draft"] and not release["prerelease"]]
if any(release["tag_name"] == tag for release in releases):
raise ValueError(f"{tag} is already a stable release")
candidates = []
for release in releases:
name = release["tag_name"]
match = re.fullmatch(r"v(\d+)\.(\d+)\.(\d+)", name)
if match and tuple(map(int, match.groups())) < target:
candidates.append((tuple(map(int, match.groups())), name))
for _, name in sorted(candidates, reverse=True):
comparison = gh(f"repos/{repository}/compare/{name}...{commit}")
if comparison["status"] in ("ahead", "identical"):
return name
if releases:
raise ValueError(f"No earlier stable release is an ancestor of {commit}")
return None
def main():
repository = os.environ["GITHUB_REPOSITORY"]
if sys.argv[1] == "--refresh-downloads":
tag = sys.argv[2]
release = gh(f"repos/{repository}/releases/tags/{tag}")
body = refresh_downloads(release["body"], release["assets"])
if body == release["body"]:
print("Desktop download section is unchanged; skipping refresh.")
return
gh(f"repos/{repository}/releases/{release['id']}", "-X", "PATCH", "-f", f"body={body}")
return
tag, commit, output_file = sys.argv[1:]
previous = previous_stable_release(repository, tag, commit)
previous_arg = ["-f", f"previous_tag_name={previous}"] if previous else []
generated = gh(
f"repos/{repository}/releases/generate-notes", "-X", "POST",
"-f", f"tag_name={tag}", "-f", f"target_commitish={commit}",
*previous_arg,
)["body"]
Path(output_file).write_text(compose(generated, fixed_issues(generated, repository), remote_install(repository, tag)))
if __name__ == "__main__":
main()