Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get-GithubRepository doesn't appear to honor ErrorAction inside a a try/catch #428

Open
jeffpatton1971 opened this issue Dec 18, 2023 · 1 comment
Labels
bug This relates to a bug in the existing module. triage needed An issue that needs to be reviewed by a member of the team.

Comments

@jeffpatton1971
Copy link

I write my code inside try/catch blocks and set ErrorActionPrefernce = "Stop" and clear the error stack. When I know something will fail and I want to handle it outside the try/catch then I will pass -ErrorAction SilentlyContinue and capture the result. In the situation described in the title, I have a pipeline in Azure Devops that I use to create a repo, but I need to test if it exists first, then create it if it doesn't. I do this because New-GithubRepository fails when you attempt to create a repo. Part of the pipeline passes the reposoity object onto the pipeline so tasks further down the flow can work properly.

I know at one point this worked as my code (similar to below) is well over a year old. I can confirm the issue exists in v16 and the current v17.
 

Issue Details

When passing -ErrorAction SilentlyContinue into cmdlet, it should be honored and not throw an error.

Steps to reproduce the issue

# JeffreyPatton@FSTNQL1 | 13:50:25 | 12-18-2023 | [88.78GB] D:\CODE\projects\mod-posh\powershell\production $  [master ≡]
$SecureString = 'SecretToken' |ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential 'ignoreme', $Securestring
Set-GitHubConfiguration -SuppressTelemetryReminder
Set-GitHubAuthentication -Credential $Credential;
$ErrorActionPreference
Continue

# JeffreyPatton@FSTNQL1 | 13:50:25 | 12-18-2023 | [88.78GB] D:\CODE\projects\mod-posh\powershell\production $  [master ≡]

$ErrorActionPreference = 'Stop'; try { $Result = Get-GitHubRepository -OwnerName 'valid-organization' -RepositoryName 'non-existent-repo' -ErrorAction SilentlyContinue; if ($Result -eq $null) {return $true} } catch {throw $_}

Invoke-WebRequest: E:\Documents\PowerShell\Modules\PowerShellForGitHub\0.17.0\GitHubCore.ps1:320
Line |
 320 |              $result = Invoke-WebRequest @params
     |                        ~~~~~~~~~~~~~~~~~~~~~~~~~
     |  {   "message": "Not Found",   "documentation_url": "https://docs.github.com/rest/repos/repos#get-a-repository" }
# JeffreyPatton@FSTNQL1 | 13:50:31 | 12-18-2023 | [88.78GB] D:\CODE\projects\mod-posh\powershell\production $  [master ≡]

Verbose logs showing the problem

VERBOSE: [0.17.0] Executing: Get-GitHubRepository -OwnerName "valid-org" -RepositoryName "non-existent-repo" -ErrorAction 0
VERBOSE: Getting valid-org/non-existent-repo
VERBOSE: Accessing [Get] https://api.github.com/repos/valid-orgc/non-existent-repo? [Timeout = 0)]
VERBOSE: Requested HTTP/1.1 GET with 0-byte payload
VERBOSE: Received HTTP/1.1 response of content type application/json of unknown size
VERBOSE: [0.17.0] Executing: Set-TelemetryException -ErrorBucket "Get-GitHubRepository"
VERBOSE: Sending telemetry event data to https://dc.services.visualstudio.com/v2/track [Timeout = 0)]
VERBOSE: Requested HTTP/1.1 POST with 1462-byte payload
VERBOSE: Received HTTP/1.1 49-byte response of content type application/json

Suggested solution to the issue

I'm unsure how to resolve this issue

Requested Assignment

Operating System

OsName               : Microsoft Windows 10 Enterprise
OsOperatingSystemSKU : EnterpriseEdition
OsArchitecture       : 64-bit
WindowsVersion       : 2009
WindowsBuildLabEx    : 19041.1.amd64fre.vb_release.191206-1406
OsLanguage           : en-US
OsMuiLanguages       : {en-US}

PowerShell Version

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module Version

Running: 0.17.0
Installed: 0.17.0
@jeffpatton1971 jeffpatton1971 added bug This relates to a bug in the existing module. triage needed An issue that needs to be reviewed by a member of the team. labels Dec 18, 2023
@jeffpatton1971
Copy link
Author

My current workaround is to test for the error in the catch block which for me is less than ideal, as now I have two places where I set the repo where before it was essentially one.

if (($_.Exception.Response.StatusCode.value__ -eq '422') -and ($_.ErrorDetails.Message |Select-String 'name already exists on this account') ) 

If this evals to true then i run get-githubrepository and capture the information I'm looking for. Again, I would prefer that when I pass -ErrorAction into the cmdlet it honors that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This relates to a bug in the existing module. triage needed An issue that needs to be reviewed by a member of the team.
Projects
None yet
Development

No branches or pull requests

1 participant