async function createIncident(authData, user) {
  // Get incident info from the user
  let incidentTitle = await ux.prompt(incidentTitlePrompt)
  while (incidentTitle.description.length > 255) {
    await ux.print('Incident title must be less than 255 characters!')
    incidentTitle = await ux.prompt(incidentTitlePrompt)
  }
  let { impact, started_at, status, message } = await ux.prompt(
    incidentStartPrompts
  )
  let { description } = incidentTitle

  // Create our timestamps
  const ts = moment().unix()
  const updated_at = ts
  started_at = moment(started_at)
    .tz(moment.tz.guess())
    .format('h:MM A z on dddd, MMMM D, YYYY')

  // Create the incident summary
  const incidentSummary = {
    description,
    impact,
    started_at,
    status,
    timeline: [
      {
        status,
        message,
        ts: updated_at,
      },
    ],
  }

  const { gitlabToken, projectId, slackWebHook, pagerDutyKey } = authData

  try {
    // Prompt for which integrations we should activate for this incident
    const { gitlab, slack, pagerDuty } = await ux.prompt(whereToCreatePrompt)

    if (gitlab) {
      await createGitlabIssue(gitlabToken, projectId, incidentSummary)
      await handleSuccess('Create GitLab Issue', incidentSummary)
    }

    if (slack) {
      await sendSlackMessage(slackWebHook, incidentSummary, user)
      await handleSuccess('Create Slack Message', incidentSummary)
    }

    if (pagerDuty) {
      // Init our PD client
      pd.initializePagerDuty(pagerDutyKey)

      // Prompt the user for which escalation policy they want to use
      let escalationPolicy
      const policies = await pd.getEscalationPolicies()
      if (policies.length) {
        // Create a name -> id object map for good natural language prompts
        // to the user
        const policyChoices = {}
        policies.map(policy => (policyChoices[policy.name] = policy.id))
        // Get user to select an escalation policy
        const answer = await ux.prompt(
          escalationPolicyPrompt(Object.keys(policyChoices))
        )
        // Pull the policy id from its name
        escalationPolicy = policyChoices[answer.escalationPolicy]
      }

      // Prompt the user for which service to create the incident for
      const services = await pd.getServices()
      const serviceChoices = {}
      services.map(service => (serviceChoices[service.name] = service.id))
      const { service } = await ux.prompt(
        servicePrompt(Object.keys(serviceChoices))
      )
      const serviceId = serviceChoices[service]

      await ux.spinner.start('🏃‍ Creating a new PagerDuty incident')
      // Create the PagerDuty incident
      const incident = await pd.newIncident(user, {
        incident: {
          title: description,
          escalation_policy: {
            id: escalationPolicy,
            type: 'escalation_policy_reference',
          },
          service: {
            id: serviceId,
            type: 'service_reference',
          },
          body: {
            type: 'incident_body',
            details: `${message}\n Started at: ${started_at}\n Impact: ${impact}\n Status: ${status}`,
          },
          urgency: getUrgency(impact),
        },
      })
      await ux.spinner.stop('🎉 PagerDuty incident created! 🎉')
      await ux.print(blue(`You can see the incident at ${incident.html_url}`))
      await handleSuccess('Create PagerDuty incident', incident)
    }
  } catch (err) {
    await handleError(err, 'Failed to create an incident')
  }
}
/**
 * createIncident prompts the user for info and creates a PagerDuty incident.
 *
 * @param {object} authData The user's entered auth data
 * @param {object} user     The current SDK user
 */
createIncident prompts the user for info and creates a PagerDuty incident.
@param {object} authData The user's entered auth data
@param {object} user     The current SDK user
createIncident prompts the user for info and creates a PagerDuty incident . @param { object } authData The user ' s entered auth data @param { object } user The current SDK user
async function createIncident(authData, user) {
  let incidentTitle = await ux.prompt(incidentTitlePrompt)
  while (incidentTitle.description.length > 255) {
    await ux.print('Incident title must be less than 255 characters!')
    incidentTitle = await ux.prompt(incidentTitlePrompt)
  }
  let { impact, started_at, status, message } = await ux.prompt(
    incidentStartPrompts
  )
  let { description } = incidentTitle
  const ts = moment().unix()
  const updated_at = ts
  started_at = moment(started_at)
    .tz(moment.tz.guess())
    .format('h:MM A z on dddd, MMMM D, YYYY')
  const incidentSummary = {
    description,
    impact,
    started_at,
    status,
    timeline: [
      {
        status,
        message,
        ts: updated_at,
      },
    ],
  }
  const { gitlabToken, projectId, slackWebHook, pagerDutyKey } = authData
  try {
    const { gitlab, slack, pagerDuty } = await ux.prompt(whereToCreatePrompt)
    if (gitlab) {
      await createGitlabIssue(gitlabToken, projectId, incidentSummary)
      await handleSuccess('Create GitLab Issue', incidentSummary)
    }
    if (slack) {
      await sendSlackMessage(slackWebHook, incidentSummary, user)
      await handleSuccess('Create Slack Message', incidentSummary)
    }
    if (pagerDuty) {
      pd.initializePagerDuty(pagerDutyKey)
      let escalationPolicy
      const policies = await pd.getEscalationPolicies()
      if (policies.length) {
        const policyChoices = {}
        policies.map(policy => (policyChoices[policy.name] = policy.id))
        const answer = await ux.prompt(
          escalationPolicyPrompt(Object.keys(policyChoices))
        )
        escalationPolicy = policyChoices[answer.escalationPolicy]
      }
      const services = await pd.getServices()
      const serviceChoices = {}
      services.map(service => (serviceChoices[service.name] = service.id))
      const { service } = await ux.prompt(
        servicePrompt(Object.keys(serviceChoices))
      )
      const serviceId = serviceChoices[service]
      await ux.spinner.start('🏃‍ Creating a new PagerDuty incident')
      const incident = await pd.newIncident(user, {
        incident: {
          title: description,
          escalation_policy: {
            id: escalationPolicy,
            type: 'escalation_policy_reference',
          },
          service: {
            id: serviceId,
            type: 'service_reference',
          },
          body: {
            type: 'incident_body',
            details: `${message}\n Started at: ${started_at}\n Impact: ${impact}\n Status: ${status}`,
          },
          urgency: getUrgency(impact),
        },
      })
      await ux.spinner.stop('🎉 PagerDuty incident created! 🎉')
      await ux.print(blue(`You can see the incident at ${incident.html_url}`))
      await handleSuccess('Create PagerDuty incident', incident)
    }
  } catch (err) {
    await handleError(err, 'Failed to create an incident')
  }
}
async function createIncident ( authData , user ) { let incidentTitle = await ux . prompt ( incidentTitlePrompt ) while ( incidentTitle . description . length > 255 ) { await ux . print ( 'Incident title must be less than 255 characters!' ) incidentTitle = await ux . prompt ( incidentTitlePrompt ) } let { impact , started_at , status , message } = await ux . prompt ( incidentStartPrompts ) let { description } = incidentTitle const ts = moment ( ) . unix ( ) const updated_at = ts started_at = moment ( started_at ) . tz ( moment . tz . guess ( ) ) . format ( 'h:MM A z on dddd, MMMM D, YYYY' ) const incidentSummary = { description , impact , started_at , status , timeline : [ { status , message , ts : updated_at , } , ] , } const { gitlabToken , projectId , slackWebHook , pagerDutyKey } = authData try { const { gitlab , slack , pagerDuty } = await ux . prompt ( whereToCreatePrompt ) if ( gitlab ) { await createGitlabIssue ( gitlabToken , projectId , incidentSummary ) await handleSuccess ( 'Create GitLab Issue' , incidentSummary ) } if ( slack ) { await sendSlackMessage ( slackWebHook , incidentSummary , user ) await handleSuccess ( 'Create Slack Message' , incidentSummary ) } if ( pagerDuty ) { pd . initializePagerDuty ( pagerDutyKey ) let escalationPolicy const policies = await pd . getEscalationPolicies ( ) if ( policies . length ) { const policyChoices = { } policies . map ( policy => ( policyChoices [ policy . name ] = policy . id ) ) const answer = await ux . prompt ( escalationPolicyPrompt ( Object . keys ( policyChoices ) ) ) escalationPolicy = policyChoices [ answer . escalationPolicy ] } const services = await pd . getServices ( ) const serviceChoices = { } services . map ( service => ( serviceChoices [ service . name ] = service . id ) ) const { service } = await ux . prompt ( servicePrompt ( Object . keys ( serviceChoices ) ) ) const serviceId = serviceChoices [ service ] await ux . spinner . start ( '🏃‍ Creating a new PagerDuty incident') const incident = await pd . newIncident ( user , { incident : { title : description , escalation_policy : { id : escalationPolicy , type : 'escalation_policy_reference' , } , service : { id : serviceId , type : 'service_reference' , } , body : { type : 'incident_body' , details : ` ${ message } \n ${ started_at } \n ${ impact } \n ${ status } ` , } , urgency : getUrgency ( impact ) , } , } ) await ux . spinner . stop ( '🎉 PagerDuty incident created! 🎉') await ux . print ( blue ( ` ${ incident . html_url } ` ) ) await handleSuccess ( 'Create PagerDuty incident' , incident ) } } catch ( err ) { await handleError ( err , 'Failed to create an incident' ) } }
createIncident prompts the user for info and creates a PagerDuty incident.
createIncident prompts the user for info and creates a PagerDuty incident .
// Get incident info from the user // Create our timestamps // Create the incident summary // Prompt for which integrations we should activate for this incident // Init our PD client // Prompt the user for which escalation policy they want to use // Create a name -> id object map for good natural language prompts // to the user // Get user to select an escalation policy // Pull the policy id from its name // Prompt the user for which service to create the incident for // Create the PagerDuty incident