Skip to content

Commit 5409929

Browse files
committed
Search comments over multiple api pages
1 parent 38a17a0 commit 5409929

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

image/tools/github_pr_comment.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
github_url = os.environ.get('GITHUB_SERVER_URL', 'https://github.com')
1919
github_api_url = os.environ.get('GITHUB_API_URL', 'https://api.github.com')
2020

21-
def github_api_request(method, *args, **kw_args):
22-
response = github.request(method, *args, **kw_args)
21+
def github_api_request(method, *args, **kwargs):
22+
response = github.request(method, *args, **kwargs)
2323

2424
if 400 <= response.status_code < 500:
2525
debug(str(response.headers))
@@ -49,21 +49,23 @@ def debug(msg: str) -> None:
4949
sys.stderr.write(msg)
5050
sys.stderr.write('\n')
5151

52-
def prs(repo: str) -> Iterable[Dict]:
53-
url = f'{github_api_url}/repos/{repo}/pulls'
52+
def paginate(url, *args, **kwargs) -> Iterable[Dict]:
5453

5554
while True:
56-
response = github_api_request('get', url, params={'state': 'all'})
55+
response = github_api_request('get', url, *args, **kwargs)
5756
response.raise_for_status()
5857

59-
for pr in response.json():
60-
yield pr
58+
yield from response.json()
6159

6260
if 'next' in response.links:
6361
url = response.links['next']['url']
6462
else:
6563
return
6664

65+
def prs(repo: str) -> Iterable[Dict]:
66+
url = f'{github_api_url}/repos/{repo}/pulls'
67+
yield from paginate(url, params={'state': 'all'})
68+
6769

6870
def find_pr() -> str:
6971
"""
@@ -152,13 +154,12 @@ def __init__(self, pr_url: str=None):
152154
response.raise_for_status()
153155

154156
self._issue_url = response.json()['_links']['issue']['href'] + '/comments'
155-
response = github_api_request('get', self._issue_url)
156-
response.raise_for_status()
157157

158158
username = current_user()
159159

160160
debug('Looking for an existing comment:')
161-
for comment in response.json():
161+
162+
for comment in paginate(self._issue_url):
162163
debug(json.dumps(comment))
163164
if comment['user']['login'] == username:
164165
match = re.match(rf'{re.escape(self._comment_identifier)}.*```(?:hcl)?(.*?)```.*', comment['body'], re.DOTALL)

0 commit comments

Comments
 (0)