Working with the Jira API with Python

Hello. There was a thought to automate the downloading of reports from Jira. Since Python was a favorite tool, the choice fell on a module from JIra allowing you to work with their API. To whom it is interesting I ask on kat.

image

Atlassian has official documentation on the use of their module. The module itself is called “jira”. By tradition, we will install the module with the command:

pip install jira

Then we import the module directly in the code:

from jira import JIRA

In order to connect to the server itself, you need to create a client by passing the required parameters into it:

jira_options = {'server': 'https://project-name.atlassian.net'}
jira = JIRA(options=jira_options, basic_auth=(login, api_key))

In order to authenticate with a password, you can transfer the password instead of api key. After authorization is passed, we have an active api client that you can access.

The possibilities are certainly not unlimited, but quite wide. I needed to pull out tasks for a specific week and compile a report on the hours spent in Excell. You can get tasks directly by the project itself, by task number, or by JQL query. Search tools are quite flexible and simple. All the information returned by the api client comes in string, so additional steps are required to work with it.
We compose a jql request and pick up the tasks on it:

jql = 'project = ' + project_key + ' AND  worklogDate >= ' + work_date
issues_list = jira.search_issues(jql) 

Unfortunately, I still did not understand why in those tasks that are obtained through such a request there is no worklog property. After some attempts to understand what was wrong, I politely asked jira for the task by number:

issue = jira.issue(issue_key)

In the task returned by this method, the worklog field with a list of worklogs was. As a result, I began to take tasks by jql query, pulled out the task numbers and after that pulled out the information I needed:

worklogs = issue.fields.worklog.worklogs

A similar line allows you to pull out all the time records from a specific task. Each entry has time information in seconds and in textual representation (1h, 3d etc).
Then everything is simple, take discards that do not fit the period, in my case the week number does not match:

worklog_date_str = re.search(r'(\d{4}-\d{2}-\d{2})', worklog.started)
            worklog_date = datetime.strptime(worklog_date_str.group(0), '%Y-%m-%d')
            if worklog_date.isocalendar()[1] == weak_number:

Since the date is returned in a string, I used a simple regular expression to pick it up, and in the next line I cite the necessary type. The expression worklog_date.isocalendar () [1] allows you to find out the number of the week, which will be compared with what needs to be taken. If it matches, stomp on and write the rest of the data.

In general, the above module allows you to solve a fairly wide range of tasks, you only need time and desire.

UPD

You can not request tasks again, just expand the required field
github.com/pycontribs/jira/blob/master/jira/client.py#L2371
thanks HSerg

Also popular now: