Skip to content

Commit d7fd713

Browse files
committed
format and lint changes
1 parent b2046f7 commit d7fd713

File tree

4 files changed

+128
-77
lines changed

4 files changed

+128
-77
lines changed

abr-testing/abr_testing/automation/jira_tool.py

Lines changed: 117 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import json
66
import webbrowser
77
import argparse
8-
from typing import Dict, List, Tuple
8+
from typing import Dict, List, Tuple, Any
99
from abr_testing.data_collection import read_robot_logs, abr_google_drive, get_run_logs
1010

11+
1112
def get_error_runs_from_robot(ip: str) -> List[str]:
1213
"""Get runs that have errors from robot."""
1314
error_run_ids = []
@@ -22,23 +23,28 @@ def get_error_runs_from_robot(ip: str) -> List[str]:
2223
if not run["current"] and num_of_errors > 0:
2324
error_run_ids.append(run_id)
2425
return error_run_ids
25-
26-
def get_error_info_from_robot(ip: str, one_run: str, storage_directory: str)-> Tuple[str, str, str, List[str], Dict[str, str]] :
26+
27+
28+
def get_error_info_from_robot(
29+
ip: str, one_run: str, storage_directory: str
30+
) -> Tuple[str, str, str, List[str], str, str]:
2731
"""Get error information from robot to fill out ticket."""
2832
description = dict()
2933
# get run information
3034
results = get_run_logs.get_run_data(one_run, ip)
3135
# save run information to local directory as .json file
32-
saved_file_path = read_robot_logs.save_run_log_to_json(ip, results, storage_directory)
33-
36+
saved_file_path = read_robot_logs.save_run_log_to_json(
37+
ip, results, storage_directory
38+
)
39+
3440
# Error Printout
3541
(
36-
num_of_errors,
37-
error_type,
38-
error_code,
39-
error_instrument,
40-
error_level,
41-
) = read_robot_logs.get_error_info(results)
42+
num_of_errors,
43+
error_type,
44+
error_code,
45+
error_instrument,
46+
error_level,
47+
) = read_robot_logs.get_error_info(results)
4248
# JIRA Ticket Fields
4349
failure_level = "Level " + str(error_level) + " Failure"
4450
components = [failure_level, "Flex-RABR"]
@@ -47,35 +53,54 @@ def get_error_info_from_robot(ip: str, one_run: str, storage_directory: str)-> T
4753
print(parent)
4854
summary = parent + "_" + str(one_run) + "_" + str(error_code) + "_" + error_type
4955
# Description of error
50-
description["protocol_name"] = results["protocol"]["metadata"].get("protocolName", "")
56+
description["protocol_name"] = results["protocol"]["metadata"].get(
57+
"protocolName", ""
58+
)
5159
description["error"] = " ".join([error_code, error_type, error_instrument])
5260
description["protocol_step"] = list(results["commands"])[-1]
5361
description["right_mount"] = results.get("right", "No attachment")
5462
description["left_mount"] = results.get("left", "No attachment")
5563
description["gripper"] = results.get("extension", "No attachment")
5664
all_modules = abr_google_drive.get_modules(results)
5765
whole_description = {**description, **all_modules}
58-
whole_description_str = ("{" + "\n".join("{!r}: {!r},".format(k, v) for k, v in whole_description.items()) + "}")
59-
60-
return summary, parent, affects_version, components, whole_description_str, saved_file_path
66+
whole_description_str = (
67+
"{"
68+
+ "\n".join("{!r}: {!r},".format(k, v) for k, v in whole_description.items())
69+
+ "}"
70+
)
71+
72+
return (
73+
summary,
74+
parent,
75+
affects_version,
76+
components,
77+
whole_description_str,
78+
saved_file_path,
79+
)
80+
6181

6282
class JiraTicket:
6383
"""Connects to JIRA ticket site."""
84+
6485
def __init__(self, url: str, api_token: str, email: str) -> None:
6586
"""Connect to jira."""
66-
87+
6788
self.url = url
6889
self.api_token = api_token
6990
self.email = email
7091
self.auth = HTTPBasicAuth(email, api_token)
7192
self.headers = {
7293
"Accept": "application/json",
73-
"Content-Type": "application/json"
74-
}
75-
76-
def issues_on_board(self, board_id) -> Dict[str, any]:
94+
"Content-Type": "application/json",
95+
}
96+
97+
def issues_on_board(self, board_id) -> List[str]:
7798
"""Print Issues on board."""
78-
response = requests.get(f"{self.url}/rest/agile/1.0/board/{board_id}/issue", headers=self.headers, auth=self.auth)
99+
response = requests.get(
100+
f"{self.url}/rest/agile/1.0/board/{board_id}/issue",
101+
headers=self.headers,
102+
auth=self.auth,
103+
)
79104
response.raise_for_status()
80105
try:
81106
board_data = response.json()
@@ -87,41 +112,47 @@ def issues_on_board(self, board_id) -> Dict[str, any]:
87112
issue_id = i.get("id")
88113
issue_ids.append(issue_id)
89114
return issue_ids
90-
91-
def open_issue(self, issue_key)-> None:
115+
116+
def open_issue(self, issue_key) -> None:
92117
"""Open issue on web browser."""
93118
url = f"{self.url}/browse/{issue_key}"
94119
webbrowser.open(url)
95-
96-
97-
def create_ticket(self, summary: str, description: str, project_key: str, issue_type: str, priority: str, components: list, affects_versions: str, robot: str) -> str:
120+
121+
def create_ticket(
122+
self,
123+
summary: str,
124+
description: str,
125+
project_key: str,
126+
issue_type: str,
127+
priority: str,
128+
components: list,
129+
affects_versions: str,
130+
robot: str,
131+
) -> Tuple[str, str]:
98132
"""Create ticket."""
99133
data = {
100-
"fields": {
101-
"project": {"id": "10273", "key": project_key},
102-
"issuetype": {"name": issue_type},
103-
"summary": summary,
104-
"reporter": {"accountId": "712020:f32d03f8-f91a-465d-871b-45135b7b955f"},
105-
"parent": {"key": robot},
106-
"priority": {"name": priority},
107-
"components": [{"name": component} for component in components],
108-
"versions": [{"name": affects_versions}],
109-
"description": {
110-
"content": [
111-
{
134+
"fields": {
135+
"project": {"id": "10273", "key": project_key},
136+
"issuetype": {"name": issue_type},
137+
"summary": summary,
138+
"reporter": {
139+
"accountId": "712020:f32d03f8-f91a-465d-871b-45135b7b955f"
140+
},
141+
"parent": {"key": robot},
142+
"priority": {"name": priority},
143+
"components": [{"name": component} for component in components],
144+
"versions": [{"name": affects_versions}],
145+
"description": {
112146
"content": [
113147
{
114-
"text": description,
115-
"type": "text"
148+
"content": [{"text": description, "type": "text"}],
149+
"type": "paragraph",
116150
}
117151
],
118-
"type": "paragraph"
119-
}
120-
],
121-
"type": "doc",
122-
"version": 1
152+
"type": "doc",
153+
"version": 1,
123154
}
124-
# Include other required fields as needed
155+
# Include other required fields as needed
125156
}
126157
}
127158
try:
@@ -132,36 +163,35 @@ def create_ticket(self, summary: str, description: str, project_key: str, issue_
132163
json=data,
133164
)
134165
response.raise_for_status()
166+
response_str = str(response.content)
135167
issue_url = response.json().get("self")
136168
issue_key = response.json().get("key")
137169
if issue_key is None:
138170
print("Error: Could not create issue. No key returned.")
139-
except requests.exceptions.HTTPError as e:
140-
print(f"HTTP error occurred: {e}")
141-
print(f"Response content: {response.content}")
142-
except json.JSONDecodeError as e:
143-
print(f"JSON decoding error occurred: {e}")
144-
print(f"Response content: {response.content}")
145-
except Exception as e:
146-
print(f"An unexpected error occurred: {e}")
171+
except requests.exceptions.HTTPError:
172+
print(f"HTTP error occurred. Response content: {response_str}")
173+
except json.JSONDecodeError:
174+
print(f"JSON decoding error occurred. Response content: {response_str}")
147175
return issue_url, issue_key
148-
176+
149177
def post_attachment_to_ticket(self, issue_id: str, attachment_path: str):
150178
"""Adds attachments to ticket."""
151-
file = {"file": open(attachment_path, 'rb')}
179+
# TODO: Ensure that file is actually uploaded.
180+
file = {"file": open(attachment_path, "rb")}
152181
JSON_headers = {"Accept": "application/json"}
153182
try:
154183
response = requests.post(
155184
f"{self.url}/rest/api/3/issue/{issue_id}/attachments",
156-
headers = JSON_headers,
157-
auth = self.auth,
158-
files = file
159-
)
185+
headers=JSON_headers,
186+
auth=self.auth,
187+
files=file,
188+
)
160189
print(response)
161-
except json.JSONDecodeError as e:
162-
print(f"JSON decoding error occurred: {e}")
163-
print(f"Response content: {response.content}")
164-
190+
except json.JSONDecodeError:
191+
error_message = str(response.content)
192+
print(f"JSON decoding error occurred. Response content: {error_message}.")
193+
194+
165195
if __name__ == "__main__":
166196
"""Create ticket for specified robot."""
167197
parser = argparse.ArgumentParser(description="Pulls run logs from ABR robots.")
@@ -173,11 +203,11 @@ def post_attachment_to_ticket(self, issue_id: str, attachment_path: str):
173203
help="Path to long term storage directory for run logs.",
174204
)
175205
parser.add_argument(
176-
"robot_ip",
206+
"robot_ip",
177207
metavar="ROBOT_IP",
178-
type = str,
179-
nargs = 1,
180-
help = "IP address of robot as string."
208+
type=str,
209+
nargs=1,
210+
help="IP address of robot as string.",
181211
)
182212
args = parser.parse_args()
183213
storage_directory = args.storage_directory[0]
@@ -186,14 +216,30 @@ def post_attachment_to_ticket(self, issue_id: str, attachment_path: str):
186216
api_token = "ATATT3xFfGF0SZgHzGog6J_bTK3j6HFRnbUC-tE_gB5Sn_56d-NSdlRb5C-ywGxNgV4fHfSGbhgENlGKI1aSPc5dy8ql0EiVYW4dHifJhyKUueu5yq6BrL9Xcsfwz_p1W6xUmSsyecvZ0lGPFLb5sreqRg5kE3FRs6SqcKaQxH6_EGFGS0Obb5c=BA8756FE"
187217
188218
board_id = "217"
189-
219+
190220
ticket = JiraTicket(url, api_token, email)
191221
error_runs = get_error_runs_from_robot(ip)
192222
one_run = error_runs[0]
193223
print(one_run)
194-
summary, robot, affects_version, components, whole_description_str, saved_file_path = get_error_info_from_robot(ip, one_run, storage_directory)
224+
(
225+
summary,
226+
robot,
227+
affects_version,
228+
components,
229+
whole_description_str,
230+
saved_file_path,
231+
) = get_error_info_from_robot(ip, one_run, storage_directory)
195232
project_key = "RABR"
196233
parent_key = project_key + "-" + robot[-1]
197-
issue_url, issue_key = ticket.create_ticket(summary, whole_description_str, project_key, "Bug", "Medium", components, affects_version, parent_key)
234+
issue_url, issue_key = ticket.create_ticket(
235+
summary,
236+
whole_description_str,
237+
project_key,
238+
"Bug",
239+
"Medium",
240+
components,
241+
affects_version,
242+
parent_key,
243+
)
198244
ticket.open_issue(issue_key)
199245
ticket.post_attachment_to_ticket(issue_key, saved_file_path)

abr-testing/abr_testing/data_collection/get_run_logs.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def get_run_ids_from_robot(ip: str) -> Set[str]:
2323
run_ids.add(run_id)
2424
return run_ids
2525

26+
2627
def get_run_data(one_run: Any, ip: str) -> Dict[str, Any]:
2728
"""Use http requests to get command, health, and protocol data from robot."""
2829
response = requests.get(
@@ -79,9 +80,9 @@ def save_runs(runs_to_save: Set[str], ip: str, storage_directory: str) -> Set[st
7980
saved_file_paths = set()
8081
for a_run in runs_to_save:
8182
data = get_run_data(a_run, ip)
82-
data_file_name = ip + "_" + data["run_id"] + ".json"
83-
saved_file_path = os.path.join(storage_directory, data_file_name)
84-
json.dump(data, open(saved_file_path, mode="w"))
83+
saved_file_path = read_robot_logs.save_run_log_to_json(
84+
ip, data, storage_directory
85+
)
8586
saved_file_paths.add(saved_file_path)
8687
print(f"Saved {len(runs_to_save)} run(s) from robot with IP address {ip}.")
8788
return saved_file_paths

abr-testing/abr_testing/data_collection/read_robot_logs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,17 @@ def get_unseen_run_ids(runs: Set[str], runs_from_storage: Set[str]) -> Set[str]:
137137
print(f"There are {str(len(runs_to_save))} new run(s) to save.")
138138
return runs_to_save
139139

140-
def save_run_log_to_json(ip:str, results: Dict[str, Any], storage_directory: str)-> str:
140+
141+
def save_run_log_to_json(
142+
ip: str, results: Dict[str, Any], storage_directory: str
143+
) -> str:
141144
"""Save run log to local json file."""
142145
data_file_name = ip + "_" + results["run_id"] + ".json"
143146
saved_file_path = os.path.join(storage_directory, data_file_name)
144-
json.dump(results, open(saved_file_path, mode = "w"))
147+
json.dump(results, open(saved_file_path, mode="w"))
145148
return saved_file_path
146149

150+
147151
def get_run_ids_from_google_drive(google_drive: Any) -> Set[str]:
148152
"""Get run ids in google drive folder."""
149153
# Run ids in google_drive_folder

abr-testing/abr_testing/tools/abr_asair_sensor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time as t
77
from typing import List
88
import argparse
9-
from abr_testing.google_automation import google_sheets_tool
9+
from abr_testing.automation import google_sheets_tool
1010

1111

1212
class _ABRAsairSensor:

0 commit comments

Comments
 (0)