-
Notifications
You must be signed in to change notification settings - Fork 34
/
update_release_notes.py
97 lines (80 loc) · 3.07 KB
/
update_release_notes.py
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
"""
Requirements:
pip3 install requests beautifulsoup4 GitPython markdownify
"""
import os
import requests
import argparse
import sys
from bs4 import BeautifulSoup
from git import Repo, GitCommandError
from markdownify import markdownify as md
from datetime import datetime
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def get_latest_version():
_, url = fetch_latest_release()
version = url.split('/')[-1]
return version
def fetch_latest_release():
url = "https://github.com/autobrr/autobrr/releases/latest"
response = requests.get(url)
response.raise_for_status()
logging.info(f"Successfully fetched data from {url}")
return response.text, response.url
def parse_release_data(html_content, url):
soup = BeautifulSoup(html_content, 'html.parser')
version = url.split('/')[-1]
time_tag = soup.find('relative-time')
release_date = time_tag['datetime'].split('T')[0] if time_tag else datetime.now().strftime('%Y-%m-%d')
body_html = soup.find('div', class_="markdown-body")
if body_html:
markdown_content = md(str(body_html), heading_style="ATX")
return version, release_date, markdown_content
else:
raise Exception("Failed to parse release notes from HTML")
def create_release_notes_folder(version, release_date, markdown_content):
folder_name = f"{release_date}-{version}"
path = os.path.join('./blog', folder_name)
os.makedirs(path, exist_ok=True)
md_file_path = os.path.join(path, 'index.md')
with open(md_file_path, 'w') as file:
file.write("---\n")
file.write(f"slug: {version}\n")
file.write(f"title: {version}\n")
file.write("authors: [rogerrabbit]\n")
file.write("---\n")
file.write(markdown_content)
logging.info(f"Release notes folder created at {path}")
return path
def git_operations():
repo = Repo('.')
origin = repo.remotes.origin
repo.git.checkout('main')
origin.pull()
branch_name = "chore/docs/update-release-notes-{}".format(datetime.now().strftime('%Y%m%d-%H%M%S'))
if branch_name in repo.heads:
branch = repo.heads[branch_name]
logging.info("Branch already exists, checking out and merging from main.")
repo.git.checkout(branch_name)
repo.git.merge('main')
else:
logging.info("Creating new branch.")
branch = repo.create_head(branch_name)
repo.git.checkout(branch_name)
print(f"::set-output name=branch::{branch_name}")
return branch
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--get-version", action="store_true", help="Get the latest version number")
args = parser.parse_args()
if args.get_version:
version = get_latest_version()
print(version)
return
git_operations()
html_content, url = fetch_latest_release()
version, release_date, markdown_content = parse_release_data(html_content, url)
path = create_release_notes_folder(version, release_date, markdown_content)
if __name__ == "__main__":
main()