-
Notifications
You must be signed in to change notification settings - Fork 12
/
create_jira.py
executable file
·100 lines (89 loc) · 3.53 KB
/
create_jira.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from jira import ( JIRA,JIRAError )
import json
import requests
import os, sys
import re
patterns = {
"Bug":"How critical is this bug to you([\n\s\d\w\-?]*)-",
"Task": "How critical is this feature to you([\n\s\d\w\-?]*)-",
}
IssuePriorityMap = {
"Blocker": "Blocker - P0",
"Critical": "Critical - P1",
"Major": "Major - P2",
"Minor": "Minor - P3"
}
def get_issue_priority(issue_body, issue_type):
'''
currently issue body has a segment which user can provide the bug/task priority'''
priorityKey = re.findall(patterns[issue_type], issue_body)[0].replace('?','').replace('\n','').strip()
for k, v in IssuePriorityMap.items():
if k == priorityKey:
return v
return "None"
issue_types = [{"Bug":"bug"},{"Task":"feature request"}]
def parse_event_context():
"""
Parse event context and run issue creation checks
"""
try:
create_issue_for = False
event = os.environ.get("EVENT_CONTEXT")
event_json = json.loads(event)
issue_type = ""
issue_body = event_json["issue"]["body"]
issue_title = event_json["issue"]["title"]
issue_url = event_json["issue"]["url"]
for label in event_json["issue"]["labels"]:
for issue_type_obj in issue_types:
for k,v in issue_type_obj.items():
if label["name"] == v:
issue_type = k
create_issue_for = True
return issue_body, issue_type, issue_title, issue_url , create_issue_for
except Exception as e:
print("Please set EVENT_CONTEXT Variable with github issue event type... {}".format(e))
sys.exit(1)
def run_create_issue(issue_body, issue_type, issue_title, issue_url):
"""
JIRA_SERVER environment variable will be feed from Github CI for creating issues
JIRA_USER, JIRA_PWD will be kept as a secret for authentication
"""
JIRA_SERVER = os.environ.get("JIRA_SERVER")
JIRA_USER = os.environ.get("JIRA_USER")
JIRA_PWD = os.environ.get("JIRA_PWD")
options = {"server": JIRA_SERVER}
try:
jira = JIRA(basic_auth=(JIRA_USER, JIRA_PWD), options=options)
except JIRAError as e:
print("Could not connect to JIRA due to Auth Failure/server not reachable: {}".format(e))
sys.exit(1)
except Exception as e:
print("Could not connect to JIRA due to : {}".format(e))
sys.exit(1)
issue_priority = get_issue_priority(issue_body, issue_type)
Body = "Github Issue: "+issue_url + "\n" + issue_body
Issue = {
'project': {'key': 'NPT'},
'summary': issue_title ,
'description': Body,
'issuetype': {'name': issue_type },
'priority': {'name': issue_priority},
}
try:
new_issue = jira.create_issue(fields=Issue)
Message="https://jira.eng.vmware.com/browse/"+new_issue.key
URL=issue_url+"/comments"
github_token = os.environ.get("GITHUBTOKEN")
resp = requests.post(URL, json = {"body":"New JIRA Created with ID: {}".format(Message)}, headers={"Authorization":"Bearer {}".format(github_token),"Accept":"application/vnd.github+json"})
print(resp.status_code)
except Exception as e:
print("Could not create JIRA due to : {}", e)
sys.exit(1)
if __name__ == "__main__":
"""
Run issue creation for bug and feature requests
"""
issue_body, issue_type, issue_title, issue_url, create_issue_for = parse_event_context()
if create_issue_for:
run_create_issue(issue_body, issue_type, issue_title, issue_url)