Skip to content

Commit

Permalink
add jira ticket assignee
Browse files Browse the repository at this point in the history
  • Loading branch information
rclarke0 committed Jun 4, 2024
1 parent 190b0b3 commit 3739737
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
59 changes: 58 additions & 1 deletion abr-testing/abr_testing/automation/jira_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import json
import webbrowser
import argparse
from typing import List
from typing import List, Dict, Any
import os


class JiraTicket:
Expand Down Expand Up @@ -54,6 +55,7 @@ def create_ticket(
description: str,
project_key: str,
reporter_id: str,
assignee_id: str,
issue_type: str,
priority: str,
components: list,
Expand All @@ -67,6 +69,7 @@ def create_ticket(
"issuetype": {"name": issue_type},
"summary": summary,
"reporter": {"id": reporter_id},
"assignee": {"id": assignee_id},
"parent": {"key": robot},
"priority": {"name": priority},
"components": [{"name": component} for component in components],
Expand Down Expand Up @@ -122,6 +125,59 @@ def post_attachment_to_ticket(self, issue_id: str, attachment_path: str) -> None
error_message = str(response.content)
print(f"JSON decoding error occurred. Response content: {error_message}.")

def get_project_issues(self, project_key: str) -> Dict[str, Any]:
"""Retrieve all issues for the given project key."""
headers = {"Accept": "application/json"}
query = {"jql": f"project={project_key}"}
response = requests.request(
"GET",
f"{self.url}/rest/api/3/search",
headers=headers,
params=query,
auth=self.auth,
)
response.raise_for_status()
return response.json()

def extract_users_from_issues(self, issues: dict) -> Dict[str, Any]:
"""Extract users from issues."""
users = dict()
for issue in issues["issues"]:
assignee = issue["fields"].get("assignee")
if assignee is not None:
account_id = assignee["accountId"]
users[account_id] = {
"displayName": assignee["displayName"],
"accountId": account_id,
}
reporter = issue["fields"].get("reporter")
if reporter is not None:
account_id = reporter["accountId"]
users[account_id] = {
"displayName": reporter["displayName"],
"accountId": account_id,
}
return users

def save_users_to_file(self, users: Dict[str, Any], storage_directory: str) -> str:
"""Save users to a JSON file."""
file_path = os.path.join(storage_directory, "RABR_Users.json")
with open(file_path, mode="w") as file:
json.dump(users, file, indent=4)
return file_path

def get_jira_users(self, storage_directory: str) -> str:
"""Get all Jira users associated with the project key."""
try:
issues = self.get_project_issues("RABR")
users = self.extract_users_from_issues(issues)
file_path = self.save_users_to_file(users, storage_directory)
except requests.RequestException as e:
print(f"Request error occurred: {e}")
except json.JSONDecodeError:
print("JSON decoding error occurred.")
return file_path


if __name__ == "__main__":
"""Create ticket for specified robot."""
Expand Down Expand Up @@ -158,6 +214,7 @@ def post_attachment_to_ticket(self, issue_id: str, attachment_path: str) -> None
)
args = parser.parse_args()
url = "https://opentrons.atlassian.net"

api_token = args.jira_api_token[0]
email = args.email[0]
board_id = args.board_id[0]
Expand Down
17 changes: 16 additions & 1 deletion abr-testing/abr_testing/data_collection/abr_robot_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
import os
import subprocess
import sys
import json


def get_user_id(user_file_path: str, assignee_name: str) -> str:
"""Get assignee account id."""
users = json.load(open(user_file_path))
assignee_id = "-1" # Code to leave issue unassigned.
for item in users:
user = users[item]
if user["displayName"] == assignee_name:
assignee_id = user["accountId"]
return assignee_id


def get_error_runs_from_robot(ip: str) -> List[str]:
Expand Down Expand Up @@ -123,13 +135,16 @@ def get_error_info_from_robot(
args = parser.parse_args()
storage_directory = args.storage_directory[0]
ip = str(input("Enter Robot IP: "))
assignee = str(input("Enter Assignee Full Name:"))
url = "https://opentrons.atlassian.net"
api_token = args.jira_api_token[0]
email = args.email[0]
board_id = args.board_id[0]
reporter_id = args.reporter_id[0]
ticket = jira_tool.JiraTicket(url, api_token, email)
ticket.issues_on_board(board_id)
users_file_path = ticket.get_jira_users(storage_directory)
assignee_id = get_user_id(users_file_path, assignee)
try:
error_runs = get_error_runs_from_robot(ip)
except requests.exceptions.InvalidURL:
Expand All @@ -144,7 +159,6 @@ def get_error_info_from_robot(
whole_description_str,
run_log_file_path,
) = get_error_info_from_robot(ip, one_run, storage_directory)
affects_version = "internal release - any"
# Get Calibration Data
saved_file_path_calibration, calibration = read_robot_logs.get_calibration_offsets(
ip, storage_directory
Expand All @@ -161,6 +175,7 @@ def get_error_info_from_robot(
whole_description_str,
project_key,
reporter_id,
assignee_id,
"Bug",
"Medium",
components,
Expand Down

0 comments on commit 3739737

Please sign in to comment.