Skip to content

Commit

Permalink
Handle pagination links in headers properly
Browse files Browse the repository at this point in the history
  • Loading branch information
sgibson91 committed Jun 16, 2023
1 parent 845a6f8 commit d5d0afa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
17 changes: 12 additions & 5 deletions extra-scripts/comment-deployment-plan-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
import io
import os
import re
import sys
import zipfile

Expand Down Expand Up @@ -45,9 +46,12 @@
# paginated and we need to loop through them to collect all the results.
# It is unlikely that we will have more than 100 artifact results for a single
# worflow ID however.
while "Link" in response.headers.keys():
response = requests.get(response.headers["Link"], headers=headers)
all_artifacts.extend(response["artifacts"])
while ("Link" in response.headers.keys()) and (
'rel="next"' in response.headers["Link"]
):
next_url = re.search(r'(?<=<)([\S]*)(?=>; rel="next")', response.headers["Link"])
response = requests.get(next_url.group(0), headers=headers)
all_artifacts.extend(response.json()["artifacts"])

# Filter for the artifact with the name we want: 'pr'
artifact_id = next(
Expand Down Expand Up @@ -84,8 +88,11 @@

# If "Link" is present in the response headers, that means that the results are
# paginated and we need to loop through them to collect all the results.
while "Link" in response.headers.keys():
response = requests.get(response.headers["Link"], headers=headers)
while ("Link" in response.headers.keys()) and (
'rel="next"' in response.headers["Link"]
):
next_url = re.search(r'(?<=<)([\S]*)(?=>; rel="next")', response.headers["Link"])
response = requests.get(next_url.group(0), headers=headers)
issue_comments.extend(response.json())

# Otherwise, if no `issue_comments`, this will be undefined
Expand Down
14 changes: 9 additions & 5 deletions extra-scripts/comment-test-link-merged-pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,15 @@
"created": f">={today}",
}
response = requests.get(url, headers=headers, params=params)
all_workflow_runs = response["workflow_runs"]

while "Link" in response.headers.keys():
reponse = requests.get(response.headers["Link"], headers=headers)
all_workflow_runs.extend(response["workflow_runs"])
all_workflow_runs = response.json()["workflow_runs"]

# Detect if pagination is required and execute as needed
while ("Link" in response.headers.keys()) and (
'rel="next"' in response.headers["Link"]
):
next_url = re.search(r'(?<=<)([\S]*)(?=>; rel="next")', response.headers["Link"])
response = requests.get(next_url.group(0), headers=headers)
all_workflow_runs.extend(response.json()["workflow_runs"])

workflow_url = next(
(
Expand Down

0 comments on commit d5d0afa

Please sign in to comment.