#!/bin/bash
# Script: upload debug information and source bundle to Sentry when deploying the new version.
#
# Positional arguments:
#   $1: Release version (commit SHA)
#
# Required environment variables:
#   SENTRY_AUTH_TOKEN: Sentry auth token (https://sentry.io/settings/account/api/auth-tokens/)
#   SENTRY_ORG: Organization slug
#   SENTRY_PROJECT: Project slug
set -eu

REVISION="${1:-}"
NAME="${2:-relay}"
GITHUB_PROJECT="getsentry/relay"

if [ -z "${REVISION}" ]; then
  echo 'No revision specified' && exit 1
fi

if [ -z "${SENTRY_AUTH_TOKEN:-}" ]; then
  echo 'No Sentry auth token found' && exit 1
fi

if [ -z "${SENTRY_ORG:-}" ] || [ -z "${SENTRY_PROJECT:-}" ]; then
  echo 'No SENTRY_ORG or SENTRY_PROJECT provided' && exit 1
fi

echo 'Downloading debug info, source bundle, system symbols...'
gsutil cp \
  "gs://dicd-team-devinfra-cd--relay/deployment-assets/${REVISION}/${NAME}/release-name" \
  "gs://dicd-team-devinfra-cd--relay/deployment-assets/${REVISION}/${NAME}/relay-debug.zip" \
  "gs://dicd-team-devinfra-cd--relay/deployment-assets/${REVISION}/${NAME}/relay.src.zip" \
  "gs://dicd-team-devinfra-cd--relay/deployment-assets/${REVISION}/${NAME}/libs.tar" \
  .

echo 'Uploading debug information and source bundle...'
sentry-cli upload-dif ./relay-debug.zip ./relay.src.zip

echo 'Uploading system symbols...'
tar xf libs.tar
sentry-cli upload-dif lib/x86_64-linux-gnu

RELEASE=$(< ./release-name)
echo "Creating a new release and deploy for ${RELEASE} in Sentry..."
sentry-cli releases new "${RELEASE}"
# NOTE: Disabled until the repository is properly configured in Sentry
# sentry-cli releases set-commits "${RELEASE}" --commit "${GITHUB_PROJECT}@${REVISION}"
sentry-cli releases deploys "${RELEASE}" new -e production
sentry-cli releases finalize "${RELEASE}"
echo 'Deploy created.'
