DEV Community

Cover image for Python - Jira ticket management
victor_dalet
victor_dalet

Posted on

Python - Jira ticket management

Hello, I've just discovered Jira Lib, and I've decided to test it in order to find the people who make the most tickets.

Code in my github : https://github.com/victordalet/Jira_python_test


I - Installation

You juste need to python and install jira lib.

pip install jira
Enter fullscreen mode Exit fullscreen mode

II - Log in

Declare the 3 variables with your information and go to https://id.atlassian.com/manage-profile/profile-and-visibility in security to generate your token (password).

JIRA_URL = "" # https://name.alassian.net
JIRA_USER = "" # me@name.fr
JIRA_PASSWORD = "" # token
Enter fullscreen mode Exit fullscreen mode

III - Lib fonctionnement

I'm creating a class to get JIRA information, which can create a kind of mysql query like the get_tickets method to find resources or tickets, projects ...

class TicketManager:
    def __init__(self):
        self.jira = JIRA(JIRA_URL, basic_auth=(JIRA_USER, JIRA_PASSWORD))

    def get_projects(self):
        return self.jira.projects()

    def get_tickets(self, project_key: str):
        return self.jira.search_issues(f'project="{project_key}"')

    @staticmethod
    def ticket_status(ticket_):
        return ticket_.fields.status

    @staticmethod
    def ticket_reporter(ticket_):
        try:
            return ticket_.fields.reporter
        except AttributeError:
            return "Unknown"

    @staticmethod
    def ticket_assignee(ticket_):
        try:
            return ticket_.fields.assignee
        except AttributeError:
            return "Unknown"
Enter fullscreen mode Exit fullscreen mode

IV - Display user activity

I browse the project to find all the tickets and add the right statues to a user dictionary.

if __name__ == '__main__':
    ticket_manager = TicketManager()
    projects = ticket_manager.get_projects()
    user = {}
    nb_total_tickets = 0
    for project in projects:
        tickets = ticket_manager.get_tickets(project.key)
        nb_total_tickets += len(tickets)
        for ticket in tickets:
            reporter = ticket_manager.ticket_reporter(ticket)
            assignee = ticket_manager.ticket_assignee(ticket)
            if assignee not in user:
                user[assignee] = {'ticket_to_do': 0, 'ticket_reported': 0}
            if reporter not in user:
                user[reporter] = {'ticket_to_do': 0, 'ticket_reported': 0}
            user[assignee]['ticket_to_do'] += 1
            user[reporter]['ticket_reported'] += 1

    print(f'There are {nb_total_tickets} tickets in total')
Enter fullscreen mode Exit fullscreen mode

V - Sorting and display result

Now I'm sorting users by the number of tickets created and used.

    user = dict(sorted(user.items(), key=lambda x: (x[1]['ticket_to_do'], x[1]['ticket_reported']), reverse=True))

    for name, value in user.items():
        print(f'{name} : {value["ticket_to_do"]} tickets to do, {value["ticket_reported"]} tickets reported')
Enter fullscreen mode Exit fullscreen mode

Result :

J.M. : 90 tickets to do, 60 tickets reported
L.M : 75 tickets to do, 21 tickets reported
J.M : 57 tickets to do, 76 tickets reported
V.M : 50 tickets to do, 0 tickets reported
Enter fullscreen mode Exit fullscreen mode

Top comments (0)