Skip to content

Instantly share code, notes, and snippets.

@ericboehs
Last active May 2, 2022 18:22
Show Gist options
  • Save ericboehs/d1a88345ac526eb9851ff41155adc3bc to your computer and use it in GitHub Desktop.
Save ericboehs/d1a88345ac526eb9851ff41155adc3bc to your computer and use it in GitHub Desktop.
Pull upcoming Pager Duty users for a given schedule
#! /usr/bin/env ruby
# https://gist.github.com/ericboehs/d1a88345ac526eb9851ff41155adc3bc
require 'open-uri'
require 'json'
BOLD = `tput bold`
NORMAL = `tput sgr0`
def print_all_schedules_names_and_ids
url = "https://api.pagerduty.com/schedules?limit=100"
puts "To list a scheudle, run one of the following commands:\n\n"
URI.open(url, 'Authorization' => "Token token=#{ENV['DSVA_PD_API_TOKEN']}") do |response|
json = JSON.parse response.gets
json['schedules'].each do |schedule|
puts "#{File.basename $PROGRAM_NAME} #{schedule['id']} #{BOLD}# #{schedule['name']}#{NORMAL}"
end
end
end
def print_schedule(id)
url = "https://dsva.pagerduty.com/api/v1/schedules/#{id}?since=#{`date +%Y-%m-%d`}&until=#{`date -v+1m +%Y-%m-%d`}"
URI.open(url, 'Authorization' => "Token token=#{ENV['DSVA_PD_API_TOKEN']}") do |response|
json = JSON.parse response.gets
json['schedule']['final_schedule']['rendered_schedule_entries'].map {|h| { h['user']['summary'] => h['start'].split('T').first } }.uniq(&:keys).map(&:invert).reduce(Hash.new, :merge).each { |date, dude| puts "#{date}: #{dude}"}
end
end
if ARGV[0] == 'list'
print_all_schedules_names_and_ids
else
print_schedule ARGV[0]
end
__END__
curl --silent --header "Authorization: Token token=$DSVA_PD_API_TOKEN" \
"https://dsva.pagerduty.com/api/v1/schedules/$1?since=$(date +%Y-%m-%d)&until=$(date -v+1m +%Y-%m-%d)" \
| jq '.schedule.final_schedule.rendered_schedule_entries | map({(.user.summary): (.start | split("T")[0])}) | unique_by(keys) | sort_by(values) | reverse | map(to_entries | map( {(.value) : .key } ) | add) | add | to_entries | sort_by(.key) | from_entries'
curl --silent --header "Authorization: Token token=$DSVA_PD_API_TOKEN" \
"https://api.pagerduty.com/schedules" \
| jq
@ericboehs
Copy link
Author

ericboehs commented Apr 5, 2022

Install and run:

curl -L https://gist.github.com/ericboehs/d1a88345ac526eb9851ff41155adc3bc/raw/va-pd-schedule > va-pd-schedule
chmod +x va-pd-schedule
export DSVA_PD_API_TOKEN=u+LsDaInt4MeSd2jf
va-pd-schedule list # Lists all Schedule Names/IDs
va-pd-schedule PPGUD8L # Platform Console Support Rotation

Set up PagerDuty

  1. Go to My Profile from drop-down
  2. User Settings tab
  3. Create API User Token

@ericboehs
Copy link
Author

ericboehs commented Apr 5, 2022

I attempted to write this in curl/jq but gave up:

curl --silent --header "Authorization: Token token=$DSVA_PD_API_TOKEN" \
 "https://dsva.pagerduty.com/api/v1/schedules/$1?since=$(date +%Y-%m-%d)&until=$(date -v+1m +%Y-%m-%d)" \
 | jq '.schedule.final_schedule.rendered_schedule_entries | map({(.user.summary): (.start | split("T")[0])}) | unique_by(keys) | sort_by(values) | reverse | map(to_entries | map( {(.value) : .key } ) | add) | add | to_entries | sort_by(.key) | from_entries'

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