Skip to content

Commit

Permalink
Merge pull request #77 from KelvinTegelaar/dev
Browse files Browse the repository at this point in the history
[pull] dev from KelvinTegelaar:dev
  • Loading branch information
pull[bot] authored Feb 12, 2025
2 parents 384b28d + 693f43a commit ebac0f8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ Function Invoke-ExecExtensionTest {
$Results = [pscustomobject]@{'Results' = 'Successfully Connected to HIBP' }
}
'GitHub' {
$GitHubResponse = Invoke-GitHubApiRequest -Method 'GET' -Path 'user'
$GitHubResponse = Invoke-GitHubApiRequest -Method 'GET' -Path 'user' -ReturnHeaders
if ($GitHubResponse.login) {
$Results = [pscustomobject]@{ 'Results' = "Successfully connected to GitHub user: $($GitHubResponse.login)" }
if ($GitHubResponse.Headers.'x-oauth-scopes') {
$Results = [pscustomobject]@{ 'Results' = "Successfully connected to GitHub user: $($GitHubResponse.login) with scopes: $($GitHubResponse.Headers.'x-oauth-scopes')" }
} else {
$Results = [pscustomobject]@{ 'Results' = "Successfully connected to GitHub user: $($GitHubResponse.login) using a Fine Grained PAT" }
}
} else {
$Results = [pscustomobject]@{ 'Results' = 'Failed to connect to GitHub. Check your API credentials and try again.' }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ Function Invoke-ListBPATemplates {
}

$Filter = "PartitionKey eq 'BPATemplate'"
$Templates = (Get-CIPPAzDataTableEntity @Table -Filter $Filter).JSON | ConvertFrom-Json
$Templates = Get-CIPPAzDataTableEntity @Table -Filter $Filter

if ($Request.Query.RawJson) {
$Templates
} else {
$Templates = $Templates | ForEach-Object {
$Template = $_
$Template = $_.JSON | ConvertFrom-Json
@{
GUID = $_.GUID
Data = $Template.fields
Name = $Template.Name
Style = $Template.Style
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,15 @@ function Invoke-ExecGitHubAction {
$Results = @(Get-GitHubBranch @SplatParams)
}
'GetOrgs' {
$Orgs = Invoke-GitHubApiRequest -Path 'user/orgs'
$Results = @($Orgs)
try {
$Orgs = Invoke-GitHubApiRequest -Path 'user/orgs'
$Results = @($Orgs)
} catch {
$Results = @{
resultText = 'You may not have permission to view organizations, check your PAT scopes and try again - {0}' -f $_.Exception.Message
state = 'error'
}
}
}
'GetFileTree' {
$Files = (Get-GitHubFileTree @SplatParams).tree | Where-Object { $_.path -match '.json$' } | Select-Object *, @{n = 'html_url'; e = { "https://github.com/$($SplatParams.FullName)/tree/$($SplatParams.Branch)/$($_.path)" } }
Expand All @@ -54,32 +61,42 @@ function Invoke-ExecGitHubAction {
$Results = Import-CommunityTemplate @SplatParams
}
'CreateRepo' {
$Repo = New-GitHubRepo @SplatParams
if ($Results.id) {
$Table = Get-CIPPTable -TableName CommunityRepos
$RepoEntity = @{
PartitionKey = 'CommunityRepos'
RowKey = [string]$Repo.id
Name = [string]$Repo.name
Description = [string]$Repo.description
URL = [string]$Repo.html_url
FullName = [string]$Repo.full_name
Owner = [string]$Repo.owner.login
Visibility = [string]$Repo.visibility
WriteAccess = [bool]$Repo.permissions.push
DefaultBranch = [string]$Repo.default_branch
Permissions = [string]($Repo.permissions | ConvertTo-Json -Compress)
}
Add-CIPPAzDataTableEntity @Table -Entity $RepoEntity -Force | Out-Null
try {
$Repo = New-GitHubRepo @SplatParams
if ($Results.id) {
$Table = Get-CIPPTable -TableName CommunityRepos
$RepoEntity = @{
PartitionKey = 'CommunityRepos'
RowKey = [string]$Repo.id
Name = [string]$Repo.name
Description = [string]$Repo.description
URL = [string]$Repo.html_url
FullName = [string]$Repo.full_name
Owner = [string]$Repo.owner.login
Visibility = [string]$Repo.visibility
WriteAccess = [bool]$Repo.permissions.push
DefaultBranch = [string]$Repo.default_branch
Permissions = [string]($Repo.permissions | ConvertTo-Json -Compress)
}
Add-CIPPAzDataTableEntity @Table -Entity $RepoEntity -Force | Out-Null

$Results = @{
resultText = "Repository '$($Results.name)' created"
state = 'success'
}
}
} catch {
$Results = @{
resultText = "Repository '$($Results.name)' created"
state = 'success'
resultText = 'You may not have permission to create repositories, check your PAT scopes and try again - {0}' -f $_.Exception.Message
state = 'error'
}
}
}
default {
$Results = "Error: Unknown action '$Action'"
$Results = @{
resultText = "Unknown action '$Action'"
state = 'error'
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ function Invoke-GitHubApiRequest {
try {
$Response = Invoke-RestMethod @RestMethod
if ($ReturnHeaders.IsPresent) {
$ResponseHeaders
$Response | Add-Member -MemberType NoteProperty -Name Headers -Value $ResponseHeaders
return $Response
} else {
$Response
return $Response
}
} catch {
throw $_.Exception.Message
Expand Down

0 comments on commit ebac0f8

Please sign in to comment.