Automate Slack notifications for JIRA tickets due today ~ python slack bot development ② ~

What to do in this article

** Automate Slack notifications for JIRA tickets that hit search criteria **

Display the corresponding JIRA tickets in Slack by sending a specific word to the bot with mentions in Slack (here confirm_today_tickets) as shown below.

スクリーンショット 2017-06-26 22.30.29.png

There is a ticket created in JIRA PJ as shown below, and it is an image to display the ticket that is caught in the search condition in Slack.

スクリーンショット 2017-06-26 22.36.29.png

I will add it to the program created in ① Automatically create JIRA tickets with slack bot ~ slack bot development with python ① ~

slackbot         #A directory that organizes programs. Any name is fine
├─ run.py        #Start the bot by running this program
├─ slackbot_settings.py   #File to write settings related to bot
└─ plugins                #Add bot functionality to this directory
   ├─ __init__.py         #A file to indicate the module. Sky is fine
   └─ my_mention.py       #Features each file. Any name is fine

The target to edit is my_mention.py again. The script looks like this:

# coding: utf-8

from slackbot.bot import respond_to	 # @botname:Decoder that reacts with
from slackbot.bot import listen_to	  #Decoder that responds to in-channel remarks
from jira import JIRA
from jira.client import JIRA

#Define variables used in functions
auth_str = 'bot_test:bot_test'
admin = '@takahirono7'
server_ip = 'http://192.168.10.10:8080'
options = {
	'server': server_ip
	}
jira = JIRA(options, basic_auth=('bot_test','bot_test'))

#Describe the jql to be used
jql_confirm_today_tickets = 'project = "BOT" AND created >=  startOfDay()'
jql_confirm_current_week_tickets = 'created > StartofWeek()'


#Confirmation of tickets scheduled to be held today
@respond_to('confirm_today_tickets')
def get_today_tickets(message):

	try:
#View tickets scheduled for work today
		today_work_tickets = jira.search_issues(jql_confirm_today_tickets)
		count_today_work_tickets = len(today_work_tickets)
		message.reply('The work scheduled to be carried out today%s cases' %(count_today_work_tickets))
		for ticket in today_work_tickets:
# fields.Get a summary with summary
			ticket_summary = ticket.fields.summary
# fields.Get a summary with summary
			if ticket.fields.assignee:
				ticket_assignee = ticket.fields.assignee.name
			else:
				ticket_assignee = 'Person in charge unassigned'
#Output to slack in the order of ticket name URL
			message.send(ticket_summary + 'Person in charge: %s URL:"%s/browse/%s"' %(ticket_assignee, server_ip, ticket))

	except Exception as e:
		print("Exception args:", e.args)
		message.reply('Ticket creation failure, check the cause%s' %(admin))

#Confirmation of tickets scheduled for this week
@respond_to('confirm_current_week_tickets')
def get_current_week_tickets(message):
	try:
		current_week_tickets = jira.search_issues(jql_confirm_current_week_tickets)
		count_current_week_tickets = len(current_week_tickets)
		message.reply('The work to be done this week%s cases' %(count_current_week_tickets))
		for ticket in current_week_tickets:
# fields.Get a summary with summary
			ticket_summary = ticket.fields.summary
#Output to slack in the order of ticket name URL
			message.send(ticket_summary + ' URL:"%s/browse/%s"' %(server_ip, ticket))
	except Exception as e:
		print("Exception args:", e.args)
		message.reply('Ticket creation failure, check the cause%s' %(admin))

Description

--By describing the JQL you want to specify in the search condition in the JQL part of the jira.search_issues (JQL) method, you will get the ticket information to hit it. --Since it can be obtained as a list type, you can access the ticket information while turning it with a for statement. --ticket.fields.summary: Summary --ticket.fields.assignee.name: Person in charge

You can get the information of the ticket field with etc.

--There is a function called get_current_week_tickets, which has the role of displaying tickets with a delivery date of less than one week. It is an image of checking a ticket that will be delivered within a week on Monday morning. JQL can display tickets from Sunday to next Monday by using Start of Week

Anything that hits in JQL can be obtained with the jira.search_issues () method, so I think you can pick up various tickets as long as you have JQL.

Relation

Automatically create JIRA tickets with slack bot ~ slack bot development with python ① ~ JIRA ticket with slack bot → Automation of Slack notification ~ slack bot development with python② ~ Notify slack when the switch sales page is updated ~ slack bot development with python ③ ~ Get the bot to tell you the weather (precipitation information) using the weather information API (YOLP) provided by Yahoo ~ slack bot development ④ ~ with python

Recommended Posts

Automate Slack notifications for JIRA tickets due today ~ python slack bot development ② ~
Automatic JIRA ticket creation with slack bot ~ slack bot development with python ① ~
Create JIRA tickets using Python
[For organizing] Python development environment
Python development environment options for May 2020
Emacs settings for Python development environment