From e445ededcbef13bf4e4cf6164bd040c5a3aa68ac Mon Sep 17 00:00:00 2001 From: Benjamin Petetot Date: Tue, 14 Jan 2020 16:51:31 +0100 Subject: [PATCH] :recycle: extract parseCommit logic --- .../gitmoji-changelog-core/src/fromGitFile.js | 52 +++++++++------ packages/gitmoji-changelog-core/src/index.js | 6 +- packages/gitmoji-changelog-core/src/parser.js | 9 ++- .../gitmoji-changelog-core/src/parser.spec.js | 65 +++++-------------- 4 files changed, 57 insertions(+), 75 deletions(-) diff --git a/packages/gitmoji-changelog-core/src/fromGitFile.js b/packages/gitmoji-changelog-core/src/fromGitFile.js index 7df2468..efc1e69 100644 --- a/packages/gitmoji-changelog-core/src/fromGitFile.js +++ b/packages/gitmoji-changelog-core/src/fromGitFile.js @@ -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, -} \ No newline at end of file + getCommits +}; diff --git a/packages/gitmoji-changelog-core/src/index.js b/packages/gitmoji-changelog-core/src/index.js index aef4f17..38ec6eb 100644 --- a/packages/gitmoji-changelog-core/src/index.js +++ b/packages/gitmoji-changelog-core/src/index.js @@ -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') @@ -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)) diff --git a/packages/gitmoji-changelog-core/src/parser.js b/packages/gitmoji-changelog-core/src/parser.js index 1fe3dee..48b3971 100644 --- a/packages/gitmoji-changelog-core/src/parser.js +++ b/packages/gitmoji-changelog-core/src/parser.js @@ -1,4 +1,3 @@ -const splitLines = require('split-lines') const nodeEmoji = require('node-emoji') const groupMapping = require('./groupMapping') const rc = require('rc') @@ -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) @@ -73,7 +72,7 @@ function parseCommit(commit) { message, group, siblings: [], - body: body.join('\n'), + body: Array.isArray(body) ? body.join('\n') : body, } } diff --git a/packages/gitmoji-changelog-core/src/parser.spec.js b/packages/gitmoji-changelog-core/src/parser.spec.js index 4c08d77..ec7d4af 100644 --- a/packages/gitmoji-changelog-core/src/parser.spec.js +++ b/packages/gitmoji-changelog-core/src/parser.spec.js @@ -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: '', })) @@ -69,9 +53,11 @@ 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: '', @@ -79,16 +65,7 @@ describe('commits parser', () => { }) 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', () => { @@ -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' })) }) })