Skip to content

Commit d5acd0f

Browse files
CellivarHowardWolosky
authored andcommitted
Add URL config for GitHub Enterprise (#101)
Adds the ability to supply a new configuration parameter (ApiHostName) in order to support GitHub Enterprise users. ApiHostName will default to github.com. GitHub Enterprise installs to a different URL and the API remains as api.somegithub.com, thus it's simple to hit that API on a different base URL. Fixed a related typo in the documentation as well. Resolves #98
1 parent 088f95b commit d5acd0f

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

CONTRIBUTING.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,16 @@ Here are some general guidelines
247247
## Adding New Configuration Properties
248248

249249
If you want to add a new configuration value to the module, you must modify the following:
250-
* In `Restore-GitHubConfiguration`, update `$config` to declare the new property along with
251-
it's default value, being sure that PowerShell will understand what its type is.
250+
* In `Import-GitHubConfiguration`, update `$config` to declare the new property along with
251+
its default value, being sure that PowerShell will understand what its type is. Properties
252+
should be alphabetical.
252253
* Update `Get-GitHubConfiguration` and add the new property name to the `ValidateSet` list
253254
so that tab-completion and documentation gets auto-updated. You shouldn't have to add anything
254-
to the body of the method.
255+
to the body of the method. Property names should be alphabetical.
255256
* Add a new explicit parameter to `Set-GitHubConfiguration` to receive the property, along with
256257
updating the CBH (Comment Based Help) by adding a new `.PAREMETER` entry. You shouldn't
257-
have to add anything to the body of the method.
258+
have to add anything to the body of the method. Parameters should be alphabetical save for the
259+
`SessionOnly` switch, which should be last.
258260

259261
----------
260262

GitHubConfiguration.ps1

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ function Set-GitHubConfiguration
6767
6868
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
6969
70+
.PARAMETER ApiHostName
71+
The hostname of the GitHub instance to communicate with. Defaults to 'github.com'. Provide a
72+
different hostname when using a GitHub Enterprise server. Do not include the HTTP/S prefix,
73+
and do not include 'api'. For example, use "github.contoso.com".
74+
7075
.PARAMETER ApplicationInsightsKey
7176
Change the Application Insights instance that telemetry will be reported to (if telemetry
7277
hasn't been disabled via DisableTelemetry).
@@ -158,10 +163,19 @@ function Set-GitHubConfiguration
158163
159164
Disables the logging of any activity to the logfile specified in LogPath, but for this
160165
session only.
166+
167+
.EXAMPLE
168+
Set-GitHubConfiguration -ApiHostName "github.contoso.com"
169+
170+
Sets all requests to connect to a GitHub Enterprise server running at
171+
github.contoso.com.
161172
#>
162173
[CmdletBinding(SupportsShouldProcess)]
163174
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
164175
param(
176+
[ValidatePattern('^(?!https?:)(?!api\.)(?!www\.).*')]
177+
[string] $ApiHostName,
178+
165179
[string] $ApplicationInsightsKey,
166180

167181
[string] $AssemblyPath,
@@ -239,7 +253,7 @@ function Get-GitHubConfiguration
239253
240254
Always returns the value for this session, which may or may not be the persisted
241255
setting (that all depends on whether or not the setting was previously modified
242-
during this session using Set-GitHubCOnfiguration -SessionOnly).
256+
during this session using Set-GitHubConfiguration -SessionOnly).
243257
244258
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
245259
@@ -257,6 +271,7 @@ function Get-GitHubConfiguration
257271
param(
258272
[Parameter(Mandatory)]
259273
[ValidateSet(
274+
'ApiHostName',
260275
'ApplicationInsightsKey',
261276
'AssemblyPath',
262277
'DefaultNoStatus',
@@ -589,6 +604,7 @@ function Import-GitHubConfiguration
589604
}
590605

591606
$config = [PSCustomObject]@{
607+
'apiHostName' = 'github.com'
592608
'applicationInsightsKey' = '66d83c52-3070-489b-886b-09860e05e78a'
593609
'assemblyPath' = [String]::Empty
594610
'disableLogging' = ([String]::IsNullOrEmpty($logPath))

GitHubCore.ps1

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
# Licensed under the MIT License.
33

44
@{
5-
gitHubApiUrl = 'https://api.github.com'
6-
gitHubApiReposUrl = 'https://api.github.com/repos'
7-
gitHubApiOrgsUrl = 'https://api.github.com/orgs'
85
defaultAcceptHeader = 'application/vnd.github.v3+json'
96
mediaTypeVersion = 'v3'
107
squirrelAcceptHeader = 'application/vnd.github.squirrel-girl-preview'
@@ -154,7 +151,9 @@ function Invoke-GHRestMethod
154151
# we'll just always continue the existing one...
155152
$stopwatch.Start()
156153

157-
$url = "$script:gitHubApiUrl/$UriFragment"
154+
$hostName = $(Get-GitHubConfiguration -Name "ApiHostName")
155+
156+
$url = "https://api.$hostName/$UriFragment"
158157

159158
# It's possible that we are directly calling the "nextLink" from a previous command which
160159
# provides the full URI. If that's the case, we'll just use exactly what was provided to us.
@@ -735,8 +734,10 @@ function Split-GitHubUri
735734
repositoryName = [String]::Empty
736735
}
737736

738-
if (($Uri -match '^https?://(?:www.)?github.com/([^/]+)/?([^/]+)?(?:/.*)?$') -or
739-
($Uri -match '^https?://api.github.com/repos/([^/]+)/?([^/]+)?(?:/.*)?$'))
737+
$hostName = $(Get-GitHubConfiguration -Name "ApiHostName")
738+
739+
if (($Uri -match "^https?://(?:www.)?$hostName/([^/]+)/?([^/]+)?(?:/.*)?$") -or
740+
($Uri -match "^https?://api.$hostName/repos/([^/]+)/?([^/]+)?(?:/.*)?$"))
740741
{
741742
$components.ownerName = $Matches[1]
742743
if ($Matches.Count -gt 2)

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ Set-GitHubConfiguration -DefaultRepositoryName PowerShellForGitHub
9999
There are more great configuration options available. Just review the help for that command for
100100
the most up-to-date list!
101101

102+
### GitHub Enterprise
103+
104+
To set the configuration to use a GitHub Enterprise server instead of GitHub.com, simply supply
105+
the `ApiHostName` parameter with the hostname of your GitHub Enterprise server.
106+
107+
```powershell
108+
Set-GitHubConfiguration -ApiHostName "github.contoso.com"
109+
```
110+
102111
----------
103112

104113
## Usage

0 commit comments

Comments
 (0)