Skip to content

Commit 2fa4e39

Browse files
committed
Reduce cyclomatic complexity of ReportSizeDeltas.get_json_response()
The cyclomatic complexity of this function exceeded the maximum of 10. To resolve this, the page count determination code was moved to a separate function.
1 parent 19e66af commit 2fa4e39

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

reportsizedeltas.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,11 @@ def get_json_response(self, url):
306306
page_count = 0
307307
additional_pages = False
308308
else:
309-
page_count = 1
310-
additional_pages = False
311-
312-
if response_data["headers"]["Link"] is not None:
313-
# Get the pagination data
314-
if response_data["headers"]["Link"].find(">; rel=\"next\"") != -1:
315-
additional_pages = True
316-
for link in response_data["headers"]["Link"].split(","):
317-
if link[-13:] == ">; rel=\"last\"":
318-
link = re.split("[?&>]", link)
319-
for parameter in link:
320-
if parameter[:5] == "page=":
321-
page_count = int(parameter.split("=")[1])
322-
break
323-
break
309+
page_count = get_page_count(link_header=response_data["headers"]["Link"])
310+
if page_count > 1:
311+
additional_pages = True
312+
else:
313+
additional_pages = False
324314

325315
return {"json_data": json_data, "additional_pages": additional_pages, "page_count": page_count}
326316
except Exception as exception:
@@ -443,6 +433,26 @@ def determine_urlopen_retry(exception):
443433
return False
444434

445435

436+
def get_page_count(link_header):
437+
"""Return the number of pages of the API response
438+
439+
Keyword arguments:
440+
link_header -- Link header of the HTTP response
441+
"""
442+
page_count = 1
443+
if link_header is not None:
444+
# Get the pagination data
445+
for link in link_header.split(","):
446+
if link[-13:] == ">; rel=\"last\"":
447+
link = re.split("[?&>]", link)
448+
for parameter in link:
449+
if parameter[:5] == "page=":
450+
page_count = int(parameter.split("=")[1])
451+
break
452+
break
453+
return page_count
454+
455+
446456
def generate_value_cell(value):
447457
"""Return the Markdown formatted text for a memory change data cell in the report table
448458

tests/test_reportsizedeltas.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ def test_determine_urlopen_retry_false(self):
415415
self.assertFalse(reportsizedeltas.determine_urlopen_retry(
416416
exception=urllib.error.HTTPError(None, 404, "Not Found", None, None)))
417417

418+
# @unittest.skip("")
419+
def test_get_page_count(self):
420+
page_count = 4
421+
link_header = ('<https://api.github.com/repositories/919161/pulls?page=2>; rel="next", '
422+
'"<https://api.github.com/repositories/919161/pulls?page=' + str(page_count) + '>; rel="last"')
423+
424+
self.assertEqual(page_count, reportsizedeltas.get_page_count(link_header=link_header))
425+
418426
# @unittest.skip("")
419427
def test_generate_value_cell(self):
420428
self.assertEqual(" | :small_red_triangle: +42", reportsizedeltas.generate_value_cell(42))

0 commit comments

Comments
 (0)