Automatic JIRA ticket creation with slack bot ~ slack bot development with python ① ~

What to do in this article

** Automation of JIRA ticket creation ** ** Create a mechanism that automatically creates JIRA tickets when you mention in Slack **

If you tweet the contents of the ticket by attaching a mention to the bot from slack as follows スクリーンショット 2017-06-20 19.07.44.png

Tickets are created in JIRA スクリーンショット 2017-06-20 19.12.40.png

Since I made such a mechanism, I will also write my own memo

Execution environment

 Vagrant.configure(2) do |config|
  config.vm.box = "bento/centos-6.7"

  config.vm.define "dev" do |node|
    node.vm.network "private_network", ip: "192.168.10.10"
    node.vm.synced_folder "/Users/takahirono7/projects/", "/root/project"
  end
end

Setting synced_folder is very convenient because you can sync directories on your local Mac and Vagrant. The method is summarized below Synchronize files between guest OS running on Vagrant and Mac

Add bot user

First you need to create a bot user that runs on Slack

Click "creating a new bot user" at the URL below to create a bot user https://api.slack.com/bot-users

スクリーンショット 2017-06-20 19.31.54.png

For details, please refer to the following people who have summarized it wonderfully. Making Slackbot with Python (1)

After completing the account creation, the following article

Add functionality to bots

Please carry out to the point Create a Slack bot with Python's slackbot library

The pioneers have explained this area in detail, so I will not describe it. I really appreciate it.

Proceeding to the above part, I think that the directory structure is as follows. Below, the above article by sukesuke is quoted.

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

We will add a function to link to jira to my_mention.py.

The implementation used the python-jira library. I thought about implementing it with curl or urllib, but I think the python-jira library is the easiest. https://jira.readthedocs.io/en/master/

Add the following to my_mention.py

from jira import JIRA
from jira.client import JIRA
@listen_to('create_ticket')
def create_tickets(message):
    options = {
        'server': 'https://**jira domain**/jira'
        }
    body  = message.body['te i xt']

    jira = JIRA(options, basic_auth=('bot_test','bot_test'))
    issue_dict = {
        'project': {'key': 'TES'},
        'summary': 'New issue from jira-python',
        'description': body ,
        'issuetype': {'name': 'task'}
        }
    new_issue = jira.create_issue(fields=issue_dict)
    message.reply('Created a ticket')
#Get object in the following format
# [<JIRA Issue: key='TES-4644', id='59893'>]
    last_ticket = jira.search_issues('project = "TES" AND reporter = bot_test ORDER BY createdDate DESC', maxResults = 1)

# last_ticket[0]so"TES-4659"などのticket keyが取得soきる
    message.reply("https://JIRA domain/jira/projects/TES/issues/%s" %(last_ticket[0]))

Description

jira = JIRA(options, basic_auth=('bot_test','bot_test'))

Add basic authentication information when instantiating JIRA. Don't forget to create a user on JIRA in advance

new_issue = jira.create_issue(fields=issue_dict)

You can create a ticket with the create_issue method. As an option, add project key, summary, description, and issue type information. Here, the content of the description is taken as the content of the slack message with body = message.body ['text'].

    last_ticket = jira.search_issues('project = "TES" AND reporter = bot_test ORDER BY createdDate DESC', maxResults = 1)

# last_ticket[0]so"TES-4659"などのticket keyが取得soきる
    message.reply("https://**jira domain**/jira/projects/TES/issues/%s" %(last_ticket[0]))

I'm searching for JIRA tickets with the jira.search_issues method. The ticket search results will be returned in the list, so set the key to [0] to get the ticket key. By adding the obtained key to the end of the URL of the jira domain, the URL of the ticket is obtained and returned.

With this kind of implementation, we were able to create tickets with slack as described at the beginning. I'm thinking of implementing more features around JIRA, so I'll post it when I have a story.

JIRA ticket with slack bot → Automation of Slack notification ~ slack bot development with python② ~

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

Automatic JIRA ticket creation with slack bot ~ slack bot development with python ① ~
Automate Slack notifications for JIRA tickets due today ~ python slack bot development ② ~
Slack chatbot creation Python
Notify slack when the switch sales page is updated ~ slack bot development with python ③ ~
Read and write files with Slackbot ~ Bot development with Python ~
Post to slack with Python 3
Othello game development made with Python
Easy with Slack using Bot #NowPlaying
Prepare Python development environment with Atom
Application development with Docker + Python + Flask
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
[Development environment] Python with Xcode [With screen transition]
Simple Slack API client made with Python
Automatic update method with python Pyinstaller exe
Develop slack bot in python using chat.postMessage
WebApi creation with Python (CRUD creation) For beginners
Excel table creation with Python [Progress management table]
Let's make a Twitter Bot with Python!
Try to make BOT by linking spreadsheet and Slack with python 2/2 (python + gspread + slackbot)
Try to make BOT by linking spreadsheet and Slack with python 1/2 (python + gspread + slackbot)
Ask the bot to tell you the weather (precipitation information) using the weather information API (YOLP) provided by Yahoo ~ slack bot development with python ④ ~
Discord bot with python raspberry pi zero with [Notes]
Make a Twitter trend bot with heroku + Python
Automatic operation of Chrome with Python + Selenium + pandas
[Web development with Python] query is also redirect
[Python] Build a Django development environment with Docker
Move THORLABS automatic stage with Python [for research]
LINE BOT with Python + AWS Lambda + API Gateway
Sample to send slack notification with python lambda
Steps to create a Twitter bot with python
Explosive speed with Python (Bottle)! Web API development
Recent ranking creation using Qiita API with Python
Simple sales tool creation with Python GUI: Quote creation
Continuation of multi-platform development with Electron and Python
[Python] Mention to multiple people with Slack API
[Web development with Python] Precautions when saving cookies
Flow to complete Slack authentication with Flask (Python)
I made a Mattermost bot with Python (+ Flask)
Get a quick Python development environment with Poetry