Skip to content

Commit

Permalink
feat(jira): resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrooot committed Nov 8, 2024
1 parent 2835101 commit c973636
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
11 changes: 11 additions & 0 deletions prowler/lib/outputs/jira/exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class JiraBaseException(ProwlerException):
"message": "No token was found.",
"remediation": "Make sure the token is set when using the Jira integration.",
},
(9018, "JiraInvalidProjectKeyError"): {
"message": "The project key is invalid.",
"remediation": "Please check the project key and try again.",
},
}

def __init__(self, code, file=None, original_exception=None, message=None):
Expand Down Expand Up @@ -218,3 +222,10 @@ def __init__(self, file=None, original_exception=None, message=None):
super().__init__(
9017, file=file, original_exception=original_exception, message=message
)


class JiraInvalidProjectKeyError(JiraBaseException):
def __init__(self, file=None, original_exception=None, message=None):
super().__init__(
9018, file=file, original_exception=original_exception, message=message
)
21 changes: 15 additions & 6 deletions prowler/lib/outputs/jira/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
JiraGetProjectsError,
JiraGetProjectsResponseError,
JiraInvalidIssueTypeError,
JiraInvalidProjectKeyError,
JiraNoProjectsError,
JiraNoTokenError,
JiraRefreshTokenError,
Expand Down Expand Up @@ -466,7 +467,7 @@ def test_connection(
)
return Connection(is_connected=False, error=error)

def get_projects(self) -> list[Dict[str, str]]:
def get_projects(self) -> Dict[str, str]:
"""Get the projects from Jira
Returns:
Expand All @@ -492,11 +493,10 @@ def get_projects(self) -> list[Dict[str, str]]:
)

if response.status_code == 200:
# Return the Project Key and Name
projects = [
{"key": project.get("key"), "name": project.get("name")}
for project in response.json()
]
# Return the Project Key and Name, using only a dictionary
projects = {
project["key"]: project["name"] for project in response.json()
}
if len(projects) == 0:
logger.error("No projects found")
raise JiraNoProjectsError(
Expand Down Expand Up @@ -1090,6 +1090,15 @@ def send_findings(
file=os.path.basename(__file__),
)

projects = self.get_projects()

if project_key not in projects:
logger.error("The project key is invalid")
raise JiraInvalidProjectKeyError(
message="The project key is invalid",
file=os.path.basename(__file__),
)

available_issue_types = self.get_available_issue_types(project_key)

if issue_type not in available_issue_types:
Expand Down
24 changes: 16 additions & 8 deletions tests/lib/outputs/jira/jira_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,7 @@ def test_get_projects_successful(
mock_get.return_value = mock_response

projects = self.jira_integration.get_projects()

expected_projects = [
{"key": "PROJ1", "name": "Project One"},
{"key": "PROJ2", "name": "Project Two"},
]
expected_projects = {"PROJ1": "Project One", "PROJ2": "Project Two"}
assert projects == expected_projects

@patch.object(Jira, "get_access_token", return_value="valid_access_token")
Expand Down Expand Up @@ -471,14 +467,20 @@ def test_get_available_issue_types_refresh_token_error(self, mock_get_access_tok
@patch.object(
Jira, "get_available_issue_types", return_value=["Bug", "Task", "Story"]
)
@patch.object(Jira, "get_projects", return_value={"TEST-1": "Test Project"})
@patch("prowler.lib.outputs.jira.jira.requests.post")
def test_send_findings_successful(
self, mock_post, mock_get_available_issue_types, mock_get_access_token
self,
mock_post,
mock_get_available_issue_types,
mock_get_projects,
mock_get_access_token,
):
"""Test successful sending of findings to Jira."""
# To disable vulture
mock_get_available_issue_types = mock_get_available_issue_types
mock_get_access_token = mock_get_access_token
mock_get_projects = mock_get_projects

mock_response = MagicMock()
mock_response.status_code = 201
Expand All @@ -502,15 +504,15 @@ def test_send_findings_successful(
self.jira_integration.cloud_id = "valid_cloud_id"

self.jira_integration.send_findings(
findings=[finding], project_key="TEST", issue_type="Bug"
findings=[finding], project_key="TEST-1", issue_type="Bug"
)

expected_url = (
"https://api.atlassian.com/ex/jira/valid_cloud_id/rest/api/3/issue"
)
expected_json = {
"fields": {
"project": {"key": "TEST"},
"project": {"key": "TEST-1"},
"summary": "[Prowler] HIGH - CHECK-1 - resource-1",
"description": {
"type": "doc",
Expand Down Expand Up @@ -1029,3 +1031,9 @@ def test_send_findings_response_error(
self.jira_integration.send_findings(
findings=[finding], project_key="TEST", issue_type="Bug"
)

def test_get_color_from_status(self):
"""Test that get_color_from_status returns the correct color for a status."""
assert self.jira_integration.get_color_from_status("FAIL") == "#FF0000"
assert self.jira_integration.get_color_from_status("PASS") == "#008000"
assert self.jira_integration.get_color_from_status("MUTED") == "#FFA500"

0 comments on commit c973636

Please sign in to comment.