microsoft / PowerShellForGitHub

Microsoft PowerShell wrapper for GitHub API
Other
588 stars 185 forks source link

Cannot Get-GitHubRepository #369

Closed Liturgist closed 2 years ago

Liturgist commented 2 years ago

Issue Details

This is probably just a getting started problem. What about this request is "bad"?

Steps to reproduce the issue

PS C:\> Test-GitHubAuthenticationConfigured
True
PS C:\> Get-GitHubRepository -OwnerName PowerShell
Invoke-WebRequest: C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1:313
Line |
 313 |              $result = Invoke-WebRequest @params
     |                        ~~~~~~~~~~~~~~~~~~~~~~~~~
     | Response status code does not indicate success: 400 (Bad Request).

PS C:\> Get-Error

Exception             :
    Type       : Microsoft.PowerShell.Commands.HttpResponseException
    Response   : StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
                 {
                 Server: GitHub.com
                 Date: Wed, 28 Sep 2022 17:56:43 GMT
                 Transfer-Encoding: chunked
                 }
    TargetSite :
        Name          : ThrowTerminatingError
        DeclaringType : System.Management.Automation.MshCommandRuntime, System.Management.Automation, Version=7.2.6.500, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message    : Response status code does not indicate success: 400 (Bad Request).
    Source     : System.Management.Automation
    HResult    : -2146233088
    StackTrace :
   at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
TargetObject          : Method: GET, RequestUri: 'https://github.optum.com/api/v3/users/PowerShell/repos?', Version: 1.1, Content: <null>, Headers:
                        {
                        Accept: application/vnd.github.nebula-preview+json
                        Accept: application/vnd.github.baptiste-preview+json
                        Accept: application/vnd.github.mercy-preview+json
                        User-Agent: PowerShellForGitHub
                        Authorization: token ▬
                        }
CategoryInfo          : InvalidOperation: (Method: GET, Reques…ization: token ▬
                        }:HttpRequestMessage) [Invoke-WebRequest], HttpResponseException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
InvocationInfo        :
    MyCommand        : Invoke-WebRequest
    ScriptLineNumber : 313
    OffsetInLine     : 23
    HistoryId        : 357
    ScriptName       : C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1
    Line             :             $result = Invoke-WebRequest @params

    PositionMessage  : At C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1:313 char:23
                       +             $result = Invoke-WebRequest @params
                       +                       ~~~~~~~~~~~~~~~~~~~~~~~~~
    PSScriptRoot     : C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1
    PSCommandPath    : C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1
    InvocationName   : Invoke-WebRequest
    CommandOrigin    : Internal
ScriptStackTrace      : at Invoke-GHRestMethod, C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1: line 313
                        at Invoke-GHRestMethodMultipleResult, C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1: line 703
                        at Get-GitHubRepository, C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubRepositories.ps1: line 860
                        at <ScriptBlock>, <No file>: line 1powershell
   # Include your repro steps here

Verbose logs showing the problem

PS C:\> Get-Content -Path $(Get-GitHubConfiguration -Name LogPath) -Last 13
2022-09-28 12:56:43 : lit : VERBOSE : [0.16.1] Executing: Get-GitHubRepository -OwnerName "PowerShell"
2022-09-28 12:56:43 : lit : VERBOSE : Getting repos for PowerShell
2022-09-28 12:56:43 : lit : VERBOSE : Accessing [Get] https://github.optum.com/api/v3/users/PowerShell/repos? [Timeout = 0)]
2022-09-28 12:56:43 : lit : ERROR :
Invoke-WebRequest: C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1:313
Line |
 313 |              $result = Invoke-WebRequest @params
     |                        ~~~~~~~~~~~~~~~~~~~~~~~~~
     | Response status code does not indicate success: 400 (Bad Request).

2022-09-28 12:56:43 : lit : VERBOSE : [0.16.1] Executing: Set-TelemetryException -ErrorBucket "Get-GitHubRepository"
2022-09-28 12:56:43 : lit : VERBOSE : Sending telemetry event data to https://dc.services.visualstudio.com/v2/track [Timeout = 0)]

Suggested solution to the issue

Requested Assignment

- I'm just reporting this problem, but don't know how to fix it.

Operating System

PS C:\>         Get-ComputerInfo -Property @(
>>             'OsName',
>>             'OsOperatingSystemSKU',
>>             'OSArchitecture',
>>             'WindowsVersion',
>>             'WindowsBuildLabEx',
>>             'OsLanguage',
>>             'OsMuiLanguages')
>>

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

PS C:\> $PSVersionTable

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

Module Version

    Please replace this whole comment with the output of this command:

PS C:\>         @(
>>             "Running: $((Get-Module -Name PowerShellForGitHub) | Select-Object -ExpandProperty Version)",
>>             "Installed: $((Get-Module -Name PowerShellForGitHub -ListAvailable) | Select-Object -ExpandProperty Version)"
>>         ) -join [Environment]::NewLine
>>
Running: 0.16.1
Installed: 0.16.1
HowardWolosky commented 2 years ago

Hey @Liturgist,

Sorry to hear you're running into issues here. Thanks for the report.

From what you've provided, it looks like you're leveraging GitHub Enterprise, and accessing a GitHub instance running on your company's own servers. While this project has support for GitHub Enterprise (thanks to @Cellivar and @v2kiran), I've never personally used it, so I don't have any experience with it. So, I'm going to shoot in the dark here just a little bit:

  1. Do you actually have a user named PowerShell in your GitHub Enterprise instance?
  2. I found it interesting/confusing that the first error reported by Get-Error reported back that the server was GitHub.com, given that everything else was accessing your company's instance instead. Not sure what to take from that.
  3. Do any of the other API's successfully work? (Like Get-GitHubUser -Current)?
Liturgist commented 2 years ago

@HowardWolosky, you are right. I did not notice that. We do have a GitHub Enterprise Server to which it appears to be connecting. How can I tell it to connect to the public github.com?

I removed the GH_HOST environment variable.

I used the EXAMPLE 5 command, but it still appears to be connecting to our GitHub Enterprise Server. The Personal Access Token used was created on the public github.com.

PS C:\Users\pwatson2> Get-GitHubRepository -Uri https://github.com/microsoft/PowerShellForGitHub
Invoke-WebRequest: C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1:313
Line |
 313 |              $result = Invoke-WebRequest @params
     |                        ~~~~~~~~~~~~~~~~~~~~~~~~~
     | Response status code does not indicate success: 400 (Bad Request).
HowardWolosky commented 2 years ago

How can I tell it to connect to the public github.com?

The README talks about how to configure GitHub Enterprise. You'll probably want to run this:

Set-GitHubConfiguration -ApiHostName 'github.com'

That should reset it to use public GitHub instead of GitHub Enterprise. Alternatively, you can run this:

Reset-GitHubConfiguration

and that will get you back to the out-of-the-box state (in case you might have messed with other configuration properties accidentally).

I removed the GH_HOST environment variable.

This project doesn't use any environment nor global variables. Everything is self-contained in the module configuration.

I used the EXAMPLE 5 command, but it still appears to be connecting to our GitHub Enterprise Server

Yeah, that wouldn't work. When you use the Uri parameter, it just parses the OwnerName and RepositoryName from the Uri, and then operates against the configured GitHub host to get the requested info. If you want to operate against the public GitHub.com, you need to fix your ApiHostName.

Liturgist commented 2 years ago

@HowardWolosky, yes, I can see how the ApiHostName would be a problem. I have reset it as yo recommended. It now appears to be github.com. I still have the same failure, but only github.com is mentioned and not out GitHub Enterprise Server. What am I missing?

This does work as expected on my personal machine that is not on the corporate network.

PS C:\Users\lit\bin> Reset-GitHubConfiguration
PS C:\Users\lit\bin> Get-GitHubConfiguration ApiHostName
github.com
PS C:\Users\lit\bin> Get-GitHubRepository -Uri https://github.com/microsoft/PowerShellForGitHub
Invoke-WebRequest: C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1:313
Line |
 313 |              $result = Invoke-WebRequest @params
     |                        ~~~~~~~~~~~~~~~~~~~~~~~~~
     | Response status code does not indicate success: 400 (Bad Request).

PS C:\Users\lit\bin> Get-Error

Exception             :
    Type       : Microsoft.PowerShell.Commands.HttpResponseException
    Response   : StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
                 {
                 Server: GitHub.com
                 Date: Thu, 29 Sep 2022 03:24:08 GMT
                 Transfer-Encoding: chunked
                 Vary: Accept-Encoding, Accept, X-Requested-With
                 X-GitHub-Request-Id: 2A87:5227:4EEC460:A101E33:63350FD8
                 }
    TargetSite :
        Name          : ThrowTerminatingError
        DeclaringType : System.Management.Automation.MshCommandRuntime, System.Management.Automation, Version=7.2.6.500, Culture=neutral,
PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message    : Response status code does not indicate success: 400 (Bad Request).
    Source     : System.Management.Automation
    HResult    : -2146233088
    StackTrace :
   at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
TargetObject          : Method: GET, RequestUri: 'https://api.github.com/repos/microsoft/PowerShellForGitHub?', Version: 1.1, Content: <null>, Headers:
                        {
                        Authorization: token ▬
                        User-Agent: PowerShellForGitHub
                        Accept: application/vnd.github.nebula-preview+json
                        Accept: application/vnd.github.baptiste-preview+json
                        Accept: application/vnd.github.mercy-preview+json
                        }
CategoryInfo          : InvalidOperation: (Method: GET, Reques…rcy-preview+json
                        }:HttpRequestMessage) [Invoke-WebRequest], HttpResponseException
FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
InvocationInfo        :
    MyCommand        : Invoke-WebRequest
    ScriptLineNumber : 313
    OffsetInLine     : 23
    HistoryId        : 47
    ScriptName       : C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1
    Line             :             $result = Invoke-WebRequest @params

    PositionMessage  : At C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1:313 char:23
                       +             $result = Invoke-WebRequest @params
                       +                       ~~~~~~~~~~~~~~~~~~~~~~~~~
    PSScriptRoot     : C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1
    PSCommandPath    : C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1
    InvocationName   : Invoke-WebRequest
    CommandOrigin    : Internal
ScriptStackTrace      : at Invoke-GHRestMethod, C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1: line 313
                        at Invoke-GHRestMethodMultipleResult, C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubCore.ps1: line 703
                        at Get-GitHubRepository, C:\Users\lit\Documents\PowerShell\Modules\PowerShellForGitHub\0.16.1\GitHubRepositories.ps1: line 860
                        at <ScriptBlock>, <No file>: line 1
HowardWolosky commented 2 years ago

I still have the same failure, but only github.com is mentioned and not out GitHub Enterprise Server. What am I missing?

This does work as expected on my personal machine that is not on the corporate network.

All I can suggest is a complete reset of everything:

Reset-GitHubConfiguration
Clear-GitHubAuthentication

and then trying an unauthenticated command

Get-GitHubRepository -OwnerName microsoft -RepositoryName PowerShellForGitHub

That request will work unauthenticated (just verified again). If it still doesn't work, and it's working fine on your personal machine, then that means that your corporate machine is running through a proxy that is modifying the REST request and causing it to fail. If that does work, then you can try adding back in your token and we can further debug what's happening from there.

Liturgist commented 2 years ago

Many thanks @HowardWolosky. It is working now. Although, I do not understand why it did not work when I used Reset-GitHubConfigration before. I will press on. Thanks.