Skip to content

Commit c0966a1

Browse files
authored
Merge pull request #6 from composer-version-manager/refactor-list-tags
Updated the list function to run through all pages
2 parents 8ac1e04 + 29ded1a commit c0966a1

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"python.linting.pylintEnabled": true,
3+
"python.linting.enabled": true
4+
}

cvm/services/composer_service.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@ class ComposerService:
1616

1717
def __init__(self, github_service: GitHubService):
1818
self.github_service = github_service
19-
self.tags = None
2019

2120
def list_tags(self):
22-
if self.tags is None:
23-
self.tags = self.github_service.list_tags()
24-
25-
return self.tags
21+
return self.github_service.list_tags()
2622

2723
def tag_exists(self, desired_tag: str) -> bool:
28-
github_tags = self.list_tags()
29-
30-
for tag_object in github_tags:
24+
for tag_object in self.list_tags():
3125
if tag_object.get('name') == desired_tag:
3226
return True
33-
3427
return False
3528

3629
@staticmethod

cvm/services/github_service.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import sys
3+
from typing import Generator
34

45
import requests
56

@@ -9,17 +10,24 @@ def __init__(self, owner_id: str, repo_id: str):
910
self.owner_id = owner_id
1011
self.repo_id = repo_id
1112

12-
def list_tags(self) -> requests.Response:
13+
def list_tags(self) -> Generator[object]:
14+
15+
page = 1
1316
headers = {'Accept': 'application/vnd.github.v3+json'}
14-
r = requests.get(
15-
"https://api.github.com/repos/{}/{}/tags".format(self.owner_id, self.repo_id),
16-
headers=headers
17-
)
17+
while True:
18+
19+
url = f"https://api.github.com/repos/{self.owner_id}/{self.repo_id}/tags?page={page}"
20+
r = requests.get(url, headers=headers)
21+
22+
if not r.ok:
23+
logging.error('Failed to fetch available tags from GitHub.')
24+
sys.exit(1)
1825

19-
if r.status_code >= 400:
20-
logging.error('Failed to fetch available tags from GitHub.')
21-
sys.exit(1)
26+
json = r.json()
27+
if not json:
28+
return
2229

23-
json = r.json()
30+
for i in json:
31+
yield i
32+
page += 1
2433

25-
return json

0 commit comments

Comments
 (0)