import { Commit } from "@octokit/webhooks-types" ; import template from "lodash.template" ; const fixKeywords = [ "#fix" , "#fixes" , "#fixed" ] ; const closeKeywords = [ "#close" , "#closes" , "#closed" ] ; const commitKeywordRegexTemplate = template ( "^(<%=project_key%>\\-\\d+)\\s?" + "(.*?)?" + `\\s?(${ fixKeywords . join ( "|" ) }|${ closeKeywords . join ( "|" ) })?$` ) ; export type ParsedCommits = Record < string , ParsedCommit [ ] > ; export type ParsedCommit = { original_message : string ; id_short : string ; tree_id_short : string ; issue_key : string | null ; keywords : string ; is_fix : boolean ; is_close : boolean ; } & Commit ; param commits param projectKey returns export const parseCommits = ( commits , projectKey ) : ParsedCommits | null => { const parsedCommits = { } ; commits . forEach ( ( commit ) => { const parsedCommit = parseCommit ( commit , projectKey ) ; if ( ! parsedCommit ?. issue_key ) return ; if ( parsedCommits [ parsedCommit . issue_key ] ) { parsedCommits [ parsedCommit . issue_key ] . push ( parsedCommit ) ; } else { parsedCommits [ parsedCommit . issue_key ] = [ parsedCommit ] ; } } ) ; const commitCount = Object . keys ( parsedCommits ) . length ; if ( commitCount === 0 ) { return null ; } return parsedCommits ; } ; param commit param projectKey returns const parseCommit = ( commit , projectKey ) : ParsedCommit | null => { const match = commit . message . match ( RegExp ( commitKeywordRegexTemplate ( { project_key : projectKey } ) , "s" ) ) ; if ( ! match ) { return null ; } const [ , issue_key = null , message = "" , keywords = "" ] = match ; return { ... commit , message , original_message : commit . message , id_short : commit . id . slice ( 0 , 10 ) , tree_id_short : commit . tree_id . slice ( 0 , 10 ) , issue_key , keywords , is_fix : fixKeywords . includes ( keywords ) , is_close : closeKeywords . includes ( keywords ) , } ; } ;