Skip to content

Commit

Permalink
Add update changelog workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
KshitijThareja committed Feb 12, 2024
1 parent 2d6342b commit 3e506a4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
2 changes: 0 additions & 2 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,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 +30,6 @@ 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

## Steps to test

Expand Down
13 changes: 11 additions & 2 deletions .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Check PR Description
name: Check PR for Changelog

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

jobs:
check-description:
Expand All @@ -18,8 +18,10 @@ jobs:
description=$(jq -r ".pull_request.body" "$GITHUB_EVENT_PATH")
if [[ "$description" == *"## Changelog"* ]]; then
echo "containsChangelog=true" >> $GITHUB_OUTPUT
echo "true" > containsChangelog.txt
else
echo "containsChangelog=false" >> $GITHUB_OUTPUT
echo "false" > containsChangelog.txt
fi
- name: Exit with appropriate code
run: |
Expand All @@ -30,3 +32,10 @@ jobs:
echo "PR does not contain Changelog section"
exit 1
fi
- name: Upload artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: pr-description-info
path: containsChangelog.txt
if-no-files-found: error
62 changes: 49 additions & 13 deletions .github/workflows/update_changelog.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,68 @@
name: Update Changelog.md

on:
workflow_run:
workflows: ["Check PR for Changelog"]
types:
- completed
pull_request:
types:
- closed

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

steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download artifact
uses: actions/download-artifact@v4
with:
name: pr-description-info

- name: Check if PR is from dependabot
id: check-dependabot
run: |
is_dependabot=$(jq -r '.pull_request.user.login' $GITHUB_EVENT_PATH)
echo "is_dependabot=$is_dependabot" >> $GITHUB_OUTPUT
- name: Read containsChangelog value
id: read-contains-changelog
run: |
containsChangelog=$(cat pr-description-info/containsChangelog.txt)
echo "Value of containsChangelog: $containsChangelog"
echo "containsChangelog=$containsChangelog" >> $GITHUB_OUTPUT
- name: Extract Changelog from PR description
if: steps.read-contains-changelog.outputs.containsChangelog == 'true'
id: extract-changelog
run: |
changelog=$(grep -Pzo "(?s)<!--CHANGELOG-->(.*?)<!--ENDCHANGELOG-->" ${{ github.event.before }}..${{ github.sha }} || true)
echo "::set-output name=changelog::${changelog}"
description=$(jq -r ".pull_request.body" "$GITHUB_EVENT_PATH")
changelog_section=$(echo "$description" | awk '/## Changelog/{flag=1; next} /##/{flag=0} flag' | sed '/<!--/,/-->/d' | sed '/<!--/,/-->/d' | sed '1d;$d')
echo "changelog=${changelog_section}" >> $GITHUB_OUTPUT
- name: Update Changelog in main branch
if: ${{ github.event.pull_request.merged == true }}
if: steps.check-dependabot.outputs.is_dependabot == 'dependabot[bot]' || steps.read-contains-changelog.outputs.containsChangelog == 'true'
run: |
echo "${{ steps.extract-changelog.outputs.changelog }}" | sed -e '1s/^/### PR details\n/' >> CHANGELOG.md
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git add CHANGELOG.md
git commit -m "Automatically update changelog [skip ci]"
git push
exit 0
changelog_content="${{ steps.extract-changelog.outputs.changelog }}"
if [ "${{ steps.check-dependabot.outputs.is_dependabot }}" == "dependabot[bot]" ]; then
changelog_content="**Description:** ${{ github.event.pull_request.title }}\n- **Products impact:** Dev Dependency upgrade\n- **Addresses:** -\n- **Components:** -\n- **Breaking:** -\n- **Impacts a11y:** -\n- **Guidance:** -\n\n"
fi
pr_number=$(jq -r ".pull_request.number" "$GITHUB_EVENT_PATH")
pr_link="[#${pr_number}]"
pr_link_ref="[#${pr_number}]: ${{ github.event.pull_request.html_url }}"
changelog_content="${pr_link}\n${changelog_content}\n${pr_link_ref}\n"
changelog_content=$(echo "$changelog_content" | sed -e 's/^[ \t]*//' -e 's/[ \t]*$//')
sed -i "/<!-- Put all new updates into this section, please -->/a\\$changelog_content" CHANGELOG.md
- name: Commit updated CHANGELOG
if: steps.check-dependabot.outputs.is_dependabot == 'dependabot[bot]' || steps.read-contains-changelog.outputs.containsChangelog == 'true'
uses: stefanzweifel/git-auto-commit-action@v4
with:
branch: ${{ github.event.pull_request.base.ref }}
commit_message: "Update CHANGELOG.md w/ PR #${{ github.event.pull_request.number }}"
file_pattern: CHANGELOG.md

0 comments on commit 3e506a4

Please sign in to comment.