Skip to content

Instantly share code, notes, and snippets.

@jbaker10
Created May 2, 2018 21:32
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 jbaker10/1a754853293f6fc171bdc48f3b411cd6 to your computer and use it in GitHub Desktop.
Save jbaker10/1a754853293f6fc171bdc48f3b411cd6 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
'''
A script to automate changing the Due Date of a ticket in FreshService
based on the "Start Date" entered into a Service Request. It will currently
only make the change to tickets that are assigned to the "Onboarding"
group, and those that have been recently updated (within the last hour).
It requires the "requests" library to be installed, which can be installed
by running "sudo easy_install requests".
'''
import base64
import requests
import datetime
import time
from time import strptime, mktime
### Global Input Variables ###
FS_URL = 'https://host.freshservice.com' ## Please enter your Fresh Service URL such as 'https://domain.freshservice.com
API_KEY = '' ## Please enter your account API key here
GROUP_ID = '' ## Please enter the Group ID that corresponds to your 'Onboarding' Group
### End Global Input Variables ###
### Standard Global Variables ###
TICKETS_URI = '/api/v2/tickets'
ob_tickets = []
recent_ob_tickets = []
b64_auth = base64.b64encode("%s:dummy" % API_KEY)
request_headers = {'Authorization': 'Basic %s' % b64_auth, 'Accept': 'application/json'}
### End Standard Global Variables ###
def updateDueDate(ticket):
'''Will make the API call to update Due Date of the ticket passed'''
#start_date_time = ticket["start_date"]
start_date_time = "2018-05-05T12:00:00Z"
new_due_date = {
"ticket": {
"due_by": start_date_time,
"description": ticket["description"],
"type": "Service Request"
}
}
print "Updating the due date for ticket [%s] to [%s]" % (ticket["id"], start_date_time)
r = requests.put(FS_URL + TICKETS_URI + '/%s' % ticket["id"], headers=request_headers, json=new_due_date)
def main():
'''Main function which will retrieve all tickets and filter down to Onboarding requests'''
## Make the API call to grab all tickets that are currently in the queue
r = requests.get(FS_URL + TICKETS_URI, headers=request_headers)
try:
if r.status_code == 200:
parsed_r = r.json()
else:
print "Did not get a good response when retrieving all tickets."
except AttributeError:
print "Something went wrong when trying to parse the response"
## Let's filter out the tickets that are not for onboarding first
for ticket in parsed_r['tickets']:
if not ticket["group_id"] == None and int(ticket["group_id"]) == int(GROUP_ID):
## Can add a secondary check if we want to look for a term in the subject
## just to be extra sure
# if "New Hire" in ticket["subject"]:
ob_tickets.append(ticket)
if len(ob_tickets) > 0:
pass
else:
exit("There were no Service Requests assigned to the Onboarding Group ID specified.")
## Filter out the tickets that have not been updated within the last hour
for ticket in ob_tickets:
current_time = datetime.datetime.utcnow()
pattern1 = "%Y-%m-%d %H:%M:%S.%f"
pattern2 = "%Y-%m-%dT%H:%M:%SZ"
epoch_now = int(time.mktime(time.strptime(str(current_time), pattern1)))
epoch_updated_time = int(time.mktime(time.strptime(ticket["updated_at"], pattern2)))
if not epoch_now - epoch_updated_time > 3600:
recent_ob_tickets.append(ticket)
## We should check if there are in fact any recently updated tickets
if len(recent_ob_tickets) > 0:
## Now we can cycle through each ticket, get the start date, and re-assign
## the due date to that time
for ticket in recent_ob_tickets:
updateDueDate(ticket)
else:
exit("There were no recently updated Onboarding Service Requests.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment