microsoft / PowerShellForGitHub

Microsoft PowerShell wrapper for GitHub API
Other
584 stars 184 forks source link

Get-GitHubRepositoryBranchProtectionRule fails with proper permissions #354

Closed HanwhaARudolph closed 1 year ago

HanwhaARudolph commented 2 years ago

Issue Details

When I try to run a Get-GitHubRepositoryBranchProtectionRule prior to applying one, the function bombs out unless you specify -AccessToken.

Steps to reproduce the issue

    $BranchProtection = Get-GitHubRepositoryBranchProtectionRule -RepositoryName $repoName -OwnerName $splatParams.OrganizationName -BranchName "main"
    If (-not $BranchProtection)
    {
        $BranchProtection = New-GitHubRepositoryBranchProtectionRule -RepositoryName $repoName `
            -OwnerName $splatParams.OrganizationName `
            -BranchName "main" `
            -RequireCodeOwnerReviews `
            -RequiredApprovingReviewCount 1
    }

Verbose logs showing the problem

The remote server returned an error: (404) Not Found. 404 | Not Found Not Found | https://docs.github.com/rest/reference/repos#get-branch-protection This typically happens when the current user isn't properly authenticated. You may need an Access Token with additional scopes checked. RequestId: XXXXXXXXX At C:\Program Files\WindowsPowerShell\Modules\PowerShellForGitHub\0.16.0\GitHubCore.ps1:557 char:9

Suggested solution to the issue

New-GitHubRepositoryBranchProtectionRule appears to honor the Set-GitHubAuthentication but the get version of the command does not.

PowerShell Version

5.1.19041.1682

Module Version

Running: 0.16.0 Installed: 0.16.0

HowardWolosky commented 1 year ago

Thanks -- will be looking into it this week.

HowardWolosky commented 1 year ago

This behavior is by design from the GitHub API perspective. You've requested the branch protection rule for a given branch, but it doesn't exist. GitHub is consistent in those scenarios. Any time you try to retrieve an object that doesn't exist, it returns a 404. It will also return a 404 if the object exists but you don't have permission to it. They obscure it this way so that you can't determine if something private exists just by looking for a different access denied error.

The specific messaging around needing an AccessToken comes from this module, not from GitHub. We add that in as a nudge to help you in the event that you forgot to authenticate, or in the event that you've authenticated against the wrong account when performing the query. I'll look at updating this string so that it's more clear that it only may be an authentication issue.

https://github.com/microsoft/PowerShellForGitHub/blob/22e3d7bdf6c3b33fdead74dac831e0bb43beb2c4/GitHubCore.ps1#L539-L544