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

const bitbucket = require('./services/bitbucket')

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

  let  {
    user = process.env.BITBUCKET_APP_USERNAME || requireValue('user'),
    password = process.env.BITBUCKET_APP_PASSWORD || requireValue('password'),
    repository = process.env.BITBUCKET_REPO_SLUG || requireValue('repository'),
    branch = 'main',
    pipeline
  } = options

  logger(`About to run ${pipeline||'default'} pipeline on ${repository}:${branch} for user ${user}`.gray)

  await bitbucket.runPipeline(
    user,
    password,
    repository,
    pipeline,
    branch
  )

  logger(`Successfully run ${pipeline||'default'} pipeline on ${repository}:${branch} for user ${user}`.green)

  return 0
}
