|
| 1 | +""" |
| 2 | +script to automate updation of changelog as I add features. |
| 3 | +""" |
| 4 | + |
| 5 | +import subprocess |
| 6 | +import datetime |
| 7 | + |
| 8 | +CHANGELOG_FILE = "CHANGELOG.md" |
| 9 | +CHECK_INTERVAL = "weekly" |
| 10 | + |
| 11 | +INTERVALS = { |
| 12 | + "daily": "1 day ago", |
| 13 | + "weekly": "1 week ago", |
| 14 | + "monthly": "1 month ago", |
| 15 | +} |
| 16 | + |
| 17 | + |
| 18 | +def get_latest_commits(since=None): |
| 19 | + """ |
| 20 | + Get the latest commits in the repository |
| 21 | + since the specified time. |
| 22 | + """ |
| 23 | + since = since or INTERVALS.get(CHECK_INTERVAL, "1 week ago") |
| 24 | + |
| 25 | + try: |
| 26 | + result = subprocess.run( |
| 27 | + [ |
| 28 | + "git", |
| 29 | + "log", |
| 30 | + "--pretty=format:%h %s", |
| 31 | + "--since={since}", |
| 32 | + ], |
| 33 | + capture_output=True, |
| 34 | + text=True, |
| 35 | + check=True, |
| 36 | + ) |
| 37 | + return result.stdout.strip().split("\n") |
| 38 | + except subprocess.CalledProcessError as e: |
| 39 | + print("Error fetching commits: ", {e}) |
| 40 | + return [] |
| 41 | + |
| 42 | + |
| 43 | +def format_commit_entries(commits): |
| 44 | + """ |
| 45 | + Format the commit entries for the changelog. |
| 46 | + """ |
| 47 | + return "\n".join([f"- {commit}" for commit in commits]) |
| 48 | + |
| 49 | + |
| 50 | +def update_changelog(commits): |
| 51 | + """ |
| 52 | + Update the changelog.md file with the latest commits. |
| 53 | + Updates happen weekly, scheduled with task manager. |
| 54 | + """ |
| 55 | + |
| 56 | + if not commits: |
| 57 | + print("No new commits to add to changelog.") |
| 58 | + return |
| 59 | + |
| 60 | + current_date = datetime.datetime.now().strftime("%Y-%m-%d") |
| 61 | + formatted_commits = format_commit_entries(commits) |
| 62 | + |
| 63 | + try: |
| 64 | + with open(CHANGELOG_FILE, "a", encoding="utf-8") as changelog_file: |
| 65 | + changelog_file.write(f"\n## {current_date}\n") |
| 66 | + changelog_file.write(commits) |
| 67 | + changelog_file.write(formatted_commits + "\n") |
| 68 | + |
| 69 | + print(f"Changelog updated with {len(commits)} new commits.") |
| 70 | + except OSError as e: |
| 71 | + print(f"Error updating changelog: {e}") |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + latest_commits = get_latest_commits() |
| 76 | + update_changelog(latest_commits) |
0 commit comments