Skip to content

Commit e483d42

Browse files
Merge pull request #8 from devwithkrishna/feature/add-pagination
DEVOPS-95 pagination of list repo end point
2 parents 6a48939 + 16bd3a6 commit e483d42

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

list_repos_in_org.py

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,37 @@ def list_repos_in_github_org(orgaization: str, search_string: str):
1717
}
1818

1919
# Define pagination parameters
20-
per_page = 100 # Number of records per page
20+
per_page = 20 # Number of records per page
2121
page = 1 # Initial page number
22-
list_of_repo_names = []
22+
params = {'per_page': per_page, 'page': page}
2323

24+
all_repositories = []
2425
while True:
2526
# Add pagination parameters to the URL
26-
params = {'per_page': per_page, 'page': page}
2727
response = requests.get(repo_url, headers=headers, params=params)
28-
response_json = response.json() ## Github repo details
29-
30-
# Checking the API status code
3128
if response.status_code == 200:
32-
print(f"API request successful on {repo_url}")
33-
# print(response_json)
29+
# Parse the JSON response
30+
repositories = response.json()
31+
if not repositories:
32+
break
33+
all_repositories.extend(repositories)
34+
params["page"] += 1
3435
else:
35-
print(f"API request failed with status code {response.status_code}:")
36-
# print(response_json)
36+
print(f"Failed to fetch repositories: {response.status_code}")
3737
break
3838

39-
# Get the repo names from the list of dictionaries and add to another list
40-
for repo in response_json:
41-
repo_dict = {}
42-
repo_dict['name'] = repo['full_name']
43-
repo_dict['id'] = repo['id']
44-
list_of_repo_names.append(repo_dict)
45-
46-
page += 1 # Move to the next page
47-
39+
# Get the repo names from the list of dictionaries and add to another list
40+
list_of_repo_names = []
41+
for repo in all_repositories:
42+
repo_dict = {}
43+
repo_dict['name'] = repo['full_name']
44+
repo_dict['id'] = repo['id']
45+
list_of_repo_names.append(repo_dict)
4846

49-
# Finding repos starting with search string
50-
matching_repos = [repo for repo in list_of_repo_names if repo['name'].startswith(f'{orgaization}/{search_string}')]
51-
print(f"Matching repos {search_string} are: {matching_repos}")
52-
return matching_repos
53-
# Break the loop if no more pages
54-
if len(response_json) < per_page:
55-
break
47+
# Finding repos starting with search string
48+
matching_repos = [repo for repo in list_of_repo_names if repo['name'].startswith(f'{orgaization}/{search_string}')]
49+
print(f"Matching repos {search_string} are: {matching_repos}")
50+
return matching_repos
5651

5752

5853
def main():

0 commit comments

Comments
 (0)