-
Notifications
You must be signed in to change notification settings - Fork 222
ci: global changelog generator script #5328
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
a0f6e3e
a2f863c
9ccb88d
2bf08d8
a849b21
73df35a
24edcd7
ef8f7bb
cab24b0
03ba768
a9d421c
9cf280c
5050e77
bf0afbd
7de437d
eb64182
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,93 @@ | ||||||||||||||
/* | ||||||||||||||
Rajdeepc marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
Copyright 2025 Adobe. All rights reserved. | ||||||||||||||
This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||||||||||||||
you may not use this file except in compliance with the License. You may obtain a copy | ||||||||||||||
of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
|
||||||||||||||
Unless required by applicable law or agreed to in writing, software distributed under | ||||||||||||||
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||||||||||||||
OF ANY KIND, either express or implied. See the License for the specific language | ||||||||||||||
governing permissions and limitations under the License. | ||||||||||||||
|
||||||||||||||
*/ | ||||||||||||||
|
||||||||||||||
import fs from 'fs'; | ||||||||||||||
import { execSync } from 'child_process'; | ||||||||||||||
import { fileURLToPath } from 'url'; | ||||||||||||||
import path from 'path'; | ||||||||||||||
|
||||||||||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||||||||||||||
const repoUrl = 'https://github.com/adobe/spectrum-web-components'; | ||||||||||||||
|
||||||||||||||
const pkg = JSON.parse( | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A best practice to keep code scoped is to wrap them in a main function and call the function at the end of the file. I think it's a good idea to maintain that best practice here that we see in our other scripts as well. |
||||||||||||||
fs.readFileSync(path.resolve(__dirname, '../package.json'), 'utf-8') | ||||||||||||||
); | ||||||||||||||
const newVersion = pkg.version; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can skip this abstraction since newVersion is only used in the following line. We also need a check for if the package doesn't load to throw that warning. You're assuming here that pkg.version exists. |
||||||||||||||
const newTag = `v${newVersion}`; | ||||||||||||||
const prevTag = execSync('git tag --sort=-creatordate') | ||||||||||||||
.toString() | ||||||||||||||
.split('\n') | ||||||||||||||
.filter(Boolean) | ||||||||||||||
.find((tag) => tag !== newTag); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs more robust failure captures. I recommend if you're going to use exec for this, separate the command execution (exec is notoriously flaky in node scripts so you need to account for it failing) from the string parsing. Check that exec returned a string and then run the split, etc. |
||||||||||||||
|
||||||||||||||
if (!prevTag) { | ||||||||||||||
console.error('No previous tag found.'); | ||||||||||||||
process.exit(1); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to think about what information I would need to debug this error. At the age of this project, there's no change that there aren't previous tags to be found so maybe we want this error to tell us why the exec command couldn't return a value we were expecting? Maybe this should log the git tag command output? |
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const date = new Date().toISOString().split('T')[0]; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This should return the result you're wanting without having to do inline array parsing (which can sometimes lead to invalid results or fail when the array isn't in the format we're expecting). |
||||||||||||||
const compareUrl = `${repoUrl}/compare/${prevTag}...${newTag}`; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is prevTag allowed to be a pre-tag or is there a requirement that prevTag must be a semver version? It seems like it must be one of the semver releases (not the betas for example) so maybe we can add a comment to that effect? |
||||||||||||||
const commitLogs = execSync(`git log ${prevTag}..HEAD --pretty=format:"%s|%h"`) | ||||||||||||||
.toString() | ||||||||||||||
.trim(); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like it's returning the commit logs but not the changelog content. Is that what we're wanting to add to the global changelog? It seems like the commit history is less useful now that we've migrated to changesets. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a great point where I would want everyone's opinion on. I don't think only a summary of the change is sufficient for the users to check what changes went along. I want to keep the CHANGELOG to follow the same pattern as we were doing during lerna which I feel the users still wants. |
||||||||||||||
|
||||||||||||||
const commits = commitLogs.split('\n').map((line) => { | ||||||||||||||
const [message, hash] = line.split('|'); | ||||||||||||||
return { message, hash }; | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
const features = []; | ||||||||||||||
const fixes = []; | ||||||||||||||
|
||||||||||||||
commits.forEach(({ message, hash }) => { | ||||||||||||||
const typeMatch = message.match(/^(feat|fix)\(([^)]+)\):\s*(.+)/i); | ||||||||||||||
if (typeMatch) { | ||||||||||||||
const [, type, scope, description] = typeMatch; | ||||||||||||||
const entry = `- **${scope}**: ${description} ([\`${hash}\`](${repoUrl}/commit/${hash}))`; | ||||||||||||||
if (type === 'feat') { | ||||||||||||||
features.push(entry); | ||||||||||||||
} else if (type === 'fix') { | ||||||||||||||
fixes.push(entry); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
}); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this can serve a temporary fix but we should really be using the changesets tooling to create this content from it's new source (which is not the commit messages): https://github.com/changesets/changesets/blob/main/docs/modifying-changelog-format.md#writing-changelog-formatting-functions I think the challenge with this as a sustainable approach is that less and less useful data is present in commit messages and the real value for customers now lives in the changesets files. |
||||||||||||||
|
||||||||||||||
// Skip if nothing relevant | ||||||||||||||
if (!features.length && !fixes.length) { | ||||||||||||||
console.log('🚫 No new feat() or fix() commits to add.'); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Since no new features or fixes isn't necessarily a failure of the script, should we format this more like a success message that it ran successfully but with no changes? |
||||||||||||||
process.exit(0); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// Format new changelog entry | ||||||||||||||
let newEntry = `# [${newVersion}](${compareUrl}) (${date})\n\n`; | ||||||||||||||
|
||||||||||||||
if (fixes.length) { | ||||||||||||||
newEntry += `### Bug Fixes\n\n${fixes.join('\n')}\n\n`; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (features.length) { | ||||||||||||||
newEntry += `### Features\n\n${features.join('\n')}\n\n`; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// Prepend to existing CHANGELOG.md | ||||||||||||||
const changelogPath = path.resolve(__dirname, '../CHANGELOG.md'); | ||||||||||||||
const existingChangelog = fs.existsSync(changelogPath) | ||||||||||||||
? fs.readFileSync(changelogPath, 'utf-8') | ||||||||||||||
: ''; | ||||||||||||||
|
||||||||||||||
fs.writeFileSync( | ||||||||||||||
changelogPath, | ||||||||||||||
`${newEntry.trim()}\n\n${existingChangelog}`, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't this push the |
||||||||||||||
'utf-8' | ||||||||||||||
); | ||||||||||||||
console.log(`✅ CHANGELOG updated for ${newVersion}`); |
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.
Shouldn’t
yarn changelog:global
be executed beforeyarn changeset version
? This is becauseyarn changelog:global
reads from changeset files located in the.changeset
directory. However, after runningyarn changeset version
, all the changesets are removed/deleted, and the changelogs are populated. Consequently,yarn changelog:global
will no longer be able to read the changeset files in the.changeset
directory.