-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathgenerate_changelog.py
88 lines (70 loc) · 2.52 KB
/
generate_changelog.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
import os
import sys
import subprocess
from openai import OpenAI
from datetime import date
def get_git_commits():
# Get the last tag
last_tag = subprocess.check_output(
["git", "describe", "--tags", "--abbrev=0"], text=True
).strip()
# Get commit messages since the last tag
commits = subprocess.check_output(
["git", "log", f"{last_tag}..HEAD", "--pretty=format:%s"], text=True
).strip()
return last_tag, commits.split("\n")
def generate_changelog(commits):
if not commits:
return "No commits to include in the changelog."
prompt = f"""
You are a helpful assistant tasked with creating a changelog. Based on these Git commit messages, generate a clear, human-readable changelog:
Commit messages:
{commits}
Format the changelog as follows:
### Features
- List features here
### Fixes
- List fixes here
### Breaking Changes
- List breaking changes here (if any)
"""
client = OpenAI(
api_key=os.environ.get("OPENAI_API_TOKEN"), # This is the default and can be omitted
)
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant for software development."},
{"role": "user", "content": prompt},
],
)
return response.choices[0].message.content.strip()
def update_changelog(version, changelog_content):
changelog_file = "CHANGELOG.md"
today = date.today().strftime("%Y-%m-%d")
new_entry = f"## [{version}] - {today}\n{changelog_content}\n\n"
if os.path.exists(changelog_file):
with open(changelog_file, "r+") as f:
old_content = f.read()
f.seek(0, 0)
f.write(new_entry + old_content)
else:
with open(changelog_file, "w") as f:
f.write(new_entry)
if __name__ == "__main__":
last_tag, commits = get_git_commits()
if not commits:
print("No new commits found.")
exit()
print("Generating changelog...")
changelog_content = generate_changelog("\n".join(commits))
print("\nGenerated Changelog:")
print(changelog_content)
# Check if a version was passed as a command-line argument
if len(sys.argv) > 1:
new_version = sys.argv[1]
else:
# Prompt for a version if none was provided
new_version = input("Enter the new version (e.g., 1.0.0): ").strip()
update_changelog(new_version, changelog_content)
print(f"Changelog updated for version {new_version}!")