Home  >  Article  >  Backend Development  >  Python - Jira ticket management

Python - Jira ticket management

WBOY
WBOYOriginal
2024-08-28 18:31:36275browse

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 = "" # me@name.fr
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

The above is the detailed content of Python - Jira ticket management. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn