Jira is a web service developed by Atlassian for project management.
Table of Content
- Introduction
- Implementation
- Case 1: Create a new Jira Ticket
- Case 2: Get the content of specific Jira Ticket
- Case 3: Leave Comment in specific Ticket
- Case 4: Upload attachments in specific Ticket
前言
In my current company, we have more complex and strict deployment flow for releasing new features in production.
It controlled by Jira Ticket and we need to create new ticket manually for submit a new release request to DevOps Team, they will work on the ticket requirement and request us to provide security scan evidence.
Our product managers hope to automate part of flow, so that’s the new research to our team.
Implementation
Are you ready to go? That’s start at Code.
General Settings
# Authorization variable
# When send requests to Jira APIs, we need to add Authorization in request Header.
# You can generate a new token in your account settings.
Auth = "Bearer <your-token>"
# In each Jira Ticket there is a key represent the ticket, it is not title, we suppose a new ticket key is PX-1996, the real scene, you need to refer your team policy.
issue_key = "PX-1996"
Case 1: Create a new Jira Ticket
def create_ticket(issue_key, title, description):
try:
# API request url, for creating the url is /rest/api/2/issue/
# The hostname, you need to refer the Jira web url you use and replace the following <jira.hostname> part.
url = "https://<jira.hostname>/rest/api/2/issue/"
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
# Put the Authorization here
'Authorization': Auth
}
data = {
'fields': {
'project': {
# Fill up the ticket key name, which we define in the above.
'key': issue_key
},
# Fill up the Ticket title
'summary': title,
# Ticket description
'description': description,
# Issue Type,example is Story, you can fill other issue type, for example: Task.
'issuetype': {
'name': 'Story'
},
# This customfield in my side represent Team, you can fill additional info by these customfield columns.
# You can go to other ticket and right Click > Inspect > point to the column you want to see the column name.
# If you don't need this, you can delete this line.
'customfield_23231': {"value": "<Team Name>"}
}
}
# Send POST request to create ticket.
response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.text)
# If get HTTP code 201, it means created success.
if response.status_code == 201:
issue_key = response.json().get('key')
print(f'Jira issue created with key: {issue_key}')
else:
print('Failed to create Jira issue..')
# Error Handle
except Exception as ex:
print(f'Exception in creating Jira issue: {ex}')
finally:
print('process finish.')
Case 2: Get the content of specific Jira Ticket
def detect_ticket_status(issue_key):
# API request url, for getting the specific ticket content, the url is /rest/api/2/issue/<issue-key>
# The hostname, you need to refer the Jira web url you use and replace the following <jira.hostname> part.
url = f"https://<jira.hostname>/rest/api/2/issue/{issue_key}"
headers = {
# Put the Authorization here
"Authorization": Auth
}
# For getting information, we use Get request
response = requests.get(url, headers=headers)
# If success, get HTTP Code 200
if response.status_code == 200:
data = response.json()
# The ticket information can be got at data.
workflow_status = data['fields']['status']['name']
print(f"{issue_key} workflow status:{workflow_status}")
else:
print(f"Error status code: {response.status_code}")
Case 3: Leave Comment in specific Ticket
def add_jira_comment(issue_key, comment):
try:
# API request url, for adding the comment in specific ticket, the url is /rest/api/2/issue/<issue_key>/comment
# The hostname, you need to refer the Jira web url you use and replace the following <jira.hostname> part.
url = f"https://<jira.hostname>/rest/api/2/issue/{issue_key}/comment"
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': Auth
}
data = {
'body': comment
}
# for adding new comment, we use POST request
response = requests.post(url, headers=headers, json=data)
print(response.status_code)
print(response.text)
# If comment added success, get HTTP Code 201
if response.status_code == 201:
print(response.json())
print(f'Jira cooment upload successful.')
else:
print('Failed to uplad jira comment..')
except Exception as ex:
print(f'Exception in uploading Jira comment: {ex}')
finally:
print('process finish.....!!!!')
Case 4: Upload attachments in specific Ticket
# Upload files format, allow upload multiple files.
# The example is uploading multiple photos.
files = [
('file', ('test_photo1.png', open('./screenshot_1.png', 'rb'), 'image/png')),
('file', ('test_photo2.png', open('./screenshot_2.png', 'rb'), 'image/png'))
]
def add_jira_attachment(issue_key, files):
# API request url, for uploading the attachments in specific ticket, the url is /rest/api/2/issue/<issue_key>/attachments
# The hostname, you need to refer the Jira web url you use and replace the following <jira.hostname> part.
url = f"https://<jira.hostname>/rest/api/2/issue/{issue_key}/attachments"
# The Authorization method here is different to previous. We need to use HTTPBasicAuth to help us create HTTPBasicAuth object.
# Parameters are your Jira name(id) and password, not email.
credentials = requests.auth.HTTPBasicAuth("Jira Username", "Jira Password")
# This header paramaeter is necessary for uploading files.
headers = {'X-Atlassian-Token': 'no-check'}
# We send POST request and put the auth and files with request
response = requests.post(url, auth=credentials, files=files, headers=headers)
# Upload success and get the response status here.
print(response.status_code)
print(response.text)
That’s all the implementation I did in this research.
More cases please refer the official document: REST APIs - Jira Server
Thank you!