const { requireValue }  = require( './services/utils')

const fetch  = require( 'node-fetch').default

const getVersion = require( './services/version')

module.exports = async (options, logger) => {

  const versionInfo = getVersion(options, logger)

  let {
    project = process.env.PROJECT_ID || requireValue('project'),
    version = process.env.VERSION || versionInfo.version || requireValue( 'version' ),
    component = process.env.COMPONENT || false
  } = options

  await fetch('https://automation.atlassian.com/pro/hooks/babff4335c2ecb135235f32bbc34d1dc56e228c9', {
    method: 'POST',
    body: JSON.stringify({
      releaseVersion: version,
      projectId: project,
      component: component
    }),
    headers: {'Content-Type': 'application/json'}
  })

  logger(`Released version "${version}" sent to Jira project ${project.toUpperCase()} ${component ? component : ''}`.green)
  return 0
}
