Skip to content

Commit

Permalink
♻️ extract parseCommit logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bpetetot committed Jan 14, 2020
1 parent 0259c5b commit e445ede
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 75 deletions.
52 changes: 32 additions & 20 deletions packages/gitmoji-changelog-core/src/fromGitFile.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
const gitRawCommits = require("git-raw-commits");
const splitLines = require("split-lines");
const { promisify } = require("util");
const gitSemverTags = require("git-semver-tags");
const through = require("through2");
const concat = require("concat-stream");

const gitRawCommits = require('git-raw-commits')
const COMMIT_FORMAT = "%n%H%n%an%n%cI%n%s%n%b";

const { promisify } = require('util')
const gitSemverTags = require('git-semver-tags')
const through = require('through2')
const concat = require('concat-stream')

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

const { parseCommit } = require('./parser')
function parseCommit(commit) {
const lines = splitLines(commit);
const [hash, author, date, subject, ...body] = lines.splice(
1,
lines.length - 2
);
return { hash, author, date, subject, body };
}

function getCommits(from, to) {
return new Promise((resolve) => {
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(data)
}))
})
to
})
.pipe(
through.obj((data, enc, next) => {
next(null, parseCommit(data.toString()));
})
)
.pipe(
concat(data => {
resolve(data);
})
);
});
}

const getTags = promisify(gitSemverTags)
const getTags = promisify(gitSemverTags);

module.exports = {
getTags,
getCommits,
}
getCommits
};
6 changes: 4 additions & 2 deletions packages/gitmoji-changelog-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const semverCompare = require('semver-compare')

const { isEmpty } = require('lodash')

const { getMergedGroupMapping } = require('./parser')
const { parseCommit, getMergedGroupMapping } = require('./parser')
const logger = require('./logger')
const { groupSentencesByDistance } = require('./utils')
const fromGitFileClient = require('./fromGitFile')
Expand Down Expand Up @@ -51,7 +51,9 @@ async function generateVersion(options) {
client,
} = options

let commits = filterCommits(await client.getCommits(from, to))
const rawCommits = await client.getCommits(from, to)

let commits = filterCommits(rawCommits.map(parseCommit))

if (groupSimilarCommits) {
commits = groupSentencesByDistance(commits.map(commit => commit.message))
Expand Down
9 changes: 4 additions & 5 deletions packages/gitmoji-changelog-core/src/parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const splitLines = require('split-lines')
const nodeEmoji = require('node-emoji')
const groupMapping = require('./groupMapping')
const rc = require('rc')
Expand Down Expand Up @@ -57,9 +56,9 @@ function getCommitGroup(emojiCode) {
return group.group
}

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

Expand All @@ -73,7 +72,7 @@ function parseCommit(commit) {
message,
group,
siblings: [],
body: body.join('\n'),
body: Array.isArray(body) ? body.join('\n') : body,
}
}

Expand Down
65 changes: 17 additions & 48 deletions packages/gitmoji-changelog-core/src/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,26 @@ describe('group mapping', () => {

describe('commits parser', () => {
it('should parse a single commit', () => {
const {
hash,
author,
date,
subject,
body,
} = sparklesCommit
const commit = `\n${hash}\n${author}\n${date}\n${subject}\n${body}\n`

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

it('should parse a unicode emoji', () => {
const {
hash,
author,
date,
body,
} = sparklesCommit
const commit = `\n${hash}\n${author}\n${date}\n✨ Upgrade brand new feature\n${body}\n`
const parsed = parseCommit(commit)
const parsed = parseCommit({
...sparklesCommit,
subject: '✨ Upgrade brand new feature',
})
expect(parsed.emoji).toEqual('✨')
expect(parsed.emojiCode).toEqual('sparkles')
expect(parsed.message).toEqual('Upgrade brand new feature')
})

it('should parse a single commit without a body', () => {
const {
hash,
author,
date,
subject,
} = sparklesCommit
const commit = `\n${hash}\n${author}\n${date}\n${subject}\n\n`
const parsed = parseCommit({
...sparklesCommit,
body: '',
})

expect(parseCommit(commit)).toEqual(expect.objectContaining({
expect(parseCommit(parsed)).toEqual(expect.objectContaining({
...sparklesCommit,
body: '',
}))
Expand All @@ -69,26 +53,19 @@ describe('commits parser', () => {
author,
date,
} = sparklesCommit
const commit = `\n${hash}\n${author}\n${date}\n\n`

expect(parseCommit(commit)).toEqual(expect.objectContaining({
expect(parseCommit({
hash,
author,
date,
})).toEqual(expect.objectContaining({
...sparklesCommit,
subject: '',
body: '',
}))
})

it('should add the group to a commit', () => {
const {
hash,
author,
date,
subject,
body,
} = sparklesCommit
const commit = `\n${hash}\n${author}\n${date}\n${subject}\n${body}\n`

expect(parseCommit(commit)).toEqual(expect.objectContaining({ group: 'added' }))
expect(parseCommit(sparklesCommit)).toEqual(expect.objectContaining({ group: 'added' }))
})

it('should handle custom commit mapping', () => {
Expand All @@ -99,16 +76,8 @@ describe('commits parser', () => {
],
}
rc.mockImplementation(() => customConfiguration)
const {
hash,
author,
date,
subject,
body,
} = sparklesCommit
const commit = `\n${hash}\n${author}\n${date}\n${subject}\n${body}\n`

expect(parseCommit(commit)).toEqual(expect.objectContaining({ group: 'custom' }))
expect(parseCommit(sparklesCommit)).toEqual(expect.objectContaining({ group: 'custom' }))
})
})

Expand Down

0 comments on commit e445ede

Please sign in to comment.