Skip to content

Commit

Permalink
Ruff: Add and fix RUF015
Browse files Browse the repository at this point in the history
  • Loading branch information
kiblik committed Sep 6, 2024
1 parent 422f0aa commit af60be8
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 41 deletions.
54 changes: 27 additions & 27 deletions dojo/reports/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,17 @@ def report_widget_factory(json_data=None, request=None, user=None, finding_notes
selected_widgets = OrderedDict()
widgets = json.loads(json_data)
for idx, widget in enumerate(widgets):
if list(widget.keys())[0] == "page-break":
selected_widgets[list(widget.keys())[0] + "-" + str(idx)] = PageBreak()
if list(widget.keys())[0] == "endpoint-list":
if next(iter(widget.keys())) == "page-break":
selected_widgets[next(iter(widget.keys())) + "-" + str(idx)] = PageBreak()
if next(iter(widget.keys())) == "endpoint-list":
endpoints = Endpoint.objects.filter(finding__active=True,
finding__verified=True,
finding__false_p=False,
finding__duplicate=False,
finding__out_of_scope=False,
).distinct()
d = QueryDict(mutable=True)
for item in widget.get(list(widget.keys())[0]):
for item in widget.get(next(iter(widget.keys()))):
if item["name"] in d:
d.appendlist(item["name"], item["value"])
else:
Expand All @@ -392,12 +392,12 @@ def report_widget_factory(json_data=None, request=None, user=None, finding_notes
endpoints = EndpointList(request=request, endpoints=endpoints, finding_notes=finding_notes,
finding_images=finding_images, host=host, user_id=user_id)

selected_widgets[list(widget.keys())[0] + "-" + str(idx)] = endpoints
selected_widgets[next(iter(widget.keys())) + "-" + str(idx)] = endpoints

if list(widget.keys())[0] == "finding-list":
if next(iter(widget.keys())) == "finding-list":
findings = Finding.objects.all()
d = QueryDict(mutable=True)
for item in widget.get(list(widget.keys())[0]):
for item in widget.get(next(iter(widget.keys()))):
if item["name"] in d:
d.appendlist(item["name"], item["value"])
else:
Expand All @@ -406,47 +406,47 @@ def report_widget_factory(json_data=None, request=None, user=None, finding_notes
filter_class = ReportFindingFilterWithoutObjectLookups if filter_string_matching else ReportFindingFilter
findings = filter_class(d, queryset=findings)
user_id = user.id if user is not None else None
selected_widgets[list(widget.keys())[0] + "-" + str(idx)] = FindingList(request=request, findings=findings,
selected_widgets[next(iter(widget.keys())) + "-" + str(idx)] = FindingList(request=request, findings=findings,
finding_notes=finding_notes,
finding_images=finding_images,
host=host, user_id=user_id)

if list(widget.keys())[0] == "custom-content":
if next(iter(widget.keys())) == "custom-content":
wysiwyg_content = WYSIWYGContent(request=request)
wysiwyg_content.heading = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "heading"), None)["value"]
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "heading"), None)["value"]
wysiwyg_content.content = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "hidden_content"), None)["value"]
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "hidden_content"), None)["value"]
wysiwyg_content.page_break_after = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "page_break_after"),
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "page_break_after"),
{"value": False})["value"]
selected_widgets[list(widget.keys())[0] + "-" + str(idx)] = wysiwyg_content
if list(widget.keys())[0] == "report-options":
selected_widgets[next(iter(widget.keys())) + "-" + str(idx)] = wysiwyg_content
if next(iter(widget.keys())) == "report-options":
options = ReportOptions(request=request)
options.include_finding_notes = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "include_finding_notes"), None)[
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "include_finding_notes"), None)[
"value"]
options.include_finding_images = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "include_finding_images"), None)[
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "include_finding_images"), None)[
"value"]
options.report_type = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "report_type"), None)["value"]
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "report_type"), None)["value"]
options.report_name = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "report_name"), None)["value"]
selected_widgets[list(widget.keys())[0]] = options
if list(widget.keys())[0] == "table-of-contents":
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "report_name"), None)["value"]
selected_widgets[next(iter(widget.keys()))] = options
if next(iter(widget.keys())) == "table-of-contents":
toc = TableOfContents(request=request)
toc.heading = next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "heading"), None)[
toc.heading = next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "heading"), None)[
"value"]
selected_widgets[list(widget.keys())[0]] = toc
if list(widget.keys())[0] == "cover-page":
selected_widgets[next(iter(widget.keys()))] = toc
if next(iter(widget.keys())) == "cover-page":
cover_page = CoverPage(request=request)
cover_page.heading = next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "heading"), None)[
cover_page.heading = next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "heading"), None)[
"value"]
cover_page.sub_heading = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "sub_heading"), None)["value"]
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "sub_heading"), None)["value"]
cover_page.meta_info = \
next((item for item in widget.get(list(widget.keys())[0]) if item["name"] == "meta_info"), None)["value"]
selected_widgets[list(widget.keys())[0]] = cover_page
next((item for item in widget.get(next(iter(widget.keys()))) if item["name"] == "meta_info"), None)["value"]
selected_widgets[next(iter(widget.keys()))] = cover_page

return selected_widgets
2 changes: 1 addition & 1 deletion dojo/tools/api_cobalt/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_connection(self):
orgs = filter(
lambda org: org["resource"]["token"] == self.org_token, data,
)
org = list(orgs)[0]
org = next(iter(orgs))
org_name = org["resource"]["name"]
return f'You have access to the "{org_name}" organization'
else:
Expand Down
2 changes: 1 addition & 1 deletion dojo/tools/burp_enterprise/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_content(self, container):
if elem.text is not None:
s += elem.text + "\n"
elif elem.text.isspace():
s += list(elem.itertext())[0]
s += next(iter(elem.itertext()))
elif elem.tag == "div" or elem.tag == "span":
s += elem.text.strip() + "\n"
else:
Expand Down
6 changes: 3 additions & 3 deletions dojo/tools/sonarqube/soprasteria_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def get_items(self, tree, test, mode):
rulesDic = {}
for rule in rules_table:
rule_properties = list(rule.iter("td"))
rule_name = list(rule_properties[0].iter("a"))[0].text.strip()
rule_details = list(rule_properties[1].iter("details"))[0]
rule_name = next(iter(rule_properties[0].iter("a"))).text.strip()
rule_details = next(iter(rule_properties[1].iter("details")))
rulesDic[rule_name] = rule_details

for vuln in vulnerabilities_table:
vuln_properties = list(vuln.iter("td"))
rule_key = list(vuln_properties[0].iter("a"))[0].text
rule_key = next(iter(vuln_properties[0].iter("a"))).text
vuln_rule_name = rule_key and rule_key.strip()
vuln_severity = SonarQubeSoprasteriaHelper().convert_sonar_severity(
vuln_properties[1].text and vuln_properties[1].text.strip(),
Expand Down
2 changes: 1 addition & 1 deletion dojo/tools/trufflehog/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_findings_v3(self, data, test):
source = {}
source_data = {}
if metadata:
source = list(metadata.keys())[0]
source = next(iter(metadata.keys()))
source_data = metadata.get(source)

file = source_data.get("file", "")
Expand Down
2 changes: 1 addition & 1 deletion dojo/tools/veracode_sca/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def get_findings_csv(self, file, test):
issueId = row.get("Issue ID", None)
if not issueId:
# Workaround for possible encoding issue
issueId = list(row.values())[0]
issueId = next(iter(row.values()))
library = row.get("Library", None)
if row.get("Package manager") == "MAVEN" and row.get(
"Coordinate 2",
Expand Down
2 changes: 1 addition & 1 deletion dojo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def match_finding_to_existing_findings(finding, product=None, engagement=None, t

deduplicationLogger.debug(
"Matching finding %i:%s to existing findings in %s %s using %s as deduplication algorithm.",
finding.id, finding.title, custom_filter_type, list(custom_filter.values())[0], deduplication_algorithm,
finding.id, finding.title, custom_filter_type, next(iter(custom_filter.values())), deduplication_algorithm,
)

if deduplication_algorithm == "hash_code":
Expand Down
1 change: 0 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ ignore = [
"E722",
"RUF010",
"RUF012",
"RUF015",
"RUF027",
]

Expand Down
2 changes: 1 addition & 1 deletion tests/Import_scanner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def test_engagement_import_scan_result(self):
found_matches[index] = matches[0]

if len(found_matches) == 1:
index = list(found_matches.keys())[0]
index = found_matches.keys()[0]
scan_map[test] = options_text[index]
elif len(found_matches) > 1:
try:
Expand Down
2 changes: 1 addition & 1 deletion unittests/tools/test_hadolint_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def test_parse_file_with_one_dockerfile(self):
findings = parser.get_findings(testfile, Test())
testfile.close()
self.assertEqual(4, len(findings))
finding = list(findings)[0]
finding = findings[0]
self.assertEqual(finding.line, 9)
self.assertEqual(finding.file_path, "django-DefectDojo\\Dockerfile.django")

Expand Down
4 changes: 2 additions & 2 deletions unittests/tools/test_intsights_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_intsights_parser_with_one_critical_vuln_has_one_findings_json(

self.assertEqual(1, len(findings))

finding = list(findings)[0]
finding = findings[0]

self.assertEqual(
"5c80dbf83b4a3900078b6be6",
Expand All @@ -32,7 +32,7 @@ def test_intsights_parser_with_one_critical_vuln_has_one_findings_csv(
findings = parser.get_findings(testfile, Test())
self.assertEqual(1, len(findings))

finding = list(findings)[0]
finding = next(iter(findings))

self.assertEqual(
"mn7xy83finmmth4ja363rci9",
Expand Down
2 changes: 1 addition & 1 deletion unittests/tools/test_mend_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_parse_file_with_one_vuln_has_one_findings(self):
parser = MendParser()
findings = parser.get_findings(testfile, Test())
self.assertEqual(1, len(findings))
finding = list(findings)[0]
finding = findings[0]
self.assertEqual(1, len(finding.unsaved_vulnerability_ids))
self.assertEqual("CVE-2019-9658", finding.unsaved_vulnerability_ids[0])
self.assertEqual("CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N", finding.cvssv3)
Expand Down

0 comments on commit af60be8

Please sign in to comment.