Skip to content

Commit dd88d3d

Browse files
authored
Merge pull request #486 from soham30rane/master
Automatically configure version information after every chagelog generation
2 parents fc123e0 + 1917edc commit dd88d3d

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

.github/workflows/generate_changelog.yml

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ jobs:
3838
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
3939
git add "src/changelogs/sage-${RELEASE_TAG}.txt"
4040
git add conf/contributors.xml
41+
git add conf/config.yaml
4142
git commit -m "Added changelog for release ${{ inputs.release_tag }}"
4243
git push

scripts/create_changelog.py

+55-14
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def merge_contributors(original_file, new_contributors):
6161
Side effects:
6262
- Modifies the original XML file in-place
6363
"""
64-
with open(original_file, 'r',encoding='utf-8') as f:
64+
with open(original_file, 'r', encoding='utf-8') as f:
6565
original_content = f.read()
6666
tree = ET.fromstring(original_content)
6767

@@ -75,7 +75,7 @@ def merge_contributors(original_file, new_contributors):
7575
sorted_contributors = sorted(
7676
tree.findall('contributor'),
7777
# Items which don't have a name attribute are placed at the end
78-
key=lambda x: unidecode(get_last_name(x.get('name', '\x7F'))) # '\x7F' is highest ASCII value
78+
key=lambda x: unidecode(get_last_name(x.get('name', '\x7F'))) # '\x7F' is highest ASCII value
7979
)
8080

8181
for cont in tree.findall('contributor'):
@@ -113,12 +113,12 @@ def merge_contributors(original_file, new_contributors):
113113
for contributor in sorted_contributors:
114114
contrib_line = '<contributor'
115115
for attr, value in contributor.attrib.items():
116-
contrib_line += f'\n {attr}="{html.escape(value,quote=False)}"'
116+
contrib_line += f'\n {attr}="{html.escape(value, quote=False)}"'
117117
contrib_line += '/>'
118118
xml_lines.append(contrib_line)
119119
xml_lines.append('</contributors>')
120120

121-
with open(original_file, 'w',encoding='utf-8') as f:
121+
with open(original_file, 'w', encoding='utf-8') as f:
122122
f.write('\n'.join(xml_lines))
123123

124124

@@ -151,16 +151,16 @@ def fetch_real_name(github_name):
151151
"""
152152
url = f"https://api.github.com/users/{github_name}"
153153
try:
154-
res = requests.get(url,headers=HEADERS)
154+
res = requests.get(url, headers=HEADERS)
155155
res.raise_for_status()
156156
user = res.json()
157157
if not user['name']:
158158
return
159159
name_parts = user['name'].split()
160-
if len(name_parts) < 2: # Full name is not available
160+
if len(name_parts) < 2: # Full name is not available
161161
return
162162
git_to_name[github_name] = user['name']
163-
new_names.append((user['name'],github_name))
163+
new_names.append((user['name'], github_name))
164164
except Exception as e:
165165
print(f"Failed to fetch real name for @{github_name} : {str(e)}")
166166

@@ -182,11 +182,11 @@ def update_names():
182182
fetch_real_name(c)
183183
for tag in all_info:
184184
for pr in all_info[tag]:
185-
pr['creator'] = git_to_name.get(pr['creator'],f"@{pr['creator']}")
186-
pr['authors'] = [git_to_name.get(a,f"@{a}") for a in pr['authors']]
187-
pr['reviewers'] = [git_to_name.get(r,f"@{r}") for r in pr['reviewers']]
188-
all_contribs = set([git_to_name.get(c,f"@{c}") for c in all_contribs])
189-
first_contribs = set([git_to_name.get(c,f"@{c}") for c in first_contribs])
185+
pr['creator'] = git_to_name.get(pr['creator'], f"@{pr['creator']}")
186+
pr['authors'] = [git_to_name.get(a, f"@{a}") for a in pr['authors']]
187+
pr['reviewers'] = [git_to_name.get(r, f"@{r}") for r in pr['reviewers']]
188+
all_contribs = set([git_to_name.get(c, f"@{c}") for c in all_contribs])
189+
first_contribs = set([git_to_name.get(c, f"@{c}") for c in first_contribs])
190190

191191

192192
def get_release_data(tag):
@@ -393,7 +393,7 @@ def save_to_file(filename, ver, date_of_release):
393393
file.write(f"their first contribution to Sage:\n\n")
394394
max_name_len = max([len(c) for c in all_contribs])
395395
for c in all_contribs:
396-
file.write(f" - {c}{' '*(max_name_len - len(c)) + ' [First contribution]' if c in first_contribs else ''}\n")
396+
file.write(f" - {c}{' ' * (max_name_len - len(c)) + ' [First contribution]' if c in first_contribs else ''}\n")
397397
file.write(f"\nRelease manager: {RELEASE_MANAGER}\n")
398398
pr_count = sum([len(all_info[tag]) for tag in all_info])
399399
file.write(f"\nWe merged {pr_count} pull requests in this release.")
@@ -408,6 +408,44 @@ def save_to_file(filename, ver, date_of_release):
408408
print(f"Saved changelog to {filename}")
409409

410410

411+
def get_latest_stable_release():
412+
"""Fetch the latest stable release tag from the API.
413+
414+
Returns:
415+
str | None: The latest release tag if successful, None if the request fails
416+
"""
417+
url = f"{BASE_URL}/releases/latest"
418+
try:
419+
res = requests.get(url, headers=HEADERS)
420+
res.raise_for_status()
421+
res = res.json()
422+
tag = res['tag_name']
423+
date = get_release_date(res)
424+
return {"tag": tag, "date": date}
425+
except Exception as e:
426+
print(f"Failed to fetch latest stable release", e)
427+
return None
428+
429+
430+
def update_config(ver: str, release_date: str, config_file_path: str):
431+
"""Update version and release date in the config file.
432+
433+
Args:
434+
ver (str): New version to set
435+
release_date (str): Release date to set
436+
config_file_path (str): Path to the config file
437+
"""
438+
with open(config_file_path, 'r') as file:
439+
content = file.read()
440+
441+
content = re.sub(r'release_date:\s*".*?"', f'release_date: "{release_date}"', content)
442+
content = re.sub(r'version:\s*".*?"', f'version: "{ver}"', content)
443+
content = re.sub(r'version_src:\s*".*?"', f'version_src: "{ver}"', content)
444+
445+
with open(config_file_path, 'w') as file:
446+
file.write(content)
447+
448+
411449
if __name__ == '__main__':
412450
parser = argparse.ArgumentParser(description="Fetch release data from GitHub and extract PR info")
413451
parser.add_argument('version', type=str, help="The release version (e.g., 10.1)")
@@ -450,12 +488,15 @@ def save_to_file(filename, ver, date_of_release):
450488
all_contribs = sorted(all_contribs, key=lambda x: (x[0].startswith('@'), x[0]))
451489
if all_info:
452490
save_to_file(filepath, ver, date_of_release)
491+
latest_release = get_latest_stable_release()
492+
if latest_release:
493+
update_config(latest_release['tag'], latest_release['date'], 'conf/config.yaml')
453494
else:
454495
print("No information found.")
455496
exit(1)
456497

457498
if new_names:
458-
merge_contributors('conf/contributors.xml',new_names)
499+
merge_contributors('conf/contributors.xml', new_names)
459500
print("Added new contributors to conf/contributors.xml")
460501
else:
461502
print("No new contributors found.")

0 commit comments

Comments
 (0)