Skip to content

Commit 86bb397

Browse files
Merge pull request #1443 from product-os/kyle/format-release-notes
Format release notes with github script
2 parents 9ec9af8 + 8432821 commit 86bb397

File tree

2 files changed

+121
-140
lines changed

2 files changed

+121
-140
lines changed

.github/workflows/flowzone.yml

Lines changed: 60 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flowzone.yml

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,79 +1422,69 @@ jobs:
14221422
- name: Format release notes
14231423
id: format_release_notes
14241424
continue-on-error: true
1425-
env:
1426-
# We need pull_requests: read to get the PR title and body but that permission
1427-
# is not always granted by default to the actions token.
1428-
GH_TOKEN: ${{ steps.gh_app_token.outputs.token || secrets.FLOWZONE_TOKEN }}
1429-
run: |
1430-
set -x
1431-
1432-
pr_title="$(gh pr view ${{ github.event.pull_request.number }} --json title --jq .title)"
1433-
raw_body="$(gh pr view ${{ github.event.pull_request.number }} --json body --jq .body)"
1434-
1435-
if [[ -n "$pr_title" ]]; then
1436-
1437-
# if Renovate did this, extract its release notes from pull request body and
1438-
# massage until they resemble balena-io-modules/balena-deploy-request output
1439-
renovate_bot="$(gh pr view ${{ github.event.pull_request.number }} --json author --jq \
1440-
'select((.author.is_bot==true) and (.author.login | contains("renovate"))).author.login')"
1441-
1442-
title_pattern="## (R|r)elease (N|n)otes"
1443-
if [[ -n "$renovate_bot" ]] && [[ "$raw_body" =~ '### Release Notes' ]]; then
1444-
# TODO: remove duplicate <details> sections on the pr_body
1445-
# https://github.com/renovatebot/renovate/issues/13037
1446-
pr_body="$(echo "${raw_body}" \
1447-
| sed -n '/^### Release Notes$/,/^---$/p' \
1448-
| sed "s|^### Release Notes$||g" \
1449-
| sed 's/^---$//g')"
1450-
1451-
notable_changes="$(echo "${pr_body}" |
1452-
# Only get lines starting with `-`, `> -` or `<summary>`
1453-
grep -E '^> -|^-|^<summary>' |
1454-
# Replace the contents of the summary tag with a list item
1455-
sed -E 's|^<summary>(.*)</summary>|-\1|g' |
1456-
# Remove duplicate lines
1457-
awk '!(NF && seen[$0]++)' |
1458-
# Remove first line
1459-
sed 1d)"
1460-
1461-
# Prepare the release notes
1462-
release_notes_changes="$(echo "${notable_changes}" | sed -E 's|^> -(.*)| -\1|g')"
1463-
release_notes="$(printf '## %s\n\n### Notable changes\n\n%s\n\n%s' "${pr_title}" "${release_notes_changes}" "${pr_body}")"
1464-
1465-
# Prepare the comment body
1466-
notable_changes="$(echo "${notable_changes}" | sed -E 's|^|* |g')"
1467-
notable_changes="$(printf 'Notable changes\n* [only keep the important and rephrase, leaving this in place will avoid posting release notes]\n%s' "${notable_changes}")"
1468-
1469-
# https://zulip.com/help/format-your-message-using-markdown
1470-
# shortnames: https://pygments.org/docs/lexers/
1471-
release_notes_comment="$(printf '#release-notes %s\n\n%s%s' "${pr_title}" "${notable_changes}" "${pr_body}")"
1472-
1473-
# Handle non-renovate cases
1474-
elif [[ "$raw_body" =~ $title_pattern ]]; then
1475-
# Find a section `## Release notes` and use that as the release notes body
1476-
release_notes_body="$(echo "${raw_body}" \
1477-
| sed -n '/^## Release Notes/I,/^## /p' \
1478-
| sed '/^## /d' \
1479-
| sed -e :a -e '/./,$!d;/^\n*$/{$d;N;};/\n$/ba')" # Remove starting/trailing empty lines
1480-
1481-
release_notes="$(printf '## %s\n%s' "${pr_title}" "${release_notes_body}")"
1482-
fi
1425+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
1426+
with:
1427+
github-token: ${{ steps.gh_app_token.outputs.token || secrets.FLOWZONE_TOKEN }}
1428+
result-encoding: string
1429+
script: |
1430+
const { data: pr } = await github.rest.pulls.get({
1431+
owner: context.repo.owner,
1432+
repo: context.repo.repo,
1433+
pull_number: context.issue.number
1434+
});
14831435
1484-
if [[ -n "$release_notes" ]]; then
1485-
EOF="$(openssl rand -hex 16)"
1486-
echo "body<<$EOF" >> $GITHUB_OUTPUT
1487-
echo "${release_notes}" >> $GITHUB_OUTPUT
1488-
echo "$EOF" >> $GITHUB_OUTPUT
1489-
fi
1436+
if (!pr.title) {
1437+
return;
1438+
}
1439+
1440+
// Check if PR is from Renovate
1441+
const isRenovate = pr.user.type === 'Bot' && pr.user.login.includes('renovate');
1442+
const renovateReleaseNotesMatch = pr.body.match(/### Release Notes([\s\S]*?)---/);
1443+
1444+
if (isRenovate && renovateReleaseNotesMatch[1]) {
1445+
const releaseNotesBody = renovateReleaseNotesMatch[1].trim();
1446+
1447+
// Extract notable changes
1448+
const notableChanges = releaseNotesBody
1449+
.split('\n')
1450+
.filter(line => line.match(/^> -|^-|^<summary>/))
1451+
.map(line => line.replace(/^<summary>(.*)<\/summary>/, '-$1'))
1452+
.filter((line, index, self) => self.indexOf(line) === index)
1453+
.slice(1);
1454+
1455+
// Format release notes
1456+
const releaseNotesChanges = notableChanges
1457+
.map(line => line.replace(/^> -(.*)/, ' -$1'))
1458+
.join('\n');
1459+
1460+
const releaseNotes = `## ${pr.title}\n\n### Notable changes\n\n${releaseNotesChanges}\n\n${releaseNotesBody}`;
1461+
core.setOutput('body', releaseNotes);
1462+
1463+
// Format comment
1464+
const notableChangesFormatted = notableChanges
1465+
.map(line => `* ${line.replace(/^> -|^-/, '')}`)
1466+
.join('\n');
1467+
1468+
const releaseNotesComment = `#release-notes ${pr.title}\n\nNotable changes\n* [only keep the important and rephrase, leaving this in place will avoid posting release notes]\n${notableChangesFormatted}\n\n${releaseNotesBody}`;
1469+
core.setOutput('comment', releaseNotesComment);
1470+
1471+
return releaseNotes;
1472+
}
1473+
1474+
// Handle custom release notes
1475+
const customReleaseNotesMatch = pr.body.match(/## Release Notes([\s\S]*?)(?=## |$)/i);
1476+
console.log('match:', JSON.stringify(customReleaseNotesMatch, null, 2));
1477+
1478+
if (customReleaseNotesMatch[1]) {
1479+
const releaseNotesBody = customReleaseNotesMatch[1].trim();
1480+
console.log('releaseNotesBody:', releaseNotesBody);
1481+
1482+
const releaseNotes = `## ${pr.title}\n${releaseNotesBody}`;
1483+
core.setOutput('body', releaseNotes);
1484+
1485+
return releaseNotes;
1486+
}
14901487
1491-
if [[ -n "$release_notes_comment" ]]; then
1492-
EOF="$(openssl rand -hex 16)"
1493-
echo "comment<<$EOF" >> $GITHUB_OUTPUT
1494-
echo "${release_notes_comment}" >> $GITHUB_OUTPUT
1495-
echo "$EOF" >> $GITHUB_OUTPUT
1496-
fi
1497-
fi
14981488
14991489
# https://octokit.github.io/rest.js/v21/#repos-list-tags
15001490
# https://docs.github.com/en/rest/git/refs

0 commit comments

Comments
 (0)