diff --git a/src/GitHub/GitHub.psd1 b/src/GitHub/GitHub.psd1 index 008af21c3..72ba5c467 100644 --- a/src/GitHub/GitHub.psd1 +++ b/src/GitHub/GitHub.psd1 @@ -2,6 +2,8 @@ # Author of this module Author = 'Marius Storhaug' + ModuleVersion = '0.0.1' + # Description of the functionality provided by this module Description = 'PowerShell Module for GitHub' diff --git a/src/GitHub/private/common.ps1 b/src/GitHub/private/common.ps1 index 8651cccb2..873fbc3ce 100644 --- a/src/GitHub/private/common.ps1 +++ b/src/GitHub/private/common.ps1 @@ -2,3 +2,5 @@ $script:Owner = '' $script:Repo = '' $script:Token = '' +$Script:Version = '' +$script:ContentType = 'application/vnd.github+json' diff --git a/src/GitHub/public/Disable-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 similarity index 50% rename from src/GitHub/public/Disable-GitHubWorkflow.ps1 rename to src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 index 4b5b63497..18ab2584b 100644 --- a/src/GitHub/public/Disable-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 @@ -1,29 +1,8 @@ -<# -.SYNOPSIS -Short description - -.DESCRIPTION -Long description - -.PARAMETER Owner -Parameter description - -.PARAMETER Repo -Parameter description - -.PARAMETER Token -Parameter description - -.PARAMETER ID -Parameter description - -.EXAMPLE -An example - -.NOTES -https://docs.github.com/en/rest/reference/actions#disable-a-workflow -#> -Function Disable-GitHubWorkflow { +Function Disable-GitHubWorkflow { + <# + .NOTES + https://docs.github.com/en/rest/reference/actions#disable-a-workflow + #> [CmdletBinding()] param ( [Parameter()] @@ -46,17 +25,15 @@ Function Disable-GitHubWorkflow { process { $InputObject = @{ - Owner = $Owner - Repo = $Repo Token = $Token Method = 'PUT' - APIEndpoint = "repos/$Owner/$Repo/actions/workflows/$ID/disable" + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/disable" } $Response = Invoke-GitHubAPI @InputObject + $Response } - end { - return $Response - } + + end {} } diff --git a/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 new file mode 100644 index 000000000..4efae3369 --- /dev/null +++ b/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 @@ -0,0 +1,39 @@ +Function Enable-GitHubWorkflow { + <# + .NOTES + https://docs.github.com/en/rest/reference/actions#enable-a-workflow + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string[]] $ID + ) + + begin {} + + process { + $InputObject = @{ + Token = $Token + Method = 'PUT' + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/enable" + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response + } + + end {} +} diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 new file mode 100644 index 000000000..da733f293 --- /dev/null +++ b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 @@ -0,0 +1,40 @@ +function Get-GitHubWorkflow { + <# + .NOTES + https://docs.github.com/en/rest/reference/actions#list-repository-workflows + #> + [CmdletBinding(DefaultParameterSetName = 'ByName')] + param ( + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + + [Parameter(ParameterSetName = 'ByName')] + [string] $Name, + + [Parameter(ParameterSetName = 'ByID')] + [string] $ID, + + [Parameter()] + [int] $PageSize = 100 + ) + + $processedPages = 0 + $workflows = @() + do { + $processedPages++ + $InputObject = @{ + Token = $Token + Method = 'GET' + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows?per_page=$PageSize&page=$processedPages" + } + $Response = Invoke-GitHubAPI @InputObject -Verbose + $workflows += $Response.workflows | Where-Object name -Match $name | Where-Object id -Match $id + } while ($workflows.count -ne $Response.total_count) + $workflows +} diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 new file mode 100644 index 000000000..d401e504a --- /dev/null +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 @@ -0,0 +1,47 @@ +Function Get-GitHubWorkflowRun { + <# + .NOTES + https://docs.github.com/en/rest/reference/actions#list-workflow-runs-for-a-repository + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + + [Parameter(ParameterSetName = 'ByName')] + [string] $Name, + + [Parameter(ParameterSetName = 'ByID')] + [string] $ID, + + [Parameter()] + [int] $PageSize = 100 + ) + + $processedPages = 0 + $workflowRuns = @() + do { + $processedPages++ + $InputObject = @{ + Token = $Token + Method = 'GET' + APIEndpoint = "/repos/$Owner/$Repo/actions/runs?per_page=$PageSize&page=$processedPages" + } + $Response = Invoke-GitHubAPI @InputObject + $workflowRuns += $Response.workflows | Where-Object name -Match $name | Where-Object id -Match $id + } until ($workflowRuns.count -eq $Response.total_count) + $workflowRuns + + + do { + $WorkflowRuns = $Response.workflow_runs + $Results += $WorkflowRuns + } while ($WorkflowRuns.count -eq 100) + return $Results | Where-Object Name -Match $Name | Where-Object workflow_id -Match $ID +} diff --git a/src/GitHub/public/Get-GitHubWorkflowUsage.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 similarity index 83% rename from src/GitHub/public/Get-GitHubWorkflowUsage.ps1 rename to src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 index 450e6e260..95afeb8f0 100644 --- a/src/GitHub/public/Get-GitHubWorkflowUsage.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 @@ -3,9 +3,15 @@ DefaultParameterSetName = 'ByName' )] param ( - $Owner = $script:Owner, - $Repo = $script:Repo, - $Token = $script:Token , + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + [Parameter( Mandatory, ValueFromPipelineByPropertyName diff --git a/src/GitHub/public/Remove-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 similarity index 100% rename from src/GitHub/public/Remove-GitHubWorkflowRun.ps1 rename to src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 diff --git a/src/GitHub/public/Start-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 similarity index 90% rename from src/GitHub/public/Start-GitHubWorkflow.ps1 rename to src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 index cd8297e50..6169e5a1a 100644 --- a/src/GitHub/public/Start-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 @@ -41,9 +41,15 @@ General notes function Start-GitHubWorkflow { [CmdletBinding()] param ( - $Owner = $script:Owner, - $Repo = $script:Repo, - $Token = $script:Token , + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + [Alias('workflow_id')] [Parameter( Mandatory, @@ -60,7 +66,9 @@ function Start-GitHubWorkflow { [Parameter()] [hashtable] $Inputs = @{} ) + begin {} + process { # API Reference # https://docs.github.com/en/free-pro-team@latest/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event @@ -86,5 +94,6 @@ function Start-GitHubWorkflow { } return $Response } + end {} } diff --git a/src/GitHub/public/Start-GitHubWorkflowReRun.ps1 b/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 similarity index 83% rename from src/GitHub/public/Start-GitHubWorkflowReRun.ps1 rename to src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 index ae765d742..c7740ea00 100644 --- a/src/GitHub/public/Start-GitHubWorkflowReRun.ps1 +++ b/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 @@ -1,9 +1,15 @@ function Start-GitHubWorkflowReRun { [CmdletBinding()] param ( - $Owner = $script:Owner, - $Repo = $script:Repo, - $Token = $script:Token , + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + [Alias('workflow_id')] [Parameter( Mandatory, @@ -11,7 +17,9 @@ )] [string] $ID ) + begin {} + process { # API Reference # https://docs.github.com/en/rest/reference/actions#re-run-a-workflow @@ -34,5 +42,6 @@ } return $Response } + end {} } diff --git a/src/GitHub/public/Stop-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 similarity index 83% rename from src/GitHub/public/Stop-GitHubWorkflowRun.ps1 rename to src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 index c004bf092..a1b89b2e1 100644 --- a/src/GitHub/public/Stop-GitHubWorkflowRun.ps1 +++ b/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 @@ -2,9 +2,15 @@ [CmdletBinding()] [alias('Cancel-GitHubWorkflowRun')] param ( - $Owner = $script:Owner, - $Repo = $script:Repo, - $Token = $script:Token , + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + [Alias('workflow_id')] [Parameter( Mandatory, @@ -12,7 +18,9 @@ )] [string] $ID ) + begin {} + process { # API Reference # https://docs.github.com/en/rest/reference/actions#cancel-a-workflow-run @@ -35,5 +43,6 @@ } return $Response } + end {} } diff --git a/src/GitHub/public/Get-GitHubRepoBranch.ps1 b/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 similarity index 79% rename from src/GitHub/public/Get-GitHubRepoBranch.ps1 rename to src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 index 1f5f0ca95..827b2ffa1 100644 --- a/src/GitHub/public/Get-GitHubRepoBranch.ps1 +++ b/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 @@ -12,11 +12,9 @@ ) $InputObject = @{ - Owner = $Owner - Repo = $Repo Token = $Token Method = 'Get' - APIEndpoint = "repos/$Owner/$Repo/branches" + APIEndpoint = "/repos/$Owner/$Repo/branches" } $Response = Invoke-GitHubAPI @InputObject diff --git a/src/GitHub/public/Connect-GitHubAccount.ps1 b/src/GitHub/public/Connect-GitHubAccount.ps1 deleted file mode 100644 index 80bf6a66f..000000000 --- a/src/GitHub/public/Connect-GitHubAccount.ps1 +++ /dev/null @@ -1,46 +0,0 @@ -<# -.SYNOPSIS -Short description - -.DESCRIPTION -Long description - -.PARAMETER Owner -Parameter description - -.PARAMETER Repo -Parameter description - -.PARAMETER Token -Parameter description - -.PARAMETER APIBaseURI -Parameter description - -.EXAMPLE -An example - -.NOTES -https://docs.github.com/en/rest/overview/other-authentication-methods#authenticating-for-saml-sso -#> -function Connect-GitHubAccount { - [CmdletBinding()] - param ( - [Parameter()] - [String] $Owner, - [Parameter()] - [String] $Repo, - [Parameter(Mandatory)] - [String] $Token, - [Parameter()] - [String] $APIBaseURI = 'https://api.github.com' - ) - - $script:APIBaseURI = $APIBaseURI - $script:Owner = $Owner - $script:Repo = $Repo - $script:Token = $Token - - Get-GitHubContext - -} diff --git a/src/GitHub/public/Core/Connect-GitHubAccount.ps1 b/src/GitHub/public/Core/Connect-GitHubAccount.ps1 new file mode 100644 index 000000000..2c232980f --- /dev/null +++ b/src/GitHub/public/Core/Connect-GitHubAccount.ps1 @@ -0,0 +1,32 @@ +function Connect-GitHubAccount { + <# + .NOTES + https://docs.github.com/en/rest/overview/other-authentication-methods#authenticating-for-saml-sso + #> + [CmdletBinding()] + param ( + [Parameter()] + [String] $Owner, + + [Parameter()] + [String] $Repo, + + [Parameter(Mandatory)] + [String] $Token, + + [Parameter()] + [String] $APIBaseURI = 'https://api.github.com', + + [Parameter()] + [string] $Version = '2022-11-28' + ) + + $script:APIBaseURI = $APIBaseURI + $script:Owner = $Owner + $script:Repo = $Repo + $script:Token = $Token + $script:Version = $Version + + Get-GitHubUser + +} diff --git a/src/GitHub/public/Core/Invoke-GitHubAPI.ps1 b/src/GitHub/public/Core/Invoke-GitHubAPI.ps1 new file mode 100644 index 000000000..d7d34bf50 --- /dev/null +++ b/src/GitHub/public/Core/Invoke-GitHubAPI.ps1 @@ -0,0 +1,52 @@ +function Invoke-GitHubAPI { + [CmdletBinding(DefaultParameterSetName = 'Body')] + param ( + [Parameter()] + [ValidateSet('GET', 'POST', 'PATCH', 'DELETE', 'PUT')] + [String] $Method = 'GET', + + [Parameter()] + [string] $APIEndpoint, + + [Parameter(ParameterSetName = 'Body')] + [hashtable] $Body = @{}, + + [Parameter(ParameterSetName = 'Data')] + [string] $Data = '', + + [Parameter()] + [string] $Token = $script:Token, + + [Parameter()] + [string] $ContentType = $script:ContentType, + + [Parameter()] + [string] $Version = $script:Version, + + [Parameter()] + [switch] $UseWebRequest + ) + + $APICall = @{ + Uri = "$script:APIBaseURI$($APIEndpoint.Replace('\', '/').Replace('//', '/'))" + Method = $Method + Body = [string]::IsNullOrEmpty($Data) ? ($Body | ConvertTo-Json -Depth 100) : $Data + Headers = @{ + Authorization = "token $Token" + 'Content-Type' = $ContentType + 'X-GitHub-Api-Version' = $Version + } + } + try { + Write-Verbose ($APICall.GetEnumerator() | Out-String) + + if ($UseWebRequest) { + return Invoke-WebRequest @APICall + } + + Invoke-RestMethod @APICall + } catch { + Write-Error $_.Exception.Message + throw $_ + } +} diff --git a/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 b/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 new file mode 100644 index 000000000..72f35c1f8 --- /dev/null +++ b/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 @@ -0,0 +1,32 @@ +function Get-GitHubEnvironment { + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token + ) + + begin {} + + process { + # API Reference + # https://docs.github.com/en/rest/reference/repos#get-all-environments + + $InputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/environments" + Method = 'GET' + Token = $Token + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response + } + + end {} +} diff --git a/src/GitHub/public/Get-GitHubEnvironmentSecrets.ps1 b/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 similarity index 72% rename from src/GitHub/public/Get-GitHubEnvironmentSecrets.ps1 rename to src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 index 7318b3b80..e72fee4f6 100644 --- a/src/GitHub/public/Get-GitHubEnvironmentSecrets.ps1 +++ b/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 @@ -1,9 +1,15 @@ function Get-GitHubEnvironmentSecrets { [CmdletBinding()] param ( - $Owner = $script:Owner, - $Repo = $script:Repo, - $Token = $script:Token , + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + [Alias('name')] [Parameter( Mandatory, @@ -11,13 +17,27 @@ )] [string] $EnvironmentName ) + begin {} + process { $RepoID = (Get-GitHubRepo).id #/repositories/{repository_id}/environments/{environment_name}/secrets/{secret_name} #/repositories/{repository_id}/environments/{environment_name}/secrets # API Reference # https://docs.github.com/en/rest/reference/repos#get-all-environments + + $InputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/environments" + Token = $Token + Method = 'GET' + Verbose = $Verbose + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response + $APICall = @{ Uri = "$APIBaseURI/repositories/$RepoID/environments/$EnvironmentName/secrets" Headers = @{ diff --git a/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 b/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 new file mode 100644 index 000000000..d0ed821df --- /dev/null +++ b/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 @@ -0,0 +1,45 @@ +function Update-GitHubEnvironment { + <# + .NOTES + https://docs.github.com/en/rest/reference/repos#create-or-update-an-environment + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token, + + [Alias('environment_name')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $Name + ) + + begin {} + + process { + $InputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/environments/$Name" + Body = @{ + owner = $Owner + repo = $Repo + environment_name = $Name + } + Method = 'PUT' + Token = $Token + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response + } + + end {} +} diff --git a/src/GitHub/public/Emojis/Get-GitHubEmojis.ps1 b/src/GitHub/public/Emojis/Get-GitHubEmojis.ps1 new file mode 100644 index 000000000..bb6639dbb --- /dev/null +++ b/src/GitHub/public/Emojis/Get-GitHubEmojis.ps1 @@ -0,0 +1,27 @@ +function Get-GitHubEmojis { + <# + .NOTES + https://docs.github.com/en/rest/reference/emojis#get-emojis + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Destination + ) + + $InputObject = @{ + APIEndpoint = '/emojis' + Method = 'GET' + Token = $Token + } + + $Response = Invoke-GitHubAPI @InputObject + + if (Test-Path -Path $Destination) { + $Response.PSobject.Properties | ForEach-Object -Parallel { + Invoke-WebRequest -Uri $_.Value -OutFile "$using:Destination/$($_.Name).png" + } + } else { + $Response + } +} diff --git a/src/GitHub/public/Enable-GitHubWorkflow.ps1 b/src/GitHub/public/Enable-GitHubWorkflow.ps1 deleted file mode 100644 index e19d0182c..000000000 --- a/src/GitHub/public/Enable-GitHubWorkflow.ps1 +++ /dev/null @@ -1,43 +0,0 @@ -Function Enable-GitHubWorkflow { - [CmdletBinding()] - param ( - [Parameter()] - [string] $Owner = $script:Owner, - - [Parameter()] - [string] $Repo = $script:Repo, - - [Parameter()] - [string] $Token = $script:Token, - - [Parameter( - Mandatory, - ValueFromPipelineByPropertyName - )] - [string[]] $ID - ) - begin {} - process { - # API Reference - # https://docs.github.com/en/rest/reference/actions#enable-a-workflow - $APICall = @{ - Uri = "$APIBaseURI/repos/$Owner/$Repo/actions/workflows/$ID/enable" - Headers = @{ - Authorization = "token $Token" - 'Content-Type' = 'application/json' - } - Method = 'PUT' - Body = @{} | ConvertTo-Json -Depth 100 - } - try { - if ($PSBoundParameters.ContainsKey('Verbose')) { - $APICall - } - $Response = Invoke-RestMethod @APICall - } catch { - throw $_ - } - return $Response - } - end {} -} diff --git a/src/GitHub/public/Get-GitHubContext.ps1 b/src/GitHub/public/Get-GitHubContext.ps1 deleted file mode 100644 index 62884e5c1..000000000 --- a/src/GitHub/public/Get-GitHubContext.ps1 +++ /dev/null @@ -1,37 +0,0 @@ -<# -.SYNOPSIS -Get the authenticated user - -.DESCRIPTION -If the authenticated user is authenticated through basic authentication or OAuth with the user scope, then the response lists public and private profile information. -If the authenticated user is authenticated through OAuth without the user scope, then the response lists only public profile information. - -.PARAMETER Token -Parameter description - -.EXAMPLE -An example - -.NOTES -https://docs.github.com/en/rest/reference/users#get-the-authenticated-user -#> -function Get-GitHubContext { - [CmdletBinding()] - param ( - [Parameter()] - [string] $Token = $script:Token - ) - - $InputObject = @{ - Owner = $Owner - Repo = $Repo - Token = $Token - Method = 'GET' - APIEndpoint = 'user' - } - - $Response = Invoke-GitHubAPI @InputObject - - return $Response -} -New-Alias -Name Get-GitHubUser -Value Get-GitHubContext -Force diff --git a/src/GitHub/public/Get-GitHubEmojis.ps1 b/src/GitHub/public/Get-GitHubEmojis.ps1 deleted file mode 100644 index e2439dc16..000000000 --- a/src/GitHub/public/Get-GitHubEmojis.ps1 +++ /dev/null @@ -1,32 +0,0 @@ -<# -.SYNOPSIS -Short description - -.DESCRIPTION -Long description - -.PARAMETER Token -Parameter description - -.EXAMPLE -An example - -.NOTES -https://docs.github.com/en/rest/reference/emojis#get-emojis -#> -function Get-GitHubEmojis { - [CmdletBinding()] - param ( - $Destination - ) - - $Response = Invoke-GitHubAPI -Method Get -APIEndpoint emojis - - if (Test-Path -Path $Destination) { - $Response.PSobject.Properties | ForEach-Object -Parallel { - Invoke-WebRequest -Uri $_.Value -OutFile "$using:Destination/$($_.Name).png" - } - } - - return $Response -} diff --git a/src/GitHub/public/Get-GitHubEnvironment.ps1 b/src/GitHub/public/Get-GitHubEnvironment.ps1 deleted file mode 100644 index b319f0fd4..000000000 --- a/src/GitHub/public/Get-GitHubEnvironment.ps1 +++ /dev/null @@ -1,32 +0,0 @@ -function Get-GitHubEnvironment { - [CmdletBinding()] - param ( - $Owner = $script:Owner, - $Repo = $script:Repo, - $Token = $script:Token - ) - begin {} - process { - # API Reference - # https://docs.github.com/en/rest/reference/repos#get-all-environments - $APICall = @{ - Uri = "$APIBaseURI/repos/$Owner/$Repo/environments" - Headers = @{ - Authorization = "token $Token" - 'Content-Type' = 'application/json' - } - Method = 'GET' - Body = @{} | ConvertTo-Json -Depth 100 - } - try { - if ($PSBoundParameters.ContainsKey('Verbose')) { - $APICall - } - $Response = Invoke-RestMethod @APICall - } catch { - throw $_ - } - return $Response.Environments - } - end {} -} diff --git a/src/GitHub/public/Get-GitHubRepoTeams.ps1 b/src/GitHub/public/Get-GitHubRepoTeams.ps1 deleted file mode 100644 index 2f9c472be..000000000 --- a/src/GitHub/public/Get-GitHubRepoTeams.ps1 +++ /dev/null @@ -1,47 +0,0 @@ -<# -.SYNOPSIS -Short description - -.DESCRIPTION -Long description - -.PARAMETER Owner -Parameter description - -.PARAMETER Repo -Parameter description - -.PARAMETER Token -Parameter description - -.EXAMPLE -An example - -.NOTES -https://docs.github.com/en/rest/reference/repos#get-a-repository -#> -function Get-GitHubRepoTeams { - [CmdletBinding()] - param ( - [Parameter()] - [string] $Owner = $script:Owner, - - [Parameter()] - [string] $Repo = $script:Repo, - - [Parameter()] - [string] $Token = $script:Token - ) - - $InputObject = @{ - Owner = $Owner - Repo = $Repo - Token = $Token - Method = 'Get' - APIEndpoint = "repos/$Owner/$Repo/teams" - } - - $Response = Invoke-GitHubAPI @InputObject - - return $Response -} diff --git a/src/GitHub/public/Get-GitHubRoot.ps1 b/src/GitHub/public/Get-GitHubRoot.ps1 deleted file mode 100644 index bf00fe2d9..000000000 --- a/src/GitHub/public/Get-GitHubRoot.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -<# -.SYNOPSIS -Short description - -.DESCRIPTION -Long description - -.PARAMETER Token -Parameter description - -.EXAMPLE -An example - -.NOTES -https://docs.github.com/en/rest/reference/meta#github-api-root -#> -function Get-GitHubRoot { - [CmdletBinding()] - param ( - $Token = $script:Token - ) - - Invoke-GitHubAPI -Token $Token - - return $Response -} diff --git a/src/GitHub/public/Get-GitHubWorkflow.ps1 b/src/GitHub/public/Get-GitHubWorkflow.ps1 deleted file mode 100644 index 26b58d7ce..000000000 --- a/src/GitHub/public/Get-GitHubWorkflow.ps1 +++ /dev/null @@ -1,63 +0,0 @@ -<# -.SYNOPSIS -Short description - -.DESCRIPTION -Long description - -.PARAMETER Owner -Parameter description - -.PARAMETER Repo -Parameter description - -.PARAMETER Token -Parameter description - -.PARAMETER Name -Parameter description - -.PARAMETER ID -Parameter description - -.PARAMETER PageSize -Parameter description - -.EXAMPLE -An example - -.NOTES -https://docs.github.com/en/rest/reference/actions#list-repository-workflows -#> -Function Get-GitHubWorkflow { - [CmdletBinding(DefaultParameterSetName = 'ByName')] - param ( - [Parameter()] - [string] $Owner = $script:Owner, - - [Parameter()] - [string] $Repo = $script:Repo, - - [Parameter()] - [string] $Token = $script:Token, - - [Parameter(ParameterSetName = 'ByName')] - [string] $Name, - - [Parameter(ParameterSetName = 'ByID')] - [string] $ID, - - [Parameter()] - [int] $PageSize = 30 - ) - - $processedPages = 0 - $workflows = @() - do { - $processedPages += 1 - $Response = Invoke-GitHubAPI -Method GET -APIEndpoint "repos/$Owner/$Repo/actions/workflows?per_page=$PageSize&page=$processedPages" - $workflows += $Response.workflows | Where-Object name -Match $name | Where-Object id -Match $id - } while ($workflows.count -ne $Response.total_count) - - return $workflows -} diff --git a/src/GitHub/public/Get-GitHubWorkflowRun.ps1 b/src/GitHub/public/Get-GitHubWorkflowRun.ps1 deleted file mode 100644 index 253c65d6f..000000000 --- a/src/GitHub/public/Get-GitHubWorkflowRun.ps1 +++ /dev/null @@ -1,38 +0,0 @@ -Function Get-GitHubWorkflowRun { - [CmdletBinding()] - param ( - $Owner = $script:Owner, - $Repo = $script:Repo, - $Token = $script:Token , - $Name, - $ID - ) - - $Results = @() - $i = 0 - # API Reference - # https://docs.github.com/en/rest/reference/actions#list-workflow-runs-for-a-repository - do { - $i++ - $APICall = @{ - Uri = "$APIBaseURI/repos/$Owner/$Repo/actions/runs?per_page=100&page=$i" - Headers = @{ - Authorization = "token $Token" - 'Content-Type' = 'application/json' - } - Method = 'GET' - Body = @{} | ConvertTo-Json -Depth 100 - } - try { - if ($PSBoundParameters.ContainsKey('Verbose')) { - $APICall - } - $Response = Invoke-RestMethod @APICall - } catch { - throw $_ - } - $WorkflowRuns = $Response.workflow_runs - $Results += $WorkflowRuns - } while ($WorkflowRuns.count -eq 100) - return $Results | Where-Object Name -Match $Name | Where-Object workflow_id -Match $ID -} diff --git a/src/GitHub/public/Invoke-GitHubAPI.ps1 b/src/GitHub/public/Invoke-GitHubAPI.ps1 deleted file mode 100644 index 92553c983..000000000 --- a/src/GitHub/public/Invoke-GitHubAPI.ps1 +++ /dev/null @@ -1,38 +0,0 @@ -function Invoke-GitHubAPI { - [CmdletBinding()] - param ( - [Parameter()] - [ValidateSet('GET', 'POST', 'PATCH', 'DELETE')] - [String] $Method = 'GET', - - [Parameter()] - [string] $APIEndpoint, - - [Parameter()] - [hashtable] $Body = @{}, - - [Parameter()] - [string] $Owner = $script:Owner, - - [Parameter()] - [string] $Repo = $script:Repo, - - [Parameter()] - [string] $Token = $script:Token - ) - $APICall = @{ - Uri = ("$script:APIBaseURI/$APIEndpoint").Replace('\', '/').Replace('//', '/') - Headers = @{ - Authorization = "token $Token" - 'Content-Type' = 'application/vnd.github.v3+json' #'application/json' - } - Method = $Method - Body = $Body | ConvertTo-Json -Depth 100 - } - try { - $Response = Invoke-RestMethod @APICall - } catch { - throw $_ - } - return $Response -} diff --git a/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 b/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 new file mode 100644 index 000000000..803faddce --- /dev/null +++ b/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 @@ -0,0 +1,36 @@ +function Get-GitHubMeta { + <# + .NOTES + https://docs.github.com/en/rest/reference/meta#github-api-root + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Token = $script:Token, + + [Parameter()] + [switch] $Text, + + [Parameter()] + [ValidateSet('markdown', 'gfm')] + [string] $Mode, + + [Parameter()] + [string] $Context + ) + + $InputObject = @{ + APIEndpoint = '/markdown' + Body = @{ + context = $Context + mode = $Mode + text = $Text + } + Method = 'POST' + $Token = $Token + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +} diff --git a/src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 b/src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 new file mode 100644 index 000000000..ac5a70259 --- /dev/null +++ b/src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 @@ -0,0 +1,29 @@ +function Get-GitHubMeta { + <# + .NOTES + https://docs.github.com/en/rest/reference/meta#github-api-root + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Token = $script:Token, + + [Parameter()] + [switch] $Text, + + [Parameter()] + [string] $Context + ) + + $InputObject = @{ + APIEndpoint = '/markdown/raw' + ContentType = 'text/plain' + Data = $Text + Method = 'POST' + $Token = $Token + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +} diff --git a/src/GitHub/public/Meta/Get-GitHubMeta.ps1 b/src/GitHub/public/Meta/Get-GitHubMeta.ps1 new file mode 100644 index 000000000..21d3242cc --- /dev/null +++ b/src/GitHub/public/Meta/Get-GitHubMeta.ps1 @@ -0,0 +1,17 @@ +function Get-GitHubMeta { + <# + .NOTES + https://docs.github.com/en/rest/reference/meta#github-api-root + #> + [CmdletBinding()] + param () + + $InputObject = @{ + APIEndpoint = '/meta' + Method = 'GET' + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +} diff --git a/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 b/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 new file mode 100644 index 000000000..e0633e3ab --- /dev/null +++ b/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 @@ -0,0 +1,17 @@ +function Get-GitHubOctocat { + <# + .NOTES + https://docs.github.com/en/rest/reference/meta#github-api-root + #> + [CmdletBinding()] + param () + + $InputObject = @{ + APIEndpoint = '/octocat' + Method = 'GET' + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +} diff --git a/src/GitHub/public/Meta/Get-GitHubRoot.ps1 b/src/GitHub/public/Meta/Get-GitHubRoot.ps1 new file mode 100644 index 000000000..dc58f4c0a --- /dev/null +++ b/src/GitHub/public/Meta/Get-GitHubRoot.ps1 @@ -0,0 +1,17 @@ +function Get-GitHubRoot { + <# + .NOTES + https://docs.github.com/en/rest/reference/meta#github-api-root + #> + [CmdletBinding()] + param () + + $InputObject = @{ + APIEndpoint = '/' + Method = 'GET' + } + + $Response = Invoke-GitHubAPI @InputObject + + return $Response +} diff --git a/src/GitHub/public/Meta/Get-GitHubVersions.ps1 b/src/GitHub/public/Meta/Get-GitHubVersions.ps1 new file mode 100644 index 000000000..8a57800aa --- /dev/null +++ b/src/GitHub/public/Meta/Get-GitHubVersions.ps1 @@ -0,0 +1,18 @@ +function Get-GitHubAPIVersions { + <# + .NOTES + https://docs.github.com/en/rest/meta/meta?apiVersion=2022-11-28#get-all-api-versions + #> + [CmdletBinding()] + [OutputType([string[]])] + param () + + $InputObject = @{ + APIEndpoint = '/versions' + Method = 'GET' + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +} diff --git a/src/GitHub/public/Meta/Get-GitHubZen.ps1 b/src/GitHub/public/Meta/Get-GitHubZen.ps1 new file mode 100644 index 000000000..fc92ce284 --- /dev/null +++ b/src/GitHub/public/Meta/Get-GitHubZen.ps1 @@ -0,0 +1,17 @@ +function Get-GitHubZen { + <# + .NOTES + https://docs.github.com/en/rest/reference/meta#github-api-root + #> + [CmdletBinding()] + param () + + $InputObject = @{ + APIEndpoint = '/zen' + Method = 'GET' + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +} diff --git a/src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 b/src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 new file mode 100644 index 000000000..84991b4dd --- /dev/null +++ b/src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 @@ -0,0 +1,27 @@ +function Get-GitHubRepoTeam { + <# + .NOTES + https://docs.github.com/en/rest/reference/repos#get-a-repository + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = $script:Owner, + + [Parameter()] + [string] $Repo = $script:Repo, + + [Parameter()] + [string] $Token = $script:Token + ) + + $InputObject = @{ + Token = $Token + Method = 'Get' + APIEndpoint = "/repos/$Owner/$Repo/teams" + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +} diff --git a/src/GitHub/public/Update-GitHubEnvironment.ps1 b/src/GitHub/public/Update-GitHubEnvironment.ps1 deleted file mode 100644 index 2d0d9708c..000000000 --- a/src/GitHub/public/Update-GitHubEnvironment.ps1 +++ /dev/null @@ -1,42 +0,0 @@ -function Update-GitHubEnvironment { - [CmdletBinding()] - param ( - $Owner = $script:Owner, - $Repo = $script:Repo, - $Token = $script:Token , - [Alias('environment_name')] - [Parameter( - Mandatory, - ValueFromPipelineByPropertyName - )] - [string] $Name - ) - begin {} - process { - # API Reference - # https://docs.github.com/en/rest/reference/repos#create-or-update-an-environment - $APICall = @{ - Uri = "$APIBaseURI/repos/$Owner/$Repo/environments/$Name" - Headers = @{ - Authorization = "token $Token" - 'Content-Type' = 'application/json' - } - Method = 'PUT' - Body = @{ - owner = $Owner - repo = $Repo - environment_name = $Name - } | ConvertTo-Json -Depth 100 - } - try { - if ($PSBoundParameters.ContainsKey('Verbose')) { - $APICall - } - $Response = Invoke-RestMethod @APICall - } catch { - throw $_ - } - $Response - } - end {} -} diff --git a/src/GitHub/public/Users/Get-GitHubUser.ps1 b/src/GitHub/public/Users/Get-GitHubUser.ps1 new file mode 100644 index 000000000..9180f84cc --- /dev/null +++ b/src/GitHub/public/Users/Get-GitHubUser.ps1 @@ -0,0 +1,35 @@ +function Get-GitHubUser { + <# + .SYNOPSIS + Get the authenticated user + + .DESCRIPTION + If the authenticated user is authenticated through basic authentication or OAuth with the user scope, then the response lists public and private profile information. + If the authenticated user is authenticated through OAuth without the user scope, then the response lists only public profile information. + + .PARAMETER Token + Parameter description + + .EXAMPLE + An example + + .NOTES + https://docs.github.com/en/rest/users/users#get-the-authenticated-user + #> + [CmdletBinding()] + [Alias('Get-GitHubContext')] + param ( + [Parameter()] + [string] $Token = $script:Token + ) + + $InputObject = @{ + APIEndpoint = '/user' + Method = 'GET' + Token = $Token + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +} diff --git a/src/GitHub/public/Users/Set-GitHubUser.ps1 b/src/GitHub/public/Users/Set-GitHubUser.ps1 new file mode 100644 index 000000000..981cacda7 --- /dev/null +++ b/src/GitHub/public/Users/Set-GitHubUser.ps1 @@ -0,0 +1,66 @@ +function Set-GitHubUser { + <# + .NOTES + https://docs.github.com/en/rest/users/users#update-the-authenticated-user + #> + [CmdletBinding()] + [Alias('Update-GitHubUser')] + param ( + # The new name of the user. + [Parameter()] + [string] $Name, + + # The publicly visible email address of the user. + [Parameter()] + [string] $Email, + + # The new blog URL of the user. + [Parameter()] + [string] $Blog, + + # The new Twitter username of the user. + [Parameter()] + [string] $TwitterUsername, + + # The new company of the user. + [Parameter()] + [string] $Company, + + # The new location of the user. + [Parameter()] + [string] $Location, + + # The new hiring availability of the user. + [Parameter()] + [boolean] $Hireable, + + # The new short biography of the user. + [Parameter()] + [string] $Bio, + + [Parameter()] + [string] $Token = $script:Token + ) + + $Body = @{} + + $PSBoundParameters['Name'] ? ($Body.Name = $Name) : $null + $PSBoundParameters['Email'] ? ($Body.Email = $Email) : $null + $PSBoundParameters['Blog'] ? ($Body.Blog = $Blog) : $null + $PSBoundParameters['TwitterUsername'] ? ($Body.TwitterUsername = $TwitterUsername) : $null + $PSBoundParameters['Company'] ? ($Body.Company = $Company) : $null + $PSBoundParameters['Location'] ? ($Body.Location = $Location) : $null + $PSBoundParameters['Hireable'] ? ($Body.Hireable = $Hireable) : $null + $PSBoundParameters['Bio'] ? ($Body.Bio = $Bio) : $null + + $InputObject = @{ + APIEndpoint = '/user' + Body = $Body + Method = 'PATCH' + Token = $Token + } + + $Response = Invoke-GitHubAPI @InputObject + + $Response +}