-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-GitHubRepositoryTopic.ps1
More file actions
224 lines (179 loc) · 7.94 KB
/
Copy pathGet-GitHubRepositoryTopic.ps1
File metadata and controls
224 lines (179 loc) · 7.94 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
function Get-GitHubRepositoryTopic
{
<#
.SYNOPSIS
Gets GitHub repository topics from an explicit repository or the current Git repository.
.DESCRIPTION
Retrieves the current topic list for a GitHub repository.
The target repository can be provided explicitly through -Repository or omitted to use the
current Git repository's origin remote.
Use -Name to filter the returned topics to one or more specific topic names. Topic filters
are trimmed, lowercased, and de-duplicated before matching. Missing requested topics are
reported in the result object instead of causing the command to fail.
The function prefers the GitHub CLI-backed API transport when `gh` is available and falls
back to the REST API otherwise.
.PARAMETER Name
Optional repository topic name or names to match.
When omitted, all topics are returned.
Topic names are normalized before matching:
- Leading and trailing whitespace is removed
- Names are lowercased
- Duplicate names are removed
.PARAMETER Repository
The target repository in OWNER/REPO or HOST/OWNER/REPO format.
When omitted, the function tries to resolve the current Git repository's origin remote. If
that cannot be determined, specify -Repository explicitly.
Examples:
- octo-org/service-api
- github.example.com/platform/service-api
.PARAMETER Token
Optional GitHub personal access token as a SecureString.
If supplied, the token is used only for the outbound `gh` or REST request and is never
written to command output.
When omitted, the function checks the environment variable named by
-TokenEnvironmentVariableName. If the GitHub CLI is installed, its existing authenticated
session can also be used when no token is supplied. If `gh` is not available and the
function falls back to REST, a token or token environment variable is required.
.PARAMETER TokenEnvironmentVariableName
The environment variable name to check for a GitHub token when -Token is not supplied.
Defaults to GH_TOKEN.
This environment variable is read only when -Token is not supplied. It is used for `gh`
authentication and REST fallback.
.EXAMPLE
PS > Get-GitHubRepositoryTopic -Repository 'octo-org/service-api'
Returns all topics for the specified repository.
.EXAMPLE
PS > Get-GitHubRepositoryTopic -Name 'powershell', 'automation'
Returns matching topics from the current Git repository and reports any requested topics
that are missing.
.EXAMPLE
PS > $token = ConvertTo-SecureString $env:GITHUB_ADMIN_TOKEN -AsPlainText -Force
PS > Get-GitHubRepositoryTopic -Repository 'octo-org/service-api' -Token $token
Uses an explicit token to retrieve repository topics.
.EXAMPLE
PS > Get-GitHubRepositoryTopic -Repository 'octo-org/service-api' -TokenEnvironmentVariableName 'GITHUB_ADMIN_TOKEN'
Reads the GitHub token from a non-default environment variable.
.OUTPUTS
GitHub.RepositoryTopics
Returns the repository topic list, any requested filters, missing requested topics, and
transport metadata.
.NOTES
Repository topics prefer the GitHub CLI-backed transport and fall back to direct REST API
requests when necessary.
.LINK
https://docs.github.com/rest/repos/repos#get-all-repository-topics
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Developer/Get-GitHubRepositoryTopic.ps1
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param(
[Parameter(Position = 0)]
[String[]]$Name,
[Parameter()]
[String]$Repository,
[Parameter()]
[SecureString]$Token,
[Parameter()]
[ValidateNotNullOrEmpty()]
[String]$TokenEnvironmentVariableName = 'GH_TOKEN'
)
function Import-GitHubConfigurationHelpersIfNeeded
{
if (-not (Get-Variable -Name 'PwshProfileGitHubConfigurationHelpers' -Scope Script -ErrorAction SilentlyContinue))
{
$dependencyDirectory = Join-Path -Path $PSScriptRoot -ChildPath 'Private'
$dependencyPath = Join-Path -Path $dependencyDirectory -ChildPath 'GitHubConfigurationHelpers.ps1'
$dependencyPath = [System.IO.Path]::GetFullPath($dependencyPath)
if (-not (Test-Path -LiteralPath $dependencyPath -PathType Leaf))
{
throw "Required dependency could not be found. Expected location: $dependencyPath"
}
try
{
. $dependencyPath
Write-Verbose "Loaded GitHub configuration helpers from: $dependencyPath"
}
catch
{
throw "Failed to load GitHub configuration helpers from '$dependencyPath': $($_.Exception.Message)"
}
}
}
Import-GitHubConfigurationHelpersIfNeeded
$helpers = $script:PwshProfileGitHubConfigurationHelpers
$maxRetryCount = $helpers.DefaultRetryCount
$initialRetryDelaySeconds = $helpers.DefaultInitialRetryDelaySeconds
$requestedTopics = @()
$requestedTopicDisplay = 'all topics'
$topicContext = & $helpers.GetRepositoryTopicsContext -Repository $Repository
$transport = & $helpers.ResolveTransport
try
{
if ($PSBoundParameters.ContainsKey('Name'))
{
$requestedTopics = @(& $helpers.NormalizeTopicNames -Names $Name)
$requestedTopicDisplay = $requestedTopics -join ', '
}
$resolveAuthContextParams = @{
Token = $Token
TokenEnvironmentVariableName = $TokenEnvironmentVariableName
RequireToken = ($transport.Name -ne 'GhCli')
}
$authContext = & $helpers.ResolveAuthContext @resolveAuthContextParams
$tryGetGitHubResourceParams = @{
Path = $topicContext.CollectionPath
BaseUri = $topicContext.ApiBaseUri
Transport = $transport
AuthContext = $authContext
MaxRetryCount = $maxRetryCount
InitialRetryDelaySeconds = $initialRetryDelaySeconds
Activity = "Get repository topics for $($topicContext.RepositoryContext.NameWithOwner)"
}
$resource = & $helpers.TryGetGitHubResource @tryGetGitHubResourceParams
if (-not $resource.Found)
{
throw "GitHub repository topics were not found for $($topicContext.DisplayTarget)."
}
$allTopics = @(& $helpers.NormalizeTopicNames -Names @($resource.Resource.names))
$matchedTopics = if ($requestedTopics.Count -gt 0)
{
@($allTopics | Where-Object { $_ -in $requestedTopics })
}
else
{
$allTopics
}
$missingTopics = if ($requestedTopics.Count -gt 0)
{
@($requestedTopics | Where-Object { $_ -notin $allTopics })
}
else
{
@()
}
return & $helpers.NewOperationResult -TypeName 'GitHub.RepositoryTopics' -Properties @{
Repository = $topicContext.RepositoryContext.GhRepository
Target = $topicContext.DisplayTarget
Topics = $matchedTopics
AllTopics = $allTopics
RequestedTopics = $requestedTopics
MissingTopics = $missingTopics
TopicCount = $matchedTopics.Count
TotalTopicCount = $allTopics.Count
Transport = $transport.Name
Authentication = $authContext.Source
}
}
catch
{
$getFriendlyErrorMessageParams = @{
Operation = 'get repository topics'
Name = $requestedTopicDisplay
Target = $topicContext.DisplayTarget
Exception = $_.Exception
}
$friendlyMessage = & $helpers.GetFriendlyErrorMessage @getFriendlyErrorMessageParams
throw $friendlyMessage
}
}