-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGet-GitHubRepositoryListByOwner.ps1
More file actions
163 lines (142 loc) · 5.37 KB
/
Get-GitHubRepositoryListByOwner.ps1
File metadata and controls
163 lines (142 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
filter Get-GitHubRepositoryListByOwner {
<#
.SYNOPSIS
List repositories for a user
.DESCRIPTION
Lists public repositories for the specified user.
Note: For GitHub AE, this endpoint will list internal repositories for the specified user.
.EXAMPLE
```powershell
Get-GitHubRepositoryListByOwner -Owner 'octocat'
```
Gets the repositories for the user 'octocat'.
.EXAMPLE
```powershell
Get-GitHubRepositoryListByOwner -Owner 'octocat' -Type 'member'
```
Gets the repositories of organizations where the user 'octocat' is a member.
.EXAMPLE
```powershell
Get-GitHubRepositoryListByOwner -Owner 'octocat' -Sort 'created' -Direction 'asc'
```
Gets the repositories for the user 'octocat' sorted by creation date in ascending order.
.OUTPUTS
GitHubRepository
.NOTES
[List repositories for a user](https://docs.github.com/rest/repos/repos#list-repositories-for-a-user)
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments', 'hasNextPage', Scope = 'Function',
Justification = 'Unknown issue with var scoping in blocks.'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseDeclaredVarsMoreThanAssignments', 'after', Scope = 'Function',
Justification = 'Unknown issue with var scoping in blocks.'
)]
[OutputType([GitHubRepository])]
[CmdletBinding()]
param(
# The account owner of the repository. The name is not case sensitive.
[Parameter(Mandatory)]
[string] $Owner,
# Limit the results to repositories with a visibility level.
[ValidateSet('Internal', 'Private', 'Public')]
[Parameter()]
[string] $Visibility,
# Limit the results to repositories where the user has this role.
[ValidateSet('Owner', 'Collaborator', 'Organization_member')]
[Parameter()]
[string[]] $Affiliation,
# Limit the results to repositories where the owner has this affiliation (e.g., OWNER only).
[ValidateSet('Owner', 'Collaborator', 'Organization_member')]
[Parameter()]
[string[]] $OwnerAffiliations = 'Owner',
# Properties to include in the returned object.
[Parameter()]
[string[]] $Property = @('Name', 'Owner', 'Url', 'Size', 'Visibility', 'CustomProperties'),
# Additional properties to include in the returned object.
[Parameter()]
[string[]] $AdditionalProperty = @(),
# The number of results per page (max 100).
[Parameter()]
[System.Nullable[int]] $PerPage,
# The context to run the command in. Used to get the details for the API call.
# Can be either a string or a GitHubContext object.
[Parameter(Mandatory)]
[object] $Context
)
begin {
$stackPath = Get-PSCallStackPath
Write-Debug "[$stackPath] - Start"
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
}
process {
$hasNextPage = $true
$after = $null
$perPageSetting = Resolve-GitHubContextSetting -Name 'PerPage' -Value $PerPage -Context $Context
$graphParams = @{
PropertyList = $Property + $AdditionalProperty
PropertyToGraphQLMap = [GitHubRepository]::PropertyToGraphQLMap
}
$graphQLFields = ConvertTo-GitHubGraphQLField @graphParams
do {
$apiParams = @{
Query = @"
query(
`$Owner: String!,
`$PerPage: Int!,
`$Cursor: String,
`$Affiliations: [RepositoryAffiliation],
`$OwnerAffiliations: [RepositoryAffiliation!],
`$Visibility: RepositoryVisibility,
`$IsArchived: Boolean,
`$IsFork: Boolean
) {
repositoryOwner(
login: `$Owner
) {
repositories(
first: `$PerPage,
after: `$Cursor,
affiliations: `$Affiliations,
ownerAffiliations: `$OwnerAffiliations,
visibility: `$Visibility,
isArchived: `$IsArchived,
isFork: `$IsFork
) {
nodes {
$graphQLFields
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
"@
Variables = @{
Owner = $Owner
PerPage = $perPageSetting
Cursor = $after
Affiliations = [string]::IsNullOrEmpty($Affiliation) ? $null : $Affiliation.ToUpper()
OwnerAffiliations = [string]::IsNullOrEmpty($OwnerAffiliations) ? $null : $OwnerAffiliations.ToUpper()
Visibility = [string]::IsNullOrEmpty($Visibility) ? $null : $Visibility.ToUpper()
IsArchived = $IsArchived
IsFork = $IsFork
}
Context = $Context
}
Invoke-GitHubGraphQLQuery @apiParams | ForEach-Object {
foreach ($repository in $_.repositoryOwner.repositories.nodes) {
[GitHubRepository]::new($repository)
}
$hasNextPage = $_.repositoryOwner.repositories.pageInfo.hasNextPage
$after = $_.repositoryOwner.repositories.pageInfo.endCursor
}
} while ($hasNextPage)
}
end {
Write-Debug "[$stackPath] - End"
}
}