Guest User

Untitled

a guest
Dec 13th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. URL_PREFACE = "https://yourproject.visualstudio.com/defaultcollection/"
  2.  
  3. def getTicketComments(ticketID):
  4. """ Gets a list of the comments (in order from oldest to newest) for a given ticket """
  5.  
  6. url = URL_PREFACE + "_apis/wit/workitems/" + str(ticketID) + "/comments?api-version=3.0-preview&order=asc"
  7. jsonDict = readURL(url)
  8.  
  9. return jsonDict["comments"]
  10.  
  11. def getHoursSum(ticketID):
  12. """ For the given ticket, gets their comments, and calculates the hours
  13. """
  14. commentList = getTicketComments(ticketID)
  15. hourSum = 0
  16. for comment in commentList:
  17. try:
  18. hourSum += float(comment["text"]) # Will break if it's not a number
  19. except:
  20. pass
  21.  
  22. return hourSum
  23.  
  24. def updateHours(ticketID, completedHours):
  25.  
  26. headers = {"Content-Type": "application/json-patch+json"}
  27.  
  28. url = URL_PREFACE + "_apis/wit/workitems/" + str(ticketID) + "?api-version=1.0"
  29.  
  30. body = """[
  31. {
  32. "op": "replace",
  33. "path": "/fields/Microsoft.VSTS.Scheduling.CompletedWork",
  34. "value": """ + str(completedHours) + """
  35. }
  36. ]"""
  37.  
  38. username = 'username' # Doesn't matter
  39. password = TOKEN
  40.  
  41. # TO GET TOKEN:
  42. # Log into https://yourproject.visualstudio.com/
  43. # Click on your name -> My Profile
  44. # In the left-hand sidebar, click on "Security"
  45. # Under "Personal Accesss Tokens," click "Add"
  46. # Under "Description" give your token a name (doesn't matter)
  47. # Choose an expiration for your token (recommend: 1 yr)
  48. # "Authorized Scopes" = "All Scopes"
  49. # Click "Save"
  50. # Copy the token it gives you into token field below (paste between quotes)
  51.  
  52. session = requests.Session()
  53. request = requests.Request(method="PATCH", headers=headers, auth=(username, password),
  54. url=url, data=body)
  55. prepped = request.prepare()
  56. response = session.send(prepped)
  57.  
  58. return response
Add Comment
Please, Sign In to add comment