module Gitlab::QA::Runtime::Env

def ci_api_v4_url

def ci_api_v4_url
  ENV['CI_API_V4_URL']
end

def ci_commit_ref_name

def ci_commit_ref_name
  ENV['CI_COMMIT_REF_NAME']
end

def ci_job_name

def ci_job_name
  ENV['CI_JOB_NAME']
end

def ci_job_token

def ci_job_token
  ENV['CI_JOB_TOKEN']
end

def ci_pipeline_id

def ci_pipeline_id
  ENV['CI_PIPELINE_ID']
end

def ci_pipeline_source

def ci_pipeline_source
  ENV['CI_PIPELINE_SOURCE']
end

def ci_pipeline_url

def ci_pipeline_url
  ENV['CI_PIPELINE_URL']
end

def ci_project_id

def ci_project_id
  ENV['CI_PROJECT_ID']
end

def deploy_environment

def deploy_environment
  ENV['DEPLOY_ENVIRONMENT'] || pipeline_from_project_name
end

def dev_access_token_variable

def dev_access_token_variable
  env_value_if_defined('GITLAB_QA_DEV_ACCESS_TOKEN')
end

def elastic_version

def elastic_version
  ENV['ELASTIC_VERSION'] || '6.4.2'.freeze
end

def enabled?(value, default: true)

def enabled?(value, default: true)
  return default if value.nil?
  (value =~ /^(false|no|0)$/i) != 0
end

def env_value_if_defined(variable)

def env_value_if_defined(variable)
  # Pass through the variables if they are defined in the environment
  return "$#{variable}" if ENV[variable]
end

def gitlab_api_base

def gitlab_api_base
  ENV['GITLAB_API_BASE'] || 'https://gitlab.com/api/v4'
end

def gitlab_bot_multi_project_pipeline_polling_token

def gitlab_bot_multi_project_pipeline_polling_token
  ENV['GITLAB_BOT_MULTI_PROJECT_PIPELINE_POLLING_TOKEN']
end

def gitlab_ci_api_token

def gitlab_ci_api_token
  ENV['GITLAB_CI_API_TOKEN']
end

def gitlab_dev_username

def gitlab_dev_username
  ENV['GITLAB_DEV_USERNAME'] || 'gitlab-qa-bot'
end

def gitlab_username

def gitlab_username
  ENV['GITLAB_USERNAME'] || 'gitlab-qa'
end

def host_artifacts_dir

def host_artifacts_dir
  @host_artifacts_dir ||= File.join(ENV['QA_ARTIFACTS_DIR'] || '/tmp/gitlab-qa', Runtime::Env.run_id)
end

def pipeline_from_project_name

def pipeline_from_project_name
  if ci_project_name.to_s.start_with?('gitlab-qa')
    if ENV['TOP_UPSTREAM_SOURCE_JOB'].to_s.start_with?('https://ops.gitlab.net')
      'staging-orchestrated'
    else
      'master'
    end
  else
    ci_project_name
  end
end

def qa_container_registry_access_token

def qa_container_registry_access_token
  ENV['GITLAB_QA_CONTAINER_REGISTRY_ACCESS_TOKEN']
end

def qa_dev_access_token

def qa_dev_access_token
  ENV['GITLAB_QA_DEV_ACCESS_TOKEN']
end

def qa_issue_url

def qa_issue_url
  ENV['GITLAB_QA_ISSUE_URL']
end

def require_ci_slack_webhook_url!

def require_ci_slack_webhook_url!
  return unless ENV['CI_SLACK_WEBHOOK_URL'].to_s.strip.empty?
  raise ArgumentError, "Please provide CI_SLACK_WEBHOOK_URL"
end

def require_kubernetes_environment!

def require_kubernetes_environment!
  %w[GCLOUD_ACCOUNT_EMAIL GCLOUD_ACCOUNT_KEY CLOUDSDK_CORE_PROJECT].each do |env_key|
    raise ArgumentError, "Environment variable #{env_key} must be set to run kubernetes specs" unless ENV.key?(env_key)
  end
end

def require_license!

def require_license!
  return if ENV.include?('EE_LICENSE')
  raise ArgumentError, 'GitLab License is not available. Please load a license into EE_LICENSE env variable.'
end

def require_no_license!

def require_no_license!
  return unless ENV.include?('EE_LICENSE')
  raise ArgumentError, "Unexpected EE_LICENSE provided. Please unset it to continue."
end

def require_qa_access_token!

def require_qa_access_token!
  return unless ENV['GITLAB_QA_ACCESS_TOKEN'].to_s.strip.empty?
  raise ArgumentError, "Please provide GITLAB_QA_ACCESS_TOKEN"
end

def require_qa_dev_access_token!

def require_qa_dev_access_token!
  return unless ENV['GITLAB_QA_DEV_ACCESS_TOKEN'].to_s.strip.empty?
  raise ArgumentError, "Please provide GITLAB_QA_DEV_ACCESS_TOKEN"
end

def require_slack_qa_channel!

def require_slack_qa_channel!
  return unless ENV['SLACK_QA_CHANNEL'].to_s.strip.empty?
  raise ArgumentError, "Please provide SLACK_QA_CHANNEL"
end

def run_id

def run_id
  @run_id ||= "gitlab-qa-run-#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}-#{SecureRandom.hex(4)}"
end

def skip_pull?

def skip_pull?
  enabled?(ENV['QA_SKIP_PULL'], default: false)
end

def variables

def variables
  vars = {}
  ENV_VARIABLES.each do |name, attribute|
    # Variables that are overridden in the environment take precedence
    # over the defaults specified by the QA runtime.
    value = env_value_if_defined(name) || send(attribute) # rubocop:disable GitlabSecurity/PublicSend
    vars[name] = value if value
  end
  vars
end