Skip to content

Commit

Permalink
chore(dev/release): add changelog generator
Browse files Browse the repository at this point in the history
Fixes #2452.
  • Loading branch information
lidavidm committed Jan 17, 2025
1 parent 74a09a3 commit fab12c8
Show file tree
Hide file tree
Showing 10 changed files with 493 additions and 29 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/dev_adbc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

name: Dev ADBC

on:
pull_request:
branches:
- main
paths:
- "dev/**"
- ".github/workflows/dev_adbc.yml"
push:
paths:
- "dev/**"
- ".github/workflows/dev_adbc.yml"

concurrency:
group: ${{ github.repository }}-${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true

permissions:
contents: read

defaults:
run:
shell: bash -l -eo pipefail {0}

jobs:
pre-commit:
name: "pre-commit"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false

- name: Cache Conda
uses: actions/cache@v4
with:
path: ~/conda_pkgs_dir
key: conda-${{ runner.os }}-${{ steps.get-date.outputs.today }}-${{ env.CACHE_NUMBER }}-${{ hashFiles('ci/**') }}
- uses: conda-incubator/setup-miniconda@v3
with:
miniforge-version: latest
use-only-tar-bz2: false
use-mamba: true

- name: Install Dependencies
run: |
mamba install -c conda-forge \
--file ci/conda_env_dev.txt \
pytest
- name: Test
run: |
pytest -vv dev/adbc_dev/
2 changes: 1 addition & 1 deletion .github/workflows/dev_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
env:
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
python .github/workflows/dev_pr/title_check.py $(pwd)/pr_checkout "$PR_TITLE"
python dev/adbc_dev/title_check.py $(pwd)/pr_checkout "$PR_TITLE"
# Pings make it into the commit message where they annoy the user every
# time the commit gets pushed somewhere
Expand Down
4 changes: 3 additions & 1 deletion ci/conda_env_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
# specific language governing permissions and limitations
# under the License.

commitizen
gh>=2.32.0
jq
pre-commit
pygit2
python
python-dotenv
twine
16 changes: 16 additions & 0 deletions dev/adbc_dev/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
135 changes: 135 additions & 0 deletions dev/adbc_dev/changelog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""Generate a changelog from our commit log."""

import argparse
import datetime
import sys
from pathlib import Path

import dotenv
import pygit2

from . import title_check


def display(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)


def get_commit(repo: pygit2.Repository, rev: str) -> pygit2.Oid:
try:
return repo.lookup_reference_dwim(rev).target
except KeyError:
return repo[rev].id


def list_commits(
repo: pygit2.Repository, from_rev: str, to_rev: str
) -> list[title_check.Commit]:
root = Path(repo.workdir)
from_commit = get_commit(repo, from_rev)
to_commit = get_commit(repo, to_rev)
walker = repo.walk(to_commit, pygit2.GIT_SORT_TIME)
walker.hide(from_commit)
commits = []
for commit in walker:
title = commit.message.strip().split("\n")[0]
commits.append(title_check.matches_commit_format(root, title))
return commits


def format_commit(commit: title_check.Commit) -> str:
components = ""
warning = ""
if commit.components:
components = f"**{', '.join(commit.components)}**: "
if commit.breaking_change:
warning = "⚠️ "
return f"{warning}{components}{commit.subject}"


def format_section(title: str, commits: list[title_check.Commit]) -> list[str]:
if not commits:
return []

lines = [f"### {title}", ""]
commits.sort(key=lambda commit: (commit.components, commit.subject))
lines.extend(f"- {format_commit(commit)}" for commit in commits)
lines.append("")
return lines


def format_changelog(
title: str, release: dict[str, str], commits: list[title_check.Commit]
) -> str:
date = datetime.date.today().strftime("%Y-%m-%d")
lines = [
f"## {title} ({date})",
"",
"### Versions",
"",
f"- C/C++/GLib/Go/Python/Ruby: {release['VERSION_NATIVE']}",
f"- C#: {release['VERSION_CSHARP']}",
f"- Java: {release['VERSION_JAVA']}",
f"- R: {release['VERSION_R']}",
f"- Rust: {release['VERSION_RUST']}",
"",
]

breaking = [commit for commit in commits if commit.breaking_change]
lines.extend(format_section("Breaking Changes", breaking))

feat = [commit for commit in commits if commit.category == "feat"]
lines.extend(format_section("New Features", feat))

fix = [commit for commit in commits if commit.category == "fix"]
lines.extend(format_section("Bugfixes", fix))

docs = [commit for commit in commits if commit.category == "docs"]
lines.extend(format_section("Documentation Improvements", docs))

perf = [commit for commit in commits if commit.category == "perf"]
lines.extend(format_section("Performance Improvements", perf))

return "\n".join(lines)


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("from_rev", help="The start revision.")
parser.add_argument("to_rev", help="The end revision.")
parser.add_argument("--name", required=True, help="The name of the release.")

args = parser.parse_args()

repo_root = Path(__file__).parent.parent.parent.resolve()
release = dotenv.dotenv_values(repo_root / "dev/release/versions.env")
display("Opening repository at", repo_root)
repo = pygit2.Repository(repo_root)

commits = list_commits(repo, args.from_rev, args.to_rev)
changelog = format_changelog(args.name, release, commits)
print(changelog)

return 0


if __name__ == "__main__":
sys.exit(main())
16 changes: 16 additions & 0 deletions dev/adbc_dev/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
Loading

0 comments on commit fab12c8

Please sign in to comment.