Skip to content

Commit

Permalink
🐛 Fix broken extraction of commit types
Browse files Browse the repository at this point in the history
It didn't work for non-gitmoji commit conventions
  • Loading branch information
priestine committed Jul 8, 2020
1 parent 6c99d31 commit 4d308e3
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions src/pure/getters/get-changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import type { Unary } from '../../types/common-types'
import type { IRawCommit } from '../../types/raw-commit'
import type { LogFatalError } from '../../utils/logger'
import { Either, IEither } from '../../utils/either'
import { IConvention } from '../../types/convention'

interface IDeps {
execEither: Unary<string, IEither<string, Error>>
logFatalError: LogFatalError
}

type Ctx = Pick<IAppCtx, 'latestVersionCommit' | 'merges'>
type Ctx = Pick<IAppCtx, 'latestVersionCommit' | 'merges' | 'conventions'>

export const getChanges = ({ execEither, logFatalError }: IDeps) => ({
latestVersionCommit,
merges,
conventions,
}: Ctx) => ({
commitList: execEither(
'git rev-list'
Expand All @@ -27,7 +29,7 @@ export const getChanges = ({ execEither, logFatalError }: IDeps) => ({
.map(normalizeChangeString)
.map((changes) => `[ ${changes} ]`)
.chain((changes) => Either.try<IRawCommit[], Error>(() => JSON.parse(changes)))
.map((changes) => changes.map(setCommitType))
.map((changes) => changes.map(setCommitType(conventions)))
.fold(logFatalError('Could not get changes since previous release.'), (changes) =>
changes.reverse(),
),
Expand Down Expand Up @@ -56,11 +58,47 @@ const filterOutCommitHeadings = (changes: string[]) =>
const normalizeChangeString = (changes: string[]) =>
changes.map((line) => line.replace(/"/g, "'").replace(/\n/g, '').replace(/\^{3}/g, '"')).join(', ')

const setCommitType = (rawCommit: IRawCommit): IRawCommit => ({
...rawCommit,
type: Either.fromNullable(/^(:.*:)/.exec(rawCommit.description)).fold(
() => ':construction:',
(match) => match[0],
),
description: rawCommit.description.replace(/^:.*:\s+/, ''),
})
const setCommitType = (conventions: IConvention[]) => (rawCommit: IRawCommit): IRawCommit => {
return {
...rawCommit,
type: Either.fromNullable(
conventions.reduce(
(acc, convention) =>
convention.match.find(
(match) =>
new RegExp(match).test(rawCommit.description) || new RegExp(match).test(rawCommit.body),
) ?? acc,
'',
),
)
.chain((match) =>
Either.fromNullable(
new RegExp(match).exec(
new RegExp(match).test(rawCommit.description) ? rawCommit.description : rawCommit.body,
),
),
)
.fold(
() => '',
(match) => match[0].trim(),
),
description: Either.fromNullable(
conventions.reduce(
(acc, convention) =>
convention.match.find((match) => new RegExp(match).test(rawCommit.description)) ?? acc,
'',
),
)
.chain((match) => Either.fromNullable(match || null))
.map((match) =>
rawCommit.description.replace(
new RegExp(match.endsWith('\\s') ? match : match.concat('\\s')),
'',
),
)
.fold(
() => rawCommit.description,
(match) => match,
),
}
}

0 comments on commit 4d308e3

Please sign in to comment.