Skip to content

Commit

Permalink
DEVOPS-95 pagination of list repo end point
Browse files Browse the repository at this point in the history
  • Loading branch information
githubofkrishnadhas committed Jun 26, 2024
1 parent c5961a3 commit 16bd3a6
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions list_repos_in_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,37 @@ def list_repos_in_github_org(orgaization: str, search_string: str):
}

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

all_repositories = []
while True:
# Add pagination parameters to the URL
params = {'per_page': per_page, 'page': page}
response = requests.get(repo_url, headers=headers, params=params)
response_json = response.json() ## Github repo details

# Checking the API status code
if response.status_code == 200:
print(f"API request successful on {repo_url}")
# print(response_json)
# Parse the JSON response
repositories = response.json()
if not repositories:
break
all_repositories.extend(repositories)
params["page"] += 1
else:
print(f"API request failed with status code {response.status_code}:")
# print(response_json)
print(f"Failed to fetch repositories: {response.status_code}")
break

# Get the repo names from the list of dictionaries and add to another list
for repo in response_json:
repo_dict = {}
repo_dict['name'] = repo['full_name']
repo_dict['id'] = repo['id']
list_of_repo_names.append(repo_dict)

page += 1 # Move to the next page

# Get the repo names from the list of dictionaries and add to another list
list_of_repo_names = []
for repo in all_repositories:
repo_dict = {}
repo_dict['name'] = repo['full_name']
repo_dict['id'] = repo['id']
list_of_repo_names.append(repo_dict)

# Finding repos starting with search string
matching_repos = [repo for repo in list_of_repo_names if repo['name'].startswith(f'{orgaization}/{search_string}')]
print(f"Matching repos {search_string} are: {matching_repos}")
return matching_repos
# Break the loop if no more pages
if len(response_json) < per_page:
break
# Finding repos starting with search string
matching_repos = [repo for repo in list_of_repo_names if repo['name'].startswith(f'{orgaization}/{search_string}')]
print(f"Matching repos {search_string} are: {matching_repos}")
return matching_repos


def main():
Expand Down

0 comments on commit 16bd3a6

Please sign in to comment.