-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FOROME-1293) Added changelog generator (#772)
- Loading branch information
1 parent
4b7a24d
commit 72fe8d0
Showing
4 changed files
with
134 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |