Get the URL of a JIRA ticket created with the jira-python library

Introduction

https://qiita.com/mu-naka/items/9ad7f509e697e83c2987 ↑ In the Python code I wrote earlier, I wrote the process of "creating a JIRA ticket", but I decided to add a Slack notification to the subsequent process. I wanted to include the newly created JIRA ticket URL in the Slack notification content, but I wrote it as a memorandum because it was difficult (not found) to find the URL even if I searched for the document. Please let me know if there is a better way. Click here for official documentation ↓ https://jira.readthedocs.io/en/master/index.html

I wrote in the previous article about how to use the JIRA library on python, so please refer to that. The python version of this article is 3.7.

review

Login

To operate JIRA, you need to log in first, so log in.

test.py


# JIRA Login
try:
    jira = JIRA(options = {'server': os.environ["JIRA URL * 1"]}, basic_auth = (JIRA user ID,JIRA password))
except JIRAError as e:
    return { "status" : "JIRA Login Failed." }

Ticket creation

test.py


# Create New Issue
newIssue = jira.create_issue(
    project = 'Project name * 2',
    summary = 'Summary (ticket title)',
    description = 'Ticket description',
    issuetype = {'name':'task'}
    )

Main subject

The return value of create_issue () at the time of ticket creation is included in newIssue. Looking at the contents of this new Issue

test.py


from pprint import pprint
#(Omitted)
    pprint(vars(newIssue))

log


{'_base_url': '{server}/rest/{rest_path}/{rest_api_version}/{path}',
 '_options': {'agile_rest_api_version': '1.0',
              'agile_rest_path': 'greenhopper',
              'async': False,
              'async_workers': 5,
              'auth_url': '/rest/auth/1/session',
              'check_update': False,
              'client_cert': None,
              'context_path': '/jira',
              'headers': {'Cache-Control': 'no-cache',
                          'Content-Type': 'application/json',
                          'X-Atlassian-Token': 'no-check'},
              'resilient': True,
              'rest_api_version': '2',
              'rest_path': 'api',
              'server': 'https://jira.hoge.com/jira',
              'verify': True},
 '_resource': 'issue/{0}',
 '_session': <jira.resilientsession.ResilientSession object at 0x7fa801472dd0>,
 'expand': 'renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations',
 'fields': <class 'jira.resources.PropertyHolder'>,
 'id': '12345',
 'key': 'HOGE-001',
 'raw': {
~ The following is omitted ~

I omitted it because it has a lot of volume, but there was no parameter that can be used as a URL as it is. However, the ticket URL has a rule of https: // [JIRA server base URL] / blowse / [ticket key], so you can generate it with the following code.

test.py



ticketUrl = newIssue._options.server + '/browse/' + newIssue.key

bonus

Use the Incoming Webhook to get to Slack and skip the notification with the following code.

test.py


requests.post('webhook url', data = json.dumps({
    'text': ticketUrl,
    'username': u'Notification user name',
    'icon_emoji': u':bow:',
    'link_names': 1
    }))

The values of various parameters are different from the sample, but I was able to include the URL of the ticket created with this in the Slack notification content. スクリーンショット 2019-11-08 15.22.21.png

Recommended Posts

Get the URL of a JIRA ticket created with the jira-python library
A network diagram was created with the data of COVID-19.
Get the id of a GPU with low memory usage
Get UNIXTIME at the beginning of today with a command
A story stuck with the installation of the machine learning library JAX
Get the filename of a directory (glob)
Get the number of searches with a regular expression. SeleniumBasic VBA Python
Get the average salary of a job with specified conditions from indeed.com
[Python] Get the files in a folder with Python
Get the caller of a function in Python
Get a list of IAM users with Boto3
Get the stock price of a Japanese company with Python and make a graph
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
Get the variable name of the variable as a character string.
Get the value of a specific key in a list from the dictionary type in the list with Python
Get the sum of each of multiple columns with awk
Take a screenshot of the LCD with Python-LEGO Mindstorms
Try to get the contents of Word with Golang
Visualize the characteristic vocabulary of a document with D3.js
[Python] Get the list of ExifTags names of Pillow library
Get the operation status of JR West with Python
Get the URL of the HTTP redirect destination in Python
Calculate the product of matrices with a character expression?
Get the trading price of virtual currency and create a chart with API of Zaif exchange
Measure the importance of features with a random forest tool
Run the intellisense of your own python library with VScode.
Your URL didn't respond with the value of the challenge parameter.
Get the number of specific elements in a python list
Get a list of purchased DMM eBooks with Python + Selenium
Settings to debug the contents of the library with VS Code
100 language processing knock-29: Get the URL of the national flag image
Analyze the topic model of becoming a novelist with GensimPy3
Make a BLE thermometer and get the temperature with Pythonista3
The story of making a question box bot with discord.py
Get the host name of the host PC with Docker on Linux
Get the source of the page to load infinitely with python.
Get the number of digits
How to display the CPU usage, pod name, and IP address of a pod created with Kubernetes
Get the link destination URL by specifying a text sentence with Python scraping (Beautiful Soup) + XPath
Process the contents of the file in order with a shell script
Get the number of PVs of Qiita articles you posted with API
Save the result of the life game as a gif with python
[python, ruby] fetch the contents of a web page with selenium-webdriver
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
If you give a list with the default argument of the function ...
The story of making a standard driver for db with python.
Here's a brief summary of how to get started with Django
[Python] Get the update date of a news article from HTML
Count the maximum concatenated part of a random graph with NetworkX
How to get the vertex coordinates of a feature in ArcPy
The idea of feeding the config file with a python file instead of yaml
To improve the reusability and maintainability of workflows created with Luigi
Create a function to get the contents of the database in Go
Get a list of files in a folder with python without a path
The story of making a module that skips mail with python
Get the number of readers of a treatise on Mendeley in Python
Create a compatibility judgment program with the random module of python.
PhytoMine-I tried to get the genetic information of plants with Python
Get the weather with Python requests