Automatically generate changelog

Fixes #1491
This commit is contained in:
Bruno Alla 2020-09-04 17:26:24 +02:00
parent 2227fe004c
commit df0d332207
4 changed files with 123 additions and 0 deletions

9
.github/changelog-template.md vendored Normal file
View File

@ -0,0 +1,9 @@
## [{{merge_date.strftime('%Y-%m-%d')}}]
{%- for change_type, pulls in grouped_pulls.items() %}
{%- if pulls %}
### {{ change_type }}
{%- for pull_request in pulls %}
- {{ pull_request.title }} ([#{{ pull_request.number }}]({{ pull_request.url }}))
{%- endfor -%}
{% endif -%}
{% endfor -%}

34
.github/workflows/update-changelog.yml vendored Normal file
View File

@ -0,0 +1,34 @@
name: Update Changelog
on:
# Every day at 2am
schedule:
- cron: "0 2 * * *"
# Manual trigger
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.8"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Update list
run: python scripts/update_changelog.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Update Changelog
file_pattern: CHANGELOG.md

View File

@ -1,6 +1,8 @@
# Change Log
All enhancements and patches to Cookiecutter Django will be documented in this file.
<!-- GENERATOR_PLACEHOLDER -->
## [2020-04-13]
### Changed
- Updated to Python 3.8 (@codnee)

View File

@ -0,0 +1,78 @@
import os
from pathlib import Path
from github import Github
from jinja2 import Template
import datetime as dt
CURRENT_FILE = Path(__file__)
ROOT = CURRENT_FILE.parents[1]
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", None)
# Generate changelog for PRs merged yesterday
MERGED_DATE = dt.date.today() - dt.timedelta(days=1)
def main() -> None:
"""
Script entry point.
"""
merged_pulls = list(iter_pulls())
if not merged_pulls:
print("Nothing was merged, existing.")
return
# Group pull requests by type of change
grouped_pulls = group_pulls_by_change_type(merged_pulls)
# Generate portion of markdown
rendered_content = generate_md(grouped_pulls)
# Update CHANGELOG.md file
file_path = ROOT / "CHANGELOG.md"
old_content = file_path.read_text()
updated_content = old_content.replace(
"<!-- GENERATOR_PLACEHOLDER -->",
f"<!-- GENERATOR_PLACEHOLDER -->\n\n{rendered_content}",
)
file_path.write_text(updated_content)
def iter_pulls():
"""Fetch merged pull requests at the date we're interested in."""
repo = Github(login_or_token=GITHUB_TOKEN).get_repo("pydanny/cookiecutter-django")
recent_pulls = repo.get_pulls(
state="closed", sort="updated", direction="desc"
).get_page(0)
for pull in recent_pulls:
if pull.merged and pull.merged_at.date() == MERGED_DATE:
yield pull
def group_pulls_by_change_type(pull_requests_list):
"""Group pull request by change type."""
grouped_pulls = {
"Changed": [],
"Fixed": [],
"Updated": [],
}
for pull in pull_requests_list:
label_names = {l.name for l in pull.labels}
if "update" in label_names:
group_name = "Updated"
elif "bug" in label_names:
group_name = "Fixed"
else:
group_name = "Changed"
grouped_pulls[group_name].append(pull)
return grouped_pulls
def generate_md(grouped_pulls):
"""Generate markdown file from Jinja template."""
changelog_template = ROOT / ".github" / "changelog-template.md"
template = Template(changelog_template.read_text(), autoescape=True)
return template.render(merge_date=MERGED_DATE, grouped_pulls=grouped_pulls)
if __name__ == "__main__":
main()