Skip to content

Commit

Permalink
✨ Generate changelog for the next release (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
frinyvonnick authored Sep 17, 2018
1 parent c177349 commit 2d783e4
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 34 deletions.
6 changes: 3 additions & 3 deletions packages/gitmoji-changelog-cli/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { changelog } = require('@gitmoji-changelog/core')
const { generateChangelog } = require('@gitmoji-changelog/core')
const { convert } = require('@gitmoji-changelog/markdown')
const fs = require('fs')

async function main({ format } = {}) {
try {
const changes = await changelog()
const changes = await generateChangelog()

switch (format) {
case 'json':
fs.writeFileSync('./CHANGELOG.json', changes)
fs.writeFileSync('./CHANGELOG.json', JSON.stringify(changes))
break
case 'markdown':
default: {
Expand Down
5 changes: 3 additions & 2 deletions packages/gitmoji-changelog-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "0.0.1",
"description": "Core tool that transform raw commits into a nice json structure",
"main": "src/index.js",
"engines" : {
"node" : ">=10"
"engines": {
"node": ">=10"
},
"repository": {
"type": "git",
Expand All @@ -19,6 +19,7 @@
"dependencies": {
"concat-stream": "^1.6.2",
"git-raw-commits": "^2.0.0",
"git-semver-tags": "^2.0.0",
"split-lines": "^2.0.0",
"through2": "^2.0.3"
}
Expand Down
31 changes: 28 additions & 3 deletions packages/gitmoji-changelog-core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
const gitRawCommits = require('git-raw-commits')
const gitSemverTags = require('git-semver-tags')
const through = require('through2')
const concat = require('concat-stream')
const { promisify } = require('util')

const { parseCommit } = require('./parser')

const gitSemverTagsAsync = promisify(gitSemverTags)

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

function changelog() {
function getCommits(from, to) {
return new Promise((resolve) => {
gitRawCommits({
format: COMMIT_FORMAT,
from,
to,
}).pipe(through.obj((data, enc, next) => {
next(null, parseCommit(data.toString()))
})).pipe(concat(data => {
resolve(JSON.stringify(data))
resolve(data)
}))
})
}

async function generateChangelog() {
let previousTag = ''
const result = {}
const tags = await gitSemverTagsAsync()

if (tags.length > 0) {
await Promise.all(tags.map(async tag => {
const commits = await getCommits(previousTag, tag)

result[tag] = { commits }
previousTag = tag
}))
}

result.next = { commits: await getCommits(previousTag) }

return result
}

module.exports = {
changelog,
generateChangelog,
}
74 changes: 62 additions & 12 deletions packages/gitmoji-changelog-core/src/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
/* eslint-disable global-require */
const gitRawCommits = require('git-raw-commits')
const gitSemverTags = require('git-semver-tags')

const { changelog } = require('./index')
const { generateChangelog } = require('./index')

describe('changelog', () => {
beforeAll(() => {
gitRawCommits.mockImplementation(() => {
beforeEach(() => {
gitRawCommits.mockImplementationOnce(() => {
const stream = require('stream')
const readable = new stream.Readable()
readable.push(`
c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c
2018-08-28T10:07:00+02:00
:sparkles: Upgrade brand new feature
Waouh this is awesome 3
`)
readable.push(null)
readable.emit('close')
return readable
})
gitRawCommits.mockImplementationOnce(() => {
const stream = require('stream')
const readable = new stream.Readable()
readable.push(`
Expand All @@ -20,18 +34,54 @@ Waouh this is awesome 2
})
})

it('should generate changelog in json format', async () => {
const result = await changelog()
it('should generate changelog in json format for next release', async () => {
gitSemverTags.mockImplementation((cb) => cb(null, []))

expect(result).toEqual(JSON.stringify([
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 2',
const result = await generateChangelog()

expect(result).toEqual({
next: {
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
date: '2018-08-28T10:07:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 3',
},
],
},
]))
})
})

it('should generate changelog in json format for all tags', async () => {
gitSemverTags.mockImplementation((cb) => cb(null, ['v1.0.0']))

const result = await generateChangelog()

expect(result).toEqual({
next: {
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
date: '2018-08-28T10:07:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 3',
},
],
},
'v1.0.0': {
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 2',
},
],
},
})
})
})

jest.mock('git-raw-commits')
jest.mock('git-semver-tags')
12 changes: 7 additions & 5 deletions packages/gitmoji-changelog-markdown/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
function convert(json) {
const commits = JSON.parse(json)
function convert(changelog) {
const versions = Object.keys(changelog)

return commits.reduce((markdown, commit) => {
return `${markdown}- ${commit.subject} (${commit.hash})\n`
}, '# Changelog\n\n')
return versions.reduce((markdown, version) => {
return changelog[version].commits.reduce((commitMarkdown, commit) => {
return `${commitMarkdown}- ${commit.subject} (${commit.hash})\n`
}, `${markdown}\n## ${version}\n\n`)
}, '# Changelog\n')
}

module.exports = {
Expand Down
38 changes: 29 additions & 9 deletions packages/gitmoji-changelog-markdown/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@ const { convert } = require('./index')

describe('Markdown converter', () => {
it('should convert json changelog into markdown', () => {
const json = [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 2',
const json = {
next: {
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c',
date: '2018-08-28T10:07:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 3',
},
],
},
]
'v1.0.0': {
commits: [
{
hash: 'c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f',
date: '2018-08-28T10:06:00+02:00',
subject: ':sparkles: Upgrade brand new feature',
body: 'Waouh this is awesome 2',
},
],
},
}

expect(convert(json)).toEqual(`# Changelog
## next
- :sparkles: Upgrade brand new feature (c40ee8669ba7ea5151adc2942fa8a7fc98d9e23c)
expect(convert(JSON.stringify(json))).toEqual(`# Changelog
## v1.0.0
- ${json[0].subject} (${json[0].hash})
- :sparkles: Upgrade brand new feature (c40ee8669ba7ea5151adc2942fa8a7fc98d9e23f)
`)
})
})
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,13 @@ git-semver-tags@^1.3.0, git-semver-tags@^1.3.6:
meow "^4.0.0"
semver "^5.5.0"

git-semver-tags@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/git-semver-tags/-/git-semver-tags-2.0.0.tgz#c218fd895bdf8e8e02f6bde555b2c3893ac73cd7"
dependencies:
meow "^4.0.0"
semver "^5.5.0"

gitconfiglocal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/gitconfiglocal/-/gitconfiglocal-1.0.0.tgz#41d045f3851a5ea88f03f24ca1c6178114464b9b"
Expand Down

0 comments on commit 2d783e4

Please sign in to comment.