diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d8283772..4e509a4e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -247,14 +247,16 @@ Here are some general guidelines ## Adding New Configuration Properties If you want to add a new configuration value to the module, you must modify the following: - * In `Restore-GitHubConfiguration`, update `$config` to declare the new property along with - it's default value, being sure that PowerShell will understand what its type is. + * In `Import-GitHubConfiguration`, update `$config` to declare the new property along with + its default value, being sure that PowerShell will understand what its type is. Properties + should be alphabetical. * Update `Get-GitHubConfiguration` and add the new property name to the `ValidateSet` list so that tab-completion and documentation gets auto-updated. You shouldn't have to add anything - to the body of the method. + to the body of the method. Property names should be alphabetical. * Add a new explicit parameter to `Set-GitHubConfiguration` to receive the property, along with updating the CBH (Comment Based Help) by adding a new `.PAREMETER` entry. You shouldn't - have to add anything to the body of the method. + have to add anything to the body of the method. Parameters should be alphabetical save for the + `SessionOnly` switch, which should be last. ---------- diff --git a/GitHubConfiguration.ps1 b/GitHubConfiguration.ps1 index 5835b606..2097a7f5 100644 --- a/GitHubConfiguration.ps1 +++ b/GitHubConfiguration.ps1 @@ -67,6 +67,11 @@ function Set-GitHubConfiguration The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + .PARAMETER ApiHostName + The hostname of the GitHub instance to communicate with. Defaults to 'github.com'. Provide a + different hostname when using a GitHub Enterprise server. Do not include the HTTP/S prefix, + and do not include 'api'. For example, use "github.contoso.com". + .PARAMETER ApplicationInsightsKey Change the Application Insights instance that telemetry will be reported to (if telemetry hasn't been disabled via DisableTelemetry). @@ -158,10 +163,19 @@ function Set-GitHubConfiguration Disables the logging of any activity to the logfile specified in LogPath, but for this session only. + + .EXAMPLE + Set-GitHubConfiguration -ApiHostName "github.contoso.com" + + Sets all requests to connect to a GitHub Enterprise server running at + github.contoso.com. #> [CmdletBinding(SupportsShouldProcess)] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] param( + [ValidatePattern('^(?!https?:)(?!api\.)(?!www\.).*')] + [string] $ApiHostName, + [string] $ApplicationInsightsKey, [string] $AssemblyPath, @@ -239,7 +253,7 @@ function Get-GitHubConfiguration Always returns the value for this session, which may or may not be the persisted setting (that all depends on whether or not the setting was previously modified - during this session using Set-GitHubCOnfiguration -SessionOnly). + during this session using Set-GitHubConfiguration -SessionOnly). The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub @@ -257,6 +271,7 @@ function Get-GitHubConfiguration param( [Parameter(Mandatory)] [ValidateSet( + 'ApiHostName', 'ApplicationInsightsKey', 'AssemblyPath', 'DefaultNoStatus', @@ -589,6 +604,7 @@ function Import-GitHubConfiguration } $config = [PSCustomObject]@{ + 'apiHostName' = 'github.com' 'applicationInsightsKey' = '66d83c52-3070-489b-886b-09860e05e78a' 'assemblyPath' = [String]::Empty 'disableLogging' = ([String]::IsNullOrEmpty($logPath)) diff --git a/GitHubCore.ps1 b/GitHubCore.ps1 index 36d5c117..6e365861 100644 --- a/GitHubCore.ps1 +++ b/GitHubCore.ps1 @@ -2,9 +2,6 @@ # Licensed under the MIT License. @{ - gitHubApiUrl = 'https://api.github.com' - gitHubApiReposUrl = 'https://api.github.com/repos' - gitHubApiOrgsUrl = 'https://api.github.com/orgs' defaultAcceptHeader = 'application/vnd.github.v3+json' mediaTypeVersion = 'v3' squirrelAcceptHeader = 'application/vnd.github.squirrel-girl-preview' @@ -154,7 +151,9 @@ function Invoke-GHRestMethod # we'll just always continue the existing one... $stopwatch.Start() - $url = "$script:gitHubApiUrl/$UriFragment" + $hostName = $(Get-GitHubConfiguration -Name "ApiHostName") + + $url = "https://api.$hostName/$UriFragment" # It's possible that we are directly calling the "nextLink" from a previous command which # 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 repositoryName = [String]::Empty } - if (($Uri -match '^https?://(?:www.)?github.com/([^/]+)/?([^/]+)?(?:/.*)?$') -or - ($Uri -match '^https?://api.github.com/repos/([^/]+)/?([^/]+)?(?:/.*)?$')) + $hostName = $(Get-GitHubConfiguration -Name "ApiHostName") + + if (($Uri -match "^https?://(?:www.)?$hostName/([^/]+)/?([^/]+)?(?:/.*)?$") -or + ($Uri -match "^https?://api.$hostName/repos/([^/]+)/?([^/]+)?(?:/.*)?$")) { $components.ownerName = $Matches[1] if ($Matches.Count -gt 2) diff --git a/README.md b/README.md index 3496db09..ebb14f45 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,15 @@ Set-GitHubConfiguration -DefaultRepositoryName PowerShellForGitHub There are more great configuration options available. Just review the help for that command for the most up-to-date list! +### GitHub Enterprise + +To set the configuration to use a GitHub Enterprise server instead of GitHub.com, simply supply +the `ApiHostName` parameter with the hostname of your GitHub Enterprise server. + + ```powershell +Set-GitHubConfiguration -ApiHostName "github.contoso.com" +``` + ---------- ## Usage