top of page
Search
Writer's pictureIgor Miazek

JIRA integration with Python

Updated: 5 days ago


Woman using a standing desk with a laptop. Bright sticky notes cover the glass wall behind her. Office setting, organized and focused mood thinking about how to do jira integration with python

JIRA is well known and matured software for project management and development. I started using it at the very beginning of my career and I still love it. It really helps to implements standards for organization processes.


Well this article will be not about how to use JIRA but how to integrate it in Your System. As programming language we will use Python and a jira-python library, documentation is here.


Create Application Link in JIRA in Order To Achievie Jira Integration


JIRA can be integrated with Your own application by application link. In JIRA go to Products and than to Application Links, look below. You can as well us this link https://[NAME].atlassian.net/plugins/servlet/applinks/listApplicationLinks, remove brackets and put Your JIRA organization name.

this image shows interface of jira in order to explain how to do jira integration with python

Remember that application link needs to be created for Generic Application.


When You will select Generic Application, You should see similar form as bellow.

this image explains how to create application link in order to do jira integration

I see that rest of the fields is required You can put there some dump value I have used example and everything was ok. When You will try to edit existing application link that we just created everything becomes clear.

this image explains configuration of creating application in order to create jira integration

JIRA can connect to Your application this maybe useful if You would like to receive real time notifications and trigger some actions in Your System. Although in this article we will focus on how to consume JIRA data so I will show You how to configure Incoming Authentication part.


If You will got to Incoming Authentication You will need to fill 3 mandatory fields: Consumer Name, Consumer Key and Public Key. In order to produce public key which will be accepted by JIRA do this in bash. It will generate 2 keys public and private.


$ openssl genrsa -out privkey.pem 2048
$ openssl rsa -pubout -in privkey.pem -out pubkey.pem

Public keys needs to be added to Incoming Authentication. Use this name for Consumer Key: my-application-consumer-key. Not sure why this name was not added automatically as we already created it in the first form.


Authorize Your System with JIRA by OAuth


So OAuth is the industry-standard protocol for authorization. OAuth is used when two services are trying to accomplish something on behalf of users or their software. Below is an example of env file, You need to set value for 3 first variables another 2 will get value after OAuth.


JIRA_CONSUMER_KEY=
JIRA_PRIV_KEY=

JIRA_SERVER=
JIRA_ACCESS_TOKEN=
JIRA_ACCESS_TOKEN_SECRET=

Below You have the full python script which will do OAuth for You. I have a separate python file where I keep settings. To populate env from env file I am using python-dotenv which is great tool.

import dotenv
from jira import jirashell
from dashboard import settings
import os


def main():
    s = settings.JIRA
    with open(s['PRIV_KEY'], 'r') as key_cert_file:
        key_cert_data = key_cert_file.read()

    oauth = jirashell.oauth_dance(
        server=s['SERVER'], consumer_key=s['CONSUMER_KEY'],
        key_cert_data=key_cert_data)

    env_file = os.environ.get("ENV_FILE")
    dotenv.set_key(env_file, "JIRA_ACCESS_TOKEN", oauth['access_token'])
    dotenv.set_key(env_file, "JIRA_ACCESS_TOKEN_SECRET", oauth['access_token_secret'])


if __name__ == "__main__":
    main()

Connect to JIRA

from dashboard import settings
from jira import JIRA

def initialize() -> JIRA:

    s = settings.JIRA

    options = {"server": s['SERVER']}

    key_cert = s['PRIV_KEY']

    with open(key_cert, 'r') as key_cert_file:
        key_cert_data = key_cert_file.read()

    # https://jira.readthedocs.io/en/latest/examples.html#oauth
    # The access token and token secret uniquely identify the user.
    # The consumer key must match the OAuth provider configured on the Jira server.
    # The key cert data must be the private key that matches the public
    # key configured on the Jira server’s OAuth provider.

    oauth_dict = {
        'access_token': s['ACCESS_TOKEN'],
        'access_token_secret': s['ACCESS_TOKEN_SECRET'],
        'consumer_key': s['CONSUMER_KEY'],
        'key_cert': key_cert_data
    }

    jira = JIRA(options, oauth=oauth_dict)

    return jira

After running the initialize function You will get a valid JIRA object, now You can follow the official documentation of jira-python and get all of Your jira object.


Know Your data with schema


I am not sure if JIRA rest api expose some schema which define resources like OpenAPI. In my case I have used pydantic which allows You to define simple data types with python typing.

from pydantic import BaseModel, Field
from typing import Any, List, Optional
from datetime import datetime

class ProjectVersion(BaseModel):
    id: int
    name: str
    description: str
    archived: bool
    released: bool
    startDate: Optional[str] = None
    releaseDate: str

class ProjectComponent(BaseModel):
    id: int
    name: str
    description: str

class ProjectItem(BaseModel):
    id: int
    key: str
    name: str
    components: List[ProjectComponent]
    versions: List[ProjectVersion]
    description: str

Thanks to above schema data You get from JIRA is already cleaned and You can safely upload it to Your database.


Thoughts


Zero information waste ? In now days organizations may struggle with correct information flow, this is so because of so many tools we are using to make things done. Project management, time tracking, emails, client relationship management. Of course I know that there are solutions which give You all in one, like salesforce ? But I never liked this kind of solutions that put You into box and don't let You go out. Integration of organization components needs a dose of flexibility as each company is unique. So let's integrate simply and smartly and use what You have first before You introduce a new system component.

49 views
bottom of page