Last active
May 2, 2022 18:22
-
-
Save ericboehs/d1a88345ac526eb9851ff41155adc3bc to your computer and use it in GitHub Desktop.
Pull upcoming Pager Duty users for a given schedule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
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
Install and run:
Set up PagerDuty