Skip to content

Instantly share code, notes, and snippets.

@julianeon
Last active December 25, 2015 01:19
Show Gist options
  • Save julianeon/6894565 to your computer and use it in GitHub Desktop.
Save julianeon/6894565 to your computer and use it in GitHub Desktop.
A Ruby script to search all schedules for a given user name and output the names of the schedules that contain it.
# Copyright (c) 2014, PagerDuty, Inc. <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of PagerDuty Inc nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'curb'
require 'json'
filename="NamesInSchedules.txt"
tokennum="CENSORED"
start="2013-10-07"
stop="2013-10-08"
endpoint="https://CENSORED.pagerduty.com/api/v1/schedules/"
person="Sandy"
#Since the API only returns 100 results by default, if you have for example 125 schedules, to check the last 25, you must rerun this script with the offset
#set at 100. By default, when set at 0 as here, it returns the first 100 (or less) results.
offnum="0"
def curl_command_string(tokennum,start,stop,endpoint,offset_number)
curl_command="curl -H \"Content-type: application/json\" -H \"Authorization: Token token=#{tokennum}\" -X GET -G \
--data-urlencode \"since=#{start}\" \
--data-urlencode \"until=#{stop}\" -d \"offset=#{offset_number}\" \
\"#{endpoint}\""
end
idarray=Array.new
curl_get_all_schedule_ids=curl_command_string(tokennum,start,stop,endpoint,offnum)
IO.popen(curl_get_all_schedule_ids).each do |line|
parsed=JSON.parse(line)
puts parsed
parsed["schedules"].each {|x| idarray.push x["id"]}
end
puts idarray.length
schedule_with_user_array = Array.new
idarray.each do |id|
url=endpoint+id
puts url
curl_get_each_schedule_content=curl_command_string(tokennum,start,stop,url,offnum)
IO.popen(curl_get_each_schedule_content).each do |line|
parsed=JSON.parse(line)
schedule_name = parsed["schedule"]["name"]
parsed["schedule"]["schedule_layers"].each do |schedule_layers|
schedule_layers["rendered_schedule_entries"].each do |schedule_details|
name= schedule_details["user"]["name"]
if name.match(person)
line_matches = "Schedule \"#{schedule_name}\" has the user \"#{name}\" in active rotation."
schedule_with_user_array.push line_matches
end
end
end
end
end
puts "There were #{schedule_with_user_array.length} matches for this name within the schedules."
puts schedule_with_user_array
file=File.open(filename,'w')
schedule_with_user_array.each {|line| file.puts line}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment