Skip to content

Commit

Permalink
New(Conventional Changelog Config): Add issuers
Browse files Browse the repository at this point in the history
  • Loading branch information
BenSeage committed Jan 24, 2024
1 parent d639c75 commit f98a765
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 147 deletions.
2 changes: 1 addition & 1 deletion packages/conventional-changelog-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"dedent": "^0.7.0"
},
"devDependencies": {
"conventional-changelog-core": "^4.2.4"
"conventional-changelog-core": "^7.0.0"
}
}
43 changes: 31 additions & 12 deletions packages/conventional-changelog-config/src/writer-opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import footerPartialPath from './templates/footer.hbs'
import commitPartialPath from './templates/commit.hbs'
import { readFileSync } from 'fs'
import path from 'path'
import https from 'node:https'

const mainTemplate = readFileSync(path.resolve(__dirname, mainTemplatePath), 'utf-8')
const footerPartial = readFileSync(path.resolve(__dirname, footerPartialPath), 'utf-8')
const commitPartial = readFileSync(path.resolve(__dirname, commitPartialPath), 'utf-8')

export default {
transform: (commit, context) => {
transform: async (commit, context) => {
const issues = []
if (commit.header) {
commit.header = commit.header
Expand Down Expand Up @@ -41,17 +42,15 @@ export default {
}

if (typeof commit.subject === 'string') {
let url = context.repository
? `${context.host}/${context.owner}/${context.repository}`
: context.repoUrl
if (url) {
url = `${url}/issues/`
// Issue URLs.
commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
issues.push(issue)
return `[#${issue}](${url}${issue})`
})
}
// Issue URLs.
const issuesUrl = context.packageData.bugs.url + '/'

commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
issues.push(issue)

return `[#${issue}](${issuesUrl}${issue})`
})

if (context.host) {
// User URLs.
commit.subject = commit.subject.replace(/(?<!['`])@([a-z0-9](?:-?[a-z0-9]){0,38})(?<!['])/gi, (_, username) => {
Expand All @@ -62,6 +61,26 @@ export default {
return `[@${username}](${context.host}/${username})`
})
}

for (const eachIssue of issues) {
const response = await new Promise<string>((resolve) => {
https.get(
`https://api.github.com/repos/${context.owner}/${context.repository}/issues/${eachIssue}`,
{ headers: { 'User-Agent': context.owner } },
response => {
let data = ''
response.on('data', (chunk) => data += chunk)
response.on('end', () => resolve(data))
}
)
})
try {
const username = JSON.parse(response).user?.login
if (username) {
commit.subject += ` [@${username}](${context.host}/${username})`
}
} catch { /* empty */ }
}
}

// remove references that already appear in the subject
Expand Down
1 change: 1 addition & 0 deletions packages/conventional-changelog-config/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ if (process.platform === 'win32') {
'New starts with `New` and sentense case'
],
(changelog) => {
expect(changelog).toMatch('[@hparra](https://github.com/hparra)')
expect(changelog).toMatch('Amazing new module')
expect(changelog).toMatch('Compiler')
expect(changelog).toMatch('Avoid a bug')
Expand Down
Loading

0 comments on commit f98a765

Please sign in to comment.