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

const request = require('request-promise-native');

module.exports = async (options, logger) => {
  let user = options.user || process.env.BITBUCKET_APP_USERNAME || requireValue('user')
  let pass = options.password || process.env.BITBUCKET_APP_PASSWORD || requireValue('password')

  logger(`About to run deploy-new-mysql pipeline from devops-cluster-configs - user ${user}`.gray)

  await request({
    method: 'POST',
    uri: 'https://api.bitbucket.org/2.0/repositories/viewdo/devops-cluster-configs/pipelines/',
    body: {
      target: {
        type: "pipeline_ref_target",
        ref_type: "branch",
        ref_name: "develop",
        selector: {
          type: "custom",
          pattern: "deploy-new-mysql"
        }
      }
    },
    auth: {
      user,
      pass
    },
    json: true
  })

  logger(`deploy-new-mysql was called with user ${user}`.green)

  return 0
}