Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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; any other failure to list them stops the run with gh's error.

## [1.5.0] - 2026-11-21

### Added
Expand Down
41 changes: 41 additions & 0 deletions test/enable-code-review.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,47 @@ 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 \(.*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);
Expand Down
57 changes: 38 additions & 19 deletions tools/Enable-CodeReview.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -87,6 +90,36 @@ 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 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
# 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
"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) {
Expand Down Expand Up @@ -152,23 +185,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")) {
Expand Down