-
Notifications
You must be signed in to change notification settings - Fork 0
125 lines (102 loc) · 4.19 KB
/
Copy pathupdate.yml
File metadata and controls
125 lines (102 loc) · 4.19 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
name: Update release notes
on:
workflow_dispatch:
jobs:
update-release-notes:
runs-on: ubuntu-latest
permissions:
contents: write # needed so we can push changes
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install python dependencies
run: pip install requests
- name: Generate [tag].md files for all releases
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
python << 'PY'
import os, pathlib, requests, re
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from requests.exceptions import ChunkedEncodingError
repo = os.environ["GITHUB_REPOSITORY"] # e.g. data-others/brain
token = os.environ.get("GITHUB_TOKEN", "")
account, repo_name = repo.split("/")
session = requests.Session()
retries = Retry(
total=5,
backoff_factor=1,
status_forcelist=[500, 502, 503, 504],
raise_on_status=False,
)
session.mount("https://", HTTPAdapter(max_retries=retries))
headers = {
"Accept": "application/vnd.github+json",
"Connection": "close",
}
if token:
headers["Authorization"] = f"Bearer {token}"
def safe_get(url):
for attempt in range(5):
try:
resp = session.get(url, headers=headers, timeout=30)
resp.raise_for_status()
return resp
except ChunkedEncodingError as e:
if attempt == 4:
raise
print(f"ChunkedEncodingError, retry {attempt+1}/5: {e}")
releases = []
url = f"https://api.github.com/repos/{repo}/releases?per_page=100"
while url:
resp = safe_get(url)
releases.extend(resp.json())
url = resp.links.get("next", {}).get("url")
for rel in releases:
tag = rel.get("tag_name") or "untagged"
title = rel.get("name") or tag
body = rel.get("body") or ""
# Normalize newlines
body = body.replace('\r\n', '\n').strip()
# --- Ensure first heading line ---
lines = body.splitlines() if body else []
if not lines:
lines = [f"# **{title}**"]
else:
first = lines[0].strip()
# if not a heading, insert one
if not first.startswith("#"):
lines.insert(0, f"# **{title}**")
else:
# if first line starts with '## ' or '### ', promote to single '# '
lines[0] = re.sub(r"^(#{2,3})\s*", "# ", first)
# --- Demote subsequent headings (# -> ##, but skip first line) ---
new_lines = [lines[0]]
for line in lines[1:]:
new_lines.append(re.sub(r"^(# )", "## ", line))
body = "\n".join(new_lines)
# --- Build release link section ---
link = f"https://github.com/{account}/{repo_name}/releases/tag/{tag}"
text = f"{body}\n\n## Release Link\n{link}\n"
fn = pathlib.Path(f"{tag}.md")
fn.write_text(text, encoding="utf-8")
PY
- name: Commit and push changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add *.md || true
if git diff --cached --quiet; then
echo "No changes to commit."
exit 0
fi
git commit -m "Update release notes from GitHub releases"
git push