Automation Jira creation on GitHub event using Python……(Part-2)
In previous blog, we have succesfully created our jira api which create an jira issue when trigger.
Now in this blog, we will write an python script which will convert into API using flask, then we will host that python script wriiten on flask on amazon ec2 from where github will access that api using github webhooks.
So let’s see how to build….
Here is the python code below which you need to run on amazon ec2 instance. How to create an instance you can refere below blog —
How to install a GUI on Amazon EC2 instance running on Amazon Linux 2? | by Aaryan Gupta | Medium
There you can run this following code. For code
vim pythonscript.py #to edit or write code
python3 pythonscript.py #to run program
from flask import Flask
import requests
from requests.auth import HTTPBasicAuth
import json
app = Flask(__name__)
@app.route("/createjira",methods=['POST'])
def createjira():
url = "https://aaryangupta2201.atlassian.net/rest/api/3/issue"
auth = HTTPBasicAuth("yourmail@gmail.com", "your api token")
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
payload = json.dumps( {
"fields": {
"description": {
"content": [
{
"content": [
{
"text": "My first Jira ticket of the day",
"type": "text"
}
],
"type": "paragraph"
}
],
"type": "doc",
"version": 1
},
"issuetype": {
"id": "10006"
},
"project": {
"key": "AAR"
},
"summary": "Main order flow broken",
},
"update": {}
} )
response = requests.request(
"POST",
url,
data=payload,
headers=headers,
auth=auth
)
return json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))
app.run('0.0.0.0')
Now before running code, you need to add some security bound rules to access flask server from anywhere…
Click on add rule>>now select all traffic>>select 0.0.0.0/0 and save the rules.
Now you can run your flask application in your instance.
After running your instance, go to github page, create an github repository/ which you want to automate, go to seetings, then select webhooks.
Click on add webhook, now paste below url into your payload url.
http://ec2-3-110-182-117.ap-south-1.compute.amazonaws.com:5000/createjira
#relace ec2-3-110-182-117.ap-south-1.compute.amazonaws.com with your public ipv4 DNS of instance
After submitting and selecting respective events which you want to automate in github repository, click on add webhooks. You will see green tick in front of your webhook if all go good. And new issue has been created into Jira platform.
Now when ever you comment /jira or /abc , whatever after”/”, it will create an jira ticket for particular issue. Now more specific, below code is updated code, it only triger when you comment “/jira” in comment for any issue which you think is genuine.
from flask import Flask
import requests
from requests.auth import HTTPBasicAuth
import json
app = Flask(__name__)
@app.route("/createjira",methods=['POST'])
def createjira():
url = "https://aaryangupta2201.atlassian.net/rest/api/3/issue"
auth = HTTPBasicAuth("yourmail@gmail.com", "your api token")
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
payload = json.dumps( {
"fields": {
"description": {
"content": [
{
"content": [
{
"text": "My first Jira ticket of the day",
"type": "text"
}
],
"type": "paragraph"
}
],
"type": "doc",
"version": 1
},
"issuetype": {
"id": "10006"
},
"project": {
"key": "AAR"
},
"summary": "Main order flow broken",
},
"update": {}
} )
webhook = request.json
response = None
if webhook['comment'].get('body') == "/jira":
response = requests.request("POST", url, data=payload, headers=headers, auth=auth)
return json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))
else:
print('Jira issue will be created if comment include /jira')
return json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))
app.run('0.0.0.0')
Thanks for reading !!!!!!!!!
for any issue or debugging contact —