Skip to content

Commit

Permalink
✨ Add the author in changelog lines (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot authored Oct 30, 2018
1 parent 7858140 commit 979da30
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/gitmoji-changelog-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ yargs

.option('format', { default: 'markdown', desc: 'changelog format (markdown, json)' })
.option('output', { desc: 'output changelog file' })
.option('author', { default: false, desc: 'add the author in changelog lines' })

.help('help')
.epilog(`For more information visit: ${homepage}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/gitmoji-changelog-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const logger = require('./logger')

const gitSemverTagsAsync = promisify(gitSemverTags)

const COMMIT_FORMAT = '%n%H%n%cI%n%s%n%b'
const COMMIT_FORMAT = '%n%H%n%an%n%cI%n%s%n%b'

function getCommits(from, to) {
return new Promise((resolve) => {
Expand Down
9 changes: 7 additions & 2 deletions packages/gitmoji-changelog-core/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const uselessCommit = {

const sparklesCommit = {
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23a',
author: 'John Doe',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 2',
Expand All @@ -28,6 +29,7 @@ const sparklesCommit = {

const recycleCommit = {
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
author: 'John Doe',
date: '2018-08-01T10:07:00+02:00',
subject: ':recycle: Upgrade brand new feature',
body: 'Waouh this is awesome 3',
Expand All @@ -39,6 +41,7 @@ const recycleCommit = {

const secondRecycleCommit = {
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23d',
author: 'John Doe',
date: '2018-08-30T10:07:00+02:00',
subject: ':recycle: Upgrade another brand new feature',
body: 'Waouh this is awesome 4',
Expand All @@ -50,6 +53,7 @@ const secondRecycleCommit = {

const lipstickCommit = {
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23e',
author: 'John Doe',
date: '2018-08-10T10:07:00+02:00',
subject: ':lipstick: Change graphics for a feature',
body: 'Waouh this is awesome 5',
Expand All @@ -61,6 +65,7 @@ const lipstickCommit = {

const secondLipstickCommit = {
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
author: 'John Doe',
date: '2018-08-18T10:07:00+02:00',
subject: ':lipstick: Change more graphics for a feature',
body: 'Waouh this is awesome 6',
Expand Down Expand Up @@ -197,9 +202,9 @@ function mockGroup(commits) {
const readable = new stream.Readable()
commits.forEach(commit => {
const {
hash, date, subject, body,
hash, author, date, subject, body,
} = commit
readable.push(`\n${hash}\n${date}\n${subject}\n${body}\n`)
readable.push(`\n${hash}\n${author}\n${date}\n${subject}\n${body}\n`)
})
readable.push(null)
readable.emit('close')
Expand Down
3 changes: 2 additions & 1 deletion packages/gitmoji-changelog-core/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ function getCommitGroup(emojiCode) {

function parseCommit(commit) {
const lines = splitLines(commit)
const [hash, date, subject, ...body] = lines.splice(1, lines.length - 2)
const [hash, author, date, subject, ...body] = lines.splice(1, lines.length - 2)
const { emoji, emojiCode, message } = parseSubject(subject)
const group = getCommitGroup(emojiCode)

return {
hash,
author,
date,
subject,
emojiCode,
Expand Down
16 changes: 11 additions & 5 deletions packages/gitmoji-changelog-core/src/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { parseCommit } = require('./parser.js')

const sparklesCommit = {
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
author: 'John Doe',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 2',
Expand All @@ -12,22 +13,24 @@ describe('commits parser', () => {
it('should parse a single commit', () => {
const {
hash,
author,
date,
subject,
body,
} = sparklesCommit
const commit = `\n${hash}\n${date}\n${subject}\n${body}\n`
const commit = `\n${hash}\n${author}\n${date}\n${subject}\n${body}\n`

expect(parseCommit(commit)).toEqual(expect.objectContaining(sparklesCommit))
})

it('should parse a unicode emoji', () => {
const {
hash,
author,
date,
body,
} = sparklesCommit
const commit = `\n${hash}\n${date}\n✨ Upgrade brand new feature\n${body}\n`
const commit = `\n${hash}\n${author}\n${date}\n✨ Upgrade brand new feature\n${body}\n`
const parsed = parseCommit(commit)
expect(parsed.emoji).toEqual('✨')
expect(parsed.emojiCode).toEqual('sparkles')
Expand All @@ -37,10 +40,11 @@ describe('commits parser', () => {
it('should parse a single commit without a body', () => {
const {
hash,
author,
date,
subject,
} = sparklesCommit
const commit = `\n${hash}\n${date}\n${subject}\n\n`
const commit = `\n${hash}\n${author}\n${date}\n${subject}\n\n`

expect(parseCommit(commit)).toEqual(expect.objectContaining({
...sparklesCommit,
Expand All @@ -51,9 +55,10 @@ describe('commits parser', () => {
it('should parse a single commit without a subject', () => {
const {
hash,
author,
date,
} = sparklesCommit
const commit = `\n${hash}\n${date}\n\n`
const commit = `\n${hash}\n${author}\n${date}\n\n`

expect(parseCommit(commit)).toEqual(expect.objectContaining({
...sparklesCommit,
Expand All @@ -65,11 +70,12 @@ describe('commits parser', () => {
it('should add the group to a commit', () => {
const {
hash,
author,
date,
subject,
body,
} = sparklesCommit
const commit = `\n${hash}\n${date}\n${subject}\n${body}\n`
const commit = `\n${hash}\n${author}\n${date}\n${subject}\n${body}\n`

expect(parseCommit(commit)).toEqual(expect.objectContaining({ group: 'added' }))
})
Expand Down
7 changes: 4 additions & 3 deletions packages/gitmoji-changelog-markdown/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function buildMarkdownFile(changelog = {}, options = {}) {
return markdownIncremental(changelog, options)
}

function toMarkdown({ meta, changes }) {
function toMarkdown({ meta, changes }, { author }) {
const template = fs.readFileSync(MARKDOWN_TEMPLATE, 'utf-8')

const compileTemplate = handlebars.compile(template)
Expand All @@ -26,13 +26,14 @@ function toMarkdown({ meta, changes }) {
subject: autolink(commit.subject, meta.repository),
message: autolink(commit.message, meta.repository),
body: autolink(commit.body, meta.repository),
author: author ? commit.author : null,
}))

return compileTemplate({ changelog })
}

function markdownFromScratch({ meta, changes }, options) {
return promisify(fs.writeFile)(options.output, `# Changelog\n\n${toMarkdown({ meta, changes })}`)
return promisify(fs.writeFile)(options.output, `# Changelog\n\n${toMarkdown({ meta, changes }, options)}`)
}

function markdownIncremental({ meta, changes }, options) {
Expand Down Expand Up @@ -70,7 +71,7 @@ function markdownIncremental({ meta, changes }, options) {
// Rewrite the release (next version)
if (previousVersionFound && !nextVersionWritten) {
nextVersionWritten = true
return `${content}${toMarkdown({ meta, changes })}${nextLine}\n`
return `${content}${toMarkdown({ meta, changes }, options)}${nextLine}\n`
}

// Just push the line without changing anything
Expand Down
64 changes: 61 additions & 3 deletions packages/gitmoji-changelog-markdown/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('Markdown converter', () => {
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
author: 'John Doe',
date: '2018-08-28T10:07:00+02:00',
subject: ':recycle: Upgrade brand new feature',
emoji: '♻️',
Expand All @@ -54,6 +55,7 @@ describe('Markdown converter', () => {
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
author: 'John Doe',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
emoji: '✨',
Expand All @@ -78,15 +80,70 @@ describe('Markdown converter', () => {
### Changed
- ♻️ Upgrade brand new feature ([c40ee86](https://github.com/frinyvonnick/gitmoji-changelog/commit/c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c))
- ♻️ Upgrade brand new feature [[c40ee86](https://github.com/frinyvonnick/gitmoji-changelog/commit/c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c)]
<a name="1.0.0"></a>
## 1.0.0 (2018-08-28)
### Added
- ✨ Upgrade brand new feature ([c40ee86](https://github.com/frinyvonnick/gitmoji-changelog/commit/c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f))
- ✨ Upgrade brand new feature [[c40ee86](https://github.com/frinyvonnick/gitmoji-changelog/commit/c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f)]
`)
})

it('should generate full changelog into markdown from scratch with author', async () => {
fs.writeFile = jest.fn((path, content, cb) => cb(null, 'done'))

const changelog = {
meta: {
repository: {
type: 'github',
domain: 'github.com',
user: 'frinyvonnick',
project: 'gitmoji-changelog',
url: 'https://github.com/frinyvonnick/gitmoji-changelog',
bugsUrl: 'https://github.com/frinyvonnick/gitmoji-changelog/issues',
},
},
changes: [
{
version: 'next',
groups: [
{
group: 'changed',
label: 'Changed',
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
author: 'John Doe',
date: '2018-08-28T10:07:00+02:00',
subject: ':recycle: Upgrade brand new feature',
emoji: '♻️',
message: 'Upgrade brand new feature',
body: 'Waouh this is awesome 3',
},
],
},
],
},
],
}

await buildMarkdownFile(changelog, { mode: 'init', output: './CHANGELOG.md', author: true })

expect(fs.writeFile).toHaveBeenCalledTimes(1)
expect(fs.writeFile.mock.calls[0][0]).toBe('./CHANGELOG.md')
expect(fs.writeFile.mock.calls[0][1]).toEqual(`# Changelog
<a name="next"></a>
## next
### Changed
- ♻️ Upgrade brand new feature [[c40ee86](https://github.com/frinyvonnick/gitmoji-changelog/commit/c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c)] (by John Doe)
`)
Expand Down Expand Up @@ -145,6 +202,7 @@ I am the last version
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
author: 'John Doe',
date: '2018-08-28T10:07:00+02:00',
subject: ':recycle: Upgrade brand new feature',
emoji: '♻️',
Expand All @@ -168,7 +226,7 @@ I am the last version
### Changed
- ♻️ Upgrade brand new feature ([c40ee86](https://github.com/frinyvonnick/gitmoji-changelog/commit/c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c))
- ♻️ Upgrade brand new feature [[c40ee86](https://github.com/frinyvonnick/gitmoji-changelog/commit/c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c)]
<a name="1.0.0"></a>
Expand Down
2 changes: 1 addition & 1 deletion packages/gitmoji-changelog-markdown/src/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
### {{label}}

{{#each commits}}
- {{emoji}} {{message}} ({{hash}})
- {{emoji}} {{message}} [{{hash}}]{{#if author}} (by {{author}}){{/if}}
{{/each}}

{{/each}}
Expand Down

0 comments on commit 979da30

Please sign in to comment.