Skip to content

Instantly share code, notes, and snippets.

@byrnedo
Created February 9, 2024 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byrnedo/b2dc60c457f460127de54ce06d4cd1d2 to your computer and use it in GitHub Desktop.
Save byrnedo/b2dc60c457f460127de54ce06d4cd1d2 to your computer and use it in GitHub Desktop.
Get next on call date from Pagerduty
#!/bin/bash
# NOTE: assumes bsd `date` (as in mac)
set -euo pipefail
user_name="Your Name"
PD_TOKEN=${PD_TOKEN:-}
if [ -z "$PD_TOKEN" ]; then
PD_TOKEN=$(cat ~/.pagerduty.token)
fi
auth_header="Authorization: Token token=$PD_TOKEN"
user_response=$(curl --silent --fail --get \
--data-urlencode "query=$user_name" \
--header 'Accept: application/json' \
--header "$auth_header" \
--header 'Content-Type: application/json' \
"https://api.pagerduty.com/users")
user_id=$(echo $user_response| /usr/local/bin/jq -r '.users[0].id')
user_name=$(echo $user_response|/usr/local/bin/jq -r '.users[0].name')
#echo pagerduty user id for $user_name is $user_id
num_days_ahead=30
until=$(date -v+${num_days_ahead}d +%Y-%m-%dT%H:%M)
on_calls_response=$(curl --silent --fail-with-body --get \
--data-urlencode "user_ids[]=$user_id" \
--data-urlencode "until=$until" \
--data-urlencode "earliest=true" \
--header 'Accept: application/json' \
--header "$auth_header" \
--header 'Content-Type: application/json' \
"https://api.pagerduty.com/oncalls")
next_start=$(echo $on_calls_response|/usr/local/bin/jq -r '.oncalls[0].start')
next_start_human=$(TZ=UTC date -jf "%Y-%m-%dT%H:%M:%SZ" "$next_start" "+%a %d")
echo "OnCall: $next_start_human"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment