"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Python - Jira ticket management

Python - Jira ticket management

Published on 2024-11-04
Browse:213

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

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 = "" # [email protected]
JIRA_PASSWORD = "" # token

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"

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')

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')

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
Release Statement This article is reproduced at: https://dev.to/victordalet/python-jira-ticket-management-1jb7?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3