Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add URL config for GitHub Enterprise #101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Cellivar marked this conversation as resolved.
Show resolved Hide resolved
HowardWolosky marked this conversation as resolved.
Show resolved Hide resolved
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.

----------

Expand Down
18 changes: 17 additions & 1 deletion GitHubConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -257,6 +271,7 @@ function Get-GitHubConfiguration
param(
[Parameter(Mandatory)]
[ValidateSet(
'ApiHostName',
'ApplicationInsightsKey',
'AssemblyPath',
'DefaultNoStatus',
Expand Down Expand Up @@ -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))
Expand Down
13 changes: 7 additions & 6 deletions GitHubCore.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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")
HowardWolosky marked this conversation as resolved.
Show resolved Hide resolved

if (($Uri -match "^https?://(?:www.)?$hostName/([^/]+)/?([^/]+)?(?:/.*)?$") -or
($Uri -match "^https?://api.$hostName/repos/([^/]+)/?([^/]+)?(?:/.*)?$"))
{
$components.ownerName = $Matches[1]
if ($Matches.Count -gt 2)
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down