Skip to content

Commit

Permalink
Improve(Conventional Changelog Config): Improve commit subject and te…
Browse files Browse the repository at this point in the history
…mplates
  • Loading branch information
1aron committed Jan 27, 2024
1 parent b6da9b7 commit b6868c0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{~/if}}

{{~!-- commit link --}} {{#if @root.linkReferences~}}
<sub><sup>[{{shortHash}}](
<sub><sup>[@{{author.login}}]({{author.html_url}}) [{{shortHash}}](
{{~#if @root.repository}}
{{~#if @root.host}}
{{~@root.host}}/
Expand All @@ -24,8 +24,7 @@

{{~!-- commit references --}}
{{~#if references~}}

{{~#each references}} {{#if @root.linkReferences~}}
<sub><sup>{{~#each references}} {{#if @root.linkReferences~}}
[
{{~#if this.owner}}
{{~this.owner}}/
Expand Down Expand Up @@ -55,6 +54,6 @@
{{~this.owner}}/
{{~/if}}
{{~this.repository}}#{{this.issue}}
{{~/if}}{{/each}}
{{~/if}}{{/each}}</sup></sub>
{{~/if}}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{#each scopes}}
{{#if title}}

##### {{title}}
###### {{title}}
{{/if}}
{{#each commits}}
{{> commit root=@root}}
Expand Down
100 changes: 21 additions & 79 deletions packages/conventional-changelog-config/src/writer-opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,88 +42,30 @@ module.exports = {
}

if (typeof commit.subject === 'string') {
const url = context.repository
? `${context.host}/${context.owner}/${context.repository}`
: context.repoUrl
if (url) {
// Issue URLs.
const issuesUrl = `${url}/issues/`

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) => {
if (username.includes('/')) {
return `@${username}`
if (process.env.NODE_ENV !== 'test') {
// get author by commit hash
try {
const response = await new Promise<string>((resolve) => {
const url = `https://api.github.com/repos/${context.owner}/${context.repository}/commits/${commit.hash}`
const headers = {
'User-Agent': context.owner
}

return `[@${username}](${context.host}/${username})`
})
}

if (process.env.NODE_ENV !== 'test') {
// get issuer by #issue
for (const eachIssue of issues) {
try {
const response = await new Promise<string>((resolve) => {
const url = `https://api.github.com/repos/${context.owner}/${context.repository}/issues/${eachIssue}`
const headers = {
'User-Agent': context.owner
}
if (process.env.GITHUB_TOKEN) {
headers['Authorization'] = `token ${process.env.GITHUB_TOKEN}`
}
https.get(url, { headers },
response => {
let data = ''
response.on('data', (chunk) => data += chunk)
response.on('end', async () => {
resolve(data)
})
}
)
})
const user = JSON.parse(response).user
if (user) {
commit.subject += ` [@${user.login}](${user.html_url})`
}
} catch (error) {
console.log(new Error(`Can't get issuer by #${eachIssue}`, { cause: error }))
if (process.env.GITHUB_TOKEN) {
headers['Authorization'] = `token ${process.env.GITHUB_TOKEN}`
}
}

// get author by commit hash
try {
const response = await new Promise<string>((resolve) => {
const url = `https://api.github.com/repos/${context.owner}/${context.repository}/commits/${commit.hash}`
const headers = {
'User-Agent': context.owner
}
if (process.env.GITHUB_TOKEN) {
headers['Authorization'] = `token ${process.env.GITHUB_TOKEN}`
https.get(url, { headers },
response => {
let data = ''
response.on('data', (chunk) => data += chunk)
response.on('end', async () => {
resolve(data)
})
}
https.get(url, { headers },
response => {
let data = ''
response.on('data', (chunk) => data += chunk)
response.on('end', async () => {
resolve(data)
})
}
)
})
const author = JSON.parse(response).author
if (author) {
commit.subject += ` [@${author.login}](${author.html_url})`
}
} catch (error) {
console.log(new Error(`Can't get author by commit hash ${commit.hash}`, { cause: error }))
}
)
})
commit.author = JSON.parse(response).author
} catch (error) {
console.log(new Error(`Can't get author by commit hash ${commit.hash}`, { cause: error }))
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/conventional-changelog-config/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if (process.platform === 'win32') {
expect(chunk).toMatch('Compiler')
expect(chunk).toMatch('Avoid a bug')
expect(chunk).toMatch('Make it faster')
expect(chunk).toMatch('[#1](https://github.com/conventional-changelog/conventional-changelog/issues/1) [#2](https://github.com/conventional-changelog/conventional-changelog/issues/2)')
expect(chunk).toMatch('#1 #2')
expect(chunk).toMatch('New Features')
expect(chunk).toMatch('Bug Fixes')
expect(chunk).toMatch('Performance Upgrades')
Expand All @@ -78,11 +78,11 @@ if (process.platform === 'win32') {
expect(chunk).not.toMatch('*** - **')
expect(chunk).not.toMatch(': Not backward compatible.')
// should replace #[0-9]+ with GitHub issue URL
expect(chunk).toMatch('[#133](https://github.com/conventional-changelog/conventional-changelog/issues/133)')
expect(chunk).toMatch('#133')
// should remove the issues that already appear in the subject
expect(chunk).toMatch('[#88](https://github.com/conventional-changelog/conventional-changelog/issues/88)')
expect(chunk).toMatch('#88')
// should replace @username with GitHub user URL
expect(chunk).toMatch('[@1aron](https://github.com/1aron)')
expect(chunk).toMatch('@1aron')
}
}, 10000)

Expand Down

0 comments on commit b6868c0

Please sign in to comment.