import { getJiraId } from "../../jira/util/id" ; import issueKeyParser from "jira-issue-key-parser" ; import { getJiraAuthor } from "../../util/jira" ; import _ from "lodash" ; /**
 * mapBranch takes a branch node from the GraphQL response and
 * attempts to find issueKeys in use anywhere in that object
 *
 * Locations can include:
 *  - Branch Name (ref)
 *  - Title of the associated Pull Request
 *  - Messages from up to the last 100 commits in that branch
 */ function mapBranch ( branch , repository ) { const branchKeys = issueKeyParser ( ) . parse ( branch . name ) || [ ] ; const pullRequestKeys = issueKeyParser ( ) . parse ( branch . associatedPullRequests . nodes . length ? branch . associatedPullRequests . nodes [ 0 ] . title : "" ) || [ ] ; const commitKeys = issueKeyParser ( ) . parse ( branch . target . message ) || [ ] ; const allKeys = branchKeys . concat ( pullRequestKeys ) . concat ( commitKeys ) . filter ( ( key ) => ! ! key ) ; if ( ! allKeys . length ) { return undefined ; } return { createPullRequestUrl : `${ repository . html_url }/pull/new/${ branch . name }` , id : getJiraId ( branch . name ) , issueKeys : allKeys , lastCommit : { author : getJiraAuthor ( branch . target . author , branch . target . history . nodes ?. [ 0 ] ?. author ) , authorTimestamp : branch . target . authoredDate , displayId : branch . target . oid . substring ( 0 , 6 ) , fileCount : branch . target . changedFiles || 0 , hash : branch . target . oid , id : branch . target . oid , issueKeys : commitKeys , message : branch . target . message , url : branch . target . url || undefined , updateSequenceId : Date . now ( ) } , name : branch . name , url : `${ repository . html_url }/tree/${ branch . name }` , updateSequenceId : Date . now ( ) } ; } /**
 * mapCommit takes the a single commit object from the array
 * of commits we got from the GraphQL response and maps the data
 * to the structure needed for the DevInfo API
 */ function mapCommit ( commit ) { const issueKeys = issueKeyParser ( ) . parse ( commit . message ) ; if ( _ . isEmpty ( issueKeys ) ) { return undefined ; } return { author : getJiraAuthor ( commit . author ) , authorTimestamp : commit . authoredDate , displayId : commit . oid . substring ( 0 , 6 ) , fileCount : 0 , hash : commit . oid , id : commit . oid , issueKeys : issueKeys || [ ] , message : commit . message , timestamp : commit . authoredDate , url : commit . url , updateSequenceId : Date . now ( ) } ; } export default ( payload ) => { const branches = payload . branches . map ( ( branch ) => mapBranch ( branch , payload . repository ) ) . filter ( ( branch ) => ! ! branch ) ; const commits = payload . branches . flatMap ( ( branch ) => branch . target . history . nodes . map ( ( commit ) => mapCommit ( commit ) ) . filter ( ( branch ) => ! ! branch ) ) ; if ( ( ! commits || ! commits . length ) && ( ! branches || ! branches . length ) ) { return undefined ; } return { branches , commits , id : payload . repository . id , name : payload . repository . name , url : payload . repository . html_url , updateSequenceId : Date . now ( ) } ; } ;