Skip to content

Commit 0c36b4d

Browse files
committed
Add BaseUri parameter to all cmdlets
This allows PSGitHub to be used with GitHub Enterprise. New-GitHubReleaseAsset had to be adapted to respect upload_url.
1 parent e95afea commit 0c36b4d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+367
-84
lines changed

Functions/Private/Invoke-GitHubApi.ps1

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
1616
.Parameter Uri
1717
This parameter is a mandatory parameter that specifies the URL to request.
18-
If not absolute, it will be resolved relative to https://api.github.com.
18+
If not absolute, it will be resolved relative to the BaseUri parameter.
19+
20+
.Parameter BaseUri
21+
Optional base URL of the GitHub API to resolve Uri from, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
22+
Defaults to "https://api.github.com".
1923
2024
.Parameter Anonymous
2125
If, for some reason, you need to ensure that the REST method is invoked anonymously, you can specify the
@@ -34,11 +38,20 @@
3438
param (
3539
[Parameter(Mandatory, Position = 0)]
3640
[string] $Uri,
41+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
3742

43+
# HTTP headers
3844
[HashTable] $Headers = @{Accept = 'application/vnd.github.v3+json' },
45+
46+
# HTTP request method
3947
[Microsoft.PowerShell.Commands.WebRequestMethod] $Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get,
48+
49+
# Request body or query parameters for GET requests
4050
$Body,
4151

52+
# File path to use as body (instead of $Body).
53+
[string] $InFile,
54+
4255
# Accept header to be added (for accessing preview APIs or different resource representations)
4356
[string[]] $Accept,
4457

@@ -55,7 +68,7 @@
5568
# Resolve the Uri parameter with https://api.github.com as a base URI
5669
# This allows to call this function with just a path,
5770
# but also supply a full URI (e.g. for a GitHub enterprise instance)
58-
$Uri = [Uri]::new([Uri]::new('https://api.github.com'), $Uri)
71+
$Uri = [Uri]::new($BaseUri, $Uri)
5972

6073
$apiRequest = @{
6174
Headers = $Headers;
@@ -80,18 +93,23 @@
8093
$apiRequest.Body = $Body
8194
Write-Verbose -Message ("Request body: " + ($Body | Out-String))
8295
}
96+
if ($InFile) {
97+
$apiRequest.InFile = $InFile
98+
}
8399

84100
# We need to communicate using TLS 1.2 against GitHub.
85101
[Net.ServicePointManager]::SecurityProtocol = 'tls12'
86102

87103
# Invoke the REST API
88104
try {
89105
Invoke-RestMethod @apiRequest -ResponseHeadersVariable responseHeaders
90-
Write-Verbose "Rate limit total: $($responseHeaders['X-RateLimit-Limit'])"
91-
Write-Verbose "Rate limit remaining: $($responseHeaders['X-RateLimit-Remaining'])"
92-
$resetUnixSeconds = [int]($responseHeaders['X-RateLimit-Reset'][0])
93-
$resetDateTime = ([System.DateTimeOffset]::FromUnixTimeSeconds($resetUnixSeconds)).DateTime
94-
Write-Verbose "Rate limit resets: $resetDateTime"
106+
if ($responseHeaders.ContainsKey('X-RateLimit-Limit')) {
107+
Write-Verbose "Rate limit total: $($responseHeaders['X-RateLimit-Limit'])"
108+
Write-Verbose "Rate limit remaining: $($responseHeaders['X-RateLimit-Remaining'])"
109+
$resetUnixSeconds = [int]($responseHeaders['X-RateLimit-Reset'][0])
110+
$resetDateTime = ([System.DateTimeOffset]::FromUnixTimeSeconds($resetUnixSeconds)).DateTime
111+
Write-Verbose "Rate limit resets: $resetDateTime"
112+
}
95113
} catch {
96114
if (
97115
$_.Exception.PSObject.TypeNames -notcontains 'Microsoft.PowerShell.Commands.HttpResponseException' -or # PowerShell Core

Functions/Public/Add-GitHubAssignee.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
# Team slugs whose members will be added as assignees to the issue (in addition to Assignees).
2222
[string[]] $TeamAssignees,
2323

24+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
25+
# Defaults to "https://api.github.com"
26+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
2427
[Security.SecureString] $Token
2528
)
2629

@@ -41,7 +44,7 @@
4144
$shouldProcessWarning = "Do you want to add $($Assignees.Count) assignees the GitHub issue `e[1m#$Number`e[0m in repository `e[1m$Owner/$RepositoryName`e[0m?"
4245

4346
if ($PSCmdlet.ShouldProcess($shouldProcessDescription, $shouldProcessWarning, $shouldProcessCaption)) {
44-
Invoke-GitHubApi -Method POST "repos/$Owner/$RepositoryName/issues/$Number/assignees" -Body ($body | ConvertTo-Json) -Token $Token |
47+
Invoke-GitHubApi -Method POST "repos/$Owner/$RepositoryName/issues/$Number/assignees" -Body ($body | ConvertTo-Json) -BaseUri $BaseUri -Token $Token |
4548
ForEach-Object {
4649
$_.PSTypeNames.Insert(0, 'PSGitHub.Issue')
4750
$_.User.PSTypeNames.Insert(0, 'PSGitHub.User')

Functions/Public/Find-GitHubIssue.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
[Alias('Direction')]
1717
[string] $Order,
1818

19+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
20+
# Defaults to "https://api.github.com"
21+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
1922
[Security.SecureString] $Token = (Get-GitHubToken)
2023
)
2124

@@ -25,7 +28,7 @@
2528
sort = $Sort
2629
order = $Order
2730
}
28-
Invoke-GitHubApi '/search/issues' -Body $params -Token $Token |
31+
Invoke-GitHubApi 'search/issues' -Body $params -BaseUri $BaseUri -Token $Token |
2932
ForEach-Object { $_.items } |
3033
ForEach-Object {
3134
$_.PSTypeNames.Insert(0, 'PSGitHub.Issue')

Functions/Public/Find-GitHubLabel.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
[Alias('Direction')]
2323
[string] $Order,
2424

25+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
26+
# Defaults to "https://api.github.com"
27+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
2528
[Security.SecureString] $Token
2629
)
2730

@@ -35,7 +38,7 @@
3538
$headers = @{
3639
Accept = 'application/vnd.github.symmetra-preview+json'
3740
}
38-
Invoke-GitHubApi '/search/labels' -Body $params -Token $Token -Headers $headers |
41+
Invoke-GitHubApi 'search/labels' -Body $params -BaseUri $BaseUri -Token $Token -Headers $headers |
3942
ForEach-Object { $_.items } |
4043
ForEach-Object {
4144
$_.PSTypeNames.Insert(0, 'PSGitHub.Label')

Functions/Public/Find-GitHubRepository.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
[Alias('Direction')]
3737
[string] $Order,
3838

39+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
40+
# Defaults to "https://api.github.com"
41+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
3942
[Security.SecureString] $Token = (Get-GitHubToken)
4043
)
4144

@@ -53,6 +56,7 @@
5356
Uri = 'search/repositories'
5457
Body = $queryParams
5558
Token = $Token
59+
BaseUri = $BaseUri
5660
}
5761
Invoke-GitHubApi @ApiCall | ForEach-Object { $_.items } | ForEach-Object {
5862
$_.PSTypeNames.Insert(0, 'PSGitHub.Repository')

Functions/Public/Find-GitHubUser.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
[Alias('Direction')]
1717
[string] $Order,
1818

19+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
20+
# Defaults to "https://api.github.com"
21+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
1922
[Security.SecureString] $Token
2023
)
2124

@@ -25,7 +28,7 @@
2528
sort = $Sort
2629
order = $Order
2730
}
28-
Invoke-GitHubApi '/search/users' -Body $params -Token $Token |
31+
Invoke-GitHubApi 'search/users' -Body $params -BaseUri $BaseUri -Token $Token |
2932
ForEach-Object { $_.items } |
3033
ForEach-Object {
3134
$_.PSTypeNames.Insert(0, 'PSGitHub.User')

Functions/Public/Get-GitHubAssignee.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ function Get-GitHubAssignee {
2323
[Alias('Repository')]
2424
[string] $RepositoryName,
2525

26+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
27+
# Defaults to "https://api.github.com"
28+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
2629
[Security.SecureString] $Token
2730
)
2831
process {
29-
Invoke-GitHubApi "/repos/$Owner/$RepositoryName/assignees" -Token $Token | ForEach-Object { $_ } | ForEach-Object {
32+
Invoke-GitHubApi "repos/$Owner/$RepositoryName/assignees" -BaseUri $BaseUri -Token $Token | ForEach-Object { $_ } | ForEach-Object {
3033
$_.PSTypeNames.Insert(0, 'PSGitHub.User')
3134
$_
3235
}

Functions/Public/Get-GitHubCombinedCommitStatus.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
[Alias('FriendlyName')]
2525
[string] $Ref,
2626

27+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
28+
# Defaults to "https://api.github.com"
29+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
2730
[Security.SecureString] $Token
2831
)
2932

3033
process {
31-
Invoke-GithubApi "/repos/$Owner/$RepositoryName/commits/$Ref/status" -Token $Token | ForEach-Object {
34+
Invoke-GitHubApi "repos/$Owner/$RepositoryName/commits/$Ref/status" -BaseUri $BaseUri -Token $Token | ForEach-Object {
3235
$_.PSTypeNames.Insert(0, 'PSGitHub.CombinedCommitStatus')
3336
$_.Repository.PSTypeNames.Insert(0, 'PSGitHub.Repository')
3437
$_.Repository.Owner.PSTypeNames.Insert(0, 'PSGitHub.User')

Functions/Public/Get-GitHubComment.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@
102102
[Parameter(ParameterSetName = 'InIssue')]
103103
[string] $Since,
104104

105+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
106+
# Defaults to "https://api.github.com"
107+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
105108
[Security.SecureString] $Token = (Get-GitHubToken)
106109
)
107110

@@ -139,6 +142,7 @@
139142
Method = 'Get';
140143
Uri = $uri
141144
Token = $Token
145+
BaseUri = $BaseUri
142146
}
143147

144148
Invoke-GitHubApi @apiCall | ForEach-Object { $_ } | ForEach-Object {

Functions/Public/Get-GitHubGist.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
[Parameter(ParameterSetName = 'Target')]
6767
[ValidateSet('Public', 'Starred')]
6868
[String]$Target,
69+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
70+
# Defaults to "https://api.github.com"
71+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
6972
[Security.SecureString] $Token = (Get-GitHubToken)
7073
)
7174

@@ -80,6 +83,7 @@
8083
Uri = $uri
8184
Method = 'Get'
8285
Token = $Token
86+
BaseUri = $BaseUri
8387
}
8488

8589
Invoke-GitHubApi @apiCall | ForEach-Object { $_ } | ForEach-Object {

Functions/Public/Get-GitHubIssue.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138

139139
[string] $Since,
140140

141+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
142+
# Defaults to "https://api.github.com"
143+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
141144
[Security.SecureString] $Token = (Get-GitHubToken)
142145
)
143146

@@ -193,6 +196,7 @@
193196
Method = 'Get';
194197
Uri = $uri
195198
Token = $Token
199+
BaseUri = $BaseUri
196200
}
197201

198202
Invoke-GitHubApi @apiCall | ForEach-Object { $_ } | ForEach-Object {

Functions/Public/Get-GitHubIssueTimeline.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
1616
[int] $Number,
1717

18+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
19+
# Defaults to "https://api.github.com"
20+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
1821
[Security.SecureString] $Token
1922
)
2023

2124
process {
2225
Invoke-GitHubApi "repos/$Owner/$RepositoryName/issues/$Number/timeline" `
2326
-Accept 'application/vnd.github.mockingbird-preview', 'application/vnd.github.starfox-preview+json' `
27+
-BaseUri $BaseUri `
2428
-Token $Token |
2529
ForEach-Object { $_ } |
2630
ForEach-Object {

Functions/Public/Get-GitHubLabel.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949

5050
[string] $Name,
5151

52+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
53+
# Defaults to "https://api.github.com"
54+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
5255
[Security.SecureString] $Token = (Get-GitHubToken)
5356
)
5457

@@ -71,6 +74,7 @@
7174
Method = 'Get'
7275
Uri = $uri
7376
Token = $Token
77+
BaseUri = $BaseUri
7478
}
7579

7680
Invoke-GitHubApi @apiCall | ForEach-Object { $_ } | ForEach-Object {

Functions/Public/Get-GitHubLicense.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
param(
2525
[Parameter(Position = 0)]
2626
[string] $LicenseId,
27+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
28+
# Defaults to "https://api.github.com"
29+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
2730
[Security.SecureString] $Token = (Get-GitHubToken)
2831
)
2932

@@ -36,9 +39,8 @@
3639

3740
$headers = @{
3841
Accept = 'application/vnd.github.drax-preview+json'
39-
Token = $Token
4042
}
41-
Invoke-GitHubApi -Method get -Uri $uri -Headers $headers -Token $Token | ForEach-Object { $_ } | ForEach-Object {
43+
Invoke-GitHubApi -Method get -Uri $uri -Headers $headers -BaseUri $BaseUri -Token $Token | ForEach-Object { $_ } | ForEach-Object {
4244
$_.PSTypeNames.Insert(0, 'PSGitHub.License')
4345
$_
4446
}

Functions/Public/Get-GitHubMilestone.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
[ValidateSet('Ascending', 'Descending')]
5454
[string] $Direction,
5555

56+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
57+
# Defaults to "https://api.github.com"
58+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
5659
[Security.SecureString] $Token = (Get-GitHubToken)
5760
)
5861

@@ -67,12 +70,12 @@
6770
$queryParams.direction = ($Direction -replace 'ending$', '').ToLower();
6871
}
6972
$Uri = if ($Number) {
70-
'/repos/{0}/{1}/milestones/{2}' -f $Owner, $RepositoryName, $Number;
73+
'repos/{0}/{1}/milestones/{2}' -f $Owner, $RepositoryName, $Number;
7174
} else {
72-
'/repos/{0}/{1}/milestones' -f $Owner, $RepositoryName;
75+
'repos/{0}/{1}/milestones' -f $Owner, $RepositoryName;
7376
}
7477

75-
Invoke-GitHubApi -Method GET $Uri -Body $queryParams -Token $Token | ForEach-Object { $_ } | ForEach-Object {
78+
Invoke-GitHubApi -Method GET $Uri -Body $queryParams -BaseUri $BaseUri -Token $Token | ForEach-Object { $_ } | ForEach-Object {
7679
$_.PSTypeNames.Insert(0, 'PSGitHub.Milestone')
7780
$_.Creator.PSTypeNames.Insert(0, 'PSGitHub.User')
7881
$_

Functions/Public/Get-GitHubOrganization.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ function Get-GitHubOrganization {
1010
[Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'Org')]
1111
[string] $OrganizationName,
1212

13+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
14+
# Defaults to "https://api.github.com"
15+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
1316
[Security.SecureString] $Token
1417
)
1518

1619
$url = if ($Username) {
17-
"/users/$Username/orgs"
20+
"users/$Username/orgs"
1821
} elseif ($OrganizationName) {
19-
"/orgs/$OrganizationName"
22+
"orgs/$OrganizationName"
2023
} else {
21-
"/organizations"
24+
"organizations"
2225
}
2326

24-
Invoke-GitHubApi $url -Token $Token | ForEach-Object { $_ } | ForEach-Object {
27+
Invoke-GitHubApi $url -BaseUri $BaseUri -Token $Token | ForEach-Object { $_ } | ForEach-Object {
2528
$_.PSTypeNames.Insert(0, 'PSGitHub.Organization')
2629
$_
2730
}

Functions/Public/Get-GitHubProject.ps1

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@ function Get-GitHubProject {
2020

2121
[string] $Name,
2222

23+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
24+
# Defaults to "https://api.github.com"
25+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
2326
[Security.SecureString] $Token
2427
)
2528

2629
process {
2730
$url = switch ($PSCmdlet.ParameterSetName) {
2831
'Repo' {
29-
"/repos/$Owner/$RepositoryName/projects"
32+
"repos/$Owner/$RepositoryName/projects"
3033
}
3134
'Org' {
32-
"/orgs/$OrganizationName/projects"
35+
"orgs/$OrganizationName/projects"
3336
}
3437
'Id' {
35-
"/projects/$ProjectId"
38+
"projects/$ProjectId"
3639
}
3740
}
38-
Invoke-GitHubApi $url -Accept 'application/vnd.github.inertia-preview+json' -Token $Token |
41+
Invoke-GitHubApi $url -Accept 'application/vnd.github.inertia-preview+json' -BaseUri $BaseUri -Token $Token |
3942
ForEach-Object { $_ } |
4043
Where-Object { -not $Name -or $_.Name -like $Name } |
4144
ForEach-Object {

0 commit comments

Comments
 (0)