Skip to content

Commit

Permalink
New(Conventional Changelog Config): Add commit author
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Jan 27, 2024
1 parent 80da403 commit 0735bf5
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

.env

# compiled output
/dist
/tmp
Expand Down
1 change: 0 additions & 1 deletion packages/conventional-changelog-config/caches.json

This file was deleted.

1 change: 1 addition & 0 deletions packages/conventional-changelog-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"devDependencies": {
"conventional-changelog-core": "^7.0.0",
"dotenv": "^16.4.1",
"semantic-release": "^23.0.0"
}
}
70 changes: 55 additions & 15 deletions packages/conventional-changelog-config/src/writer-opts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,64 @@ module.exports = {
})
}

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))
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 }))
}
}

// get author by commit hash
try {
const username = JSON.parse(response).user?.login
if (username) {
commit.subject += ` [@${username}](${context.host}/${username})`
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)
})
}
)
})
const author = JSON.parse(response).author
if (author) {
commit.subject += ` [@${author.login}](${context.html_url})`
}
} catch { /* empty */ }
} catch (error) {
console.log(new Error(`Can't get author by commit hash ${commit.hash}`, { cause: error }))
}
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions packages/conventional-changelog-config/tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import path, { resolve } from 'path'
import exec from '../../../utils/exec'
import commit from '../../../utils/commit'
import initFakeGit from '../../../utils/init-fake-git'
import dotenv from 'dotenv'

const createPreset = require('../dist')
const conventionalChangelogCore = require('conventional-changelog-core')

dotenv.config({ path: path.resolve(__dirname, '../../../.env') })

if (!fs.existsSync(path.join(__dirname, './dist'))) {
fs.mkdirSync(path.join(__dirname, './dist'))
}
Expand Down Expand Up @@ -54,8 +57,7 @@ if (process.platform === 'win32') {
cwd: process.cwd()
})) {
chunk = chunk.toString()
writeFile(resolve(__dirname, './dist/CHANGELOG-1.md'), chunk, () => { })
expect(chunk).toMatch('[@hparra](https://github.com/hparra)')
writeFileSync(resolve(__dirname, './dist/CHANGELOG-1.md'), chunk)
expect(chunk).toMatch('Amazing new module')
expect(chunk).toMatch('Compiler')
expect(chunk).toMatch('Avoid a bug')
Expand Down Expand Up @@ -91,7 +93,7 @@ if (process.platform === 'win32') {
cwd: process.cwd()
})) {
chunk = chunk.toString()
writeFile(resolve(__dirname, './dist/CHANGELOG-2.md'), chunk, () => { })
writeFileSync(resolve(__dirname, './dist/CHANGELOG-2.md'), chunk)
expect(chunk).toMatch('`it` alias for `test`')
// not to include provious changes
expect(chunk).not.toMatch('Breaking Changes')
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0735bf5

Please sign in to comment.