module "datadog_lambda_github_public_repo_audit" {
  source = "git@github.com:theorchard/terraform-datadog.git//modules/lambda?ref=6.13.4"

  environment                       = var.environment
  service_name                      = "${var.service_name}-${var.lambda_github_public_repo_audit_name}"
  notification_endpoints            = var.datadog_notification_endpoints
  escalation_notification_endpoints = var.datadog_notification_endpoints

  lambda_invocation_monitor_enabled = true
  lambda_invocation_time            = "last_1h"
  lambda_no_data_timeframe          = 120

  lambda_error_monitor_enabled          = true
  lambda_error_critical_number          = 1
  lambda_error_warning_number           = 0.8
  lambda_error_critical_recovery_number = 0.7
  lambda_error_warning_recovery_number  = 0.5
}

resource "datadog_monitor" "github_public_repo_audit_monitor" {
  name               = "GitHub Public Repo Audit Monitor"
  type               = "metric alert"
  message            = "GitHub public repository detected! Please review immediately. Notify: ${var.datadog_notification_endpoints}"
  escalation_message = "GitHub public repo alert still unresolved. Escalation to ${var.datadog_notification_endpoints}"

  query = "max(last_1h):sum:github_public_repo_audit.public_repos{*} > 0"

  notify_no_data      = true
  renotify_interval   = 60
  require_full_window = false
  no_data_timeframe   = 120

  tags = [
    "environment:${var.environment}",
    "service_name:${var.service_name}-${var.lambda_github_public_repo_audit_name}",
    "application_family:${var.application_family}"
  ]
}
