From a9b2d878b1d7671f8ff11b9851badf00df0f08c7 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 19:33:44 +0200 Subject: [PATCH 01/25] Update Invoke-API to have accept by default --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index e3ec7793d..39941257f 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -42,7 +42,7 @@ # The 'Accept' header for the API request. If not provided, the default will be used by GitHub's API. [Parameter()] - [string] $Accept, + [string] $Accept = 'application/vnd.github+json', # Specifies the HTTP version used for the request. [Parameter()] From 4b611a747463dcff99e15b95c36cbfcfd5eaf026 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 19:34:01 +0200 Subject: [PATCH 02/25] clean --- src/GitHub/public/Meta/Get-GitHubOctocat.ps1 | 4 ++-- src/GitHub/public/Meta/Get-GitHubRoot.ps1 | 4 ++-- src/GitHub/public/Meta/Get-GitHubZen.ps1 | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 b/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 index 946bfe86c..156584cea 100644 --- a/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 +++ b/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 @@ -27,7 +27,7 @@ [Alias('Say')] [Alias('Saying')] [string] - $S = "The glass is never half empty. Its just twice as big as it needs to be." + $S = 'The glass is never half empty. Its just twice as big as it needs to be.' ) # $query = [System.Web.HttpUtility]::UrlEncode($S) @@ -38,7 +38,7 @@ } $inputObject = @{ - APIEndpoint = "/octocat" + APIEndpoint = '/octocat' Method = 'GET' Body = $body } diff --git a/src/GitHub/public/Meta/Get-GitHubRoot.ps1 b/src/GitHub/public/Meta/Get-GitHubRoot.ps1 index a08b507df..d7c7b4418 100644 --- a/src/GitHub/public/Meta/Get-GitHubRoot.ps1 +++ b/src/GitHub/public/Meta/Get-GitHubRoot.ps1 @@ -17,12 +17,12 @@ [CmdletBinding()] param () - $InputObject = @{ + $inputObject = @{ APIEndpoint = '/' Method = 'GET' } - $response = Invoke-GitHubAPI @InputObject + $response = Invoke-GitHubAPI @inputObject $response } diff --git a/src/GitHub/public/Meta/Get-GitHubZen.ps1 b/src/GitHub/public/Meta/Get-GitHubZen.ps1 index 866265a06..01de64490 100644 --- a/src/GitHub/public/Meta/Get-GitHubZen.ps1 +++ b/src/GitHub/public/Meta/Get-GitHubZen.ps1 @@ -17,12 +17,12 @@ [CmdletBinding()] param () - $InputObject = @{ + $inputObject = @{ APIEndpoint = '/zen' Method = 'GET' } - $Response = Invoke-GitHubAPI @InputObject + $response = Invoke-GitHubAPI @inputObject - $Response + $response } From a096fcdf533ea9528f3972f91ee997d1db7e569a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 19:34:20 +0200 Subject: [PATCH 03/25] Add Get/Set User --- src/GitHub/public/Users/Get-GitHubUser.ps1 | 31 ++++++++ src/GitHub/public/Users/Set-GitHubUser.ps1 | 83 ++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/GitHub/public/Users/Get-GitHubUser.ps1 create mode 100644 src/GitHub/public/Users/Set-GitHubUser.ps1 diff --git a/src/GitHub/public/Users/Get-GitHubUser.ps1 b/src/GitHub/public/Users/Get-GitHubUser.ps1 new file mode 100644 index 000000000..578587675 --- /dev/null +++ b/src/GitHub/public/Users/Get-GitHubUser.ps1 @@ -0,0 +1,31 @@ +function Get-GitHubUser { + <# + .SYNOPSIS + Get the authenticated user + + .DESCRIPTION + If the authenticated user is authenticated with an OAuth token 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. + + .EXAMPLE + Get-GitHubUser + + Get the authenticated user + + .NOTES + https://docs.github.com/rest/users/users#get-the-authenticated-user + #> + [OutputType([pscustomobject])] + [Alias('Get-GitHubContext')] + [CmdletBinding()] + param () + + $inputObject = @{ + APIEndpoint = '/user' + Method = 'GET' + } + + $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..832d3893a --- /dev/null +++ b/src/GitHub/public/Users/Set-GitHubUser.ps1 @@ -0,0 +1,83 @@ +function Set-GitHubUser { + <# + .SYNOPSIS + Update the authenticated user + + .DESCRIPTION + **Note:** If your email is set to private and you send an `email` parameter as part of this request + to update your profile, your privacy settings are still enforced: the email address will not be + displayed on your public profile or via the API. + + .EXAMPLE + Set-GitHubUser -Name 'octocat' + + Update the authenticated user's name to 'octocat' + + .EXAMPLE + Set-GitHubUser -Location 'San Francisco' + + Update the authenticated user's location to 'San Francisco' + + .EXAMPLE + Set-GitHubUser -Hireable $true -Bio 'I love programming' + + Update the authenticated user's hiring availability to 'true' and their biography to 'I love programming' + + .NOTES + https://docs.github.com/rest/users/users#update-the-authenticated-user + #> + [OutputType([void])] + [Alias('Update-GitHubUser')] + [CmdletBinding()] + 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 + ) + + $body = @{} + + $PSBoundParameters.GetEnumerator() | ForEach-Object { + $body.($_.Key) = $_.Value + } + + $inputObject = @{ + APIEndpoint = '/user' + Body = $body + Method = 'PATCH' + } + + $inputObject + + $response = Invoke-GitHubAPI @inputObject -Verbose + + $response +} From 5106e5cc0364034406f9ed2ae1a02787d8c6da19 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 19:39:14 +0200 Subject: [PATCH 04/25] remove some hardcoded verbose --- src/GitHub/GitHub.ps1 | 2 +- src/GitHub/public/Config/Set-GitHubConfig.ps1 | 2 +- src/GitHub/public/Users/Set-GitHubUser.ps1 | 2 +- tools/utilities/GitHubAPI.ps1 | 3 +++ 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/GitHub/GitHub.ps1 b/src/GitHub/GitHub.ps1 index a0a45426b..79c1d6ddf 100644 --- a/src/GitHub/GitHub.ps1 +++ b/src/GitHub/GitHub.ps1 @@ -1,6 +1,6 @@ $scriptFilePath = $MyInvocation.MyCommand.Path -Write-Verbose "[$scriptFilePath] - Initializing GitHub module..." -Verbose +Write-Verbose "[$scriptFilePath] - Initializing GitHub module..." Initialize-SecretVault -Name $script:SecretVault.Name -Type $script:SecretVault.Type diff --git a/src/GitHub/public/Config/Set-GitHubConfig.ps1 b/src/GitHub/public/Config/Set-GitHubConfig.ps1 index e4432fa35..ab215ef15 100644 --- a/src/GitHub/public/Config/Set-GitHubConfig.ps1 +++ b/src/GitHub/public/Config/Set-GitHubConfig.ps1 @@ -165,7 +165,7 @@ Vault = $script:SecretVault.Name Metadata = $newSecretMetadata } - Set-SecretInfo @secretSetInfoParam -Verbose + Set-SecretInfo @secretSetInfoParam } #endregion AccessToken } diff --git a/src/GitHub/public/Users/Set-GitHubUser.ps1 b/src/GitHub/public/Users/Set-GitHubUser.ps1 index 832d3893a..a0f98deca 100644 --- a/src/GitHub/public/Users/Set-GitHubUser.ps1 +++ b/src/GitHub/public/Users/Set-GitHubUser.ps1 @@ -77,7 +77,7 @@ $inputObject - $response = Invoke-GitHubAPI @inputObject -Verbose + $response = Invoke-GitHubAPI @inputObject $response } diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index 9292af143..123bc7b5a 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -15,3 +15,6 @@ $Response = Invoke-RestMethod -Uri $APIDocURI -Method Get $Response.paths.'/meta'.get + + +$Response.paths.'/user'.get From 711082f01051900829b2804b2e869a7ebf101089 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 20:38:21 +0200 Subject: [PATCH 05/25] fix --- src/GitHub/public/Users/Set-GitHubUser.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/GitHub/public/Users/Set-GitHubUser.ps1 b/src/GitHub/public/Users/Set-GitHubUser.ps1 index a0f98deca..d4fd0144b 100644 --- a/src/GitHub/public/Users/Set-GitHubUser.ps1 +++ b/src/GitHub/public/Users/Set-GitHubUser.ps1 @@ -75,8 +75,6 @@ Method = 'PATCH' } - $inputObject - $response = Invoke-GitHubAPI @inputObject $response From ebdf140cd437083c270111be877ab3c14c886103 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 21:37:01 +0200 Subject: [PATCH 06/25] Swap to header auth --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 39941257f..93010fee4 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -76,12 +76,29 @@ $URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '') + $AccessTokenAsPlainText = ConvertFrom-SecureString $AccessToken -AsPlainText + # Swap out this by using the -Authentication Bearer -Token $AccessToken + switch -Regex ($AccessTokenAsPlainText) { + '^ghp_|^github_pat_' { + $headers.authorization = "token $AccessTokenAsPlainText" + } + '^ghu_|^gho_' { + $headers.authorization = "Bearer $AccessTokenAsPlainText" + } + default { + $tokenPrefix = $AccessTokenAsPlainText -replace '_.*$', '_*' + $errorMessage = "Unexpected AccessToken format: $tokenPrefix" + Write-Error $errorMessage + throw $errorMessage + } + } + $APICall = @{ Uri = $URI Method = $Method Headers = $Headers - Authentication = 'Bearer' - Token = $AccessToken + # Authentication = 'Bearer' + # Token = $AccessToken ContentType = $ContentType HttpVersion = $HttpVersion FollowRelLink = $FollowRelLink From b042a646a45aab54c356259db0b5ed934915cd30 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 22:05:33 +0200 Subject: [PATCH 07/25] rev --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 36 +++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 93010fee4..a63151a5b 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -76,29 +76,29 @@ $URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '') - $AccessTokenAsPlainText = ConvertFrom-SecureString $AccessToken -AsPlainText - # Swap out this by using the -Authentication Bearer -Token $AccessToken - switch -Regex ($AccessTokenAsPlainText) { - '^ghp_|^github_pat_' { - $headers.authorization = "token $AccessTokenAsPlainText" - } - '^ghu_|^gho_' { - $headers.authorization = "Bearer $AccessTokenAsPlainText" - } - default { - $tokenPrefix = $AccessTokenAsPlainText -replace '_.*$', '_*' - $errorMessage = "Unexpected AccessToken format: $tokenPrefix" - Write-Error $errorMessage - throw $errorMessage - } - } + # $AccessTokenAsPlainText = ConvertFrom-SecureString $AccessToken -AsPlainText + # # Swap out this by using the -Authentication Bearer -Token $AccessToken + # switch -Regex ($AccessTokenAsPlainText) { + # '^ghp_|^github_pat_' { + # $headers.authorization = "token $AccessTokenAsPlainText" + # } + # '^ghu_|^gho_' { + # $headers.authorization = "Bearer $AccessTokenAsPlainText" + # } + # default { + # $tokenPrefix = $AccessTokenAsPlainText -replace '_.*$', '_*' + # $errorMessage = "Unexpected AccessToken format: $tokenPrefix" + # Write-Error $errorMessage + # throw $errorMessage + # } + # } $APICall = @{ Uri = $URI Method = $Method Headers = $Headers - # Authentication = 'Bearer' - # Token = $AccessToken + Authentication = 'Bearer' + Token = $AccessToken ContentType = $ContentType HttpVersion = $HttpVersion FollowRelLink = $FollowRelLink From a1ba6be622c4b3065d4ef5cca444910feb98703c Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 23:25:11 +0200 Subject: [PATCH 08/25] Updates --- src/GitHub/private/Menu/Invoke-DrawMenu.ps1 | 29 ++++++++ src/GitHub/private/Menu/Invoke-Meny.ps1 | 29 ++++++++ src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 2 +- .../public/Actions/Disable-GitHubWorkflow.ps1 | 35 ++++++++++ .../public/Actions/Enable-GitHubWorkflow.ps1 | 35 ++++++++++ .../public/Actions/Get-GitHubWorkflow.ps1 | 54 +++++++++++++++ .../public/Actions/Get-GitHubWorkflowRun.ps1 | 43 ++++++++++++ .../Actions/Get-GitHubWorkflowUsage.ps1 | 36 ++++++++++ .../Actions/Remove-GitHubWorkflowRun.ps1 | 31 +++++++++ .../public/Actions/Start-GitHubWorkflow.ps1 | 66 +++++++++++++++++++ .../Actions/Start-GitHubWorkflowReRun.ps1 | 33 ++++++++++ .../public/Actions/Stop-GitHubWorkflowRun.ps1 | 34 ++++++++++ .../public/Branches/Get-GitHubRepoBranch.ps1 | 19 ++++++ src/GitHub/public/Config/Get-GitHubConfig.ps1 | 15 +++++ .../Deployments/Get-GitHubEnvironment.ps1 | 28 ++++++++ .../Get-GitHubEnvironmentSecrets.ps1 | 38 +++++++++++ .../Deployments/Update-GitHubEnvironment.ps1 | 41 ++++++++++++ src/GitHub/public/Emojis/Get-GitHubEmojis.ps1 | 26 ++++++++ .../public/Markdown/Get-GitHubMarkdown.ps1 | 32 +++++++++ .../public/Markdown/Get-GitHubMarkdownRaw.ps1 | 25 +++++++ .../public/Teams/Get-GitHubRepoTeam.ps1 | 23 +++++++ tools/utilities/GHAPI.ps1 | 35 ++++++++++ tools/utilities/GitHubAPI.ps1 | 22 +++---- tools/utilities/Local-Testing.ps1 | 10 ++- tools/utilities/StopWorkflowsCustom.ps1 | 64 ++++++++++++++++++ 25 files changed, 792 insertions(+), 13 deletions(-) create mode 100644 src/GitHub/private/Menu/Invoke-DrawMenu.ps1 create mode 100644 src/GitHub/private/Menu/Invoke-Meny.ps1 create mode 100644 src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 create mode 100644 src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 create mode 100644 src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 create mode 100644 src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 create mode 100644 src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 create mode 100644 src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 create mode 100644 src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 create mode 100644 src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 create mode 100644 src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 create mode 100644 src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 create mode 100644 src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 create mode 100644 src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 create mode 100644 src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 create mode 100644 src/GitHub/public/Emojis/Get-GitHubEmojis.ps1 create mode 100644 src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 create mode 100644 src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 create mode 100644 src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 create mode 100644 tools/utilities/GHAPI.ps1 create mode 100644 tools/utilities/StopWorkflowsCustom.ps1 diff --git a/src/GitHub/private/Menu/Invoke-DrawMenu.ps1 b/src/GitHub/private/Menu/Invoke-DrawMenu.ps1 new file mode 100644 index 000000000..4ca69a42e --- /dev/null +++ b/src/GitHub/private/Menu/Invoke-DrawMenu.ps1 @@ -0,0 +1,29 @@ +function Invoke-DrawMenu { + ## supportfunction to the Menu function below + param ( + $menuItems, + $menuPosition, + $menuTitel + ) + $fcolor = $host.UI.RawUI.ForegroundColor + $bcolor = $host.UI.RawUI.BackgroundColor + $l = $menuItems.length + 1 + Clear-Host + $menuwidth = $menuTitel.length + 4 + Write-Host "`t" -NoNewline + Write-Host ('*' * $menuwidth) -fore $fcolor -back $bcolor + Write-Host "`t" -NoNewline + Write-Host "* $menuTitel *" -fore $fcolor -back $bcolor + Write-Host "`t" -NoNewline + Write-Host ('*' * $menuwidth) -fore $fcolor -back $bcolor + Write-Host '' + Write-Debug "L: $l MenuItems: $menuItems MenuPosition: $menuposition" + for ($i = 0; $i -le $l; $i++) { + Write-Host "`t" -NoNewline + if ($i -eq $menuPosition) { + Write-Host "$($menuItems[$i])" -fore $bcolor -back $fcolor + } else { + Write-Host "$($menuItems[$i])" -fore $fcolor -back $bcolor + } + } +} diff --git a/src/GitHub/private/Menu/Invoke-Meny.ps1 b/src/GitHub/private/Menu/Invoke-Meny.ps1 new file mode 100644 index 000000000..dcdbe6091 --- /dev/null +++ b/src/GitHub/private/Menu/Invoke-Meny.ps1 @@ -0,0 +1,29 @@ +function Menu { + ## Generate a small "DOS-like" menu. + ## Choose a menuitem using up and down arrows, select by pressing ENTER + param ( + [array]$menuItems, + $menuTitel = 'MENU' + ) + $vkeycode = 0 + $pos = 0 + Invoke-DrawMenu $menuItems $pos $menuTitel + while ($vkeycode -ne 13) { + $press = $host.ui.rawui.readkey('NoEcho,IncludeKeyDown') + $vkeycode = $press.virtualkeycode + Write-Host "$($press.character)" -NoNewline + if ($vkeycode -eq 38) { $pos-- } + if ($vkeycode -eq 40) { $pos++ } + if ($pos -lt 0) { $pos = 0 } + if ($pos -ge $menuItems.length) { $pos = $menuItems.length - 1 } + Invoke-DrawMenu $menuItems $pos $menuTitel + } + Write-Output $($menuItems[$pos]) +} + + +<# +? What account do you want to log into? [Use arrows to move, type to filter] +> GitHub.com + GitHub Enterprise Server +#> diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index a63151a5b..63466f444 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -118,7 +118,7 @@ try { Invoke-RestMethod @APICall | Write-Output Write-Verbose ($StatusCode | ConvertTo-Json -Depth 100) - Write-Verbose ($ResponseHeaders | ConvertTo-Json -Depth 100) + Write-Verbose ($responseHeaders | ConvertTo-Json -Depth 100) } catch [System.Net.WebException] { Write-Error "[$functionName] - WebException - $($_.Exception.Message)" throw $_ diff --git a/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 new file mode 100644 index 000000000..9846a37b7 --- /dev/null +++ b/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 @@ -0,0 +1,35 @@ +Function Disable-GitHubWorkflow { + <# + .NOTES + https://docs.github.com/en/rest/reference/actions#disable-a-workflow + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string[]] $ID + ) + + begin {} + + process { + $inputObject = @{ + Method = 'PUT' + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/disable" + } + + $response = Invoke-GitHubAPI @inputObject + + $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..b2df37395 --- /dev/null +++ b/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 @@ -0,0 +1,35 @@ +Function Enable-GitHubWorkflow { + <# + .NOTES + https://docs.github.com/en/rest/reference/actions#enable-a-workflow + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string[]] $ID + ) + + begin {} + + process { + $inputObject = @{ + 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..90d27fd85 --- /dev/null +++ b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 @@ -0,0 +1,54 @@ +function Get-GitHubWorkflow { + <# + .SYNOPSIS + Lists the workflows in a repository. + + .DESCRIPTION + Anyone with read access to the repository can use this endpoint. + If the repository is private you must use an access token with the repo scope. + GitHub Apps must have the actions:read permission to use this endpoint. + + .EXAMPLE + Get-GitHubWorkflow -Owner 'octocat' -Repo 'hello-world' + + Gets all workflows in the 'octocat/hello-world' repository. + + .EXAMPLE + Get-GitHubWorkflow -Owner 'octocat' -Repo 'hello-world' -Name 'hello-world.yml' + + Gets the 'hello-world.yml' workflow in the 'octocat/hello-world' repository. + + .NOTES + https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#list-repository-workflows + #> + [CmdletBinding(DefaultParameterSetName = 'ByName')] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Parameter(ParameterSetName = 'ByName')] + [string] $Name, + + [Parameter(ParameterSetName = 'ByID')] + [string] $ID, + + [Parameter()] + [int] $PageSize = 100 + ) + + $processedPages = 0 + $workflows = @() + do { + $processedPages++ + $inputObject = @{ + Method = 'GET' + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows?per_page=$PageSize&page=$processedPages" + } + $response = Invoke-GitHubAPI @inputObject + $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..a9ee5a988 --- /dev/null +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 @@ -0,0 +1,43 @@ +Function Get-GitHubWorkflowRun { + <# + .NOTES + https://docs.github.com/en/rest/reference/actions#list-workflow-runs-for-a-repository + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Parameter(ParameterSetName = 'ByName')] + [string] $Name, + + [Parameter(ParameterSetName = 'ByID')] + [string] $ID, + + [Parameter()] + [int] $PageSize = 100 + ) + + $processedPages = 0 + $workflowRuns = @() + do { + $processedPages++ + $inputObject = @{ + 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/Actions/Get-GitHubWorkflowUsage.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 new file mode 100644 index 000000000..a0d69fc88 --- /dev/null +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 @@ -0,0 +1,36 @@ +Function Get-GitHubWorkflowUsage { + [CmdletBinding( + DefaultParameterSetName = 'ByName' + )] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string[]] $ID + ) + + begin {} + + process { + # API Reference + # https://docs.github.com/en/rest/reference/actions#get-workflow-usage + + + $inputObject = @{ + Method = 'GET' + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/timing" + } + $response = Invoke-GitHubAPI @inputObject + + $response #billable? + } + + end {} +} diff --git a/src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 new file mode 100644 index 000000000..b65ab5ee1 --- /dev/null +++ b/src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 @@ -0,0 +1,31 @@ +function Remove-GitHubWorkflowRun { + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $ID + ) + + begin {} + + process { + $inputObject = @{ + APIEndpoint = "repos/$Owner/$Repo/actions/runs/$ID" + Method = 'DELETE' + } + + $response = Invoke-GitHubAPI @inputObject + + $response + } + + end {} +} diff --git a/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 new file mode 100644 index 000000000..510344589 --- /dev/null +++ b/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 @@ -0,0 +1,66 @@ + + +<# + .SYNOPSIS + Start a workflow run using the workflow's ID. + + .DESCRIPTION + Start a workflow run using the workflow's ID. + + .EXAMPLE + Get-GitHubWorkflow | Where-Object name -NotLike '.*' | Start-GitHubWorkflow -Inputs @{ + staticValidation = $true + deploymentValidation = $false + removeDeployment = $true + prerelease = $false + } + + .NOTES + # API Reference + # https://docs.github.com/en/free-pro-team@latest/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event +#> +function Start-GitHubWorkflow { + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Alias('workflow_id')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $ID, + + [Parameter( + ValueFromPipelineByPropertyName + )] + [Alias('branch', 'tag')] + [string] $Ref = 'main', + + [Parameter()] + [hashtable] $Inputs = @{} + ) + + begin {} + + process { + + $inputObject = @{ + Method = 'POST' + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/dispatches" + Body = @{ + ref = $Ref + inputs = $Inputs + } + } + $response = Invoke-GitHubAPI @inputObject + + $response + } + + end {} +} diff --git a/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 b/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 new file mode 100644 index 000000000..7f7aa80a7 --- /dev/null +++ b/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 @@ -0,0 +1,33 @@ +# API Reference +# https://docs.github.com/en/rest/reference/actions#re-run-a-workflow +function Start-GitHubWorkflowReRun { + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Alias('workflow_id')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $ID + ) + + begin {} + + process { + $inputObject = @{ + Method = 'POST' + APIEndpoint = "/repos/$Owner/$Repo/actions/runs/$ID/rerun" + } + $response = Invoke-GitHubAPI @inputObject + + return $response + } + + end {} +} diff --git a/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 new file mode 100644 index 000000000..f276b9c83 --- /dev/null +++ b/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 @@ -0,0 +1,34 @@ +function Stop-GitHubWorkflowRun { + [CmdletBinding()] + [alias('Cancel-GitHubWorkflowRun')] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Alias('workflow_id')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [string] $ID + ) + + begin {} + + process { + # API Reference + # https://docs.github.com/en/rest/reference/actions#cancel-a-workflow-run + $inputObject = @{ + Method = 'POST' + APIEndpoint = "/repos/$Owner/$Repo/actions/runs/$ID/cancel" + } + $response = Invoke-GitHubAPI @inputObject + + $response + } + + end {} +} diff --git a/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 b/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 new file mode 100644 index 000000000..fe747e161 --- /dev/null +++ b/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 @@ -0,0 +1,19 @@ +function Get-GitHubRepoBranch { + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo) + ) + + $inputObject = @{ + Method = 'GET' + APIEndpoint = "/repos/$Owner/$Repo/branches" + } + + $response = Invoke-GitHubAPI @inputObject + + $response +} diff --git a/src/GitHub/public/Config/Get-GitHubConfig.ps1 b/src/GitHub/public/Config/Get-GitHubConfig.ps1 index d872ed894..b6ee6d2ac 100644 --- a/src/GitHub/public/Config/Get-GitHubConfig.ps1 +++ b/src/GitHub/public/Config/Get-GitHubConfig.ps1 @@ -18,6 +18,21 @@ param ( # Choose a configuration name to get. [Parameter()] + [ValidateSet( + 'AccessToken', + 'AccessTokenExpirationDate', + 'AccessTokenType', + 'ApiBaseUri', + 'ApiVersion', + 'AuthType', + 'DeviceFlowType', + 'Owner', + 'RefreshToken', + 'RefreshTokenExpirationDate', + 'Repo', + 'Scope', + 'UserName' + )] [string] $Name ) diff --git a/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 b/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 new file mode 100644 index 000000000..67ddea6ad --- /dev/null +++ b/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 @@ -0,0 +1,28 @@ +function Get-GitHubEnvironment { + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo) + ) + + begin {} + + process { + # API Reference + # https://docs.github.com/en/rest/reference/repos#get-all-environments + + $inputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/environments" + Method = 'GET' + } + + $response = Invoke-GitHubAPI @inputObject + + $response + } + + end {} +} diff --git a/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 b/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 new file mode 100644 index 000000000..b883e8ffa --- /dev/null +++ b/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 @@ -0,0 +1,38 @@ +function Get-GitHubEnvironmentSecrets { + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [Alias('name')] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName + )] + [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 = "/repositories/$RepoID/environments/$EnvironmentName/secrets" + Method = 'GET' + } + + $response = Invoke-GitHubAPI @inputObject + + $response + } + + end {} +} diff --git a/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 b/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 new file mode 100644 index 000000000..93f10437f --- /dev/null +++ b/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 @@ -0,0 +1,41 @@ +function Update-GitHubEnvironment { + <# + .NOTES + https://docs.github.com/en/rest/reference/repos#create-or-update-an-environment + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + [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' + } + + $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..b90763bc8 --- /dev/null +++ b/src/GitHub/public/Emojis/Get-GitHubEmojis.ps1 @@ -0,0 +1,26 @@ +function Get-GitHubEmojis { + <# + .NOTES + https://docs.github.com/en/rest/reference/emojis#get-emojis + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Destination + ) + + $inputObject = @{ + APIEndpoint = '/emojis' + Method = 'GET' + } + + $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/Markdown/Get-GitHubMarkdown.ps1 b/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 new file mode 100644 index 000000000..074e8e3de --- /dev/null +++ b/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 @@ -0,0 +1,32 @@ +function Get-GitHubMarkdown { + <# + .NOTES + https://docs.github.com/en/rest/reference/meta#github-api-root + #> + [CmdletBinding()] + param ( + [Parameter()] + [switch] $Text, + + [Parameter()] + [ValidateSet('markdown', 'gfm')] + [string] $Mode, + + [Parameter()] + [string] $Context + ) + + $inputObject = @{ + APIEndpoint = '/markdown' + Body = @{ + context = $Context + mode = $Mode + text = $Text + } + Method = 'POST' + } + + $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..7bc718839 --- /dev/null +++ b/src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 @@ -0,0 +1,25 @@ +function Get-GitHubMarkdownRaw { + <# + .NOTES + https://docs.github.com/en/rest/reference/meta#github-api-root + #> + [CmdletBinding()] + param ( + [Parameter()] + [switch] $Text, + + [Parameter()] + [string] $Context + ) + + $inputObject = @{ + APIEndpoint = '/markdown/raw' + ContentType = 'text/plain' + Data = $Text + Method = 'POST' + } + + $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..384798886 --- /dev/null +++ b/src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 @@ -0,0 +1,23 @@ +function Get-GitHubRepoTeam { + <# + .NOTES + https://docs.github.com/en/rest/reference/repos#get-a-repository + #> + [CmdletBinding()] + param ( + [Parameter()] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo) + ) + + $inputObject = @{ + Method = 'Get' + APIEndpoint = "/repos/$Owner/$Repo/teams" + } + + $response = Invoke-GitHubAPI @inputObject + + $response +} diff --git a/tools/utilities/GHAPI.ps1 b/tools/utilities/GHAPI.ps1 new file mode 100644 index 000000000..3e9d36477 --- /dev/null +++ b/tools/utilities/GHAPI.ps1 @@ -0,0 +1,35 @@ +# API Authorization +# https://docs.github.com/en/rest/overview/other-authentication-methods + + +# https://docs.github.com/en/rest/overview/resources-in-the-rest-api +# https://docs.github.com/en/rest/reference + +$GHOwner = 'Org' +$GHRepo = 'RepoA' +$GHToken = 'ABC123' + +$GHAPIBaseURI = 'https://api.github.com' + +Function Get-GHActionRuns { + [CmdletBinding()] + param () + + # API Reference + # https://docs.github.com/en/rest/reference/actions#list-workflow-runs-for-a-repository + $APICall = @{ + Uri = "$GHRepoURI/repos/$GHOwner/$GHRepo/actions/runs" + Headers = @{ + Authorization = "token $GHToken" + 'Content-Type' = 'application/json' + } + Method = 'GET' + Body = @{} | ConvertTo-Json -Depth 100 + } + try { + $response = Invoke-RestMethod @APICall + } catch { + throw $_ + } + return $response.value +} diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index 123bc7b5a..641542f90 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -2,19 +2,19 @@ $APIDocURI = 'https://raw.githubusercontent.com/github/rest-api-description/main $Bundled = '/descriptions/api.github.com/api.github.com.json' $Dereferenced = 'descriptions/api.github.com/dereferenced/api.github.com.deref.json' $APIDocURI = $APIDocURI + $Bundled -$Response = Invoke-RestMethod -Uri $APIDocURI -Method Get +$response = Invoke-RestMethod -Uri $APIDocURI -Method Get -# $Response.info # API name = GitHub REST API -# $Response.openapi # Spec version = 3.0.3 -# $Response.servers # API URL = api.github.com -# $Response.externalDocs # API docs URL = docs.github.com/rest -# $Response.components # Type specs -# $Response.paths # API endpoints -# $Response.tags # API categories -# $Response.'x-webhooks' # Webhooks/event docs +# $response.info # API name = GitHub REST API +# $response.openapi # Spec version = 3.0.3 +# $response.servers # API URL = api.github.com +# $response.externalDocs # API docs URL = docs.github.com/rest +# $response.components # Type specs +# $response.paths # API endpoints +# $response.tags # API categories +# $response.'x-webhooks' # Webhooks/event docs -$Response.paths.'/meta'.get +$response.paths.'/meta'.get -$Response.paths.'/user'.get +$response.paths.'/user'.get diff --git a/tools/utilities/Local-Testing.ps1 b/tools/utilities/Local-Testing.ps1 index c99643b25..fe8cbc8a5 100644 --- a/tools/utilities/Local-Testing.ps1 +++ b/tools/utilities/Local-Testing.ps1 @@ -24,8 +24,16 @@ Get-GitHubConfig -Name AccessTokenExpirationDate Get-GitHubConfig -Name RefreshToken Get-GitHubConfig -Name RefreshTokenExpirationDate Get-GitHubConfig -Name ApiBaseUri -Invoke-GitHubAPI -Method Get -ApiEndpoint / +Invoke-GitHubAPI -Method Get -ApiEndpoint /user Get-GitHubMeta Get-GitHubOctocat -S 'Hello, World!' Disconnect-GitHubAccount -Verbose $VerbosePreference = 'SIlentlyContinue' + + +$str = '2023-10-27 17:43:40 UTC' +$format = "yyyy-MM-dd HH:mm:ss 'UTC'" + +$date = [datetime]::ParseExact($str, $format, $null) + +$date diff --git a/tools/utilities/StopWorkflowsCustom.ps1 b/tools/utilities/StopWorkflowsCustom.ps1 new file mode 100644 index 000000000..dbee1aee6 --- /dev/null +++ b/tools/utilities/StopWorkflowsCustom.ps1 @@ -0,0 +1,64 @@ +$Owner = 'MariusStorhaug' +$Repo = 'ResourceModules' + + +Install-Module -Name GitHub -Force -AllowClobber +Connect-GitHubAccount -Owner $Owner -Repo $Repo -Verbose + +Get-GitHubWorkflow -Verbose + +# Disable all workflows +Get-GitHubWorkflow | Where-Object state -EQ 'active' | Disable-GitHubWorkflow + +# Enable all workflows +Get-GitHubWorkflow | Where-Object state -NE 'active' | Enable-GitHubWorkflow + +# Cancel all started workflows +Get-GitHubWorkflowRun | Where-Object status -NE Completed | Stop-GitHubWorkflowRun + +# Remove all completed workflows +Get-GitHubWorkflowRun | Where-Object status -EQ Completed | Remove-GitHubWorkflowRun + +# Disable all workflows +Get-GitHubWorkflow | Disable-GitHubWorkflow + +# Cancel all started workflows +Get-GitHubWorkflowRun | Where-Object status -NE completed | Stop-GitHubWorkflowRun + +Get-GitHubRepoTeams + + +(Get-GitHubWorkflow).count + +Get-GitHubWorkflow | Select-Object -first 1 -Property * + +Get-GitHubWorkflow | Select-Object Name, state +Get-GitHubWorkflow | Where-Object state -NE disabled_manually | Disable-GitHubWorkflow +Get-GitHubWorkflow | Disable-GitHubWorkflow +Get-GitHubWorkflow | Select-Object Name, state + +Get-GitHubWorkflow | Select-Object Name, state +Get-GitHubWorkflow | Where-Object name -NotLike '.*' | Enable-GitHubWorkflow +Get-GitHubWorkflow | Select-Object Name, state + +Get-GitHubWorkflow | Select-Object Name, state +Get-GitHubWorkflow | Enable-GitHubWorkflow +Get-GitHubWorkflow | Select-Object Name, state + +Get-GitHubWorkflow | Select-Object Name | Sort-Object Name -Unique + +(Get-GitHubWorkflow | Get-GitHubWorkflowRun).count +Get-GitHubWorkflowRun | Cancel-GitHubWorkflowRun +Get-GitHubWorkflowRun | Remove-GitHubWorkflowRun + +Get-GitHubWorkflowRun | Select-Object -Property name, display_title, created_at, run_started_at, updated_at, @{name = 'duration'; expression = { $_.updated_at - $_.run_started_at } }, @{name = 'wait_duration'; expression = { $_.updated_at - $_.created_at } } | Format-Table -AutoSize + +Get-GitHubWorkflowRun | Where-Object run_started_at -le (Get-Date).AddDays(-1) | Remove-GitHubWorkflowRun + + +Get-GitHubWorkflow | Where-Object name -NotLike '.*' | Start-GitHubWorkflow -Inputs @{ + staticValidation = $true + deploymentValidation = $false + removeDeployment = $true + prerelease = $false +} From dbcfa028bb5be2ed1fbf3cdc002927a7fbf22266 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 27 Sep 2023 23:28:15 +0200 Subject: [PATCH 09/25] Test auto paging in Get-GitHubWorkflow --- .../public/Actions/Get-GitHubWorkflow.ps1 | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 index 90d27fd85..e849f9f17 100644 --- a/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 @@ -39,16 +39,10 @@ [int] $PageSize = 100 ) - $processedPages = 0 - $workflows = @() - do { - $processedPages++ - $inputObject = @{ - Method = 'GET' - APIEndpoint = "/repos/$Owner/$Repo/actions/workflows?per_page=$PageSize&page=$processedPages" - } - $response = Invoke-GitHubAPI @inputObject - $workflows += $response.workflows | Where-Object name -Match $name | Where-Object id -Match $id - } while ($workflows.count -ne $response.total_count) - $workflows + $inputObject = @{ + Method = 'GET' + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows?per_page=$PageSize" + } + $response = Invoke-GitHubAPI @inputObject + $response } From 725f164fc8d98a3853f3084d96298b09ca4346cd Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 22:31:14 +0200 Subject: [PATCH 10/25] Add support for passing query params in to body for GET --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 63466f444..33344c856 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -105,9 +105,19 @@ StatusCodeVariable = 'StatusCode' ResponseHeadersVariable = 'ResponseHeaders' } - Remove-HashTableEntries -Hashtable $APICall -NullOrEmptyValues + $APICall | Remove-HashTableEntries -NullOrEmptyValues if ($Body) { + $Body | Remove-HashTableEntries -NullOrEmptyValues + + # Use body to create the query string for GET requests + if ($Method -eq 'GET') { + $queryParams = ($Body.GetEnumerator() | + ForEach-Object { "$([System.Web.HttpUtility]::UrlEncode($_.Key))=$([System.Web.HttpUtility]::UrlEncode($_.Value))" }) -join '&' + if ($queryParams) { + $APICall.Uri = $APICall.Uri + '?' + $queryParams + } + } if ($Body -is [string]) { $APICall.Body = $Body } else { From 1aa38313712cf9bdac95429a55f0189de421315e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 22:32:54 +0200 Subject: [PATCH 11/25] Added 2 more function --- .../public/Users/Get-GitHubUserCard.ps1 | 47 +++++++++++++++++++ .../public/Users/Get-GitHubUserList.ps1 | 43 +++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 src/GitHub/public/Users/Get-GitHubUserCard.ps1 create mode 100644 src/GitHub/public/Users/Get-GitHubUserList.ps1 diff --git a/src/GitHub/public/Users/Get-GitHubUserCard.ps1 b/src/GitHub/public/Users/Get-GitHubUserCard.ps1 new file mode 100644 index 000000000..6617af34e --- /dev/null +++ b/src/GitHub/public/Users/Get-GitHubUserCard.ps1 @@ -0,0 +1,47 @@ +function Get-GitHubUserCard { + <# + .SYNOPSIS + Get contextual information for a user + + .DESCRIPTION + Provides hovercard information when authenticated through basic auth or OAuth with the `repo` scope. You can find out more about someone in relation to their pull requests, issues, repositories, and organizations. + + The `subject_type` and `subject_id` parameters provide context for the person's hovercard, which returns more information than without the parameters. For example, if you wanted to find out more about `octocat` who owns the `Spoon-Knife` repository via cURL, it would look like this: + + ```shell + curl -u username:token + https://api.github.com/users/octocat/hovercard?subject_type=repository&subject_id=1300192 + ``` + + .EXAMPLE + + .NOTES + https://docs.github.com/rest/users/users#get-contextual-information-for-a-user + + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + [Parameter(Mandatory)] + [string] $Username, + [Parameter()] + [ValidateSet('organization', 'repository', 'issue', 'pull_request')] + [string] $SubjectType, + [Parameter()] + [int] $SubjectID = '' + ) + + $body = @{ + subject_type = $SubjectType + subject_id = $SubjectID + } + + $inputObject = @{ + APIEndpoint = "/users/$Username/hovercard" + Method = 'GET' + Body = $body + } + + Invoke-GitHubAPI @inputObject + +} diff --git a/src/GitHub/public/Users/Get-GitHubUserList.ps1 b/src/GitHub/public/Users/Get-GitHubUserList.ps1 new file mode 100644 index 000000000..960bfbe2c --- /dev/null +++ b/src/GitHub/public/Users/Get-GitHubUserList.ps1 @@ -0,0 +1,43 @@ +function Get-GitHubUserList { + <# + .SYNOPSIS + List users + + .DESCRIPTION + Lists all users, in the order that they signed up on GitHub. This list includes personal user accounts and organization accounts. + + Note: Pagination is powered exclusively by the `since` parameter. Use the [Link header](https://docs.github.com/rest/guides/using-pagination-in-the-rest-api#using-link-headers) to get the URL for the next page of users. + + .EXAMPLE + Get-GitHubUserList -Since 17722253 + + Get the authenticated user + + .NOTES + https://docs.github.com/rest/users/users#list-users + #> + [OutputType([pscustomobject])] + [CmdletBinding()] + param ( + # A user ID. Only return users with an ID greater than this ID. + [Parameter()] + [int] $Since = 0, + # The number of results per page (max 100). + [Parameter()] + [int] $PerPage = 100 + ) + + $body = @{ + since = $Since + per_page = $PerPage + } + + $inputObject = @{ + APIEndpoint = "/users" + Method = 'GET' + Body = $body + } + + Invoke-GitHubAPI @inputObject + +} From 07f4601045c7fac8d8a95ee9949b00b6d31857f8 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 22:33:10 +0200 Subject: [PATCH 12/25] updated the API helpscript --- tools/utilities/GitHubAPI.ps1 | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index 641542f90..f9d160452 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -13,8 +13,18 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # $response.tags # API categories # $response.'x-webhooks' # Webhooks/event docs +$path = '/users/{username}/hovercard' +$response.paths.$path.get.tags | clip # -> Namespace/foldername +$response.paths.$path.get.operationId | clip # -> FunctionName +$response.paths.$path.get.summary | clip # -> Synopsis +$response.paths.$path.get.description | clip # -> Description +$response.paths.$path.get.externalDocs.url | clip # -> Notes +$response.paths.$path.get.'x-github'.category | clip # -> Namespace/foldername +$response.paths.$path.get.'x-github'.subcategory | clip # -> Namespace/foldername +$response.paths.$path.get.'x-github'.enabledForGitHubApps | clip # -> Note + Warning if running as GitHub App +$response.paths.$path.get.'x-github'.githubCloudOnly | clip # -> Note +$response.paths.$path.get.parameters # -> Parameter list +$response.paths.$path.get.responses.'200'.content.'application/json'.schema # -> OutputType qualifyer +$response.paths.$path.get.responses.'200'.content.'application/json'.schema.items # -> OutputType +$response.paths.$path.get -$response.paths.'/meta'.get - - -$response.paths.'/user'.get From 5942e5c0bf579bbb75538e94940f14a868557f6e Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 23:06:35 +0200 Subject: [PATCH 13/25] Align command layout --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 34 ++++++------ .../public/Actions/Disable-GitHubWorkflow.ps1 | 5 +- .../public/Actions/Enable-GitHubWorkflow.ps1 | 5 +- .../public/Actions/Get-GitHubWorkflow.ps1 | 51 +++++++++++------- .../public/Actions/Get-GitHubWorkflowRun.ps1 | 44 ++++++++++------ .../Actions/Get-GitHubWorkflowUsage.ps1 | 29 +++++++++-- .../Actions/Remove-GitHubWorkflowRun.ps1 | 4 +- .../public/Actions/Start-GitHubWorkflow.ps1 | 52 +++++++++---------- .../Actions/Start-GitHubWorkflowReRun.ps1 | 31 +++++++++-- .../public/Actions/Stop-GitHubWorkflowRun.ps1 | 29 +++++++++-- .../public/Branches/Get-GitHubRepoBranch.ps1 | 5 +- .../Deployments/Get-GitHubEnvironment.ps1 | 24 +++++++-- .../Get-GitHubEnvironmentSecrets.ps1 | 30 ++++++++--- .../Deployments/Update-GitHubEnvironment.ps1 | 15 +++--- .../public/Markdown/Get-GitHubMarkdown.ps1 | 15 +++--- .../public/Markdown/Get-GitHubMarkdownRaw.ps1 | 10 ++-- .../public/Meta/Get-GitHubApiVersions.ps1 | 3 +- src/GitHub/public/Meta/Get-GitHubMeta.ps1 | 3 +- src/GitHub/public/Meta/Get-GitHubOctocat.ps1 | 8 +-- src/GitHub/public/Meta/Get-GitHubRoot.ps1 | 3 +- src/GitHub/public/Meta/Get-GitHubZen.ps1 | 3 +- .../public/Teams/Get-GitHubRepoTeam.ps1 | 3 +- src/GitHub/public/Users/Get-GitHubUser.ps1 | 3 +- src/GitHub/public/Users/Set-GitHubUser.ps1 | 3 +- tools/utilities/Local-Testing.ps1 | 2 - 25 files changed, 256 insertions(+), 158 deletions(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 33344c856..913148085 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -1,27 +1,27 @@ function Invoke-GitHubAPI { <# - .SYNOPSIS - Calls the GitHub API using the provided parameters. + .SYNOPSIS + Calls the GitHub API using the provided parameters. - .DESCRIPTION - This function is a wrapper around Invoke-RestMethod tailored for calling GitHub's API. - It automatically handles the endpoint URI construction, headers, and token authentication. + .DESCRIPTION + This function is a wrapper around Invoke-RestMethod tailored for calling GitHub's API. + It automatically handles the endpoint URI construction, headers, and token authentication. - .EXAMPLE - Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET + .EXAMPLE + Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET - Gets all open pull requests for the specified repository. + Gets all open pull requests for the specified repository. - .EXAMPLE - Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET -Body @{ state = 'open' } + .EXAMPLE + Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET -Body @{ state = 'open' } - Gets all open pull requests for the specified repository, filtered by the 'state' parameter. + Gets all open pull requests for the specified repository, filtered by the 'state' parameter. - .EXAMPLE - Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET -Body @{ state = 'open' } -Accept 'application/vnd.github.v3+json' + .EXAMPLE + Invoke-GitHubAPI -ApiEndpoint '/repos/user/repo/pulls' -Method GET -Body @{ state = 'open' } -Accept 'application/vnd.github.v3+json' - Gets all open pull requests for the specified repository, filtered by the 'state' parameter, and using the specified 'Accept' header. -#> + Gets all open pull requests for the specified repository, filtered by the 'state' parameter, and using the specified 'Accept' header. + #> [CmdletBinding()] param ( # The HTTP method to be used for the API request. It can be one of the following: GET, POST, PUT, DELETE, or PATCH. @@ -46,11 +46,11 @@ # Specifies the HTTP version used for the request. [Parameter()] - $HttpVersion = '2.0', + [version] $HttpVersion = '2.0', # Support Pagination Relation Links per RFC5988. [Parameter()] - $FollowRelLink = $true, + [bool] $FollowRelLink = $true, # The secure token used for authentication in the GitHub API. It should be stored as a SecureString to ensure it's kept safe in memory. [Parameter()] diff --git a/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 index 9846a37b7..10b9ea6e2 100644 --- a/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 @@ -22,13 +22,12 @@ process { $inputObject = @{ - Method = 'PUT' APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/disable" + Method = 'PUT' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } end {} diff --git a/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 index b2df37395..7579c289e 100644 --- a/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 @@ -22,13 +22,12 @@ process { $inputObject = @{ - Method = 'PUT' APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/enable" + Method = 'PUT' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } end {} diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 index e849f9f17..f5540b373 100644 --- a/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 @@ -1,25 +1,25 @@ function Get-GitHubWorkflow { <# - .SYNOPSIS - Lists the workflows in a repository. + .SYNOPSIS + Lists the workflows in a repository. - .DESCRIPTION - Anyone with read access to the repository can use this endpoint. - If the repository is private you must use an access token with the repo scope. - GitHub Apps must have the actions:read permission to use this endpoint. + .DESCRIPTION + Anyone with read access to the repository can use this endpoint. + If the repository is private you must use an access token with the repo scope. + GitHub Apps must have the actions:read permission to use this endpoint. - .EXAMPLE - Get-GitHubWorkflow -Owner 'octocat' -Repo 'hello-world' + .EXAMPLE + Get-GitHubWorkflow -Owner 'octocat' -Repo 'hello-world' - Gets all workflows in the 'octocat/hello-world' repository. + Gets all workflows in the 'octocat/hello-world' repository. - .EXAMPLE - Get-GitHubWorkflow -Owner 'octocat' -Repo 'hello-world' -Name 'hello-world.yml' + .EXAMPLE + Get-GitHubWorkflow -Owner 'octocat' -Repo 'hello-world' -Name 'hello-world.yml' - Gets the 'hello-world.yml' workflow in the 'octocat/hello-world' repository. + Gets the 'hello-world.yml' workflow in the 'octocat/hello-world' repository. - .NOTES - https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#list-repository-workflows + .NOTES + https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#list-repository-workflows #> [CmdletBinding(DefaultParameterSetName = 'ByName')] param ( @@ -36,13 +36,24 @@ [string] $ID, [Parameter()] - [int] $PageSize = 100 + [int] $PerPage = 100 ) - $inputObject = @{ - Method = 'GET' - APIEndpoint = "/repos/$Owner/$Repo/actions/workflows?per_page=$PageSize" + begin {} + + process { + + $body = @{ + per_page = $PerPage + } + + $inputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/actions/workflows" + Method = 'GET' + Body = $body + } + + Invoke-GitHubAPI @inputObject + } - $response = Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 index a9ee5a988..df7c55509 100644 --- a/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 @@ -1,7 +1,8 @@ Function Get-GitHubWorkflowRun { <# .NOTES - https://docs.github.com/en/rest/reference/actions#list-workflow-runs-for-a-repository + https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow + https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository #> [CmdletBinding()] param ( @@ -18,26 +19,37 @@ [string] $ID, [Parameter()] - [int] $PageSize = 100 + [int] $PerPage = 100 ) - $processedPages = 0 - $workflowRuns = @() - do { - $processedPages++ + begin {} + + process { + + $body = @{ + per_page = $PerPage + } + + if ($Name) { + $ID = (Get-GitHubWorkflow -Owner $Owner -Repo $Repo -Name $Name).id + } + + if ($ID) { + $Uri = "/repos/$Owner/$Repo/actions/workflows/$ID/runs" + } else { + $Uri = "/repos/$Owner/$Repo/actions/runs" + } + $inputObject = @{ + APIEndpoint = $Uri Method = 'GET' - APIEndpoint = "/repos/$Owner/$Repo/actions/runs?per_page=$PageSize&page=$processedPages" + Body = $body } - $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 + Invoke-GitHubAPI @inputObject + + } + + end {} - 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/Actions/Get-GitHubWorkflowUsage.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 index a0d69fc88..b9c6dc56a 100644 --- a/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 @@ -1,4 +1,26 @@ Function Get-GitHubWorkflowUsage { + <# + .SYNOPSIS + Short description + + .DESCRIPTION + Long description + + .PARAMETER Owner + Parameter description + + .PARAMETER Repo + Parameter description + + .PARAMETER ID + Parameter description + + .EXAMPLE + An example + + .NOTES + https://docs.github.com/en/rest/reference/actions#get-workflow-usage + #> [CmdletBinding( DefaultParameterSetName = 'ByName' )] @@ -19,17 +41,14 @@ begin {} process { - # API Reference - # https://docs.github.com/en/rest/reference/actions#get-workflow-usage - $inputObject = @{ Method = 'GET' APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/timing" } - $response = Invoke-GitHubAPI @inputObject - $response #billable? + Invoke-GitHubAPI @inputObject + } end {} diff --git a/src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 index b65ab5ee1..6e1438563 100644 --- a/src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 +++ b/src/GitHub/public/Actions/Remove-GitHubWorkflowRun.ps1 @@ -17,14 +17,14 @@ begin {} process { + $inputObject = @{ APIEndpoint = "repos/$Owner/$Repo/actions/runs/$ID" Method = 'DELETE' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } end {} diff --git a/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 index 510344589..1d1ae9a6b 100644 --- a/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Start-GitHubWorkflow.ps1 @@ -1,25 +1,23 @@ - +function Start-GitHubWorkflow { + <# + .SYNOPSIS + Start a workflow run using the workflow's ID. -<# - .SYNOPSIS - Start a workflow run using the workflow's ID. + .DESCRIPTION + Start a workflow run using the workflow's ID. - .DESCRIPTION - Start a workflow run using the workflow's ID. - - .EXAMPLE - Get-GitHubWorkflow | Where-Object name -NotLike '.*' | Start-GitHubWorkflow -Inputs @{ - staticValidation = $true - deploymentValidation = $false - removeDeployment = $true - prerelease = $false - } + .EXAMPLE + Get-GitHubWorkflow | Where-Object name -NotLike '.*' | Start-GitHubWorkflow -Inputs @{ + staticValidation = $true + deploymentValidation = $false + removeDeployment = $true + prerelease = $false + } - .NOTES - # API Reference - # https://docs.github.com/en/free-pro-team@latest/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event -#> -function Start-GitHubWorkflow { + .NOTES + # API Reference + # https://docs.github.com/en/free-pro-team@latest/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event + #> [CmdletBinding()] param ( [Parameter()] @@ -49,17 +47,19 @@ function Start-GitHubWorkflow { process { + $body = @{ + ref = $Ref + inputs = $Inputs + } + $inputObject = @{ - Method = 'POST' APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/dispatches" - Body = @{ - ref = $Ref - inputs = $Inputs - } + Method = 'POST' + Body = $body } - $response = Invoke-GitHubAPI @inputObject - $response + Invoke-GitHubAPI @inputObject + } end {} diff --git a/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 b/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 index 7f7aa80a7..3c9ddd66a 100644 --- a/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 +++ b/src/GitHub/public/Actions/Start-GitHubWorkflowReRun.ps1 @@ -1,6 +1,26 @@ -# API Reference -# https://docs.github.com/en/rest/reference/actions#re-run-a-workflow -function Start-GitHubWorkflowReRun { +function Start-GitHubWorkflowReRun { + <# + .SYNOPSIS + Short description + + .DESCRIPTION + Long description + + .PARAMETER Owner + Parameter description + + .PARAMETER Repo + Parameter description + + .PARAMETER ID + Parameter description + + .EXAMPLE + An example + + .NOTES + https://docs.github.com/en/rest/reference/actions#re-run-a-workflow + #> [CmdletBinding()] param ( [Parameter()] @@ -20,13 +40,14 @@ function Start-GitHubWorkflowReRun { begin {} process { + $inputObject = @{ Method = 'POST' APIEndpoint = "/repos/$Owner/$Repo/actions/runs/$ID/rerun" } - $response = Invoke-GitHubAPI @inputObject - return $response + Invoke-GitHubAPI @inputObject + } end {} diff --git a/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 index f276b9c83..4821d0737 100644 --- a/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 +++ b/src/GitHub/public/Actions/Stop-GitHubWorkflowRun.ps1 @@ -1,4 +1,26 @@ function Stop-GitHubWorkflowRun { + <# + .SYNOPSIS + Short description + + .DESCRIPTION + Long description + + .PARAMETER Owner + Parameter description + + .PARAMETER Repo + Parameter description + + .PARAMETER ID + Parameter description + + .EXAMPLE + An example + + .NOTES + https://docs.github.com/en/rest/reference/actions#cancel-a-workflow-run + #> [CmdletBinding()] [alias('Cancel-GitHubWorkflowRun')] param ( @@ -19,15 +41,14 @@ begin {} process { - # API Reference - # https://docs.github.com/en/rest/reference/actions#cancel-a-workflow-run + $inputObject = @{ Method = 'POST' APIEndpoint = "/repos/$Owner/$Repo/actions/runs/$ID/cancel" } - $response = Invoke-GitHubAPI @inputObject - $response + Invoke-GitHubAPI @inputObject + } end {} diff --git a/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 b/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 index fe747e161..a8691d6af 100644 --- a/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 +++ b/src/GitHub/public/Branches/Get-GitHubRepoBranch.ps1 @@ -9,11 +9,10 @@ ) $inputObject = @{ - Method = 'GET' APIEndpoint = "/repos/$Owner/$Repo/branches" + Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 b/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 index 67ddea6ad..15a39c62e 100644 --- a/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 +++ b/src/GitHub/public/Deployments/Get-GitHubEnvironment.ps1 @@ -1,4 +1,23 @@ function Get-GitHubEnvironment { + <# + .SYNOPSIS + Get GitHub environment + + .DESCRIPTION + Long description + + .PARAMETER Owner + Parameter description + + .PARAMETER Repo + Parameter description + + .EXAMPLE + An example + + .NOTES + https://docs.github.com/en/rest/reference/repos#get-all-environments + #> [CmdletBinding()] param ( [Parameter()] @@ -11,17 +30,14 @@ begin {} process { - # API Reference - # https://docs.github.com/en/rest/reference/repos#get-all-environments $inputObject = @{ APIEndpoint = "/repos/$Owner/$Repo/environments" Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } end {} diff --git a/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 b/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 index b883e8ffa..602e2d922 100644 --- a/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 +++ b/src/GitHub/public/Deployments/Get-GitHubEnvironmentSecrets.ps1 @@ -1,4 +1,26 @@ function Get-GitHubEnvironmentSecrets { + <# + .SYNOPSIS + Get GitHub environment secrets + + .DESCRIPTION + Long description + + .PARAMETER Owner + Parameter description + + .PARAMETER Repo + Parameter description + + .PARAMETER EnvironmentName + Parameter description + + .EXAMPLE + An example + + .NOTES + https://docs.github.com/en/rest/reference/repos#get-all-environments + #> [CmdletBinding()] param ( [Parameter()] @@ -19,19 +41,13 @@ 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 = "/repositories/$RepoID/environments/$EnvironmentName/secrets" Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject - - $response + Invoke-GitHubAPI @inputObject } end {} diff --git a/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 b/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 index 93f10437f..25da09436 100644 --- a/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 +++ b/src/GitHub/public/Deployments/Update-GitHubEnvironment.ps1 @@ -22,19 +22,20 @@ begin {} process { + $body = @{ + owner = $Owner + repo = $Repo + environment_name = $Name + } + $inputObject = @{ APIEndpoint = "/repos/$Owner/$Repo/environments/$Name" - Body = @{ - owner = $Owner - repo = $Repo - environment_name = $Name - } Method = 'PUT' + Body = $body } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } end {} diff --git a/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 b/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 index 074e8e3de..ebcb6ed92 100644 --- a/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 +++ b/src/GitHub/public/Markdown/Get-GitHubMarkdown.ps1 @@ -16,17 +16,18 @@ [string] $Context ) + $body = @{ + context = $Context + mode = $Mode + text = $Text + } + $inputObject = @{ APIEndpoint = '/markdown' - Body = @{ - context = $Context - mode = $Mode - text = $Text - } Method = 'POST' + Body = $body } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 b/src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 index 7bc718839..4316e10a3 100644 --- a/src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 +++ b/src/GitHub/public/Markdown/Get-GitHubMarkdownRaw.ps1 @@ -6,20 +6,16 @@ [CmdletBinding()] param ( [Parameter()] - [switch] $Text, - - [Parameter()] - [string] $Context + [switch] $Text ) $inputObject = @{ APIEndpoint = '/markdown/raw' ContentType = 'text/plain' - Data = $Text + Body = $Text Method = 'POST' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Meta/Get-GitHubApiVersions.ps1 b/src/GitHub/public/Meta/Get-GitHubApiVersions.ps1 index ed1443291..cbb1bc3b6 100644 --- a/src/GitHub/public/Meta/Get-GitHubApiVersions.ps1 +++ b/src/GitHub/public/Meta/Get-GitHubApiVersions.ps1 @@ -23,7 +23,6 @@ Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Meta/Get-GitHubMeta.ps1 b/src/GitHub/public/Meta/Get-GitHubMeta.ps1 index cae684cf8..ccc4c9cbf 100644 --- a/src/GitHub/public/Meta/Get-GitHubMeta.ps1 +++ b/src/GitHub/public/Meta/Get-GitHubMeta.ps1 @@ -29,7 +29,6 @@ Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 b/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 index 156584cea..96ce2fa89 100644 --- a/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 +++ b/src/GitHub/public/Meta/Get-GitHubOctocat.ps1 @@ -27,12 +27,9 @@ [Alias('Say')] [Alias('Saying')] [string] - $S = 'The glass is never half empty. Its just twice as big as it needs to be.' + $S ) - # $query = [System.Web.HttpUtility]::UrlEncode($S) - # $query = [System.Uri]::EscapeDataString($S) - $body = @{ s = $S } @@ -43,7 +40,6 @@ Body = $body } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Meta/Get-GitHubRoot.ps1 b/src/GitHub/public/Meta/Get-GitHubRoot.ps1 index d7c7b4418..c4a52e16a 100644 --- a/src/GitHub/public/Meta/Get-GitHubRoot.ps1 +++ b/src/GitHub/public/Meta/Get-GitHubRoot.ps1 @@ -22,7 +22,6 @@ Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Meta/Get-GitHubZen.ps1 b/src/GitHub/public/Meta/Get-GitHubZen.ps1 index 01de64490..8ddf4a7e7 100644 --- a/src/GitHub/public/Meta/Get-GitHubZen.ps1 +++ b/src/GitHub/public/Meta/Get-GitHubZen.ps1 @@ -22,7 +22,6 @@ Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 b/src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 index 384798886..cc3516f1b 100644 --- a/src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 +++ b/src/GitHub/public/Teams/Get-GitHubRepoTeam.ps1 @@ -17,7 +17,6 @@ function Get-GitHubRepoTeam { APIEndpoint = "/repos/$Owner/$Repo/teams" } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Users/Get-GitHubUser.ps1 b/src/GitHub/public/Users/Get-GitHubUser.ps1 index 578587675..c1632017c 100644 --- a/src/GitHub/public/Users/Get-GitHubUser.ps1 +++ b/src/GitHub/public/Users/Get-GitHubUser.ps1 @@ -25,7 +25,6 @@ Method = 'GET' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/src/GitHub/public/Users/Set-GitHubUser.ps1 b/src/GitHub/public/Users/Set-GitHubUser.ps1 index d4fd0144b..8a388a937 100644 --- a/src/GitHub/public/Users/Set-GitHubUser.ps1 +++ b/src/GitHub/public/Users/Set-GitHubUser.ps1 @@ -75,7 +75,6 @@ Method = 'PATCH' } - $response = Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject - $response } diff --git a/tools/utilities/Local-Testing.ps1 b/tools/utilities/Local-Testing.ps1 index fe8cbc8a5..62dbc2087 100644 --- a/tools/utilities/Local-Testing.ps1 +++ b/tools/utilities/Local-Testing.ps1 @@ -33,7 +33,5 @@ $VerbosePreference = 'SIlentlyContinue' $str = '2023-10-27 17:43:40 UTC' $format = "yyyy-MM-dd HH:mm:ss 'UTC'" - $date = [datetime]::ParseExact($str, $format, $null) - $date From 5da8b82fec57461d1693f22d133bf90e08ab5a76 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 23:26:50 +0200 Subject: [PATCH 14/25] Expand contents of get workflow --- src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 index f5540b373..d5368fac7 100644 --- a/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflow.ps1 @@ -53,7 +53,7 @@ Body = $body } - Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject | Select-Object -ExpandProperty workflows | Write-Output } } From cdec13ae292355bb542992fe1fb11ea3ee21b275 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 23:27:12 +0200 Subject: [PATCH 15/25] Update Get-Config to return metadata table --- src/GitHub/public/Config/Get-GitHubConfig.ps1 | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/GitHub/public/Config/Get-GitHubConfig.ps1 b/src/GitHub/public/Config/Get-GitHubConfig.ps1 index b6ee6d2ac..a279f7989 100644 --- a/src/GitHub/public/Config/Get-GitHubConfig.ps1 +++ b/src/GitHub/public/Config/Get-GitHubConfig.ps1 @@ -38,6 +38,10 @@ $prefix = $script:SecretVault.Prefix + $RefreshTokenData = (Get-SecretInfo -Name "$prefix`RefreshToken").Metadata | ConvertFrom-HashTable | ConvertTo-HashTable + $AccessTokenData = (Get-SecretInfo -Name "$prefix`AccessToken").Metadata | ConvertFrom-HashTable | ConvertTo-HashTable + $metadata = Join-Hashtable -Main $RefreshTokenData -Overrides $AccessTokenData + switch($Name) { 'AccessToken' { Get-Secret -Name "$prefix`AccessToken" @@ -45,13 +49,12 @@ 'RefreshToken' { Get-Secret -Name "$prefix`RefreshToken" } - 'RefreshTokenExpirationDate' { - $RefreshTokenData = Get-SecretInfo -Name "$prefix`RefreshToken" - $RefreshTokenData.Metadata | ConvertFrom-HashTable | ConvertTo-HashTable | Select-Object -ExpandProperty $Name - } default { - $AccessTokenData = Get-SecretInfo -Name "$prefix`AccessToken" - $AccessTokenData.Metadata | ConvertFrom-HashTable | ConvertTo-HashTable | Select-Object -ExpandProperty $Name + if ($Name) { + $metadata.$Name + } else { + $metadata + } } } } From 2d71ef9bf7c9e5c522283f01ba650971df156ff6 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 23:42:43 +0200 Subject: [PATCH 16/25] Better error message --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index 913148085..a4e7bf7f7 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -133,7 +133,10 @@ Write-Error "[$functionName] - WebException - $($_.Exception.Message)" throw $_ } catch { - Write-Error "[$functionName] - GeneralException - $($_.Exception.Message)" + Write-Error "[$functionName] - GeneralException - $_" + $err = $_ | ConvertFrom-Json -Depth 10 + Write-Error "[$functionName] - $($err.Message)" + Write-Error "[$functionName] - For more info please see: [$($err.documentation_url)]" throw $_ } } From 1fa719f1d26501a3f903308fb8f881e4e2c47b65 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 23:43:00 +0200 Subject: [PATCH 17/25] Andle default param to blank --- src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 index df7c55509..5ed559f9b 100644 --- a/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 @@ -4,7 +4,7 @@ https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository #> - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = 'Repo')] param ( [Parameter()] [string] $Owner = (Get-GitHubConfig -Name Owner), From 7dcaebe6c2ac57bc354487e4b220f41aa8a09e30 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Thu, 28 Sep 2023 23:43:11 +0200 Subject: [PATCH 18/25] Add some local testing stuff --- .../public/Auth/Connect-GitHubAccount.ps1 | 19 ++++++++++++++++++- tools/utilities/Local-Testing.ps1 | 2 +- tools/utilities/StopWorkflowsCustom.ps1 | 2 ++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 index a91cce1e3..4b57c307d 100644 --- a/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 +++ b/src/GitHub/public/Auth/Connect-GitHubAccount.ps1 @@ -62,7 +62,15 @@ Mandatory, ParameterSetName = 'PAT' )] - [switch] $AccessToken + [switch] $AccessToken, + + # Set the default owner to use in commands. + [Parameter()] + [string] $Owner, + + # Set the default repository to use in commands. + [Parameter()] + [string] $Repo ) $envVar = Get-ChildItem -Path 'Env:' | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1 @@ -172,4 +180,13 @@ Write-Host '✓ ' -ForegroundColor Green -NoNewline Write-Host 'Logged in to GitHub!' + + if ($Owner) { + Set-GitHubConfig -Owner $Owner + } + + if ($Repo) { + Set-GitHubConfig -Repo $Repo + } + } diff --git a/tools/utilities/Local-Testing.ps1 b/tools/utilities/Local-Testing.ps1 index 62dbc2087..3157efdae 100644 --- a/tools/utilities/Local-Testing.ps1 +++ b/tools/utilities/Local-Testing.ps1 @@ -8,7 +8,7 @@ Get-SecretInfo Get-Module -Name GitHub -ListAvailable $VerbosePreference = 'Continue' -Install-Module -Name GitHub -Verbose -Force -AllowPrerelease +Install-Module -Name GitHub -Force -Verbose -AllowPrerelease # $env:PSModulePath += ';C:\Repos\GitHub\PSModule\Modules\GitHub\outputs' # Import-Module -Name 'C:\Repos\GitHub\PSModule\Modules\GitHub\src\GitHub\GitHub.psm1' -Verbose -Force diff --git a/tools/utilities/StopWorkflowsCustom.ps1 b/tools/utilities/StopWorkflowsCustom.ps1 index dbee1aee6..3510d410e 100644 --- a/tools/utilities/StopWorkflowsCustom.ps1 +++ b/tools/utilities/StopWorkflowsCustom.ps1 @@ -5,6 +5,8 @@ $Repo = 'ResourceModules' Install-Module -Name GitHub -Force -AllowClobber Connect-GitHubAccount -Owner $Owner -Repo $Repo -Verbose +Set-GitHubConfig -Owner $Owner -Repo $Repo -Verbose +Get-GitHubConfig Get-GitHubWorkflow -Verbose # Disable all workflows From 8b4dbcd35ecc29a59037c954eac84edaf4135722 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 29 Sep 2023 00:06:53 +0200 Subject: [PATCH 19/25] Fix error handling for api calls --- src/GitHub/public/API/Invoke-GitHubAPI.ps1 | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 index a4e7bf7f7..c8ab7a099 100644 --- a/src/GitHub/public/API/Invoke-GitHubAPI.ps1 +++ b/src/GitHub/public/API/Invoke-GitHubAPI.ps1 @@ -129,14 +129,10 @@ Invoke-RestMethod @APICall | Write-Output Write-Verbose ($StatusCode | ConvertTo-Json -Depth 100) Write-Verbose ($responseHeaders | ConvertTo-Json -Depth 100) - } catch [System.Net.WebException] { - Write-Error "[$functionName] - WebException - $($_.Exception.Message)" - throw $_ } catch { - Write-Error "[$functionName] - GeneralException - $_" + Write-Error "[$functionName] - Status code - [$StatusCode]" $err = $_ | ConvertFrom-Json -Depth 10 Write-Error "[$functionName] - $($err.Message)" Write-Error "[$functionName] - For more info please see: [$($err.documentation_url)]" - throw $_ } } From 129c811fc4578d2b1a040d6e60056a0108e7889b Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 29 Sep 2023 00:59:09 +0200 Subject: [PATCH 20/25] Fix output --- src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 index 5ed559f9b..15206b51c 100644 --- a/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowRun.ps1 @@ -46,7 +46,7 @@ Body = $body } - Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject | Select-Object -ExpandProperty workflow_runs | Write-Output } From 050d340aab30e54a1b4c6487bb1f3bcfd214a005 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 29 Sep 2023 00:59:17 +0200 Subject: [PATCH 21/25] local tests --- tools/utilities/StopWorkflowsCustom.ps1 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/utilities/StopWorkflowsCustom.ps1 b/tools/utilities/StopWorkflowsCustom.ps1 index 3510d410e..f7a4d2ab5 100644 --- a/tools/utilities/StopWorkflowsCustom.ps1 +++ b/tools/utilities/StopWorkflowsCustom.ps1 @@ -1,6 +1,9 @@ $Owner = 'MariusStorhaug' $Repo = 'ResourceModules' +$Owner = 'PSModule' +$Repo = 'GitHub' + Install-Module -Name GitHub -Force -AllowClobber Connect-GitHubAccount -Owner $Owner -Repo $Repo -Verbose From 4e205aa9509bc6e416d00f4706b5bd02edf8dcc7 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 29 Sep 2023 00:59:35 +0200 Subject: [PATCH 22/25] Title readme --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 293c5a4aa..988e606df 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -# GitHub Powershell Module - # GitHub PowerShell The **GitHub PowerShell** module serves as a convenient API wrapper around [GitHub's REST API](https://docs.github.com/en/rest), making the functionalities and data available on GitHub accessible through PowerShell commands. This module is tailored for developers, administrators, and GitHub enthusiasts who are familiar with PowerShell and want to integrate or manage their GitHub repositories seamlessly. From b77aa59e311f12a3dfb79d357382f2840b070ce1 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 29 Sep 2023 01:04:38 +0200 Subject: [PATCH 23/25] stream output + --- src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 | 2 +- src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 | 2 +- src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 | 2 +- tools/utilities/StopWorkflowsCustom.ps1 | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 index 10b9ea6e2..e430c3b64 100644 --- a/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Disable-GitHubWorkflow.ps1 @@ -26,7 +26,7 @@ Method = 'PUT' } - Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject | Out-Null } diff --git a/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 b/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 index 7579c289e..d46c2e569 100644 --- a/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 +++ b/src/GitHub/public/Actions/Enable-GitHubWorkflow.ps1 @@ -26,7 +26,7 @@ Method = 'PUT' } - Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject | Out-Null } diff --git a/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 b/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 index b9c6dc56a..4ca08c6ec 100644 --- a/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 +++ b/src/GitHub/public/Actions/Get-GitHubWorkflowUsage.ps1 @@ -47,7 +47,7 @@ APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/timing" } - Invoke-GitHubAPI @inputObject + Invoke-GitHubAPI @inputObject | Select-Object -ExpandProperty billable | Write-Output } diff --git a/tools/utilities/StopWorkflowsCustom.ps1 b/tools/utilities/StopWorkflowsCustom.ps1 index f7a4d2ab5..b73b89246 100644 --- a/tools/utilities/StopWorkflowsCustom.ps1 +++ b/tools/utilities/StopWorkflowsCustom.ps1 @@ -51,7 +51,7 @@ Get-GitHubWorkflow | Enable-GitHubWorkflow Get-GitHubWorkflow | Select-Object Name, state Get-GitHubWorkflow | Select-Object Name | Sort-Object Name -Unique - +Get-GitHubWorkflow | Get-GitHubWorkflowUsage (Get-GitHubWorkflow | Get-GitHubWorkflowRun).count Get-GitHubWorkflowRun | Cancel-GitHubWorkflowRun Get-GitHubWorkflowRun | Remove-GitHubWorkflowRun From 3869736f2c47c964b98750ced54e50ec98551dfc Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 29 Sep 2023 17:51:45 +0200 Subject: [PATCH 24/25] update testing files --- tools/utilities/GitHubAPI.ps1 | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index f9d160452..a5b3004e9 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -4,17 +4,25 @@ $Dereferenced = 'descriptions/api.github.com/dereferenced/api.github.com.deref.j $APIDocURI = $APIDocURI + $Bundled $response = Invoke-RestMethod -Uri $APIDocURI -Method Get -# $response.info # API name = GitHub REST API -# $response.openapi # Spec version = 3.0.3 -# $response.servers # API URL = api.github.com -# $response.externalDocs # API docs URL = docs.github.com/rest -# $response.components # Type specs -# $response.paths # API endpoints -# $response.tags # API categories -# $response.'x-webhooks' # Webhooks/event docs +$response.info # API name = GitHub REST API +$response.openapi # Spec version = 3.0.3 +$response.servers # API URL = api.github.com +$response.externalDocs # API docs URL = docs.github.com/rest +$response.components # Type specs +$response.paths # API endpoints # -> Namespaces, PascalCase! +$response.tags # API categories +$response.'x-webhooks' # Webhooks/event docs + +$response.paths.psobject.Properties | Select-Object ` + Name, ` + @{n = 'Get'; e = { (($_.value.psobject.Properties.Name) -contains 'Get') } }, ` + @{n = 'Post'; e = { (($_.value.psobject.Properties.Name) -contains 'Post') } }, ` + @{n = 'Delete'; e = { (($_.value.psobject.Properties.Name) -contains 'Delete') } }, ` + @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` + @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | format-table $path = '/users/{username}/hovercard' -$response.paths.$path.get.tags | clip # -> Namespace/foldername +$response.paths.$path.get.tags | clip # -> Namespace/foldername $response.paths.$path.get.operationId | clip # -> FunctionName $response.paths.$path.get.summary | clip # -> Synopsis $response.paths.$path.get.description | clip # -> Description @@ -28,3 +36,4 @@ $response.paths.$path.get.responses.'200'.content.'application/json'.schema $response.paths.$path.get.responses.'200'.content.'application/json'.schema.items # -> OutputType $response.paths.$path.get + From 857cc7b80d0896b110e3ed8e7bc6baa506f412fd Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Fri, 29 Sep 2023 18:05:02 +0200 Subject: [PATCH 25/25] Feature bump --- src/GitHub/GitHub.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitHub/GitHub.psd1 b/src/GitHub/GitHub.psd1 index 531b269f3..14bc32924 100644 --- a/src/GitHub/GitHub.psd1 +++ b/src/GitHub/GitHub.psd1 @@ -3,7 +3,7 @@ Author = 'Marius Storhaug' # Version number of this module - ModuleVersion = '0.0.1' + ModuleVersion = '0.1.1' # Description of the functionality provided by this module Description = 'GitHub PowerShell Module'