Skip to content

Commit 6ad6949

Browse files
authored
Merge pull request #173 from RooVetGit/chores/changelog-format
Forcing preferred changelog format
2 parents 01eeed5 + bbf7ba7 commit 6ad6949

File tree

3 files changed

+90
-41
lines changed

3 files changed

+90
-41
lines changed

.changeset/changelog-config.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Half-works to simplify the format but needs 'overwrite_changeset_changelog.py' in GHA to finish formatting
2+
13
const getReleaseLine = async (changeset) => {
24
const [firstLine] = changeset.summary
35
.split('\n')
@@ -10,14 +12,9 @@ const getDependencyReleaseLine = async () => {
1012
return '';
1113
};
1214

13-
const getReleaseSummary = async (release) => {
14-
return `## [${release.newVersion}]\n\n`;
15-
};
16-
1715
const changelogFunctions = {
1816
getReleaseLine,
1917
getDependencyReleaseLine,
20-
getReleaseSummary,
2118
};
2219

2320
module.exports = changelogFunctions;
Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
"""
2-
This script updates a specific version's release notes section in CHANGELOG.md with new content.
2+
This script updates a specific version's release notes section in CHANGELOG.md with new content
3+
or reformats existing content.
34
45
The script:
5-
1. Takes a version number, changelog path, and new content as input from environment variables
6+
1. Takes a version number, changelog path, and optionally new content as input from environment variables
67
2. Finds the section in the changelog for the specified version
7-
3. Replaces the content between the current version header and the next version header
8-
(or end of file if it's the latest version) with the new content
8+
3. Either:
9+
a) Replaces the content with new content if provided, or
10+
b) Reformats existing content by:
11+
- Removing the first two lines of the changeset format
12+
- Ensuring version numbers are wrapped in square brackets
913
4. Writes the updated changelog back to the file
1014
1115
Environment Variables:
1216
CHANGELOG_PATH: Path to the changelog file (defaults to 'CHANGELOG.md')
13-
VERSION: The version number to update notes for
14-
PREV_VERSION: The previous version number (optional)
15-
NEW_CONTENT: The new content to insert for this version
17+
VERSION: The version number to update/format
18+
PREV_VERSION: The previous version number (used to locate section boundaries)
19+
NEW_CONTENT: Optional new content to insert for this version
1620
"""
1721

1822
#!/usr/bin/env python3
@@ -22,39 +26,37 @@
2226
CHANGELOG_PATH = os.environ.get("CHANGELOG_PATH", "CHANGELOG.md")
2327
VERSION = os.environ['VERSION']
2428
PREV_VERSION = os.environ.get("PREV_VERSION", "")
25-
NEW_CONTENT = os.environ['NEW_CONTENT']
26-
27-
def overwrite_changelog_section(content: str):
28-
"""Replace a specific version section in the changelog content.
29-
30-
Args:
31-
content: The full changelog content as a string
32-
33-
Returns:
34-
The updated changelog content with the new section
35-
36-
Example:
37-
>>> content = "## 1.2.0\\nOld changes\\n## 1.1.0\\nOld changes"
38-
>>> NEW_CONTENT = "New changes"
39-
>>> overwrite_changelog_section(content)
40-
'## 1.2.0\\nNew changes\\n## 1.1.0\\nOld changes'
41-
"""
29+
NEW_CONTENT = os.environ.get("NEW_CONTENT", "")
30+
31+
def overwrite_changelog_section(changelog_text: str, new_content: str):
4232
# Find the section for the specified version
4333
version_pattern = f"## {VERSION}\n"
34+
prev_version_pattern = f"## [{PREV_VERSION}]\n"
4435
print(f"latest version: {VERSION}")
45-
notes_start_index = content.find(version_pattern) + len(version_pattern)
4636
print(f"prev_version: {PREV_VERSION}")
47-
prev_version_pattern = f"## {PREV_VERSION}\n"
48-
notes_end_index = content.find(prev_version_pattern, notes_start_index) if PREV_VERSION and prev_version_pattern in content else len(content)
49-
return content[:notes_start_index] + f"{NEW_CONTENT}\n" + content[notes_end_index:]
5037

51-
with open(CHANGELOG_PATH, 'r') as f:
52-
content = f.read()
38+
notes_start_index = changelog_text.find(version_pattern) + len(version_pattern)
39+
notes_end_index = changelog_text.find(prev_version_pattern, notes_start_index) if PREV_VERSION and prev_version_pattern in changelog_text else len(changelog_text)
5340

54-
new_changelog = overwrite_changelog_section(content)
41+
if new_content:
42+
return changelog_text[:notes_start_index] + f"{new_content}\n" + changelog_text[notes_end_index:]
43+
else:
44+
changeset_lines = changelog_text[notes_start_index:notes_end_index].split("\n")
45+
# Remove the first two lines from the regular changeset format, ex: \n### Patch Changes
46+
parsed_lines = "\n".join(changeset_lines[2:])
47+
updated_changelog = changelog_text[:notes_start_index] + parsed_lines + changelog_text[notes_end_index:]
48+
updated_changelog = updated_changelog.replace(f"## {VERSION}", f"## [{VERSION}]")
49+
return updated_changelog
5550

56-
print(new_changelog)
51+
with open(CHANGELOG_PATH, 'r') as f:
52+
changelog_content = f.read()
5753

54+
new_changelog = overwrite_changelog_section(changelog_content, NEW_CONTENT)
55+
print("----------------------------------------------------------------------------------")
56+
print(new_changelog)
57+
print("----------------------------------------------------------------------------------")
5858
# Write back to CHANGELOG.md
5959
with open(CHANGELOG_PATH, 'w') as f:
6060
f.write(new_changelog)
61+
62+
print(f"{CHANGELOG_PATH} updated successfully!")

.github/workflows/changeset-release.yml

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name: Changeset Release
2-
run-name: Changeset Release ${{ github.actor != 'R00-B0T' && '- Create PR' || '- Ready for Review' }}
2+
run-name: Changeset Release ${{ github.actor != 'R00-B0T' && '- Create PR' || '- Update Changelog' }}
33

44
on:
55
pull_request:
6-
types: [closed, opened, synchronize, labeled]
6+
types: [closed, opened, labeled]
77

88
env:
99
REPO_PATH: ${{ github.repository }}
@@ -76,15 +76,65 @@ jobs:
7676
token: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}
7777
fetch-depth: 0
7878
ref: ${{ env.GIT_REF }}
79-
# Auto-approve PR
79+
80+
# Get current and previous versions to edit changelog entry
81+
- name: Get version
82+
id: get_version
83+
run: |
84+
VERSION=$(git show HEAD:package.json | jq -r '.version')
85+
echo "version=$VERSION" >> $GITHUB_OUTPUT
86+
PREV_VERSION=$(git show origin/main:package.json | jq -r '.version')
87+
echo "prev_version=$PREV_VERSION" >> $GITHUB_OUTPUT
88+
echo "version=$VERSION"
89+
echo "prev_version=$PREV_VERSION"
90+
91+
# Update CHANGELOG.md with proper format
92+
- name: Update Changelog Format
93+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
94+
env:
95+
VERSION: ${{ steps.get_version.outputs.version }}
96+
PREV_VERSION: ${{ steps.get_version.outputs.prev_version }}
97+
run: python .github/scripts/overwrite_changeset_changelog.py
98+
99+
# Commit and push changelog updates
100+
- name: Push Changelog updates
101+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
102+
run: |
103+
git config user.name "R00-B0T"
104+
git config user.email [email protected]
105+
git status
106+
echo "Running git add and commit..."
107+
git add CHANGELOG.md
108+
git commit -m "Updating CHANGELOG.md format"
109+
echo "--------------------------------------------------------------------------------"
110+
echo "Pushing to remote..."
111+
echo "--------------------------------------------------------------------------------"
112+
git push
113+
114+
# Add label to indicate changelog has been formatted
115+
- name: Add changelog-ready label
116+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'changelog-ready') }}
117+
uses: actions/github-script@v7
118+
with:
119+
github-token: ${{ secrets.GITHUB_TOKEN }}
120+
script: |
121+
await github.rest.issues.addLabels({
122+
owner: context.repo.owner,
123+
repo: context.repo.repo,
124+
issue_number: context.issue.number,
125+
labels: ['changelog-ready']
126+
});
127+
128+
# Auto-approve PR only after it has been labeled
80129
- name: Auto approve PR
130+
if: contains(github.event.pull_request.labels.*.name, 'changelog-ready')
81131
uses: hmarr/auto-approve-action@v4
82132
with:
83133
review-message: "I'm approving since it's a bump version PR"
84134

85135
# Auto-merge PR
86136
- name: Automerge on PR
87-
if: false # Needs enablePullRequestAutoMerge in repo settings to work
137+
if: false # Needs enablePullRequestAutoMerge in repo settings to work contains(github.event.pull_request.labels.*.name, 'changelog-ready')
88138
run: gh pr merge --auto --merge ${{ github.event.pull_request.number }}
89139
env:
90140
GH_TOKEN: ${{ secrets.CROSS_REPO_ACCESS_TOKEN }}

0 commit comments

Comments
 (0)