Skip to content

Commit b6868c0

Browse files
committed
Improve(Conventional Changelog Config): Improve commit subject and templates
1 parent b6da9b7 commit b6868c0

File tree

4 files changed

+29
-88
lines changed

4 files changed

+29
-88
lines changed

packages/conventional-changelog-config/src/templates/commit.hbs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{{~/if}}
66

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

2525
{{~!-- commit references --}}
2626
{{~#if references~}}
27-
28-
{{~#each references}} {{#if @root.linkReferences~}}
27+
<sub><sup>{{~#each references}} {{#if @root.linkReferences~}}
2928
[
3029
{{~#if this.owner}}
3130
{{~this.owner}}/
@@ -55,6 +54,6 @@
5554
{{~this.owner}}/
5655
{{~/if}}
5756
{{~this.repository}}#{{this.issue}}
58-
{{~/if}}{{/each}}
57+
{{~/if}}{{/each}}</sup></sub>
5958
{{~/if}}
6059

packages/conventional-changelog-config/src/templates/template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{{#each scopes}}
66
{{#if title}}
77

8-
##### {{title}}
8+
###### {{title}}
99
{{/if}}
1010
{{#each commits}}
1111
{{> commit root=@root}}

packages/conventional-changelog-config/src/writer-opts.ts

Lines changed: 21 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -42,88 +42,30 @@ module.exports = {
4242
}
4343

4444
if (typeof commit.subject === 'string') {
45-
const url = context.repository
46-
? `${context.host}/${context.owner}/${context.repository}`
47-
: context.repoUrl
48-
if (url) {
49-
// Issue URLs.
50-
const issuesUrl = `${url}/issues/`
51-
52-
commit.subject = commit.subject.replace(/#([0-9]+)/g, (_, issue) => {
53-
issues.push(issue)
54-
55-
return `[#${issue}](${issuesUrl}${issue})`
56-
})
57-
58-
if (context.host) {
59-
// User URLs.
60-
commit.subject = commit.subject.replace(/(?<!['`])@([a-z0-9](?:-?[a-z0-9]){0,38})(?<!['])/gi, (_, username) => {
61-
if (username.includes('/')) {
62-
return `@${username}`
45+
if (process.env.NODE_ENV !== 'test') {
46+
// get author by commit hash
47+
try {
48+
const response = await new Promise<string>((resolve) => {
49+
const url = `https://api.github.com/repos/${context.owner}/${context.repository}/commits/${commit.hash}`
50+
const headers = {
51+
'User-Agent': context.owner
6352
}
64-
65-
return `[@${username}](${context.host}/${username})`
66-
})
67-
}
68-
69-
if (process.env.NODE_ENV !== 'test') {
70-
// get issuer by #issue
71-
for (const eachIssue of issues) {
72-
try {
73-
const response = await new Promise<string>((resolve) => {
74-
const url = `https://api.github.com/repos/${context.owner}/${context.repository}/issues/${eachIssue}`
75-
const headers = {
76-
'User-Agent': context.owner
77-
}
78-
if (process.env.GITHUB_TOKEN) {
79-
headers['Authorization'] = `token ${process.env.GITHUB_TOKEN}`
80-
}
81-
https.get(url, { headers },
82-
response => {
83-
let data = ''
84-
response.on('data', (chunk) => data += chunk)
85-
response.on('end', async () => {
86-
resolve(data)
87-
})
88-
}
89-
)
90-
})
91-
const user = JSON.parse(response).user
92-
if (user) {
93-
commit.subject += ` [@${user.login}](${user.html_url})`
94-
}
95-
} catch (error) {
96-
console.log(new Error(`Can't get issuer by #${eachIssue}`, { cause: error }))
53+
if (process.env.GITHUB_TOKEN) {
54+
headers['Authorization'] = `token ${process.env.GITHUB_TOKEN}`
9755
}
98-
}
99-
100-
// get author by commit hash
101-
try {
102-
const response = await new Promise<string>((resolve) => {
103-
const url = `https://api.github.com/repos/${context.owner}/${context.repository}/commits/${commit.hash}`
104-
const headers = {
105-
'User-Agent': context.owner
106-
}
107-
if (process.env.GITHUB_TOKEN) {
108-
headers['Authorization'] = `token ${process.env.GITHUB_TOKEN}`
56+
https.get(url, { headers },
57+
response => {
58+
let data = ''
59+
response.on('data', (chunk) => data += chunk)
60+
response.on('end', async () => {
61+
resolve(data)
62+
})
10963
}
110-
https.get(url, { headers },
111-
response => {
112-
let data = ''
113-
response.on('data', (chunk) => data += chunk)
114-
response.on('end', async () => {
115-
resolve(data)
116-
})
117-
}
118-
)
119-
})
120-
const author = JSON.parse(response).author
121-
if (author) {
122-
commit.subject += ` [@${author.login}](${author.html_url})`
123-
}
124-
} catch (error) {
125-
console.log(new Error(`Can't get author by commit hash ${commit.hash}`, { cause: error }))
126-
}
64+
)
65+
})
66+
commit.author = JSON.parse(response).author
67+
} catch (error) {
68+
console.log(new Error(`Can't get author by commit hash ${commit.hash}`, { cause: error }))
12769
}
12870
}
12971
}

packages/conventional-changelog-config/tests/test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ if (process.platform === 'win32') {
6262
expect(chunk).toMatch('Compiler')
6363
expect(chunk).toMatch('Avoid a bug')
6464
expect(chunk).toMatch('Make it faster')
65-
expect(chunk).toMatch('[#1](https://github.com/conventional-changelog/conventional-changelog/issues/1) [#2](https://github.com/conventional-changelog/conventional-changelog/issues/2)')
65+
expect(chunk).toMatch('#1 #2')
6666
expect(chunk).toMatch('New Features')
6767
expect(chunk).toMatch('Bug Fixes')
6868
expect(chunk).toMatch('Performance Upgrades')
@@ -78,11 +78,11 @@ if (process.platform === 'win32') {
7878
expect(chunk).not.toMatch('*** - **')
7979
expect(chunk).not.toMatch(': Not backward compatible.')
8080
// should replace #[0-9]+ with GitHub issue URL
81-
expect(chunk).toMatch('[#133](https://github.com/conventional-changelog/conventional-changelog/issues/133)')
81+
expect(chunk).toMatch('#133')
8282
// should remove the issues that already appear in the subject
83-
expect(chunk).toMatch('[#88](https://github.com/conventional-changelog/conventional-changelog/issues/88)')
83+
expect(chunk).toMatch('#88')
8484
// should replace @username with GitHub user URL
85-
expect(chunk).toMatch('[@1aron](https://github.com/1aron)')
85+
expect(chunk).toMatch('@1aron')
8686
}
8787
}, 10000)
8888

0 commit comments

Comments
 (0)