Repository navigation
Expand file tree
/
Copy pathticketFeedbackHandler.py
More file actions
137 lines (123 loc) · 6.95 KB
/
Copy pathticketFeedbackHandler.py
File metadata and controls
137 lines (123 loc) · 6.95 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from utils.github_api import GithubAPI
import aiohttp, sys
from datetime import datetime
from utils.runtime_vars import MARKDOWN_TEMPLATE_HEADERS
headerMessages = {
"Product Name": "Product Name - Please add a heading called Product Name and mention the name of the product below it. ",
"Project": "Project/Project Name",
"Project Name": "Project/Project Name",
"Organization Name": "Organization Name",
"Domain": "Domain - Area of governance",
"Tech Skills Needed": "Tech Skills Needed - Please add relevant tech skills ",
"Mentor(s)": "Mentors(s) Please tag the relevant mentors on the ticket",
"Complexity": "Complexity - Please mention the complexity only as High/Medium/Low",
"Category": "Category - Please add one or more of these options [CI/CD], [Integrations], [Performance Improvement], [Security], [UI/UX/Design], [Bug], [Feature], [Documentation], [Deployment], [Test], [PoC]",
"Sub Category": "Sub-Category - Please mention the sub-category if any for the ticket",
"Invalid Complexity": "Complexity must be High/Medium/Low"
}
class TicketFeedbackHandler:
def __init__(self):
return
def evaluateDict(self,md_dict):
missing_headers = []
for header in MARKDOWN_TEMPLATE_HEADERS:
if header not in md_dict.keys():
missing_headers.append(header)
if ("Product" in missing_headers or "Product Name" in missing_headers) and not ("Product" in missing_headers and "Product Name" in missing_headers):
if "Product"in missing_headers:
missing_headers.remove("Product")
elif"Product Name" in missing_headers:
missing_headers.remove("Product Name")
#Project Name is in the template but project name is being taken from the title of the ticket
if "Project"in missing_headers:
missing_headers.remove("Project")
if "Project Name" in missing_headers:
missing_headers.remove("Project Name")
return missing_headers
def feedBackMessageCreator(self, markdown_dict):
missing_headers = self.evaluateDict(markdown_dict)
if "Product" in missing_headers and "Product Name" in missing_headers:
missing_headers.remove("Product")
mandatoryHeaders = ''
optionalHeaders = ''
heads = ''
for header in missing_headers:
if header in ['Product Name', 'Complexity', 'Category', 'Mentor(s)', 'Tech Skills Needed']:
mandatoryHeaders+=f'- {headerMessages[header]}\n'
else:
optionalHeaders+=f'- {headerMessages[header]}\n'
if markdown_dict.get("Complexity") and markdown_dict.get("Complexity").lower() not in ['low', 'medium', 'high']:
mandatoryHeaders+=f'- {headerMessages["Invalid Complexity"]}\n'
if 'Mentor(s)' in markdown_dict and not markdown_dict["Mentor(s)"]:
mandatoryHeaders+=f'-Please make sure the Mentor(s) field is not empty\n'
mandatoryHeaderText = f'''\nMandatory Details - The following details essential to submit tickets to C4GT Community Program are missing. Please add them!
{mandatoryHeaders}\nWithout these details, the ticket cannot be listed on the C4GT Community Listing.\n'''
optionalHeaderText = f'''\nImportant Details - These following details are helpful for contributors to effectively identify and contribute to tickets.
{optionalHeaders}\n'''
body = f'''Hi! {mandatoryHeaderText if mandatoryHeaders else ''}{optionalHeaderText if optionalHeaders else ''}
Please update the ticket
'''
return body
async def createComment(self, owner, repo, issue_number, markdown_dict):
return None
token = await GithubAPI().authenticate_app_as_installation(repo_owner=owner)
print('token checked ', token)
print(f'creating comments at {datetime.now()}')
url = f'https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}/comments'
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28'
}
data = {
'body': f'{self.feedBackMessageCreator(markdown_dict=markdown_dict)}'
}
print(f'posting data at {datetime.now()} with {data}')
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as response:
if response.status == 201:
print(f'Comment created successfully. at {datetime.now()}')
return await response.json()
else:
print(f'Error creating comment. Status code: {response.status}', sys.stderr)
response_text = await response.text()
print(f'Response body: {response_text}', file=sys.stderr)
async def updateComment(self, owner, repo, comment_id, markdown_dict):
return None
token = await GithubAPI().authenticate_app_as_installation(repo_owner=owner)
print('token checked ', token)
url = f'https://api.github.com/repos/{owner}/{repo}/issues/comments/{comment_id}'
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28'
}
data = {
'body': f'{self.feedBackMessageCreator(markdown_dict)}'
}
print(f'updating comments at {datetime.now()} with {data}')
async with aiohttp.ClientSession() as session:
async with session.patch(url, headers=headers, json=data) as response:
if response.status == 200:
print('Comment updated successfully.')
return await response.json()
else:
print(f'Error updating comment. Status code: {response.status}', file=sys.stderr)
response_text = await response.text()
print(f'Response body: {response_text}',file=sys.stderr)
async def deleteComment(self, owner, repo, comment_id):
token = await GithubAPI().authenticate_app_as_installation(repo_owner=owner)
url = f'https://api.github.com/repos/{owner}/{repo}/issues/comments/{comment_id}'
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {token}',
'X-GitHub-Api-Version': '2022-11-28'
}
async with aiohttp.ClientSession() as session:
async with session.delete(url, headers=headers) as response:
if response.status == 204:
print('Comment deleted successfully.')
else:
print(f'Error deleting comment. Status code: {response.status}', file=sys.stderr)
response_text = await response.text()
print(f'Response body: {response_text}', file=sys.stderr)