[PYTHON] Use JIRA API

Hello. I'm Matsumoto from the Media System Development Department on the 17th day of mediba advent calendar 2016.

At mediba, multiple teams use atlassian's JIRA for project management. I also use it to manage tasks that occur on a regular basis, but I publish a story every time and publish subtasks for members ... Because it's sober and sometimes inadvertently forgotten, I tried to simplify the work by using the API.

Thing you want to do

--Create weekly periodical report issues using JIRA API and create as many subtask issues as there are members --The issue is created every Monday and the deadline for reporting is Thursday of the same week.

Environment / preparation

Implementation

import

import


import requests
import json
import datetime

API authentication

There are two authentication methods to use JIRA's API, this time using Basic HTTP.

Create an authentication key using BASE64 encoding.

Authentication


import base64
auth_str = 'JIRA account:JIRA password'
authkey = base64.encodestring(auth_str.encode("utf8")).decode("ascii")

Get an issue

For parameter analysis such as issue type, acquire and refer to existing issues.

Get issue


issue_data = requests.get(
    'https://JIRA URL/rest/api/2/issue/Issue ID you want to get',
    headers={'Content-Type': 'application/json',
             'Authorization': 'Authentication key created on Basic}'})

You can access the API by including the authentication key in the header information. From the response, get the information needed to create the issue.

--Priority = priority 3 is medium --Issue type = issue type 5 is a subtask

I think this area will change depending on the JIRA settings.

Add an issue

First, prepare the parameters to be used in the request.

Parameters


#Person in charge of child issue
assignee = ['Person in charge 1 ID', 'ID of person in charge 2', 'ID of person in charge 3']
#Report deadline
duedate = str(datetime.date.today() + datetime.timedelta(3))

This is a request to add an issue.

Add issue


addissue = requests.post(
    'https://JIRA URL/rest/api/2/issue/',
    json.dumps({
        "fields": {
            'project': {
                'key': 'Project key'
            },
            'assignee': {
                'name': 'Person in charge of parent issue'
            },
            'reporter': {
                'name': 'Reporter of parent issue'
            },
            'priority': {
                'id': '3'
            },
            'issuetype': {
                'id': '10001'
            },
       'labels': 'Label name',
            'summary':'Summary',
            'description': description,
            'duedate': duedate,
        }
    }),
    headers={'Content-Type': 'application/json',
             'Authorization': 'Basic authentication key}'}
)

Now that the parent issue is added, add the child issue as a subtask. Since the ID of the parent issue is required for parent, the one obtained from the previous request result will be used.

Add issue


for name in assignee:
  addsub = requests.post(
      'https://JIRA URL/rest/api/2/issue/',
      json.dumps({
          "fields": {
              'project': {'key': 'Project key'},
              'summary': summary + '_' + name,
              'assignee': {
                  'name': name
              },
              'reporter': {
                  'name': 'Parent ticket reporter'
              },
              'priority': {
                  'id': '3'
              },
              'parent': {
                  'id': addissue.json()['id']
              },
              'issuetype': {
                  'id': '5'
              },
              'labels': 'Label name',
              'description': description,
              'duedate': duedate,
          }
      }),
      headers={'Content-Type': 'application/json',
               'Authorization': 'Basic authentication key}'}
  )

Execution result

The above, the response of the execution result.

response


{'id': '42601',
 'key': 'Project key-452',
 'self': 'https://JIRA URL/rest/api/2/issue/42601'}
'42601'
{'id': '42602',
 'key': 'Project key-453',
 'self': 'https://JIRA URL/rest/api/2/issue/42602'}
{'id': '42603',
 'key': 'Project key-454',
 'self': 'https://JIRA URL/rest/api/2/issue/42603'}
{'id': '42604',
 'key': 'Project key-455',
 'self': 'https://JIRA URL/rest/api/2/issue/42604'}

Actually access JIRA, confirm that the parent issue and child issue have been added normally, and finish.

Summary

Once you know the parameters, it will work easily just by requesting the API, so if you are using JIRA, why not consider it? Of course, curl can be used instead. Tomorrow 18th will be "Remote Development Initiatives" by Mr. Sone.

Recommended Posts

Use JIRA API
Use Twitter API with Python
Use configparser when using API
Use subsonic API with python3
Use e-Stat API from Python
Use REST API in JIRA (user registration as an example)
Use API not implemented in twython
Use kabu Station® API from Python
How to use OpenPose's Python API
Use the Flickr API from Python
How to use bing search api
[Python] How to use Typetalk API
Use Google Analytics API from Python
Use DeepLabCut
Use Google Cloud Vision API from Python
Use pycscope
Use collections.Counter
Use: Django-MySQL
Use Pygments.rb
Use Numpy
use pandas-ply
How to use GCP's Cloud Vision API
Use GitPython
Use Miniconda
Use twitter API (API account registration and tweet acquisition)
Procedure to use TeamGant's WEB API (using python)
Use the MediaWiki API to get Wiki information
How to use the Google Cloud Translation API
Until you can use the Google Speech API
How to use the NHK program guide API
Use the Kaggle API inside a Docker container
[Google Cloud Platform] Use Google Cloud API using API Client Library