From b6868c09eeba3f2b90eee5888254ccee1a56def4 Mon Sep 17 00:00:00 2001 From: 1aron Date: Sun, 28 Jan 2024 03:49:08 +0800 Subject: [PATCH] Improve(Conventional Changelog Config): Improve commit subject and templates --- .../src/templates/commit.hbs | 7 +- .../src/templates/template.hbs | 2 +- .../src/writer-opts.ts | 100 ++++-------------- .../tests/test.ts | 8 +- 4 files changed, 29 insertions(+), 88 deletions(-) diff --git a/packages/conventional-changelog-config/src/templates/commit.hbs b/packages/conventional-changelog-config/src/templates/commit.hbs index e1e2f26..b653bcb 100644 --- a/packages/conventional-changelog-config/src/templates/commit.hbs +++ b/packages/conventional-changelog-config/src/templates/commit.hbs @@ -5,7 +5,7 @@ {{~/if}} {{~!-- commit link --}} {{#if @root.linkReferences~}} -[{{shortHash}}]( +[@{{author.login}}]({{author.html_url}}) [{{shortHash}}]( {{~#if @root.repository}} {{~#if @root.host}} {{~@root.host}}/ @@ -24,8 +24,7 @@ {{~!-- commit references --}} {{~#if references~}} - -{{~#each references}} {{#if @root.linkReferences~}} +{{~#each references}} {{#if @root.linkReferences~}} [ {{~#if this.owner}} {{~this.owner}}/ @@ -55,6 +54,6 @@ {{~this.owner}}/ {{~/if}} {{~this.repository}}#{{this.issue}} -{{~/if}}{{/each}} +{{~/if}}{{/each}} {{~/if}} diff --git a/packages/conventional-changelog-config/src/templates/template.hbs b/packages/conventional-changelog-config/src/templates/template.hbs index d177b88..bbcdcd5 100644 --- a/packages/conventional-changelog-config/src/templates/template.hbs +++ b/packages/conventional-changelog-config/src/templates/template.hbs @@ -5,7 +5,7 @@ {{#each scopes}} {{#if title}} -##### {{title}} +###### {{title}} {{/if}} {{#each commits}} {{> commit root=@root}} diff --git a/packages/conventional-changelog-config/src/writer-opts.ts b/packages/conventional-changelog-config/src/writer-opts.ts index 6e83c10..0a4d47b 100644 --- a/packages/conventional-changelog-config/src/writer-opts.ts +++ b/packages/conventional-changelog-config/src/writer-opts.ts @@ -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(/(? { - if (username.includes('/')) { - return `@${username}` + if (process.env.NODE_ENV !== 'test') { + // get author by commit hash + try { + const response = await new Promise((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((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((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 })) } } } diff --git a/packages/conventional-changelog-config/tests/test.ts b/packages/conventional-changelog-config/tests/test.ts index 9a8fd87..b420778 100644 --- a/packages/conventional-changelog-config/tests/test.ts +++ b/packages/conventional-changelog-config/tests/test.ts @@ -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') @@ -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)