From 98fb7d8779022c304beea3a8b9b75ac7b2f5cdf8 Mon Sep 17 00:00:00 2001 From: Marcin Polewski Date: Sat, 21 Nov 2026 17:15:10 +0000 Subject: [PATCH 1/2] Warn and go on when GitHub refuses to list an app installation's repositories --- CHANGELOG.md | 4 +++ test/enable-code-review.test.ts | 22 ++++++++++++++ tools/Enable-CodeReview.ps1 | 51 +++++++++++++++++++++------------ 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6304050..f08c585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - This repository's own review pins NCode Review v1.5.0 and passes `approver-private-key` from its `code-review` environment, so the NCode Review Approver submits the approval of a clean review ([#234](https://github.com/NCodeGroup/code-review/issues/234), [ADR-1011](docs/decisions/1011-the-approval-is-submitted-by-an-approver-app-that-can-write.md)). +### Fixed + +- `tools/Enable-CodeReview.ps1` warns and goes on when GitHub refuses to list the repositories of an app installed on selected repositories, which it does for the token `gh auth login` gives: it names the installation's settings page, where the repository is selected by hand, and still sets up the environment and its secrets. + ## [1.5.0] - 2026-11-21 ### Added diff --git a/test/enable-code-review.test.ts b/test/enable-code-review.test.ts index 3227d47..3fbb4df 100644 --- a/test/enable-code-review.test.ts +++ b/test/enable-code-review.test.ts @@ -320,6 +320,28 @@ describe('tools/Enable-CodeReview.ps1', { concurrency: true }, () => { assert.doesNotMatch(output, /not-a-real-key/); }); + test("A refused installation list warns, names the installation's settings, and still sets the secret", { + skip, + }, async () => { + const pem = path.join(tempDir(), 'approver.pem'); + writeFileSync(pem, fakeApproverKey); + const refused: Answer = { + match: '^api user/installations/6/repositories --paginate', + stderr: 'gh: You must authenticate with an access token authorized to a GitHub App (HTTP 403)', + exit: 1, + }; + const { status, output, calls } = await run( + ['-Repo', 'alpha', '-ApproverPrivateKeyPath', pem, '-SkipBedrock'], + [refused, ...scenario()], + ); + assert.equal(status, 0, output); + assert.match(output, /Couldn't list the repositories of the NCode Review Approver installation/); + assert.match(output, /settings\/installations\/6/); + const lines = joined(calls); + assert.ok(!lines.some((line) => line.startsWith('api -X PUT user/installations/6/')), lines.join('\n')); + assert.ok(lines.includes(`secret set ${approverKey} --env code-review --repo ${org}/alpha`), lines.join('\n')); + }); + test('-ApproverPrivateKeyPath without -Repo stops before any change', { skip }, async () => { const pem = path.join(tempDir(), 'approver.pem'); writeFileSync(pem, fakeApproverKey); diff --git a/tools/Enable-CodeReview.ps1 b/tools/Enable-CodeReview.ps1 index 035c543..549a310 100644 --- a/tools/Enable-CodeReview.ps1 +++ b/tools/Enable-CodeReview.ps1 @@ -16,7 +16,9 @@ For each repository in -Repo, it: - checks that the NCode Review app is installed on it, and adds it to the installation when the app is installed - on selected repositories only; + on selected repositories only. GitHub lists and changes an installation's repositories only for a GitHub App's + user token or a personal access token, never for the token gh auth login gives, so when it refuses, the run + warns and goes on: select the repository in the app's installation settings yourself; - creates or updates its `code-review` environment so only the default branch can deploy to it. The environment holds no secrets: Anthropic's federation rule trusts only jobs in it, so only default-branch jobs get an Anthropic token; @@ -26,7 +28,8 @@ -ApproverPrivateKeyPath stores the NCode Review Approver app's private key as the secret NCODE_REVIEW_APPROVER_PRIVATE_KEY of each -Repo repository's `code-review` environment, and adds each repository to - the approver's installation when the app is installed on selected repositories only. With the key, the review's + the approver's installation when the app is installed on selected repositories only, or warns as for the NCode + Review app when GitHub refuses to list the installation's repositories. With the key, the review's approval is submitted as the approver, whose approval meets a ruleset's required approval. -AppPrivateKeyPath sets or replaces the app key. -UpdateBedrockKey replaces the Bedrock key; it's also set whenever @@ -87,6 +90,30 @@ function Invoke-Gh { $output } +# Adds the repository to an app's installation when the app is installed on selected repositories only. GitHub lists +# and changes an installation's repositories only for a GitHub App's user token or a personal access token, never for the +# OAuth token gh auth login gives, so when it refuses the list, this warns, names the installation's settings page and +# goes on, and the rest of the run still sets up the repository. +function Add-ToInstallation($Installation, $Info, [string]$AppName) { + if (-not $Installation -or $Installation.repository_selection -eq 'all') { return } + $full = $Info.full_name + # A refusal is a non-zero exit, or a terminating error where gh's stderr is one. + $installed = $null + try { + $installed = & gh api "user/installations/$($Installation.id)/repositories" --paginate --jq '.repositories[].full_name' 2>$null + $listed = -not $LASTEXITCODE + } + catch { $listed = $false } + if (-not $listed) { + Write-Warning "Couldn't list the repositories of the $AppName installation: GitHub lists them only for a GitHub App's user token or a personal access token. Make sure $full is selected at https://github.com/organizations/$Org/settings/installations/$($Installation.id)." + return + } + if ($installed -notcontains $full -and $PSCmdlet.ShouldProcess($full, "Add the repository to the $AppName installation")) { + Invoke-Gh api -X PUT "user/installations/$($Installation.id)/repositories/$($Info.id)" --silent + "Added $full to the $AppName installation." + } +} + # The organization secret's visibility and, when it's limited to selected repositories, their full names. # Returns $null when the secret doesn't exist (HTTP 404), and throws on any other failure to read it. function Get-OrgSecret([string]$Name) { @@ -152,23 +179,9 @@ foreach ($info in $repoInfos) { $full = $info.full_name $defaultBranch = $info.default_branch - # The app. - if ($installation.repository_selection -ne 'all') { - $installed = Invoke-Gh api "user/installations/$($installation.id)/repositories" --paginate --jq '.repositories[].full_name' - if ($installed -notcontains $full -and $PSCmdlet.ShouldProcess($full, 'Add the repository to the NCode Review installation')) { - Invoke-Gh api -X PUT "user/installations/$($installation.id)/repositories/$($info.id)" --silent - "Added $full to the NCode Review installation." - } - } - - # The approver app. - if ($approverInstallation -and $approverInstallation.repository_selection -ne 'all') { - $approverInstalled = Invoke-Gh api "user/installations/$($approverInstallation.id)/repositories" --paginate --jq '.repositories[].full_name' - if ($approverInstalled -notcontains $full -and $PSCmdlet.ShouldProcess($full, 'Add the repository to the NCode Review Approver installation')) { - Invoke-Gh api -X PUT "user/installations/$($approverInstallation.id)/repositories/$($info.id)" --silent - "Added $full to the NCode Review Approver installation." - } - } + # The app, and the approver app. + Add-ToInstallation $installation $info 'NCode Review' + Add-ToInstallation $approverInstallation $info 'NCode Review Approver' # The environment: only the default branch may deploy to it. if ($PSCmdlet.ShouldProcess("$full environment $environment", "Limit deployments to $defaultBranch")) { From 3a5ca98bbdfbf4a1dc79e12d32c0eeab25bcf48b Mon Sep 17 00:00:00 2001 From: Marcin Polewski Date: Sat, 21 Nov 2026 17:17:32 +0000 Subject: [PATCH 2/2] Address review: warn only on GitHub's 403 refusal, stop with gh's error otherwise, and say why gh is called directly --- CHANGELOG.md | 2 +- test/enable-code-review.test.ts | 21 ++++++++++++++++++++- tools/Enable-CodeReview.ps1 | 30 ++++++++++++++++++------------ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f08c585..73eabcb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), ### Fixed -- `tools/Enable-CodeReview.ps1` warns and goes on when GitHub refuses to list the repositories of an app installed on selected repositories, which it does for the token `gh auth login` gives: it names the installation's settings page, where the repository is selected by hand, and still sets up the environment and its secrets. +- `tools/Enable-CodeReview.ps1` warns and goes on when GitHub refuses to list the repositories of an app installed on selected repositories, which it does for the token `gh auth login` gives: it names the installation's settings page, where the repository is selected by hand, and still sets up the environment and its secrets; any other failure to list them stops the run with gh's error. ## [1.5.0] - 2026-11-21 diff --git a/test/enable-code-review.test.ts b/test/enable-code-review.test.ts index 3fbb4df..8db09c6 100644 --- a/test/enable-code-review.test.ts +++ b/test/enable-code-review.test.ts @@ -335,13 +335,32 @@ describe('tools/Enable-CodeReview.ps1', { concurrency: true }, () => { [refused, ...scenario()], ); assert.equal(status, 0, output); - assert.match(output, /Couldn't list the repositories of the NCode Review Approver installation/); + assert.match(output, /Couldn't list the repositories of the NCode Review Approver installation \(.*HTTP 403\)/); assert.match(output, /settings\/installations\/6/); const lines = joined(calls); assert.ok(!lines.some((line) => line.startsWith('api -X PUT user/installations/6/')), lines.join('\n')); assert.ok(lines.includes(`secret set ${approverKey} --env code-review --repo ${org}/alpha`), lines.join('\n')); }); + test("Another failure to list an installation's repositories stops the run with gh's error, before setting a secret", { + skip, + }, async () => { + const pem = path.join(tempDir(), 'approver.pem'); + writeFileSync(pem, fakeApproverKey); + const failed: Answer = { + match: '^api user/installations/6/repositories --paginate', + stderr: 'gh: Server Error (HTTP 502)', + exit: 1, + }; + const { status, output, calls } = await run( + ['-Repo', 'alpha', '-ApproverPrivateKeyPath', pem, '-SkipBedrock'], + [failed, ...scenario()], + ); + assert.notEqual(status, 0, output); + assert.match(output, /Couldn't list the repositories of the NCode Review Approver installation: .*HTTP 502/); + assert.ok(!joined(calls).some((line) => line.startsWith('secret set')), joined(calls).join('\n')); + }); + test('-ApproverPrivateKeyPath without -Repo stops before any change', { skip }, async () => { const pem = path.join(tempDir(), 'approver.pem'); writeFileSync(pem, fakeApproverKey); diff --git a/tools/Enable-CodeReview.ps1 b/tools/Enable-CodeReview.ps1 index 549a310..a4d7ee8 100644 --- a/tools/Enable-CodeReview.ps1 +++ b/tools/Enable-CodeReview.ps1 @@ -92,21 +92,27 @@ function Invoke-Gh { # Adds the repository to an app's installation when the app is installed on selected repositories only. GitHub lists # and changes an installation's repositories only for a GitHub App's user token or a personal access token, never for the -# OAuth token gh auth login gives, so when it refuses the list, this warns, names the installation's settings page and -# goes on, and the rest of the run still sets up the repository. +# OAuth token gh auth login gives, so when it refuses the list with HTTP 403, this warns, names the installation's +# settings page and goes on, and the rest of the run still sets up the repository. Any other failure stops the run with +# gh's error. function Add-ToInstallation($Installation, $Info, [string]$AppName) { if (-not $Installation -or $Installation.repository_selection -eq 'all') { return } $full = $Info.full_name - # A refusal is a non-zero exit, or a terminating error where gh's stderr is one. - $installed = $null - try { - $installed = & gh api "user/installations/$($Installation.id)/repositories" --paginate --jq '.repositories[].full_name' 2>$null - $listed = -not $LASTEXITCODE - } - catch { $listed = $false } - if (-not $listed) { - Write-Warning "Couldn't list the repositories of the $AppName installation: GitHub lists them only for a GitHub App's user token or a personal access token. Make sure $full is selected at https://github.com/organizations/$Org/settings/installations/$($Installation.id)." - return + # Invoke-Gh throws on any failure, and this call must tell GitHub's refusal of gh's token apart from other failures, + # so it runs gh itself and keeps gh's error lines apart from the list. + $ErrorActionPreference = 'Continue' + $output = @(& gh api "user/installations/$($Installation.id)/repositories" --paginate --jq '.repositories[].full_name' 2>&1) + $exitCode = $LASTEXITCODE + $ErrorActionPreference = 'Stop' + $errorLines = @($output | Where-Object { $_ -is [Management.Automation.ErrorRecord] } | ForEach-Object { "$_".Trim() } | Where-Object { $_ }) + $installed = @($output | Where-Object { $_ -isnot [Management.Automation.ErrorRecord] } | ForEach-Object { "$_" }) + if ($exitCode) { + $why = if ($errorLines.Count) { $errorLines[0] } else { "exit code $exitCode" } + if (($errorLines -join ' ') -match 'HTTP 403|authorized to a GitHub App') { + Write-Warning "Couldn't list the repositories of the $AppName installation ($why): GitHub lists them only for a GitHub App's user token or a personal access token. Make sure $full is selected at https://github.com/organizations/$Org/settings/installations/$($Installation.id)." + return + } + throw "Couldn't list the repositories of the $AppName installation: gh api user/installations/$($Installation.id)/repositories exited with code $exitCode ($why)." } if ($installed -notcontains $full -and $PSCmdlet.ShouldProcess($full, "Add the repository to the $AppName installation")) { Invoke-Gh api -X PUT "user/installations/$($Installation.id)/repositories/$($Info.id)" --silent