Skip to content
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

build: use release-it to generate release data #817

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
// 👇 Added `(coverage|dist)/**/*` to be able to run 'eslint .' successfully
// When upgrading to v9, can include .gitignore here. In v8, you need to add a CLI flag
// Given 'eslint .' is not something ran often, it's good enough as ienough as is
"ignorePatterns": ["projects/**/*", "coverage/**/*", "dist/**/*", "!.*.json"],
"ignorePatterns": [
"projects/**/*",
"coverage/**/*",
"dist/**/*",
"!.*.json",
"!.*.ts"
],
"overrides": [
{
"files": ["*.ts", "*.mts"],
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/reusable-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ jobs:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
with:
#👇 To generate release information
fetch-depth: 0
ref: ${{ inputs.ref }}
lfs: true
- name: Setup
uses: ./.github/actions/setup
- name: Generate info for Angular cache key
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ $RECYCLE.BIN/
/junit-lint.xml
/assets/*-subset.*
/release.json
/src/release.json
/src/environments/environment.pull-request.ts
.nx/cache
.idea/git_toolbox_blame.xml
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
enable-pre-post-scripts=false
public-hoist-pattern[]=conventional-recommended-bump
156 changes: 156 additions & 0 deletions .release-it.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { Config } from 'release-it'
import { BumperRecommendation, Preset } from 'conventional-recommended-bump'

export const createReleaseItConfig = ({
gitRawCommitsOpts,
}: ExtraConfig = {}): Config => ({
git: {
commit: false,
push: false,
},
// @ts-expect-error Invalid type definition. TODO: PR for this
npm: false,
plugins: {
'@release-it/conventional-changelog': {
preset: {
name: 'conventionalcommits',
types: [
{
type: 'feat',
section: 'Features',
},
{
type: 'fix',
section: 'Bug Fixes',
},
{
type: 'perf',
section: 'Performance Improvements',
},
{
type: 'revert',
section: 'Reverts',
},
{
type: 'docs',
section: 'Documentation',
},
{
type: 'style',
section: 'Style',
},
{
type: 'chore',
section: 'Miscellaneous Chores',
},
{
type: 'refactor',
section: 'Code Refactoring',
},
{
type: 'test',
section: 'Tests',
},
{
type: 'build',
section: 'Build System',
},
{
type: 'ci',
section: 'Continuous (Integration|Deployment)',
},
],
},
whatBump,
gitRawCommitsOpts,
},
},
})

/**
* ⚠️ Conventional Changelog version bumpers always returns at least a patch release
*
* Therefore, `release-it/conventional-changelog` behaves the same, as it's just a wrapper over it
* Here, the recommended version bumper is tuned so that not every commit triggers a version bump.
*
* See:
*
* - GitHub issue about it: https://github.com/release-it/conventional-changelog/issues/22
* - Original implementation: https://github.com/conventional-changelog/conventional-changelog/blob/conventional-recommended-bump-v10.0.0/packages/conventional-changelog-conventionalcommits/src/whatBump.js
*
* @param commits
*/
const whatBump: Preset['whatBump'] = async (commits) => {
const commitsByLevel = commits.reduce(
(results, commit) => {
const addToResults = (key: number) => {
results[key].push(commit)
return results
}
// As notes are only parsed for breaking changes
// https://github.com/conventional-changelog/conventional-changelog/blob/conventional-recommended-bump-v10.0.0/packages/conventional-changelog-conventionalcommits/src/parser.js
const hasBreakingChanges = addBangNotes(commit).length > 0
if (hasBreakingChanges) {
return addToResults(RELEASE_LEVEL_MAJOR)
}
if (commit.type === 'feat' || commit.type === 'feature') {
return addToResults(RELEASE_LEVEL_MINOR)
}
if (commit.type === 'fix' || commit.type === 'perf') {
return addToResults(RELEASE_LEVEL_PATCH)
}
return results
},
[[], [], []] as [Commit[], Commit[], Commit[]],
)
const level = ifMinusOneToUndefined(
commitsByLevel.findIndex((commits) => commits.length > 0),
) as BumperRecommendation['level']
const reason =
level === undefined
? 'No commit needs to be released according to bump rules'
: 'These are the commits that triggered a release. The highest release level was chosen\n' +
commitsByLevel
.map(
(commitHeaders, level) =>
` - ${RELEASE_LEVEL_NAMES[level]}: ${commitHeaders.map((header) => `"${header}"`).join(', ')}`,
)
.join('\n')
return { level, reason }
}

export type ExtraConfig = Partial<{
gitRawCommitsOpts: { from: string; to: string }
}>

// https://github.com/conventional-changelog/conventional-changelog/blob/conventional-changelog-conventionalcommits-v8.0.0/packages/conventional-changelog-conventionalcommits/src/utils.js
function addBangNotes(commit: Commit) {
const breakingHeaderPattern = /^(\w*)(?:\((.*)\))?!: (.*)$/
const match = commit.header?.match(breakingHeaderPattern)
if (match && commit.notes.length === 0) {
const noteText = match[3] // the description of the change.

return [
{
title: 'BREAKING CHANGE',
text: noteText,
},
]
}

return commit.notes
}

// https://stackoverflow.com/a/52331580/3263250
type Unpacked<T> = T extends (infer U)[] ? U : T
type Commit = Unpacked<Parameters<Preset['whatBump']>[0]>

const ifMinusOneToUndefined = (n: number) => (n !== -1 ? n : undefined)

// https://github.com/conventional-changelog/conventional-changelog/blob/conventional-recommended-bump-v10.0.0/packages/conventional-recommended-bump/src/bumper.ts#L24-L28
const RELEASE_LEVEL_NAMES = ['major', 'minor', 'patch'] as const
const [RELEASE_LEVEL_MAJOR, RELEASE_LEVEL_MINOR, RELEASE_LEVEL_PATCH] = [
...RELEASE_LEVEL_NAMES.keys(),
]

export default createReleaseItConfig()
1 change: 1 addition & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"src/assets",
"src/site.webmanifest",
"src/browserconfig.xml",
"src/release.json",
{
"glob": "profile.jpg",
"input": "src/",
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@
"lint:gh-actions": "actionlint || true",
"prebuild:font-subsets": "tsm scripts/src/generate-font-subsets.ts",
"prebuild:release-file": "tsx scripts/src/generate-release-file.mts",
"prebuild:release-info": "tsx scripts/src/generate-release-info.mts",
"prebuild:render-templates": "tsm scripts/src/generate-templated-files.ts",
"prebuild:simple-icons": "tsm scripts/src/generate-simple-icons.ts",
"release:dry-run": "release-it --dry-run",
"release:dry-run:local": "pnpm run release:dry-run --no-git.requireCleanWorkingDir --no-git.requireUpstream",
"security-txt": "tsm scripts/src/generate-security-txt.ts",
"serve:ssr:@davidlj95/website": "node dist/@davidlj95/website/server/server.mjs",
"start": "ng serve",
Expand Down Expand Up @@ -94,6 +97,7 @@
"@commitlint/types": "19.5.0",
"@cypress/code-coverage": "3.13.4",
"@lhci/cli": "0.14.0",
"@release-it/conventional-changelog": "9.0.3",
"@types/compression": "1.7.5",
"@types/express": "4.17.21",
"@types/jasmine": "5.1.4",
Expand Down Expand Up @@ -127,6 +131,7 @@
"ng-mocks": "14.13.1",
"nyc": "17.1.0",
"prettier": "3.3.3",
"release-it": "17.10.0",
"semantic-release": "24.2.0",
"subset-font": "2.3.0",
"tsm": "2.3.0",
Expand Down
Loading
Loading