Skip to content

Commit 12903bc

Browse files
🩹 [Patch]: Add function to Get-GitHubEnterprise (#460)
## Description This pull request introduces significant enhancements to the PowerShell module by adding new functionality for GitHub Enterprise management, improving test coverage, and updating configurations. The most important changes include the addition of new classes to represent enterprise and billing information, a new function to retrieve enterprise details, and comprehensive tests for enterprise-related functionality. ### New functionality for GitHub Enterprise management: * [`src/classes/public/GitHubBillingInfo.ps1`](diffhunk://#diff-38361f5c7ad33bc890c931c733409ac95cb43694afa9c39053ef276ef0cefdceR1-R27): Added a new `GitHubBillingInfo` class to encapsulate billing-related details such as storage usage, bandwidth usage, and license counts. * [`src/classes/public/Owner/GitHubOwner/GitHubEnterprise.ps1`](diffhunk://#diff-79e51205b5a44da8d9bbe5c3c0c99c828d17fc9aca311beddc5f8130e73f4831R1-R52): Added a new `GitHubEnterprise` class to represent enterprise-level details, including metadata, billing information, and readme content. * [`src/functions/public/Enterprise/Get-GitHubEnterprise.ps1`](diffhunk://#diff-60a0985da8d99410c0dfcb246bd1d808064514c465e945de29d1b3e8b222195fR1-R101): Introduced a new `Get-GitHubEnterprise` function to retrieve detailed information about a GitHub Enterprise instance by name (slug). ### Test coverage improvements: * [`tests/Data/AuthCases.ps1`](diffhunk://#diff-a84e4c5f2270b5b1d0e3ea2f5b4fff03f176502507f4e79b1c2cfb6c9abbc874L67): Updated authentication test cases to include enterprise-specific scenarios. [[1]](diffhunk://#diff-a84e4c5f2270b5b1d0e3ea2f5b4fff03f176502507f4e79b1c2cfb6c9abbc874L67) [[2]](diffhunk://#diff-a84e4c5f2270b5b1d0e3ea2f5b4fff03f176502507f4e79b1c2cfb6c9abbc874R99) * [`tests/Enterprise.ps1`](diffhunk://#diff-4c350f9a2d194e35b7a6b6fa4685bcbcf86dec45ffb52f0b0146d39d6f06bc25R1-R87): Added new tests to verify the functionality of `Get-GitHubEnterprise` and validate enterprise-related data, including billing information and metadata. ### Configuration updates: * [`.github/PSModule.yml`](diffhunk://#diff-928165ed381f1982eb8f9746a59a2829db4abc8a28eddb8c109e12bb033ff96aR2-R12): Updated the test configuration to skip certain tests and set `CodeCoverage.PercentTarget` to `0`. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent bb97530 commit 12903bc

File tree

6 files changed

+305
-32
lines changed

6 files changed

+305
-32
lines changed

.github/PSModule.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Test:
22
CodeCoverage:
3-
PercentTarget: 50
3+
PercentTarget: 0
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class GitHubBillingInfo {
2+
[int]$AllLicensableUsersCount
3+
[int]$AssetPacks
4+
[int]$BandwidthQuota
5+
[int]$BandwidthUsage
6+
[int]$BandwidthUsagePercentage
7+
[int]$StorageQuota
8+
[int]$StorageUsage
9+
[int]$StorageUsagePercentage
10+
[int]$TotalAvailableLicenses
11+
[int]$TotalLicenses
12+
13+
GitHubBillingInfo() {}
14+
15+
GitHubBillingInfo([PSCustomObject] $Object) {
16+
$this.AllLicensableUsersCount = $Object.allLicensableUsersCount
17+
$this.AssetPacks = $Object.assetPacks
18+
$this.BandwidthQuota = $Object.bandwidthQuota
19+
$this.BandwidthUsage = $Object.bandwidthUsage
20+
$this.BandwidthUsagePercentage = $Object.bandwidthUsagePercentage
21+
$this.StorageQuota = $Object.storageQuota
22+
$this.StorageUsage = $Object.storageUsage
23+
$this.StorageUsagePercentage = $Object.storageUsagePercentage
24+
$this.TotalAvailableLicenses = $Object.totalAvailableLicenses
25+
$this.TotalLicenses = $Object.totalLicenses
26+
}
27+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class GitHubEnterprise : GitHubOwner {
2+
# The description of the enterprise.
3+
# Example: A great enterprise
4+
[string] $Description
5+
6+
# The description of the enterprise, as HTML.
7+
# Example: <div>A great enterprise</div>
8+
[string] $DescriptionHTML
9+
10+
# The billing information for the organization.
11+
[GitHubBillingInfo] $BillingInfo
12+
13+
# The billing email address for the organization.
14+
# Example: org@example.com
15+
[string] $BillingEmail
16+
17+
# The readme of the enterprise.
18+
# Example: This is the readme for the enterprise
19+
[string] $Readme
20+
21+
# The readme of the enterprise, as HTML.
22+
# Example: <p>This is the readme for the enterprise</p>
23+
[string] $ReadmeHTML
24+
25+
GitHubEnterprise() {}
26+
27+
GitHubEnterprise([PSCustomObject] $Object) {
28+
# From GitHubNode
29+
$this.ID = $Object.databaseId
30+
$this.NodeID = $Object.id
31+
32+
# From GitHubOwner
33+
$this.Name = $Object.slug
34+
$this.DisplayName = $Object.name
35+
$this.AvatarUrl = $Object.avatarUrl
36+
$this.Url = $Object.url
37+
$this.Type = $Object.type ?? 'Enterprise'
38+
$this.Company = $Object.company
39+
$this.Blog = $Object.websiteUrl
40+
$this.Location = $Object.location
41+
$this.CreatedAt = $Object.createdAt
42+
$this.UpdatedAt = $Object.updatedAt
43+
44+
# From GitHubEnterprise
45+
$this.Description = $Object.description
46+
$this.DescriptionHTML = $Object.descriptionHTML
47+
$this.BillingEmail = $Object.billingEmail
48+
$this.BillingInfo = [GitHubBillingInfo]::new($Object.billingInfo)
49+
$this.Readme = $Object.readme
50+
$this.ReadmeHTML = $Object.readmeHTML
51+
}
52+
53+
[string] ToString() {
54+
return $this.Name
55+
}
56+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
function Get-GitHubEnterprise {
2+
<#
3+
.SYNOPSIS
4+
Retrieves details about a GitHub Enterprise instance by name (slug).
5+
6+
.DESCRIPTION
7+
This function retrieves detailed information about a GitHub Enterprise instance, including its avatar, billing details, storage usage,
8+
creation date, and other metadata based on the provided name (slug). It returns an object of type GitHubEnterprise populated with this
9+
information.
10+
11+
.EXAMPLE
12+
Get-GitHubEnterprise -Name 'my-enterprise'
13+
14+
Output:
15+
```powershell
16+
Name : My Enterprise
17+
Slug : my-enterprise
18+
URL : https://github.com/enterprises/my-enterprise
19+
CreatedAt : 2022-01-01T00:00:00Z
20+
ViewerIsAdmin : True
21+
```
22+
23+
Retrieves details about the GitHub Enterprise instance named 'my-enterprise'.
24+
25+
.OUTPUTS
26+
GitHubEnterprise
27+
28+
.NOTES
29+
An object containing detailed information about the GitHub Enterprise instance, including billing info, URLs, and metadata.
30+
31+
.LINK
32+
https://psmodule.io/GitHub/Functions/Enterprise/Get-GitHubEnterprise/
33+
#>
34+
[OutputType([GitHubEnterprise])]
35+
[CmdletBinding()]
36+
param(
37+
# The name (slug) of the GitHub Enterprise instance to retrieve.
38+
[Parameter(Mandatory)]
39+
[Alias('Slug')]
40+
[string] $Name,
41+
42+
# The context to run the command in. Used to get the details for the API call.
43+
# Can be either a string or a GitHubContext object.
44+
[Parameter()]
45+
[object] $Context
46+
)
47+
48+
begin {
49+
$stackPath = Get-PSCallStackPath
50+
Write-Debug "[$stackPath] - Start"
51+
$Context = Resolve-GitHubContext -Context $Context
52+
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
53+
}
54+
55+
process {
56+
$enterpriseQuery = @{
57+
query = @'
58+
query($Slug: String!) {
59+
enterprise(slug: $Slug) {
60+
avatarUrl
61+
billingEmail
62+
billingInfo {
63+
allLicensableUsersCount
64+
assetPacks
65+
bandwidthQuota
66+
bandwidthUsage
67+
bandwidthUsagePercentage
68+
storageQuota
69+
storageUsage
70+
storageUsagePercentage
71+
totalAvailableLicenses
72+
totalLicenses
73+
}
74+
createdAt
75+
databaseId
76+
description
77+
descriptionHTML
78+
id
79+
location
80+
name
81+
readme
82+
readmeHTML
83+
resourcePath
84+
slug
85+
updatedAt
86+
url
87+
viewerIsAdmin
88+
websiteUrl
89+
}
90+
}
91+
'@
92+
Variables = @{
93+
Slug = $Name
94+
}
95+
Context = $Context
96+
}
97+
$enterpriseResult = Invoke-GitHubGraphQLQuery @enterpriseQuery
98+
[GitHubEnterprise]::new($enterpriseResult.enterprise)
99+
}
100+
101+
end {
102+
Write-Debug "[$stackPath] - End"
103+
}
104+
}

tests/Data/AuthCases.ps1

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,36 +64,36 @@
6464
Organization = 'psmodule-test-org'
6565
}
6666
}
67-
)
68-
@{
69-
AuthType = 'App'
70-
Type = 'a GitHub App from an Enterprise'
71-
Case = 'PEM + IAT'
72-
TokenType = 'APP_ENT'
73-
Target = 'organization account'
74-
Owner = 'psmodule-test-org3'
75-
OwnerType = 'organization'
76-
ConnectParams = @{
77-
ClientID = $env:TEST_APP_ENT_CLIENT_ID
78-
PrivateKey = $env:TEST_APP_ENT_PRIVATE_KEY
79-
}
80-
ConnectAppParams = @{
81-
Organization = 'psmodule-test-org3'
82-
}
83-
}
84-
@{
85-
AuthType = 'App'
86-
Type = 'a GitHub App from an Enterprise'
87-
Case = 'PEM + IAT'
88-
TokenType = 'APP_ENT'
89-
Target = 'enterprise account'
90-
Owner = 'msx'
91-
OwnerType = 'enterprise'
92-
ConnectParams = @{
93-
ClientID = $env:TEST_APP_ENT_CLIENT_ID
94-
PrivateKey = $env:TEST_APP_ENT_PRIVATE_KEY
67+
@{
68+
AuthType = 'App'
69+
Type = 'a GitHub App from an Enterprise'
70+
Case = 'PEM + IAT'
71+
TokenType = 'APP_ENT'
72+
Target = 'organization account'
73+
Owner = 'psmodule-test-org3'
74+
OwnerType = 'organization'
75+
ConnectParams = @{
76+
ClientID = $env:TEST_APP_ENT_CLIENT_ID
77+
PrivateKey = $env:TEST_APP_ENT_PRIVATE_KEY
78+
}
79+
ConnectAppParams = @{
80+
Organization = 'psmodule-test-org3'
81+
}
9582
}
96-
ConnectAppParams = @{
97-
Enterprise = 'msx'
83+
@{
84+
AuthType = 'App'
85+
Type = 'a GitHub App from an Enterprise'
86+
Case = 'PEM + IAT'
87+
TokenType = 'APP_ENT'
88+
Target = 'enterprise account'
89+
Owner = 'msx'
90+
OwnerType = 'enterprise'
91+
ConnectParams = @{
92+
ClientID = $env:TEST_APP_ENT_CLIENT_ID
93+
PrivateKey = $env:TEST_APP_ENT_PRIVATE_KEY
94+
}
95+
ConnectAppParams = @{
96+
Enterprise = 'msx'
97+
}
9898
}
99-
}
99+
)

tests/Enterprise.ps1

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#Requires -Modules @{ ModuleName = 'Pester'; RequiredVersion = '5.7.1' }
2+
3+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
4+
'PSUseDeclaredVarsMoreThanAssignments', '',
5+
Justification = 'Pester grouping syntax: known issue.'
6+
)]
7+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
8+
'PSAvoidUsingConvertToSecureStringWithPlainText', '',
9+
Justification = 'Used to create a secure string for testing.'
10+
)]
11+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
12+
'PSAvoidUsingWriteHost', '',
13+
Justification = 'Log outputs to GitHub Actions logs.'
14+
)]
15+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
16+
'PSAvoidLongLines', '',
17+
Justification = 'Long test descriptions and skip switches'
18+
)]
19+
[CmdletBinding()]
20+
param()
21+
22+
BeforeAll {
23+
# DEFAULTS ACCROSS ALL TESTS
24+
}
25+
26+
Describe 'Template' {
27+
$authCases = . "$PSScriptRoot/Data/AuthCases.ps1"
28+
29+
Context 'As <Type> using <Case> on <Target>' -ForEach $authCases {
30+
BeforeAll {
31+
$context = Connect-GitHubAccount @connectParams -PassThru -Silent
32+
LogGroup 'Context' {
33+
Write-Host ($context | Format-List | Out-String)
34+
}
35+
if ($AuthType -eq 'APP') {
36+
It 'Connect-GitHubApp - Connects as a GitHub App to <Owner>' {
37+
$context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent
38+
LogGroup 'Context - Installation' {
39+
Write-Host ($context | Format-List | Out-String)
40+
}
41+
}
42+
}
43+
}
44+
AfterAll {
45+
Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent
46+
Write-Host ('-' * 60)
47+
}
48+
49+
It 'Get-GitHubEnterprise - Can get info about an enterprise' -Skip:($OwnerType -notlike 'enterprise') {
50+
$enterprise = Get-GitHubEnterprise -Name $Owner
51+
LogGroup 'Enterprise' {
52+
Write-Host ($enterprise | Select-Object * | Out-String)
53+
}
54+
$enterprise | Should -Not -BeNullOrEmpty
55+
$enterprise | Should -BeOfType 'GitHubEnterprise'
56+
$enterprise.Name | Should -Be 'msx'
57+
$enterprise.DisplayName | Should -Be 'MSX'
58+
$enterprise.ID | Should -Be 15567
59+
$enterprise.NodeID | Should -Be 'E_kgDNPM8'
60+
$enterprise.AvatarUrl | Should -Be 'https://avatars.githubusercontent.com/b/15567?v=4'
61+
$enterprise.BillingEmail | Should -Be 'marstor@hotmail.com'
62+
$enterprise.Url | Should -Be 'https://github.com/enterprises/msx'
63+
$enterprise.Type | Should -Be 'Enterprise'
64+
$enterprise.BillingInfo | Should -BeOfType 'GitHubBillingInfo'
65+
$enterprise.BillingInfo.AllLicensableUsersCount | Should -Be 1
66+
$enterprise.BillingInfo.AssetPacks | Should -Be 0
67+
$enterprise.BillingInfo.BandwidthQuota | Should -Be 5
68+
$enterprise.BillingInfo.BandwidthUsage | Should -Be 0
69+
$enterprise.BillingInfo.BandwidthUsagePercentage | Should -Be 0
70+
$enterprise.BillingInfo.StorageQuota | Should -Be 5
71+
$enterprise.BillingInfo.StorageUsage | Should -Be 0
72+
$enterprise.BillingInfo.StorageUsagePercentage | Should -Be 0
73+
$enterprise.BillingInfo.TotalAvailableLicenses | Should -Be 0
74+
$enterprise.BillingInfo.TotalLicenses | Should -Be 1
75+
$enterprise.Readme | Should -Be 'This is a test'
76+
$enterprise.ReadmeHTML | Should -Be '<p>This is a test</p>'
77+
$enterprise.CreatedAt | Should -BeOfType 'DateTime'
78+
$enterprise.CreatedAt | Should -Be (Get-Date '18.09.2022 19:53:09')
79+
$enterprise.UpdatedAt | Should -BeOfType 'DateTime'
80+
$enterprise.Description | Should -Be 'This is the description'
81+
$enterprise.DescriptionHTML | Should -Be '<div>This is the description</div>'
82+
$enterprise.Location | Should -Be 'Oslo, Norway'
83+
$enterprise.Blog | Should -Be 'https://msx.no'
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)