Skip to content

Commit 8912572

Browse files
kiblikMaffooch
andauthored
Ruff: Add and fix TRY300 (#11643)
Co-authored-by: Cody Maffucci <46459665+Maffooch@users.noreply.github.com>
1 parent 99cb28b commit 8912572

File tree

14 files changed

+47
-44
lines changed

14 files changed

+47
-44
lines changed

dojo/api_v2/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,12 @@ def update_jira_epic(self, request, pk=None):
667667
{"info": "Jira Epic create query sent"},
668668
status=status.HTTP_200_OK,
669669
)
670-
return response
671670
except ValidationError:
672671
return Response(
673672
{"error": "Bad Request!"},
674673
status=status.HTTP_400_BAD_REQUEST,
675674
)
675+
return response
676676

677677

678678
# @extend_schema_view(**schema_with_prefetch())

dojo/authorization/roles_permissions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class Roles(IntEnum):
1212
def has_value(cls, value):
1313
try:
1414
Roles(value)
15-
return True
1615
except ValueError:
1716
return False
17+
return True
1818

1919

2020
def django_enum(cls):
@@ -129,9 +129,9 @@ class Permissions(IntEnum):
129129
def has_value(cls, value):
130130
try:
131131
Permissions(value)
132-
return True
133132
except ValueError:
134133
return False
134+
return True
135135

136136
@classmethod
137137
def get_engagement_permissions(cls):

dojo/context_processors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import contextlib
2+
13
# import the settings file
24
from django.conf import settings
35

@@ -47,12 +49,10 @@ def bind_alert_count(request):
4749
def bind_announcement(request):
4850
from dojo.models import UserAnnouncement
4951

50-
try:
52+
with contextlib.suppress(Exception): # TODO: this should be replaced with more meaningful exception
5153
if request.user.is_authenticated:
5254
user_announcement = UserAnnouncement.objects.select_related(
5355
"announcement",
5456
).get(user=request.user)
5557
return {"announcement": user_announcement.announcement}
56-
return {}
57-
except Exception:
58-
return {}
58+
return {}

dojo/jira_link/helper.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ def get_jira_connection_raw(jira_server, jira_username, jira_password):
406406

407407
logger.debug("logged in to JIRA %s successfully", jira_server)
408408

409-
return jira
410409
except JIRAError as e:
411410
logger.exception("logged in to JIRA %s unsuccessful", jira_server)
412411

@@ -428,6 +427,8 @@ def get_jira_connection_raw(jira_server, jira_username, jira_password):
428427
add_error_message_to_response("Unable to authenticate to JIRA. Please check the URL, username, password, captcha challenge, Network connection. Details in alert on top right. " + str(error_message))
429428
raise
430429

430+
return jira
431+
431432

432433
# Gets a connection to a Jira server based on the finding
433434
def get_jira_connection(obj):
@@ -1185,10 +1186,10 @@ def is_jira_project_valid(jira_project):
11851186
try:
11861187
jira = get_jira_connection(jira_project)
11871188
get_issuetype_fields(jira, jira_project.project_key, jira_project.jira_instance.default_issue_type)
1188-
return True
11891189
except JIRAError:
11901190
logger.debug("invalid JIRA Project Config, can't retrieve metadata for '%s'", jira_project)
11911191
return False
1192+
return True
11921193

11931194

11941195
def jira_attachment(finding, jira, issue, file, jira_filename=None):
@@ -1209,11 +1210,11 @@ def jira_attachment(finding, jira, issue, file, jira_filename=None):
12091210
# read and upload a file
12101211
with open(file, "rb") as f:
12111212
jira.add_attachment(issue=issue, attachment=f)
1212-
return True
12131213
except JIRAError as e:
12141214
logger.exception("Unable to add attachment")
12151215
log_jira_alert("Attachment: " + e.text, finding)
12161216
return False
1217+
return True
12171218
return None
12181219

12191220

@@ -1263,11 +1264,11 @@ def close_epic(eng, push_to_jira, **kwargs):
12631264
if r.status_code != 204:
12641265
logger.warning(f"JIRA close epic failed with error: {r.text}")
12651266
return False
1266-
return True
12671267
except JIRAError as e:
12681268
logger.exception("Jira Engagement/Epic Close Error")
12691269
log_jira_generic_alert("Jira Engagement/Epic Close Error", str(e))
12701270
return False
1271+
return True
12711272
return None
12721273
add_error_message_to_response("Push to JIRA for Epic skipped because enable_engagement_epic_mapping is not checked for this engagement")
12731274
return False
@@ -1304,15 +1305,15 @@ def update_epic(engagement, **kwargs):
13041305
if (epic_priority := kwargs.get("epic_priority")) is not None:
13051306
jira_issue_update_kwargs["priority"] = {"name": epic_priority}
13061307
issue.update(**jira_issue_update_kwargs)
1307-
return True
13081308
except JIRAError as e:
13091309
logger.exception("Jira Engagement/Epic Update Error")
13101310
log_jira_generic_alert("Jira Engagement/Epic Update Error", str(e))
13111311
return False
1312-
else:
1313-
add_error_message_to_response("Push to JIRA for Epic skipped because enable_engagement_epic_mapping is not checked for this engagement")
13141312

1315-
return False
1313+
return True
1314+
1315+
add_error_message_to_response("Push to JIRA for Epic skipped because enable_engagement_epic_mapping is not checked for this engagement")
1316+
return False
13161317

13171318

13181319
@dojo_model_to_id
@@ -1359,7 +1360,6 @@ def add_epic(engagement, **kwargs):
13591360
engagement=engagement,
13601361
jira_project=jira_project)
13611362
j_issue.save()
1362-
return True
13631363
except JIRAError as e:
13641364
# should we try to parse the errors as JIRA is very strange in how it responds.
13651365
# for example a non existent project_key leads to "project key is required" which sounds like something is missing
@@ -1376,9 +1376,11 @@ def add_epic(engagement, **kwargs):
13761376
log_jira_generic_alert("Jira Engagement/Epic Creation Error",
13771377
message + error)
13781378
return False
1379-
else:
1380-
add_error_message_to_response("Push to JIRA for Epic skipped because enable_engagement_epic_mapping is not checked for this engagement")
1381-
return False
1379+
1380+
return True
1381+
1382+
add_error_message_to_response("Push to JIRA for Epic skipped because enable_engagement_epic_mapping is not checked for this engagement")
1383+
return False
13821384

13831385

13841386
def jira_get_issue(jira_project, issue_key):
@@ -1415,10 +1417,10 @@ def add_comment(obj, note, *, force_push=False, **kwargs):
14151417
jira.add_comment(
14161418
j_issue.jira_id,
14171419
f"({note.author.get_full_name() or note.author.username}): {note.entry}")
1418-
return True
14191420
except JIRAError as e:
14201421
log_jira_generic_alert("Jira Add Comment Error", str(e))
14211422
return False
1423+
return True
14221424
return None
14231425
return None
14241426

@@ -1437,10 +1439,10 @@ def add_simple_jira_comment(jira_instance, jira_issue, comment):
14371439
jira.add_comment(
14381440
jira_issue.jira_id, comment,
14391441
)
1440-
return True
14411442
except Exception as e:
14421443
log_jira_generic_alert("Jira Add Comment Error", str(e))
14431444
return False
1445+
return True
14441446

14451447

14461448
def jira_already_linked(finding, jira_issue_key, jira_id) -> Finding | None:

dojo/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3085,9 +3085,9 @@ def has_github_issue(self):
30853085
try:
30863086
# Attempt to access the github issue if it exists. If not, an exception will be caught
30873087
_ = self.github_issue
3088-
return True
30893088
except GITHUB_Issue.DoesNotExist:
30903089
return False
3090+
return True
30913091

30923092
def github_conf(self):
30933093
try:

dojo/templatetags/get_banner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ def get_banner_conf(attribute):
2323
attributes=allowed_attributes,
2424
css_sanitizer=CSSSanitizer(allowed_css_properties=["color", "font-weight"])))
2525
return value
26-
return False
2726
except Exception:
2827
return False
28+
return False

dojo/tools/anchore_enterprise/parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ def policy_name(policies, policy_id):
114114
def extract_vulnerability_id(trigger_id):
115115
try:
116116
vulnerability_id, _ = trigger_id.split("+", 2)
117-
if vulnerability_id.startswith("CVE"):
118-
return vulnerability_id
119-
return None
120117
except ValueError:
121118
return None
119+
if vulnerability_id.startswith("CVE"):
120+
return vulnerability_id
121+
return None
122122

123123

124124
def search_filepath(text):

dojo/tools/anchorectl_policies/parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ def policy_name(policies, policy_id):
116116
def extract_vulnerability_id(trigger_id):
117117
try:
118118
vulnerability_id, _ = trigger_id.split("+", 2)
119-
if vulnerability_id.startswith("CVE"):
120-
return vulnerability_id
121-
return None
122119
except ValueError:
123120
return None
121+
if vulnerability_id.startswith("CVE"):
122+
return vulnerability_id
123+
return None
124124

125125

126126
def search_filepath(text):

dojo/tools/api_sonarqube/importer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ def import_hotspots(self, test):
323323
)
324324
items.append(find)
325325

326-
return items
327-
328326
except Exception as e:
329327
logger.exception("SonarQube API import issue")
330328
create_notification(
@@ -336,6 +334,8 @@ def import_hotspots(self, test):
336334
obj=test.engagement.product,
337335
)
338336

337+
return items
338+
339339
@staticmethod
340340
def clean_rule_description_html(raw_html):
341341
search = re.search(

dojo/tools/kubescape/parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ def parse_resource_id(self, resource_id):
4242
parts = resource_id.split("/")
4343
resource_type = parts[-2]
4444
resource_name = parts[-1]
45-
return resource_type, resource_name
4645
except IndexError:
4746
return None, None
4847

48+
return resource_type, resource_name
49+
4950
def get_findings(self, filename, test):
5051
findings = []
5152
try:

0 commit comments

Comments
 (0)