Skip to content

Commit

Permalink
Update output based on latest official package
Browse files Browse the repository at this point in the history
  • Loading branch information
stormwarning committed Nov 17, 2023
1 parent 6440ce6 commit eefce14
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"access": "public",
"baseBranch": "main",
"changelog": [
"@changesets/changelog-github",
"../packages/changesets-changelog/index.js",
{ "repo": "stormwarning/zazen" }
],
"commit": false,
Expand Down
5 changes: 5 additions & 0 deletions .changeset/friendly-lions-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@zazen/changesets-changelog': major
---

Initial release
108 changes: 88 additions & 20 deletions packages/changesets-changelog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,124 @@
* @see https://github.com/atlassian/changesets/blob/main/packages/changelog-github/src/index.ts
*/

import { getInfo } from '@changesets/get-github-info'
import { getInfo, getInfoFromPullRequest } from '@changesets/get-github-info'

const REPO = 'stormwarning/zazen'
/**
* @param {Record<string, any> | null} options
*/
function validateOptions(options) {
if (!options || !options.repo) {
throw new Error(
'Please provide a repo to this changelog generator like this:\n"changelog": ["@zazen/changesets-changelog", { "repo": "org/repo" }]',
)
}
}

/** @type {import('@changesets/types').ChangelogFunctions} */
const changelogFunctions = {
async getDependencyReleaseLine(changesets, dependenciesUpdated) {
async getDependencyReleaseLine(changesets, dependenciesUpdated, options) {
validateOptions(options)
if (dependenciesUpdated.length === 0) return ''

let changesetCommits = await Promise.all(
changesets.map(async (cs) => {
if (cs.commit) {
let { links } = await getInfo({
repo: REPO,
repo: options.repo,
commit: cs.commit,
})
return links.commit
}
}),
)
let changesetLink = `- Updated dependencies [${changesetCommits
let changesetLink = `- Updated dependencies ${changesetCommits
.filter(Boolean)
.join(', ')}]:`

.join(', ')}:`
let updatedDepenenciesList = dependenciesUpdated.map(
(dependency) => ` - ${dependency.name}@${dependency.newVersion}`,
)

return [changesetLink, ...updatedDepenenciesList].join('\n')
},
async getReleaseLine(changeset) {
let [firstLine, ...futureLines] = changeset.summary

async getReleaseLine(changeset, options) {
validateOptions(options)

let [repoOwner] = options.repo.split('/')
/** @type {number} */
let prFromSummary
/** @type {string} */
let commitFromSummary
/** @type {Array<string>} */
let usersFromSummary = []

let replacedSummary = changeset.summary
.replace(/^\s*(?:pr|pull|pull\s+request):\s*#?(\d+)/im, (_, pr) => {
let prNumber = Number(pr)
if (!Number.isNaN(prNumber)) prFromSummary = prNumber
return ''
})
.replace(/^\s*commit:\s*(\S+)/im, (_, commit) => {
commitFromSummary = commit
return ''
})
.replaceAll(/^\s*(?:author|user):\s*@?(\S+)/gim, (_, user) => {
// Ignore user if it's your own repo.
if (!user.includes(repoOwner)) usersFromSummary.push(user)
return ''
})
.trim()

let [firstLine, ...futureLines] = replacedSummary
.split('\n')
.map((l) => l.trimEnd())

if (changeset.commit) {
let { links } = await getInfo({
repo: REPO,
commit: changeset.commit,
})
let links = await (async () => {
if (prFromSummary !== undefined) {
let { links } = await getInfoFromPullRequest({
repo: options.repo,
pull: prFromSummary,
})

if (commitFromSummary) {
links = {
...links,
commit: `[\`${commitFromSummary}\`](https://github.com/${options.repo}/commit/${commitFromSummary})`,
}
}

let versionInfo =
links.pull === null ? changeset.commit : links.pull
return links
}

let summary = `- ${firstLine} (${versionInfo})`
let commitToFetchFrom = commitFromSummary || changeset.commit
if (commitToFetchFrom) {
let { links } = await getInfo({
repo: options.repo,
commit: commitToFetchFrom,
})
return links
}

return `${summary}\n${futureLines.map((l) => ` ${l}`).join('\n')}`
}
return {
commit: null,
pull: null,
user: null,
}
})()
let users =
usersFromSummary.length > 0
? usersFromSummary
.map(
(userFromSummary) =>
`[@${userFromSummary}](https://github.com/${userFromSummary})`,
)
.join(', ')
: links.user.includes(repoOwner) && links.user
let userThanks = users === null ? '' : `\n Thanks ${users}!`
let versionInfo =
links.pull === null ? ` ${links.commit}` : ` (${links.pull})`

return `\n\n- ${firstLine}\n${futureLines
return `\n\n- ${firstLine}${versionInfo}${userThanks}\n${futureLines
.map((l) => ` ${l}`)
.join('\n')}`
},
Expand Down
2 changes: 1 addition & 1 deletion packages/changesets-changelog/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zazen/changesets-changelog",
"version": "6.4.0",
"version": "1.0.0",
"description": "Generate changelogs, free of weariness and confusion",
"keywords": [
"changelog",
Expand Down

0 comments on commit eefce14

Please sign in to comment.