Skip to content

Instantly share code, notes, and snippets.

@kubijo
Last active September 30, 2024 08:56
Show Gist options
  • Save kubijo/e2f62a33808fd17ac5b35c6700b9ee98 to your computer and use it in GitHub Desktop.
Save kubijo/e2f62a33808fd17ac5b35c6700b9ee98 to your computer and use it in GitHub Desktop.
Download $release source maps from sentry
#!/usr/bin/env bash
SELF="${0##*/}"
function usage {
cat <<EOF
Download & server production release source-map files
if they exist in the sentry release files archive.
usage:
$0 [-h|--help] <HASH>
EOF
}
if [[ "$1" == "-h" || "$1" == "--help" || $# -eq 0 ]]; then
usage
exit 0
fi
function fail {
printf >&2 "${SELF} / Error: $1\n"
exit 1
}
set -e
HASH="$1"
PORT=5050
[[ -n $2 ]] && PORT=$2
# Check params & environment
[[ -z "${HASH}" ]] && fail "Deploy hash is required!"
[[ -z "$(which jq)" ]] && fail "\`jq\` command is required!"
[[ -z "$(which curl)" ]] && fail "\`curl\` command is required!"
[[ -z "$(which python3)" ]] && fail "\`python3\` command is required!"
DIR=/tmp/js-source-maps/${HASH}
function cleanup {
rm -rf "${DIR:?}"
}
# Remove the artifacts on EXIT
cleanup
trap cleanup EXIT
set -x
# Get the artifacts
# - fetch the list of release files
# - select only javascript source maps (id & name)
# - fetch the files into temporary directory
mkdir -p "${DIR}"
cd "${DIR}"
curl \
-s -L "${SENTRY_URL}/api/0/projects/${SENTRY_ORG}/${SENTRY_PROJECT}/releases/${HASH}/files/" \
-H "Authorization: Bearer ${SENTRY_AUTH_TOKEN}" \
| jq -r '.[] | select(.name|endswith(".js.map")) | [.id, .name] | @tsv' \
| while IFS=$'\t' read -r FILE_ID FILE_PATH; do
curl \
-s -L "${SENTRY_URL}/api/0/projects/${SENTRY_ORG}/${SENTRY_PROJECT}/releases/${HASH}/files/${FILE_ID}/" \
-H "Authorization: Bearer ${SENTRY_AUTH_TOKEN}" \
--output "$(basename "${FILE_PATH}")"
done
# Print the files list
{
printf '\n'
ls -1AhgG --time-style='+' --hyperlink=always --color=always "${DIR}"
printf '\n'
} 2>/dev/null
# Run the server
python3 -m http.server "${PORT}" --directory="${DIR}" --bind 127.0.0.1
@RobertoMaurizzi
Copy link

Hello! I found this from your Issue opened on GitHub. I'd like to set up something similar for one of our projects but I can't understand what to use for the file_id parameter?

@kubijo
Copy link
Author

kubijo commented Sep 27, 2024

Hey. Well, it's parsed from the result of the previous API call…

${SENTRY_URL}/api/0/projects/${SENTRY_ORG}/${SENTRY_PROJECT}/releases/${HASH}/files/ returns info about files associated with a release, and you can then download each one-by-one

That said, this was done quite some time ago, and I am not using this actively… so I have no idea whether it still works

@RobertoMaurizzi
Copy link

Thanks! Their current API docs mention a different endpoint but doesn't explain what is the file id or where to get it from 😅

@kubijo
Copy link
Author

kubijo commented Sep 30, 2024

Yeah, man, they seem to approach their tech and docs similarly to Ikea or Acer ... in that you should be happy you got it working.

@RobertoMaurizzi
Copy link

😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment