import envVars from "../../config/env" ; import { getLogger } from "../../config/logger" ; import { JiraIssue } from "../../interfaces/jira" ; const logger = getLogger ( "jira.util" ) ; export default ( jiraClient ) => { const containsReferenceLink = ( line ) => { if ( line . split ( " " ) . length === 2 ) { const hasSquareBrackets = line . charAt ( 0 ) === "[" && line . includes ( "]:" ) ; const hasUrl = line . includes ( "http://" ) || line . includes ( "https://" ) ; return hasSquareBrackets && hasUrl ; } return false ; } ; const checkForReferenceText = ( text ) => { const splitTextByNewLine = text . split ( "\n" ) ; return splitTextByNewLine . filter ( ( line ) => containsReferenceLink ( line ) ) . map ( ( referenceLink ) => referenceLink . slice ( 1 , referenceLink . indexOf ( "]" ) ) ) ; } ; function addJiraIssueLinks ( text , issues ) { const referenceRegex = /\[([A-Z]+-[0-9]+)\](?!\()/g ; const issueMap = issues . reduce ( ( acc , issue ) => ( { ... acc , [ issue . key ] : issue } ) , { } ) ; const links = [ ] ; const keys = checkForReferenceText ( text ) ; while ( referenceRegex . lastIndex < 1000 ) { const match = referenceRegex . exec ( text ) ; if ( ! match ) { break ; } const [ , key ] = match ; if ( keys . includes ( key ) || ! issueMap [ key ] ) { continue ; } const issueTrackingParam = envVars . JIRA_LINK_TRACKING_ID ? `?atlOrigin=${ envVars . JIRA_LINK_TRACKING_ID }` : "" ; const link = `${ jiraClient . baseURL }/browse/${ key }${ issueTrackingParam }` ; const reference = `[${ key }]: ${ link }` ; if ( text . includes ( reference ) ) { continue ; } links . push ( reference ) ; } return links . length ? [ text , links . join ( "\n" ) ] . join ( "\n\n" ) : text ; } async function unfurl ( text ) { try { const issues = jiraClient . issues . parse ( text ) ; if ( ! issues ) return undefined ; const validIssues = await jiraClient . issues . getAll ( issues ) ; if ( ! validIssues . length ) return undefined ; const linkifiedBody = addJiraIssueLinks ( text , validIssues ) ; if ( linkifiedBody === text ) return undefined ; return linkifiedBody ; } catch ( err ) { logger . warn ( { err , issueText : text } , "Error getting all JIRA issues" ) ; return undefined ; } } async function runJiraCommands ( commands ) { return Promise . all ( commands . map ( command => { if ( command . kind === "comment" ) { return Promise . all ( command . issueKeys . map ( issueKey => jiraClient . issues . comments . addForIssue ( issueKey , { body : command . text } ) ) ) ; } if ( command . kind === "worklog" ) { return Promise . all ( command . issueKeys . map ( issueKey => jiraClient . issues . worklogs . addForIssue ( issueKey , { timeSpentSeconds : command . time , comment : command . text } ) ) ) ; } if ( command . kind === "transition" ) { return Promise . all ( command . issueKeys . map ( async ( issueKey ) => { const transitions = ( await jiraClient . issues . transitions . getForIssue ( issueKey ) ) . data . transitions . map ( transition => ( { id : transition . id , name : transition . name . replace ( " " , "-" ) . toLowerCase ( ) } ) ) . filter ( transition => transition . name . startsWith ( command . name ) ) ; if ( transitions . length !== 1 ) { return Promise . resolve ( ) ; } if ( command . text ) { await jiraClient . issues . comments . addForIssue ( issueKey , { body : command . text } ) ; } return jiraClient . issues . transitions . updateForIssue ( issueKey , transitions [ 0 ] . id ) ; } ) ) ; } return Promise . resolve ( ) ; } ) ) ; } return { addJiraIssueLinks , runJiraCommands , unfurl } ; } ;