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.
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." }
test.py
# Create New Issue
newIssue = jira.create_issue(
project = 'Project name * 2',
summary = 'Summary (ticket title)',
description = 'Ticket description',
issuetype = {'name':'task'}
)
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
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.
Recommended Posts