0

I am new to python automation and here is one logic i wanna explore

i have a ticket NeonTest-01 in jira, i want to fetch all the tickets linked to NeonTest-01,

Lets say NeonTest-02 and NeonTest-03 are linked to NeonTest-01,

and NeonTest-04 is linked to NeonTest-02

and NeonTest-02 linked to NeonTest-01

ultimately i want tickets=[NeonTest-01,NeonTest-02,NeonTest-03,NeonTest-04]

Here is my code:

class Neon:
    def fetch_tickets(self, ticket_list):
        payload = {}
        linked_tickets = []
        for ticket in ticket_list:
            if ticket is None or ticket == "":
                continue

            url = '/'.join(s.strip('/') for s in [
                self.base_url, f'rest/api/2/search?jql=key%3D{ticket}'
            ])

            try:
                response = requests.request("GET", url, headers=self.headers(), data=payload, verify=False)
            except (TimeoutError, ConnectionError) as e:
                self.log.error(f"There was an issue getting data from {url}: {e}")
                continue

            status = response.status_code
            if status != 200:
                self.log.error(
                    f'An issue with key "{ticket}" does not exist, Status code: {status}, Content: {response.content}')
                continue

            data = response.json()
            if "issues" not in data:
                # No matching issues
                return []

            issues = data["issues"]
            for issue in issues:
                if "fields" in issue:
                    issue_fields = issue['fields']
                    linked_issues = issue_fields['issuelinks']

                    try:
                        for every in linked_issues:
                            each = every.get('outwardIssue') or every.get('inwardIssue')
                            key = each['key']
                            linked_tickets.append(key)
                    except KeyError:
                        print('Error!!')

        print('initial Ticket', ticket_list)
        print('Linked Tickets:', linked_tickets)
        ticket_list.extend(linked_tickets)

        Duplicated = set(ticket_list)
        new_tickets_list = list(Duplicated)
        print("Overall Tickets:", new_tickets_list)
        return new_tickets_list

I am expecting a logic/Snippet code where i could fetch the tickets linked and subtickets of linked tickets

0 Answers0