Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import sys
- username='xxxxx'
- password='xxxxx'
- base_url='https://alt-jira.rl.lan/rest/api/2/{}{}'
- undesirable_text='The sample has been overridden'
- class Comment(object):
- def __init__(self, author='', text=''):
- self._author = author
- self._text = text
- @property
- def author(self):
- return self._author
- @author.setter
- def author(self, author):
- self._author = author
- @property
- def text(self):
- return self._text
- @text.setter
- def text(self, text):
- self._text = text
- class JiraTicket(object):
- def __init__(self, parent='', summary='', comments=[]):
- self._parent = parent
- self._summary = summary
- self._comments = comments
- @property
- def parent(self):
- return self._parent
- @parent.setter
- def parent(self, parent):
- self._parent = parent
- @property
- def summary(self):
- return self._summary
- @summary.setter
- def summary(self, summary):
- @comments.setter
- def comments(self, comments):
- self._comments = comments
- def generate_mail_body(jira_ticket):
- headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
- url = base_url.format('search?jql=parent=', jira_ticket)
- r=requests.get(url, auth=(username, password), headers=headers)
- if r.status_code != 200:
- print 'Some error occured. Please check your credentials and ticket issue key.'
- exit(1)
- json = r.json()
- issues = {}
- ticket = JiraTicket(parent=jira_ticket)
- for issue in json.get('issues'):
- issues[issue.get('key')] = issue.get('fields').get('summary')
- tickets = []
- for issue_key, summary in issues.items():
- url = base_url.format('issue/', issue_key + '/comment')
- r=requests.get(url, auth=(username, password), headers=headers)
- json = r.json()
- comments = []
- for comment in json.get('comments'):
- author = comment.get('author').get('displayName')
- text = comment.get('body')
- if text.startswith(undesirable_text):
- continue
- comments.append(Comment(author, text))
- ticket = JiraTicket(parent=jira_ticket, summary=summary, comments = comments[:])
- tickets.append(ticket)
- for ticket in tickets:
- print(ticket.summary)
- for comment in ticket.comments:
- print(comment.author)
- print(comment.text)
- print('')
- if __name__ == '__main__':
- if len(sys.argv) !=2:
- print 'You must provide a parrent issue key as an argument!'
- exit(1)
- generate_mail_body(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement