Advertisement
Guest User

Untitled

a guest
Apr 5th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import requests
  2. import sys
  3.  
  4. username='xxxxx'
  5. password='xxxxx'
  6. base_url='https://alt-jira.rl.lan/rest/api/2/{}{}'
  7.  
  8. undesirable_text='The sample has been overridden'
  9.  
  10. class Comment(object):
  11.  
  12.         def __init__(self, author='', text=''):
  13.                 self._author = author
  14.                 self._text = text
  15.  
  16.         @property
  17.         def author(self):
  18.                 return self._author
  19.  
  20.         @author.setter
  21.         def author(self, author):
  22.                 self._author = author
  23.  
  24.  
  25.         @property
  26.         def text(self):
  27.                 return self._text
  28.  
  29.         @text.setter
  30.         def text(self, text):
  31.                 self._text = text
  32.  
  33.  
  34. class JiraTicket(object):
  35.  
  36.         def __init__(self, parent='', summary='', comments=[]):
  37.                 self._parent = parent
  38.                 self._summary = summary
  39.                 self._comments = comments
  40.  
  41.         @property
  42.         def parent(self):
  43.                 return self._parent
  44.  
  45.         @parent.setter
  46.         def parent(self, parent):
  47.                 self._parent = parent
  48.  
  49.         @property
  50.         def summary(self):
  51.                 return self._summary
  52.  
  53.         @summary.setter
  54.         def summary(self, summary):
  55.  
  56.         @comments.setter
  57.         def comments(self, comments):
  58.                 self._comments = comments
  59.  
  60. def generate_mail_body(jira_ticket):
  61.         headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
  62.         url = base_url.format('search?jql=parent=', jira_ticket)
  63.         r=requests.get(url, auth=(username, password), headers=headers)
  64.         if r.status_code != 200:
  65.                 print 'Some error occured. Please check your credentials and ticket issue key.'
  66.                 exit(1)
  67.         json = r.json()
  68.         issues = {}
  69.  
  70.         ticket = JiraTicket(parent=jira_ticket)
  71.  
  72.         for issue in json.get('issues'):
  73.                 issues[issue.get('key')] = issue.get('fields').get('summary')
  74.  
  75.  
  76.         tickets = []
  77.  
  78.         for issue_key, summary in issues.items():
  79.                 url = base_url.format('issue/', issue_key + '/comment')
  80.                 r=requests.get(url, auth=(username, password), headers=headers)
  81.                 json = r.json()
  82.                 comments = []
  83.                 for comment in json.get('comments'):
  84.                         author = comment.get('author').get('displayName')
  85.                         text = comment.get('body')
  86.                         if text.startswith(undesirable_text):
  87.                                 continue
  88.                         comments.append(Comment(author, text))
  89.  
  90.                 ticket = JiraTicket(parent=jira_ticket, summary=summary, comments = comments[:])
  91.                 tickets.append(ticket)
  92.         for ticket in tickets:
  93.                 print(ticket.summary)
  94.                 for comment in ticket.comments:
  95.                         print(comment.author)
  96.                         print(comment.text)
  97.  
  98.                 print('')
  99.  
  100. if __name__ == '__main__':
  101.         if len(sys.argv) !=2:
  102.                 print 'You must provide a parrent issue key as an argument!'
  103.                 exit(1)
  104.         generate_mail_body(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement