-
Notifications
You must be signed in to change notification settings - Fork 825
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
feat: add script to update changelogs on release preparation #4315
Merged
pichlermarc
merged 8 commits into
open-telemetry:main
from
dynatrace-oss-contrib:chore/update-changelog-script
Nov 30, 2023
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4049054
feat: add script to update changelogs on releases
pichlermarc 36692da
Merge branch 'main' into chore/update-changelog-script
pichlermarc a9bf07a
fix: address comments
pichlermarc 12050e0
Merge branch 'main' into chore/update-changelog-script
pichlermarc 75211c8
Apply suggestions from code review
pichlermarc 90b3977
fix: apply suggestions from code review
pichlermarc 41a5738
fix: use packageJson.version instead of version
pichlermarc 8c73052
Merge branch 'main' into chore/update-changelog-script
pichlermarc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* This script updates changelogs after lerna has updated versions in the respective areas (packages/*, experimental/packages/*) | ||
* - removes all empty subsections (bugs, enhancements, etc.) in the changelog. | ||
* - replaces the "Unreleased"-header with the version from the first non-private package in the directory (versions are expected to be uniform across a changelog) | ||
* - adds a new "Unreleased"-header with empty subsections at the top | ||
* | ||
* Usage (from project root): | ||
* - node scripts/update-changelog.js [PATH TO CHANGELOG] [DIRECTORY CONTAINING ASSOCIATED PACKAGES] | ||
* Examples: | ||
* - node scripts/update-changelog.js ./CHANGELOG.md ./packages | ||
* - node scripts/update-changelog.js ./experimental/CHANGELOG.md ./experimental/packages | ||
*/ | ||
|
||
const fs = require('fs'); | ||
const path = require("path"); | ||
|
||
const EMPTY_UNRELEASED_SECTION = `## Unreleased | ||
|
||
### :boom: Breaking Change | ||
|
||
### :rocket: (Enhancement) | ||
|
||
### :bug: (Bug Fix) | ||
|
||
### :books: (Refine Doc) | ||
|
||
### :house: (Internal) | ||
|
||
` | ||
|
||
function findFirstPackageVersion(basePath){ | ||
const packageDirs = fs.readdirSync(basePath); | ||
for(const packageDir of packageDirs){ | ||
const packageJsonPath = path.join(basePath, packageDir, 'package.json'); | ||
try { | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); | ||
|
||
if(packageJson.private === true || packageJson.private === 'true'){ | ||
console.log('Skipping version from private package at', packageJsonPath); | ||
continue; | ||
} | ||
|
||
if(packageJson.version != null){ | ||
return version; | ||
} | ||
|
||
console.log('Version in', packageJsonPath, 'was null or undefined, skipping'); | ||
} catch (err) { | ||
console.log('Could not get package JSON', packageJsonPath, err); | ||
} | ||
} | ||
throw new Error('Unable to extract version from packages in ' + basePath); | ||
} | ||
|
||
function replaceEmptySection(changelog){ | ||
// Only match ## at the end in case the last header does not have any entries and the next one is '## version', | ||
// this makes it safe to replace with only '##' | ||
return changelog.replace(RegExp('###.*\n*##', 'gm'), '##'); | ||
pichlermarc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// no special handling for bad args as this is only intended for use via predefined npm scripts. | ||
const changelogPath = path.resolve(process.argv[2]); | ||
const version = findFirstPackageVersion(path.resolve(process.argv[3])); | ||
pichlermarc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let changelog = fs.readFileSync(changelogPath, 'utf8').toString(); | ||
let previousChangelog = replaceEmptySection(changelog); | ||
|
||
// keep replacing until there's nothing to replace anymore | ||
while(changelog !== previousChangelog){ | ||
previousChangelog = changelog; | ||
changelog = replaceEmptySection(changelog); | ||
} | ||
|
||
// replace unreleased header with new unreleased section and a version header for the former unreleased section | ||
changelog = changelog.replace(RegExp('## Unreleased'), EMPTY_UNRELEASED_SECTION + '## ' + version); | ||
|
||
fs.writeFileSync(changelogPath, changelog); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cleanup was done by the script when I ran it locally. It removes all empty sub-sections.