Skip to content

Commit 2c7d5c4

Browse files
committed
Add pagination
1 parent 28c8482 commit 2c7d5c4

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

backup_github_repositories.ps1

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ $stopwatch = [System.Diagnostics.Stopwatch]::startNew()
120120
# @see https://developer.github.com/v3/repos/#list-organization-repositories
121121
# @see https://developer.github.com/v3/repos/#list-your-repositories
122122
#
123-
if($organisationName) {
123+
if ($organisationName) {
124124

125-
$gitHubRepositoriesUrl = "https://api.github.com/orgs/${organisationName}/repos?type=all&per_page=100&page=1"
125+
$gitHubRepositoriesUrl = "https://api.github.com/orgs/${organisationName}/repos?type=all&per_page=50"
126126

127127
} else {
128128

129-
$gitHubRepositoriesUrl = "https://api.github.com/user/repos?affiliation=owner&per_page=100&page=1"
129+
$gitHubRepositoriesUrl = "https://api.github.com/user/repos?affiliation=owner&per_page=50"
130130
}
131131

132132
#
@@ -142,11 +142,22 @@ $requestHeaders = @{
142142
Authorization = "Basic $encodedBasicAuthenticationCredentials"
143143
}
144144

145-
# Request the GitHub API to get all repositories of a user or an organisation.
146-
Write-Message "Requesting '${gitHubRepositoriesUrl}'..."
147-
$repositories = Invoke-WebRequest -Uri $gitHubRepositoriesUrl -Headers $requestHeaders | `
148-
Select-Object -ExpandProperty Content | `
149-
ConvertFrom-Json
145+
# Request the paginated GitHub API to get all repositories of a user or an organisation.
146+
$repositories = @()
147+
$pageNumber = 0
148+
Do {
149+
150+
$pageNumber++
151+
$paginatedGitHubApiUri = "${gitHubRepositoriesUrl}&page=${pageNumber}"
152+
153+
Write-Message "Requesting '${paginatedGitHubApiUri}'..."
154+
$paginatedRepositories = Invoke-WebRequest -Uri $paginatedGitHubApiUri -Headers $requestHeaders | `
155+
Select-Object -ExpandProperty Content | `
156+
ConvertFrom-Json
157+
158+
$repositories += $paginatedRepositories
159+
160+
} Until ($paginatedRepositories.Count -eq 0)
150161

151162
# Print a userfriendly message what will happen next.
152163
$totalSizeInMegabytes = Get-TotalRepositoriesSizeInMegabytes -repositories $repositories

0 commit comments

Comments
 (0)