-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70a8ebd
commit ed53bb9
Showing
12 changed files
with
400 additions
and
16 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
.github/actions/changeset-release-notes/__snapshots__/changelog.test.ts.snap
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,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`getChangesForVersion parse changelog with multiple versions 1`] = ` | ||
[ | ||
{ | ||
"changes": [ | ||
"**events**: Introduce \`PUT\` endpoint for \`/events\` API ([e8bc23f](https://github.com/fingerprintjs/fingerprint-pro-server-api-openapi/commit/e8bc23f115c3b01f9d0d472b02093d0d05d3f4a5))", | ||
"**visits**: Model fixes ([e8bc23f](https://github.com/fingerprintjs/fingerprint-pro-server-api-openapi/commit/e8bc23f115c3b01f9d0d472b02093d0d05d3f4a5))", | ||
], | ||
"type": "Minor Changes", | ||
}, | ||
] | ||
`; |
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,9 @@ | ||
name: 'Changeset release notes' | ||
description: 'Returns release notes for latest changeset release' | ||
outputs: | ||
release-notes: | ||
description: 'Release notes' | ||
|
||
runs: | ||
using: 'node20' | ||
main: 'dist/index.js' |
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,25 @@ | ||
import { getChangesForVersion } from './changelog' | ||
|
||
describe('getChangesForVersion', () => { | ||
it('parse changelog with multiple versions', () => { | ||
const changelogText = `# fingerprint-pro-server-api-openapi | ||
## 1.1.0 | ||
### Minor Changes | ||
- **events**: Introduce \`PUT\` endpoint for \`/events\` API ([e8bc23f](https://github.com/fingerprintjs/fingerprint-pro-server-api-openapi/commit/e8bc23f115c3b01f9d0d472b02093d0d05d3f4a5)) | ||
- **visits**: Model fixes ([e8bc23f](https://github.com/fingerprintjs/fingerprint-pro-server-api-openapi/commit/e8bc23f115c3b01f9d0d472b02093d0d05d3f4a5)) | ||
## 1.0.0 | ||
### Minor Changes | ||
- Initial release | ||
` | ||
|
||
const result = getChangesForVersion('1.1.0', changelogText) | ||
|
||
expect(result).toMatchSnapshot() | ||
}) | ||
}) |
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,106 @@ | ||
import { NewChangeset, PackageJSON } from '@changesets/types' | ||
import { sync as globSync } from 'glob' | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
export type ChangeLogEntry = { | ||
version: string | ||
changes: string[] | ||
} | ||
|
||
type Project = { | ||
version: string | ||
changelogPath: string | ||
} | ||
|
||
export type ReleaseNotes = { | ||
projectName: string | ||
changes: Changes[] | ||
currentVersion: string | ||
} | ||
|
||
function listProjects(changesets: NewChangeset[]) { | ||
const ids = new Set<string>(...changesets.map((c) => c.id)) | ||
const packageJsons = globSync('**/package.json', { | ||
ignore: ['**/node_modules/**'], | ||
}) | ||
|
||
const projects = new Map<string, Project>() | ||
|
||
packageJsons.forEach((packageJsonPath) => { | ||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) as PackageJSON | ||
if (!ids.has(packageJson.name)) { | ||
return | ||
} | ||
|
||
const changelogPath = path.join(path.dirname(packageJsonPath), 'CHANGELOG.md') | ||
if (fs.existsSync(changelogPath)) { | ||
projects.set(packageJson.name, { | ||
version: packageJson.version, | ||
changelogPath: changelogPath, | ||
}) | ||
} | ||
}) | ||
|
||
return projects | ||
} | ||
|
||
export function listChangesForAllProjects(changesets: NewChangeset[]) { | ||
const notes: ReleaseNotes[] = [] | ||
|
||
const changelogs = listProjects(changesets) | ||
|
||
changelogs.forEach((project, projectName) => { | ||
const changelog = fs.readFileSync(project.changelogPath, 'utf-8') | ||
notes.push({ | ||
changes: getChangesForVersion(project.version, changelog), | ||
projectName: projectName, | ||
currentVersion: project.version, | ||
}) | ||
}) | ||
|
||
return notes | ||
} | ||
|
||
export type Changes = { type: string; changes: string[] } | ||
|
||
export function getChangesForVersion(version: string, changelog: string): Changes[] { | ||
// Split the changelog into lines for easier processing | ||
const lines = changelog.split('\n') | ||
|
||
// Initialize variables to track the current version and changes | ||
let currentVersion = '' | ||
let changeType = '' | ||
const changes: Changes[] = [] | ||
|
||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i].trim() | ||
|
||
// Check for a version line (e.g., "## 1.1.0") | ||
const versionMatch = line.match(/^## (\d+\.\d+\.\d+)/) | ||
if (versionMatch) { | ||
currentVersion = versionMatch[1] | ||
// If the current version matches the requested version, continue processing | ||
if (currentVersion === version) { | ||
continue | ||
} else if (changes.length > 0) { | ||
// If we've already collected changes for the desired version, break the loop | ||
break | ||
} | ||
} | ||
|
||
// Check for a change type line (e.g., "### Minor Changes") | ||
if (currentVersion === version && line.startsWith('###')) { | ||
changeType = line.replace(/^###\s*/, '') | ||
changes.push({ type: changeType, changes: [] }) | ||
} | ||
|
||
// Collect changes under the current change type | ||
if (currentVersion === version && line.startsWith('-')) { | ||
// Add the change to the last changeType entry | ||
changes[changes.length - 1].changes.push(line.slice(2).trim()) | ||
} | ||
} | ||
|
||
return changes | ||
} |
42 changes: 42 additions & 0 deletions
42
.github/actions/changeset-release-notes/changeset-release-notes.ts
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,42 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import { PackageJSON } from '@changesets/types' | ||
import * as core from '@actions/core' | ||
import * as cp from 'child_process' | ||
import readChangesets from '@changesets/read' | ||
import { getReleaseNotes } from './notes' | ||
|
||
function getCurrentVersion() { | ||
const pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8')) | ||
|
||
return (pkg as PackageJSON).version | ||
} | ||
|
||
function doVersion() { | ||
const lastVersion = getCurrentVersion() | ||
cp.execSync('pnpm exec changeset version') | ||
const nextVersion = getCurrentVersion() | ||
|
||
return lastVersion !== nextVersion | ||
} | ||
|
||
async function main() { | ||
const changesets = await readChangesets(process.cwd()) | ||
if (!changesets.length) { | ||
return | ||
} | ||
|
||
if (!doVersion()) { | ||
return | ||
} | ||
|
||
const notes = getReleaseNotes(changesets) | ||
|
||
if (notes) { | ||
core.setOutput('release-notes', notes) | ||
} | ||
} | ||
|
||
main().catch((err) => { | ||
core.setFailed(err) | ||
}) |
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,22 @@ | ||
import { NewChangeset } from '@changesets/types' | ||
import { listChangesForAllProjects } from './changelog' | ||
|
||
export function getReleaseNotes(changesets: NewChangeset[]) { | ||
let result = '' | ||
|
||
const changes = listChangesForAllProjects(changesets) | ||
|
||
changes.forEach((change) => { | ||
result += `## ${change.projectName}@${change.currentVersion}\n\n` | ||
|
||
change.changes.forEach((change) => { | ||
result += `### ${change.type}` | ||
|
||
change.changes.forEach((description) => { | ||
result += `\n- ${description}` | ||
}) | ||
}) | ||
}) | ||
|
||
return result | ||
} |
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,48 @@ | ||
name: 'Preview changeset release' | ||
on: | ||
workflow_call: | ||
inputs: | ||
pr-title: | ||
description: Title of created PR | ||
required: true | ||
type: string | ||
node-version: | ||
description: 'Node version to use' | ||
required: false | ||
type: string | ||
default: 'lts/*' | ||
|
||
jobs: | ||
preview: | ||
name: Generate release notes | ||
runs-on: ubuntu-latest | ||
if: ${{ !contains('[changeset] ', inputs.pr-title) }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: 'Install latest node version' | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: ${{ inputs.node-version }} | ||
|
||
- name: 'Get release notes' | ||
id: notes | ||
# TODO Replace with this | ||
#uses: fingerprintjs/dx-team-toolkit/.github/actions/update-sdk-schema@v1 | ||
uses: ./.github/actions/changeset-release-notes | ||
|
||
- name: 'Add comment to PR' | ||
if: steps.notes.outputs.release-notes != '' | ||
uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31 | ||
with: | ||
header: Release notes | ||
recreate: true | ||
message: ${{ steps.notes.outputs.release-notes }} | ||
|
||
- name: 'Add release notes preview to the job summary' | ||
if: steps.notes.outputs.release-notes != '' | ||
run: | | ||
echo "${{ steps.notes.outputs.release-notes }}" >> $GITHUB_STEP_SUMMARY | ||
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,17 @@ | ||
name: 'Preview changeset release' | ||
on: | ||
pull_request: | ||
|
||
permissions: | ||
pull-requests: write | ||
contents: write | ||
|
||
jobs: | ||
preview: | ||
name: Preview changeset release | ||
uses: ./.github/workflows/preview-changeset-release.yml | ||
with: | ||
pr-title: ${{ github.event.pull_request.title }} | ||
|
||
|
||
|
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
Oops, something went wrong.