Auto generate changelog and readme entries (simplified version) #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 'Generate changelog' | |
on: | |
pull_request: | |
types: [edited] | |
branches: | |
- trunk | |
- develop | |
permissions: {} | |
jobs: | |
generate_changelog: | |
name: 'Generates a changelog entry' | |
runs-on: ubuntu-20.04 | |
permissions: | |
pull-requests: write | |
steps: | |
- name: 'Collect changelog data from PR description' | |
run: | | |
DO_NOT_REQUIRE_CHANGELOG=$( cat ${{ github.event.pull_request.description }} | grep -E -o '.{0,3} This Pull Request does not require a changelog entry' | awk '{print substr($0,2,1)}' ) | |
if [[ $DO_NOT_REQUIRE_CHANGELOG == 'x' ]]; then | |
echo "This PR does not require a changelog entry. Exiting." | |
exit 0 | |
fi | |
TYPE_FIX=$( cat ${{ github.event.pull_request.description }} | grep -E -o '.{0,3} Fix - ' | awk '{print substr($0,2,1)}' ) | |
TYPE_ADD=$( cat ${{ github.event.pull_request.description }} | grep -E -o '.{0,3} Add - ' | awk '{print substr($0,2,1)}' ) | |
TYPE_UPDATE=$( cat ${{ github.event.pull_request.description }} | grep -E -o '.{0,3} Update - ' | awk '{print substr($0,2,1)}' ) | |
TYPE_DEV=$( cat ${{ github.event.pull_request.description }} | grep -E -o '.{0,3} Dev - ' | awk '{print substr($0,2,1)}' ) | |
TYPE_TWEAK=$( cat ${{ github.event.pull_request.description }} | grep -E -o '.{0,3} Tweak - ' | awk '{print substr($0,2,1)}' ) | |
CHANGELOG_TYPE='' | |
if [[ $TYPE_FIX == 'x' ]]; then | |
CHANGELOG_TYPE='Fix' | |
elif [[ $TYPE_ADD == 'x' ]]; then | |
CHANGELOG_TYPE='Add' | |
elif [[ $TYPE_UPDATE == 'x' ]]; then | |
CHANGELOG_TYPE='Update' | |
elif [[ $TYPE_DEV == 'x' ]]; then | |
CHANGELOG_TYPE='Dev' | |
elif [[ $TYPE_TWEAK == 'x' ]]; then | |
CHANGELOG_TYPE='Tweak' | |
else | |
echo "This PR does not have a valid changelog entry. Exiting." | |
exit 0 | |
fi | |
CHANGELOG_CONTENT=$( cat ${{ github.event.pull_request.description }} | grep -wns '#### Message <!-- Add a changelog message here -->' -A2 | tail -1 | awk '{print substr($0,index($0," - ")+4)}' ) | |
if [[ CHANGELOG_CONTENT == '</details>' ]]; then | |
echo "This PR does not contain a changelog entry. Exiting." | |
exit 0 | |
fi | |
CHANGELOG_ENTRY="* $CHANGELOG_TYPE - $CHANGELOG_CONTENT" | |
echo "CHANGELOG_ENTRY=$CHANGELOG_ENTRY" >> $GITHUB_OUTPUT | |
id: collect_changelog_data | |
- name: 'Add changelog entry to changelog.txt' | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const fs = require( 'node:fs' ); | |
const token = 'xxxx-xx-xx ='; | |
const filename = './changelog.txt'; | |
const changelog = fs.readFileSync( filename, 'utf-8' ); | |
const changelog_parts = changelog.split( token ); | |
changelog_parts[1] = '\n' + $( steps.collect_changelog_data.outputs.CHANGELOG_ENTRY ) + changelog_parts[1]; | |
const updatedChangelog = changelog_parts.join( token ); | |
fs.writeFile( filename, updatedChangelog, err => { | |
if ( err ) { | |
console.error( 'Unable to update changelog entries in changelog.txt' ); | |
} | |
} ) | |
id: add_changelog_txt_entry | |
- name: 'Add changelog entry to readme.txt' | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const fs = require( 'node:fs' ); | |
const token = 'xxxx-xx-xx ='; | |
const filename = './readme.txt'; | |
const changelog = fs.readFileSync( filename, 'utf-8' ); | |
const changelog_parts = changelog.split( token ); | |
changelog_parts[1] = '\n' + $( steps.collect_changelog_data.outputs.CHANGELOG_ENTRY ) + changelog_parts[1]; | |
const updatedChangelog = changelog_parts.join( token ); | |
fs.writeFile( filename, updatedChangelog, err => { | |
if ( err ) { | |
console.error( 'Unable to update changelog entries in readme.txt' ); | |
} | |
} ) | |
id: add_readme_txt_entry | |
- name: 'Commit changes' | |
run: | | |
git config user.name "bot-wc-stripe" | |
git config user.email "[email protected]" | |
git add changelog.txt | |
git add readme.txt | |
git commit -m "Add changelog entry for PR ${{ github.event.pull_request.number }}" | |
git push | |
id: commit_changes | |