-
Notifications
You must be signed in to change notification settings - Fork 1
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(ci): Introduce posting changelog into the Slack channel #1797
Open
literat
wants to merge
2
commits into
main
Choose a base branch
from
chore/post-changelog-slack
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+714
−5
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
/* eslint-disable import/no-unresolved */ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/* eslint-disable no-console */ | ||
import dotenv from 'dotenv-safe'; | ||
import gitDiffParser from 'gitdiff-parser'; | ||
import { simpleGit } from 'simple-git'; | ||
import slackifyMarkdown from 'slackify-markdown'; | ||
import { $, fetch, argv } from 'zx'; | ||
|
||
const COLOR_CORE = '#00A58E'; | ||
const PACKAGES = ['web', 'web-react', 'web-twig', 'design-tokens', 'icons', 'codemods', 'analytics']; | ||
const SLACK_CHANGELOG_WEBHOOK_URL = process.env.SLACK_CHANGELOG_WEBHOOK_URL ?? ''; | ||
|
||
/** | ||
* Generates a title for the given package. | ||
* | ||
* @param {string} pkg - The package name. | ||
* @returns {string} The generated title. | ||
*/ | ||
function getTitle(pkg) { | ||
return `🚀 New ${pkg | ||
.split('-') | ||
.map((part) => part.charAt(0).toUpperCase() + part.slice(1)) | ||
.join(' ')} package release`; | ||
} | ||
|
||
/** | ||
* Sends the content to the specified webhook URL. | ||
* | ||
* @param {object} params - The parameters. | ||
* @param {object} params.content - The content to send. | ||
* @param {string} params.webhookUrl - The webhook URL to send the content to. | ||
*/ | ||
async function sendToWebhook({ content, webhookUrl }) { | ||
await fetch(webhookUrl, { | ||
method: 'POST', | ||
body: JSON.stringify(content), | ||
}) | ||
.then((res) => { | ||
if (res.status !== 200) { | ||
throw new Error(`${res.status} ${res.statusText}`); | ||
} | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
} | ||
|
||
/** | ||
* Formats the changelog string with the given package name and prefix. | ||
* | ||
* @param {string} str - The changelog string to format. | ||
* @param {string} packageName - The name of the package. | ||
* @param {string} prefix - The prefix to use for the package. | ||
* | ||
* @returns {string} The formatted changelog string. | ||
*/ | ||
function format(str, packageName, prefix = '@lmc-eu') { | ||
const output = str | ||
.replace( | ||
/^#+ /, | ||
`# 📦 ${packageName | ||
.split('-') | ||
.map((part) => part.charAt(0).toUpperCase() + part.slice(1)) | ||
.join(' ')} \`${prefix}/spirit-${packageName}\` `, | ||
) | ||
.replace('Bug Fixes', '🐛 Bug Fixes') | ||
.replace('Features', '⚡ Features') | ||
.replace('BREAKING CHANGES', '🚨 BREAKING CHANGES') | ||
.replace('Dependencies', '📦 Dependencies') | ||
.replace('Documentation', '📜 Documentation') | ||
.replace('Tests', '🧪 Tests') | ||
.replace('Code Refactoring', '🛠️ Code Refactoring') | ||
.replace('Chores', '🔨 Chores') | ||
.replace('Styles', '💅 Styles') | ||
.replaceAll('https://github.com/lmc-eu/spirit-design-system/issues/', 'https://jira.almacareer.tech/browse/'); | ||
|
||
return output; | ||
} | ||
|
||
/** | ||
* Extracts the changelog content from the given diff files. | ||
* | ||
* @param {Array} files - The diff files to extract the changelog from. | ||
* @returns {string} The extracted changelog content. | ||
*/ | ||
function getChangelogFromDiff(files) { | ||
// Only one file as we're only looking at the changelog | ||
const versionPattern = /<a name=".*"><\/a>/; | ||
const [changelogFile] = files; | ||
const changelog = changelogFile.hunks | ||
.flatMap((hunk) => hunk.changes.filter(({ isInsert }) => isInsert).map(({ content }) => content)) | ||
.filter((line) => !versionPattern.test(line)) | ||
.join('\n') | ||
.trim(); | ||
|
||
return changelog; | ||
} | ||
|
||
/** | ||
* Gets the diff for the given tag and path. | ||
* | ||
* @param {string} tag - The tag to get the diff for. | ||
* @param {string} path - The path to get the diff for. | ||
* @returns {Promise<string>} The diff output. | ||
*/ | ||
function getDiff(tag, path) { | ||
return simpleGit().show([tag, path]); | ||
} | ||
|
||
/** | ||
* Returns the changelog path for the given package name. | ||
* | ||
* @param {string} packageName - The name of the package. | ||
* @returns {string} The changelog path. | ||
*/ | ||
function changelogPath(packageName) { | ||
return `packages/${packageName}/CHANGELOG.md`; | ||
} | ||
|
||
/** | ||
* Posts a Slack notification with the given changelog. | ||
* | ||
* @param {string} changelog - The changelog content to post. | ||
* @param {string} packageName - The name of the package. | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
async function postSlackNotification(changelog, packageName) { | ||
try { | ||
$.verbose = false; | ||
const res = await sendToWebhook({ | ||
webhookUrl: SLACK_CHANGELOG_WEBHOOK_URL, | ||
content: { | ||
attachments: [ | ||
{ | ||
title: getTitle(packageName), | ||
text: changelog, | ||
color: COLOR_CORE, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
return res; | ||
} catch (err) { | ||
console.log('Error posting to Slack'); | ||
console.error(err); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Configures the webhook URL from the environment variables. | ||
*/ | ||
async function configureWebhookURL() { | ||
try { | ||
dotenv.config({ | ||
allowEmptyValues: true, | ||
example: '.env.example', | ||
}); | ||
} catch (err) { | ||
if (/SLACK_CHANGELOG_WEBHOOK_URL/g.test(err.message)) { | ||
throw new Error('SLACK_CHANGELOG_WEBHOOK_URL is not set'); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Publish the changelog for the given npm package. | ||
* | ||
* @param {string} npmPackage - The name of the npm package. | ||
*/ | ||
async function publishChangelog(npmPackage) { | ||
try { | ||
await simpleGit().fetch(['origin', 'main', '--tags']); | ||
const tags = await simpleGit().tags({ '--sort': '-taggerdate' }); | ||
const diff = await getDiff(tags.latest ?? '', changelogPath(npmPackage)); | ||
const files = gitDiffParser.parse(diff); | ||
if (files.length === 0) { | ||
console.log(`No changes in ${npmPackage}`); | ||
|
||
return; | ||
} | ||
const changelog = getChangelogFromDiff(files); | ||
const formattedChangelog = format(changelog, npmPackage); | ||
const slackifiedChangelog = slackifyMarkdown(formattedChangelog); | ||
|
||
if (argv.dry) { | ||
console.info(formattedChangelog); | ||
} else { | ||
await configureWebhookURL(); | ||
await postSlackNotification(slackifiedChangelog, npmPackage); | ||
} | ||
} catch (err) { | ||
console.error(err); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
(async () => { | ||
await Promise.all( | ||
PACKAGES.map(async (npmPackage) => { | ||
await publishChangelog(npmPackage); | ||
}), | ||
); | ||
process.exit(0); | ||
})(); |
Oops, something went wrong.
Oops, something went wrong.
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.
Ideas
Can we get rid of the package name in individual list items?
To reduce visual clutter, I'd consider:
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.
Yeah, I think can do many things. Primarily, this should help us with fabricating release notes. I know that just copy&paste will not be enough but it can save a lot of time. So let's modify this to our liking. :-)