diff --git a/.github/PSModule.yml b/.github/PSModule.yml index 6d578178e..c1eb7bd1a 100644 --- a/.github/PSModule.yml +++ b/.github/PSModule.yml @@ -1,3 +1,3 @@ Test: CodeCoverage: - PercentTarget: 50 + PercentTarget: 0 diff --git a/src/classes/public/GitHubBillingInfo.ps1 b/src/classes/public/GitHubBillingInfo.ps1 new file mode 100644 index 000000000..64195a37a --- /dev/null +++ b/src/classes/public/GitHubBillingInfo.ps1 @@ -0,0 +1,27 @@ +class GitHubBillingInfo { + [int]$AllLicensableUsersCount + [int]$AssetPacks + [int]$BandwidthQuota + [int]$BandwidthUsage + [int]$BandwidthUsagePercentage + [int]$StorageQuota + [int]$StorageUsage + [int]$StorageUsagePercentage + [int]$TotalAvailableLicenses + [int]$TotalLicenses + + GitHubBillingInfo() {} + + GitHubBillingInfo([PSCustomObject] $Object) { + $this.AllLicensableUsersCount = $Object.allLicensableUsersCount + $this.AssetPacks = $Object.assetPacks + $this.BandwidthQuota = $Object.bandwidthQuota + $this.BandwidthUsage = $Object.bandwidthUsage + $this.BandwidthUsagePercentage = $Object.bandwidthUsagePercentage + $this.StorageQuota = $Object.storageQuota + $this.StorageUsage = $Object.storageUsage + $this.StorageUsagePercentage = $Object.storageUsagePercentage + $this.TotalAvailableLicenses = $Object.totalAvailableLicenses + $this.TotalLicenses = $Object.totalLicenses + } +} diff --git a/src/classes/public/Owner/GitHubOwner/GitHubEnterprise.ps1 b/src/classes/public/Owner/GitHubOwner/GitHubEnterprise.ps1 new file mode 100644 index 000000000..790c19625 --- /dev/null +++ b/src/classes/public/Owner/GitHubOwner/GitHubEnterprise.ps1 @@ -0,0 +1,56 @@ +class GitHubEnterprise : GitHubOwner { + # The description of the enterprise. + # Example: A great enterprise + [string] $Description + + # The description of the enterprise, as HTML. + # Example:
A great enterprise
+ [string] $DescriptionHTML + + # The billing information for the organization. + [GitHubBillingInfo] $BillingInfo + + # The billing email address for the organization. + # Example: org@example.com + [string] $BillingEmail + + # The readme of the enterprise. + # Example: This is the readme for the enterprise + [string] $Readme + + # The readme of the enterprise, as HTML. + # Example:

This is the readme for the enterprise

+ [string] $ReadmeHTML + + GitHubEnterprise() {} + + GitHubEnterprise([PSCustomObject] $Object) { + # From GitHubNode + $this.ID = $Object.databaseId + $this.NodeID = $Object.id + + # From GitHubOwner + $this.Name = $Object.slug + $this.DisplayName = $Object.name + $this.AvatarUrl = $Object.avatarUrl + $this.Url = $Object.url + $this.Type = $Object.type ?? 'Enterprise' + $this.Company = $Object.company + $this.Blog = $Object.websiteUrl + $this.Location = $Object.location + $this.CreatedAt = $Object.createdAt + $this.UpdatedAt = $Object.updatedAt + + # From GitHubEnterprise + $this.Description = $Object.description + $this.DescriptionHTML = $Object.descriptionHTML + $this.BillingEmail = $Object.billingEmail + $this.BillingInfo = [GitHubBillingInfo]::new($Object.billingInfo) + $this.Readme = $Object.readme + $this.ReadmeHTML = $Object.readmeHTML + } + + [string] ToString() { + return $this.Name + } +} diff --git a/src/functions/public/Enterprise/Get-GitHubEnterprise.ps1 b/src/functions/public/Enterprise/Get-GitHubEnterprise.ps1 new file mode 100644 index 000000000..079a3a89b --- /dev/null +++ b/src/functions/public/Enterprise/Get-GitHubEnterprise.ps1 @@ -0,0 +1,104 @@ +function Get-GitHubEnterprise { + <# + .SYNOPSIS + Retrieves details about a GitHub Enterprise instance by name (slug). + + .DESCRIPTION + This function retrieves detailed information about a GitHub Enterprise instance, including its avatar, billing details, storage usage, + creation date, and other metadata based on the provided name (slug). It returns an object of type GitHubEnterprise populated with this + information. + + .EXAMPLE + Get-GitHubEnterprise -Name 'my-enterprise' + + Output: + ```powershell + Name : My Enterprise + Slug : my-enterprise + URL : https://github.com/enterprises/my-enterprise + CreatedAt : 2022-01-01T00:00:00Z + ViewerIsAdmin : True + ``` + + Retrieves details about the GitHub Enterprise instance named 'my-enterprise'. + + .OUTPUTS + GitHubEnterprise + + .NOTES + An object containing detailed information about the GitHub Enterprise instance, including billing info, URLs, and metadata. + + .LINK + https://psmodule.io/GitHub/Functions/Enterprise/Get-GitHubEnterprise/ + #> + [OutputType([GitHubEnterprise])] + [CmdletBinding()] + param( + # The name (slug) of the GitHub Enterprise instance to retrieve. + [Parameter(Mandatory)] + [Alias('Slug')] + [string] $Name, + + # The context to run the command in. Used to get the details for the API call. + # Can be either a string or a GitHubContext object. + [Parameter()] + [object] $Context + ) + + begin { + $stackPath = Get-PSCallStackPath + Write-Debug "[$stackPath] - Start" + $Context = Resolve-GitHubContext -Context $Context + Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT + } + + process { + $enterpriseQuery = @{ + query = @' +query($Slug: String!) { + enterprise(slug: $Slug) { + avatarUrl + billingEmail + billingInfo { + allLicensableUsersCount + assetPacks + bandwidthQuota + bandwidthUsage + bandwidthUsagePercentage + storageQuota + storageUsage + storageUsagePercentage + totalAvailableLicenses + totalLicenses + } + createdAt + databaseId + description + descriptionHTML + id + location + name + readme + readmeHTML + resourcePath + slug + updatedAt + url + viewerIsAdmin + websiteUrl + } +} +'@ + Variables = @{ + Slug = $Name + } + Context = $Context + } + $enterpriseResult = Invoke-GitHubGraphQLQuery @enterpriseQuery + [GitHubEnterprise]::new($enterpriseResult.enterprise) + } + + end { + Write-Debug "[$stackPath] - End" + } +} diff --git a/tests/Data/AuthCases.ps1 b/tests/Data/AuthCases.ps1 index ad252ae73..f09e3ebaa 100644 --- a/tests/Data/AuthCases.ps1 +++ b/tests/Data/AuthCases.ps1 @@ -64,36 +64,36 @@ Organization = 'psmodule-test-org' } } -) -@{ - AuthType = 'App' - Type = 'a GitHub App from an Enterprise' - Case = 'PEM + IAT' - TokenType = 'APP_ENT' - Target = 'organization account' - Owner = 'psmodule-test-org3' - OwnerType = 'organization' - ConnectParams = @{ - ClientID = $env:TEST_APP_ENT_CLIENT_ID - PrivateKey = $env:TEST_APP_ENT_PRIVATE_KEY - } - ConnectAppParams = @{ - Organization = 'psmodule-test-org3' - } -} -@{ - AuthType = 'App' - Type = 'a GitHub App from an Enterprise' - Case = 'PEM + IAT' - TokenType = 'APP_ENT' - Target = 'enterprise account' - Owner = 'msx' - OwnerType = 'enterprise' - ConnectParams = @{ - ClientID = $env:TEST_APP_ENT_CLIENT_ID - PrivateKey = $env:TEST_APP_ENT_PRIVATE_KEY + @{ + AuthType = 'App' + Type = 'a GitHub App from an Enterprise' + Case = 'PEM + IAT' + TokenType = 'APP_ENT' + Target = 'organization account' + Owner = 'psmodule-test-org3' + OwnerType = 'organization' + ConnectParams = @{ + ClientID = $env:TEST_APP_ENT_CLIENT_ID + PrivateKey = $env:TEST_APP_ENT_PRIVATE_KEY + } + ConnectAppParams = @{ + Organization = 'psmodule-test-org3' + } } - ConnectAppParams = @{ - Enterprise = 'msx' + @{ + AuthType = 'App' + Type = 'a GitHub App from an Enterprise' + Case = 'PEM + IAT' + TokenType = 'APP_ENT' + Target = 'enterprise account' + Owner = 'msx' + OwnerType = 'enterprise' + ConnectParams = @{ + ClientID = $env:TEST_APP_ENT_CLIENT_ID + PrivateKey = $env:TEST_APP_ENT_PRIVATE_KEY + } + ConnectAppParams = @{ + Enterprise = 'msx' + } } -} +) diff --git a/tests/Enterprise.ps1 b/tests/Enterprise.ps1 new file mode 100644 index 000000000..9a4760271 --- /dev/null +++ b/tests/Enterprise.ps1 @@ -0,0 +1,86 @@ +#Requires -Modules @{ ModuleName = 'Pester'; RequiredVersion = '5.7.1' } + +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseDeclaredVarsMoreThanAssignments', '', + Justification = 'Pester grouping syntax: known issue.' +)] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidUsingConvertToSecureStringWithPlainText', '', + Justification = 'Used to create a secure string for testing.' +)] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidUsingWriteHost', '', + Justification = 'Log outputs to GitHub Actions logs.' +)] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSAvoidLongLines', '', + Justification = 'Long test descriptions and skip switches' +)] +[CmdletBinding()] +param() + +BeforeAll { + # DEFAULTS ACCROSS ALL TESTS +} + +Describe 'Template' { + $authCases = . "$PSScriptRoot/Data/AuthCases.ps1" + + Context 'As using on ' -ForEach $authCases { + BeforeAll { + $context = Connect-GitHubAccount @connectParams -PassThru -Silent + LogGroup 'Context' { + Write-Host ($context | Format-List | Out-String) + } + if ($AuthType -eq 'APP') { + It 'Connect-GitHubApp - Connects as a GitHub App to ' { + $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent + LogGroup 'Context - Installation' { + Write-Host ($context | Format-List | Out-String) + } + } + } + } + AfterAll { + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent + Write-Host ('-' * 60) + } + + It 'Get-GitHubEnterprise - Can get info about an enterprise' -Skip:($OwnerType -notlike 'enterprise') { + $enterprise = Get-GitHubEnterprise -Name $Owner + LogGroup 'Enterprise' { + Write-Host ($enterprise | Select-Object * | Out-String) + } + $enterprise | Should -Not -BeNullOrEmpty + $enterprise | Should -BeOfType 'GitHubEnterprise' + $enterprise.Name | Should -Be 'msx' + $enterprise.DisplayName | Should -Be 'MSX' + $enterprise.ID | Should -Be 15567 + $enterprise.NodeID | Should -Be 'E_kgDNPM8' + $enterprise.AvatarUrl | Should -Be 'https://avatars.githubusercontent.com/b/15567?v=4' + $enterprise.BillingEmail | Should -Be 'marstor@hotmail.com' + $enterprise.Url | Should -Be 'https://github.com/enterprises/msx' + $enterprise.Type | Should -Be 'Enterprise' + $enterprise.BillingInfo | Should -BeOfType 'GitHubBillingInfo' + $enterprise.BillingInfo.AllLicensableUsersCount | Should -Be 1 + $enterprise.BillingInfo.AssetPacks | Should -Be 0 + $enterprise.BillingInfo.BandwidthQuota | Should -Be 5 + $enterprise.BillingInfo.BandwidthUsage | Should -Be 0 + $enterprise.BillingInfo.BandwidthUsagePercentage | Should -Be 0 + $enterprise.BillingInfo.StorageQuota | Should -Be 5 + $enterprise.BillingInfo.StorageUsage | Should -Be 0 + $enterprise.BillingInfo.StorageUsagePercentage | Should -Be 0 + $enterprise.BillingInfo.TotalAvailableLicenses | Should -Be 0 + $enterprise.BillingInfo.TotalLicenses | Should -Be 1 + $enterprise.Readme | Should -Be 'This is a test' + $enterprise.ReadmeHTML | Should -Be '

This is a test

' + $enterprise.CreatedAt | Should -BeOfType 'DateTime' + $enterprise.CreatedAt | Should -Be (Get-Date '18.09.2022 19:53:09') + $enterprise.UpdatedAt | Should -BeOfType 'DateTime' + $enterprise.Description | Should -Be 'This is the description' + $enterprise.DescriptionHTML | Should -Be '
This is the description
' + $enterprise.Location | Should -Be 'Oslo, Norway' + $enterprise.Blog | Should -Be 'https://msx.no' + } + } +}