Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate changelog updates #547

Merged
merged 11 commits into from
Aug 28, 2024
4 changes: 2 additions & 2 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Addresses #*PR# HERE*
<!-- Insert images here if applicable -->

## Changelog
<!-- [DO NOT REMOVE-USED BY GH ACTION] CHANGELOG START -->

<!--
- Fill in the changelog item(s) below. If there are more groups of closely
Expand All @@ -22,7 +23,6 @@ Addresses #*PR# HERE*
items in CHANGELOG.md as examples
-->

- [#PR no]
- **Description:** Summary of change(s)
- **Products impact:** Choose from - none (for internal updates) / bugfix / new API / updated API / removed API. If it's 'none', use "-" for all items below to indicate they are not relevant.
- **Addresses:** Link(s) to GH issue(s) addressed. Include KDS links as well as links to related issues in a consumer product repository too.
Expand All @@ -31,7 +31,7 @@ Addresses #*PR# HERE*
- **Impacts a11y:** Does this change improve a11y or adds new features that can be used to improve it? Choose from: yes / no
- **Guidance:** Why and how to introduce this update to a consumer? Required for breaking changes, appreciated for changes with a11y impact, and welcomed for non-breaking changes when relevant.

[#PR no]: PR link
<!-- [DO NOT REMOVE-USED BY GH ACTION] CHANGELOG END -->

## Steps to test

Expand Down
60 changes: 29 additions & 31 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
name: Check Changelog.md
name: Check PR for Changelog

on:
pull_request:
types: [opened, synchronize, reopened]
types: [opened, synchronize, edited, reopened]

jobs:
check-changelog:
runs-on: ubuntu-latest
permissions: write-all
steps:
- uses: actions/checkout@v4
- name: Fetch changed files
id: fetch-changed-files
run: |
response=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
repos/${owner}/${REPO}/pulls/${PULL_NUMBER}/files)
echo "$response" | jq '.' > response.json


env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
owner: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
PULL_NUMBER: ${{ github.event.pull_request.number }}
- name: Check if Changelog.md is changed
run: |
if [[ $(jq '.[].filename' response.json) == *"CHANGELOG.md"* ]]; then
echo "Changelog.md is changed"
exit 0
else
echo "Changelog.md is not changed"
exit 1
fi
check-description:
runs-on: ubuntu-latest
steps:

- name: Check PR Description
id: check_description
run: |
description=$(jq -r ".pull_request.body" "$GITHUB_EVENT_PATH" | awk '/## Changelog/{flag=1; next} /##/{flag=0} flag' | grep -oP '(?<=- \*\*Description:\*\* ).*' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "$description"
default_description="Summary of change(s)"
if [ -n "$description" ] && [ "$description" != "$default_description" ]; then
echo "containsChangelog=true" >> $GITHUB_OUTPUT
else
echo "containsChangelog=false" >> $GITHUB_OUTPUT
fi

- name: Results
run: |
if [[ "${{ steps.check_description.outputs.containsChangelog }}" == "true" || "${{ github.event.pull_request.user.login }}" == "dependabot[bot]" ]]; then
echo "PR contains Changelog section/ PR made by Dependabot"
exit 0
else
echo "Changelog section is missing or does not contain the required details"
exit 1
fi
77 changes: 77 additions & 0 deletions .github/workflows/update_changelog.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Update Changelog.md

on:
pull_request_target:
types:
- closed

jobs:
update-changelog:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest

steps:

- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.ref }}
fetch-depth: 0
# Prevent later git operations from reusing the same authentication
persist-credentials: false

- name: Extract and update Changelog
id: modify-changelog
shell: python
run: |
import re
if "${{ github.event.pull_request.user.login }}" == "dependabot[bot]":
changelog = " - **Description:** ${{ github.event.pull_request.title }}\n - **Products impact:** Dev Dependency upgrade\n - **Addresses:** -\n - **Components:** -\n - **Breaking:** -\n - **Impacts a11y:** -\n - **Guidance:** -"
else:
description = """${{ github.event.pull_request.body }}"""
capture = re.compile("<!-- \[DO NOT REMOVE-USED BY GH ACTION\] CHANGELOG START -->\s+<!--(\n|.)+?(?=- \*\*Description)(?P<body>(\n|.)+?(?=<!-- \[DO NOT REMOVE-USED BY GH ACTION\] CHANGELOG END -->))")
match = capture.search(description)
changelog = match.groupdict()["body"].strip()

final_changelog = "<!-- [DO NOT REMOVE-USED BY GH ACTION] PASTE CHANGELOG -->\n\n"

pr_number = "${{ github.event.pull_request.number }}"
pr_link = "[#{}]".format(pr_number)
pr_link_ref = pr_link + ": ${{ github.event.pull_request.html_url }}"

for changeline in changelog.splitlines():
changeline = changeline.strip()
if not changeline:
continue

if changeline.startswith("- **Description:**"):
final_changelog += "\n" + "- " + pr_link + "\n"

final_changelog += " " + changeline + "\n"

if changeline.startswith("- **Guidance:**"):
final_changelog += "\n" + pr_link_ref + "\n"

with open("CHANGELOG.md", "r") as f:
current_changelog = f.read()

new_changelog = current_changelog.replace("<!-- [DO NOT REMOVE-USED BY GH ACTION] PASTE CHANGELOG -->", final_changelog, 1)

with open("CHANGELOG.md", "w") as f:
f.write(new_changelog)
- uses: tibdex/github-app-token@v1
id: generate-token
with:
app_id: ${{ secrets.LE_BOT_APP_ID }}
private_key: ${{ secrets.LE_BOT_PRIVATE_KEY }}
- name: Commit updated CHANGELOG.md
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add CHANGELOG.md
git commit -m "Update CHANGELOG.md for PR #${{ github.event.pull_request.number }}"
- name: Push changes
uses: ad-m/[email protected]
with:
github_token: ${{ steps.generate-token.outputs.token }}
branch: ${{ github.event.pull_request.base.ref }}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ Changelog is rather internal in nature. See release notes for the public overvie

## Upcoming version 5.x.x (`develop` branch)

<!-- [DO NOT REMOVE-USED BY GH ACTION] PASTE CHANGELOG -->

- [547]
- **Description:** Automates changelog update process in the pull requests by adding two GitHub actions: (1) to check for the presence of changelog items(s) in the pull request description, (2) to paste the item(s) to CHANGELOG.md after the PR merged.
- **Products impact:** none
- **Addresses:** https://github.com/learningequality/kolibri-design-system/issues/533
- **Components:** -
- **Breaking:** -
- **Impacts a11y:** -
- **Guidance:** -

[547]: https://github.com/learningequality/kolibri-design-system/pull/547

- [#753]
- **Description:** Bump KDS version to 5.0.0-rc3.
- **Products impact:** -.
Expand Down
Loading