parent
c071fffce3
commit
a2683c03b5
2 changed files with 74 additions and 44 deletions
12
.github/workflows/update_changelog.yml
vendored
12
.github/workflows/update_changelog.yml
vendored
|
|
@ -1,3 +1,5 @@
|
|||
name: Update changelog
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [master]
|
||||
|
|
@ -13,22 +15,22 @@ jobs:
|
|||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
|
||||
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: '3.x'
|
||||
|
||||
|
||||
- name: Install Dependencies
|
||||
run: pip install pyyaml
|
||||
|
||||
|
||||
- name: Extract Changelog from PR
|
||||
run: python3 Tools/update_changelog.py "${{ github.event.pull_request.body }}" "Resources/Changelog/ChangelogSunrise.yml"
|
||||
|
||||
|
||||
- name: Commit and Push Changes
|
||||
run: |
|
||||
git config --global user.name 'github-actions[bot]'
|
||||
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
|
||||
git add Resources/Changelog/ChangelogSunrise.yml
|
||||
git commit -m 'Update changelog from PR #${{ github.event.pull_request.number }}'
|
||||
git push
|
||||
git push
|
||||
|
|
|
|||
|
|
@ -1,15 +1,20 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import yaml
|
||||
import re
|
||||
import datetime
|
||||
import os
|
||||
from typing import List, Any
|
||||
import yaml
|
||||
import argparse
|
||||
import datetime
|
||||
|
||||
MAX_ENTRIES = 500
|
||||
HEADER_RE = r"(?::cl:|🆑)\s*(.*)"
|
||||
ENTRY_RE = r"^ *[*-] *(add|remove|tweak|fix): *(.*)"
|
||||
|
||||
HEADER_RE = r"(?::cl:|🆑) *\r?\n(.+)$"
|
||||
ENTRY_RE = r"^ *[*-]? *(\S[^\n\r]+)\r?$"
|
||||
|
||||
CATEGORY_MAIN = "Main"
|
||||
|
||||
# From https://stackoverflow.com/a/37958106/4678631
|
||||
class NoDatesSafeLoader(yaml.SafeLoader):
|
||||
@classmethod
|
||||
def remove_implicit_resolver(cls, tag_to_remove):
|
||||
|
|
@ -21,60 +26,83 @@ class NoDatesSafeLoader(yaml.SafeLoader):
|
|||
for tag, regexp in mappings
|
||||
if tag != tag_to_remove]
|
||||
|
||||
# Hrm yes let's make the fucking default of our serialization library to PARSE ISO-8601
|
||||
# but then output garbage when re-serializing.
|
||||
NoDatesSafeLoader.remove_implicit_resolver('tag:yaml.org,2002:timestamp')
|
||||
|
||||
def parse_changelog(pr_body: str) -> List[dict]:
|
||||
header_match = re.search(HEADER_RE, pr_body, re.MULTILINE)
|
||||
if not header_match:
|
||||
return []
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("changelog_file")
|
||||
parser.add_argument("parts_dir")
|
||||
parser.add_argument("--category", default=CATEGORY_MAIN)
|
||||
|
||||
changelog_entries = []
|
||||
for match in re.finditer(ENTRY_RE, pr_body, re.MULTILINE):
|
||||
changelog_entries.append({
|
||||
'type': match.group(1),
|
||||
'description': match.group(2)
|
||||
})
|
||||
args = parser.parse_args()
|
||||
|
||||
return changelog_entries
|
||||
category = args.category
|
||||
|
||||
def update_changelog(changelog_file: str, pr_body: str):
|
||||
new_entries = parse_changelog(pr_body)
|
||||
if not new_entries:
|
||||
print("No changelog entries found.")
|
||||
return
|
||||
|
||||
with open(changelog_file, "r", encoding="utf-8-sig") as f:
|
||||
with open(args.changelog_file, "r", encoding="utf-8-sig") as f:
|
||||
current_data = yaml.load(f, Loader=NoDatesSafeLoader)
|
||||
|
||||
entries_list: List[Any]
|
||||
if current_data is None:
|
||||
entries_list = []
|
||||
else:
|
||||
entries_list = current_data.get("Entries", [])
|
||||
entries_list = current_data["Entries"]
|
||||
|
||||
max_id = max(map(lambda e: e.get("id", 0), entries_list), default=0)
|
||||
max_id = max(map(lambda e: e["id"], entries_list), default=0)
|
||||
|
||||
for entry in new_entries:
|
||||
max_id += 1
|
||||
entry["id"] = max_id
|
||||
entry["time"] = datetime.datetime.now(datetime.timezone.utc).isoformat()
|
||||
entry["author"] = "VigersRay" # Обновите, чтобы извлекать фактического автора, если нужно
|
||||
entries_list.append(entry)
|
||||
for partname in os.listdir(args.parts_dir):
|
||||
if not partname.endswith(".yml"):
|
||||
continue
|
||||
|
||||
partpath = os.path.join(args.parts_dir, partname)
|
||||
print(partpath)
|
||||
|
||||
with open(partpath, "r", encoding="utf-8-sig") as f:
|
||||
partyaml = yaml.load(f, Loader=NoDatesSafeLoader)
|
||||
|
||||
part_category = partyaml.get("category", CATEGORY_MAIN)
|
||||
if part_category != category:
|
||||
print(f"Skipping: wrong category ({part_category} vs {category})")
|
||||
continue
|
||||
|
||||
author = partyaml["author"]
|
||||
time = partyaml.get(
|
||||
"time", datetime.datetime.now(datetime.timezone.utc).isoformat()
|
||||
)
|
||||
changes = partyaml["changes"]
|
||||
url = partyaml.get("url")
|
||||
|
||||
if not isinstance(changes, list):
|
||||
changes = [changes]
|
||||
|
||||
if len(changes):
|
||||
# Don't add empty changelog entries...
|
||||
max_id += 1
|
||||
new_id = max_id
|
||||
|
||||
entries_list.append(
|
||||
{"author": author, "time": time, "changes": changes, "id": new_id, "url": url}
|
||||
)
|
||||
|
||||
os.remove(partpath)
|
||||
|
||||
print(f"Have {len(entries_list)} changelog entries")
|
||||
|
||||
entries_list.sort(key=lambda e: e["id"])
|
||||
|
||||
overflow = len(entries_list) - MAX_ENTRIES
|
||||
if overflow > 0:
|
||||
print(f"Removing {overflow} old entries.")
|
||||
entries_list = entries_list[overflow:]
|
||||
|
||||
new_data = {"Entries": entries_list}
|
||||
with open(changelog_file, "w", encoding="utf-8-sig") as f:
|
||||
for key, value in current_data.items():
|
||||
if key != "Entries":
|
||||
new_data[key] = value
|
||||
|
||||
with open(args.changelog_file, "w", encoding="utf-8-sig") as f:
|
||||
yaml.safe_dump(new_data, f)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 3:
|
||||
print("Usage: update_changelog.py <pr_body> <changelog_file>")
|
||||
sys.exit(1)
|
||||
|
||||
pr_body = sys.argv[1]
|
||||
changelog_file = sys.argv[2]
|
||||
update_changelog(changelog_file, pr_body)
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue