Skip to content

Commit

Permalink
feat(FOROME-1293) Added changelog generator (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyUstyumenko authored Jun 30, 2022
1 parent 4b7a24d commit 72fe8d0
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/versioning.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
git fetch --tags
yarn config set version-git-message "[RELEASE] %s"
yarn config set version-commit-hooks false
yarn create:changelog
git add .
yarn version --new-version `node ./tools/getNextVersion.js`
git push --tags --no-verify &&
git push --no-verify
21 changes: 0 additions & 21 deletions env.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@
"cy:open": "cypress open",
"cy:run": "cypress run",
"storybook": "start-storybook -p 6006 -s public",
"build-storybook": "build-storybook -s public"
"build-storybook": "build-storybook -s public",
"create:changelog": "node ./tools/generate-changelog.js"
},
"simple-git-hooks": {
"pre-commit": "yarn lint-stage && yarn lint:fix",
Expand Down
130 changes: 130 additions & 0 deletions tools/generate-changelog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const { execSync } = require('child_process')
const fs = require('fs').promises

const versionRegex = /([\d.\d.\d]+)/
const taskNumberRegex = /(FOROME-\d+)/
const delimeter = '??'
const maxBuffer = 50 * 1024 * 1024
const logFormat = `--pretty=format:'%h${delimeter}%an${delimeter}%ad${delimeter}%s${delimeter}%d'`
const techAccountName = 'Version Bot'
const baseJiraUrl = 'https://quantori.atlassian.net/browse/FOROME-'
const baseGitHubUrl =
'https://github.com/ForomePlatform/Anfisa-React-Client/commit/'

const execCommand = (command, options) => {
return new Promise((resolve, reject) => {
let output
try {
output = execSync(`${command}`, options)
} catch (error) {
reject()
process.exit(0)
}

resolve(output)
})
}

const getVersionFormatted = tag => {
if (!tag) {
return ''
}

const matched = tag.match(versionRegex)

if (matched) {
return matched[0]
}
return ''
}

const getDateFormatted = date => {
return new Date(date).toLocaleDateString('en-US')
}

const fetchLog = async () => {
const gitCommand = `git log develop --tags ${logFormat}`

const log = await execCommand(gitCommand, {
encoding: 'utf8',
maxBuffer,
})

return log.toString()
}

const pipe =
(...args) =>
value => {
return args.reduce((res, func) => {
return func(res)
}, value)
}

const parse = rawLog => {
const parsedLog = rawLog.split('\n').reduce((acc, commit) => {
const [hash, userName, date, message, tag] = commit.split(delimeter)
const isFix = message.includes('fix(')
const isFeature = message.includes('feat(') || message.includes('feature(')
const isRefactoring = message.includes('refactor(')
const isTechnicalCommit = userName === techAccountName

if (isTechnicalCommit || isFix || isFeature || isRefactoring) {
acc.push({
hash,
userName,
date: getDateFormatted(date),
message,
tag: isTechnicalCommit ? getVersionFormatted(tag) : undefined,
isFix,
isFeature,
isRefactoring,
})
}

return acc
}, [])

return parsedLog
}

const createResult = parsedLog => {
let resultText = '# Change log \n'
parsedLog.forEach(logItem => {
const { tag, date, ...rest } = logItem
if (tag) {
resultText += `\n## <small>${tag} (${date})</small>\n\n`
} else {
resultText += formatRow(rest)
}
})

return resultText
}

const formatRow = ({ message, hash }) => {
const GHUrl = `${baseGitHubUrl}${hash}`
const taskNumberMatch = message.match(taskNumberRegex)
let commitMesage = message

if (taskNumberMatch) {
const label = taskNumberMatch[1]
const taskNumber = label.split('-')[1]
const jiraUrl = `${baseJiraUrl}${taskNumber}`
commitMesage = message.split(label).join(`[${label}](${jiraUrl})`)
}

const commitRow = `* ${commitMesage} ([${hash}](${GHUrl}))\n`

return commitRow
}

const generate = async () => {
const gitLog = await fetchLog()

const result = pipe(parse, createResult)(gitLog)

await fs.writeFile('CHANGELOG.md', result)
}

generate()

0 comments on commit 72fe8d0

Please sign in to comment.