-
Notifications
You must be signed in to change notification settings - Fork 1
Gate Studio GitHub PR writes behind explicit spec approval #1799
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
Merged
groupthinking
merged 4 commits into
main
from
copilot/open-github-prs-for-approved-specs
Sep 12, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
65dcc53
Initial plan
Copilot ecfa45b
feat(web): open GitHub PRs for approved studio specs
Copilot b72aa66
Fix: The `openGitHubPrsForApprovedSpecs` server action performs privi…
vercel[bot] 87b44b8
Merge branch 'main' into copilot/open-github-prs-for-approved-specs
groupthinking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('@/lib/github-pr-client', () => ({ | ||
| createOrUpdatePullRequestsForApprovedSpecs: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('next/headers', () => ({ | ||
| headers: vi.fn(async () => new Headers()), | ||
| })); | ||
|
|
||
| vi.mock('@/lib/billing/billing-context', () => ({ | ||
| resolveTrustedBillingEmail: vi.fn(async () => 'pro@example.com'), | ||
| })); | ||
|
|
||
| vi.mock('@/lib/billing/entitlement-store', () => ({ | ||
| isProSubscriber: vi.fn(async () => true), | ||
| })); | ||
|
|
||
| import { openGitHubPrsForApprovedSpecs } from '@/app/studio/actions'; | ||
| import { createOrUpdatePullRequestsForApprovedSpecs } from '@/lib/github-pr-client'; | ||
| import { isProSubscriber } from '@/lib/billing/entitlement-store'; | ||
|
|
||
| const mockedOpen = vi.mocked(createOrUpdatePullRequestsForApprovedSpecs); | ||
| const mockedIsPro = vi.mocked(isProSubscriber); | ||
|
|
||
| describe('openGitHubPrsForApprovedSpecs', () => { | ||
| const originalToken = process.env.GITHUB_TOKEN; | ||
| const originalRepo = process.env.GITHUB_REPOSITORY; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| process.env.GITHUB_TOKEN = 'test-token'; | ||
| process.env.GITHUB_REPOSITORY = 'octo/eventrelay'; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (originalToken === undefined) delete process.env.GITHUB_TOKEN; | ||
| else process.env.GITHUB_TOKEN = originalToken; | ||
| if (originalRepo === undefined) delete process.env.GITHUB_REPOSITORY; | ||
| else process.env.GITHUB_REPOSITORY = originalRepo; | ||
| }); | ||
|
|
||
| it('refuses callers without a Pro entitlement', async () => { | ||
| mockedIsPro.mockResolvedValueOnce(false); | ||
|
|
||
| const result = await openGitHubPrsForApprovedSpecs([ | ||
| { id: 'spec-1', title: 'Approved', body: 'Body', head: 'spec/approved', approved: true }, | ||
| ]); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.error).toMatch(/pro subscription/i); | ||
| expect(mockedOpen).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('fails closed when no spec is approved', async () => { | ||
| const result = await openGitHubPrsForApprovedSpecs([ | ||
| { id: 'spec-1', title: 'Spec', body: 'Body', head: 'spec/one', approved: false }, | ||
| ]); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.error).toMatch(/approve at least one spec/i); | ||
| expect(mockedOpen).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('calls the GitHub write flow with only approved specs', async () => { | ||
| mockedOpen.mockResolvedValue([ | ||
| { specId: 'spec-1', number: 10, htmlUrl: 'https://github.com/o/r/pull/10', action: 'created' }, | ||
| ]); | ||
|
|
||
| const result = await openGitHubPrsForApprovedSpecs([ | ||
| { id: 'spec-1', title: 'Approved', body: 'Body', head: 'spec/approved', approved: true }, | ||
| { id: 'spec-2', title: 'Skipped', body: 'Body', head: 'spec/skipped', approved: false }, | ||
| ]); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
| expect(mockedOpen).toHaveBeenCalledWith( | ||
| [ | ||
| { id: 'spec-1', title: 'Approved', body: 'Body', head: 'spec/approved', approved: true }, | ||
| ], | ||
| expect.objectContaining({ | ||
| owner: 'octo', | ||
| repo: 'eventrelay', | ||
| token: 'test-token', | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('fails closed when GitHub integration is not configured', async () => { | ||
| delete process.env.GITHUB_TOKEN; | ||
| const result = await openGitHubPrsForApprovedSpecs([ | ||
| { id: 'spec-1', title: 'Approved', body: 'Body', head: 'spec/approved', approved: true }, | ||
| ]); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| expect(result.error).toMatch(/not configured/i); | ||
| expect(mockedOpen).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| 'use server'; | ||
|
|
||
| import 'server-only'; | ||
| import { headers } from 'next/headers'; | ||
| import { | ||
| createOrUpdatePullRequestsForApprovedSpecs, | ||
| type ApprovedSpecPullRequestInput, | ||
| type GitHubPullRequestWriteResult, | ||
| } from '@/lib/github-pr-client'; | ||
| import { resolveTrustedBillingEmail } from '@/lib/billing/billing-context'; | ||
| import { isProSubscriber } from '@/lib/billing/entitlement-store'; | ||
|
|
||
| export interface OpenGitHubPrsResult { | ||
| ok: boolean; | ||
| error?: string; | ||
| pullRequests: GitHubPullRequestWriteResult[]; | ||
| } | ||
|
|
||
| function parseRepository(value: string): { owner: string; repo: string } | null { | ||
| const [owner, repo, ...rest] = value.trim().split('/'); | ||
| if (!owner || !repo || rest.length > 0) return null; | ||
| return { owner, repo }; | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the trusted billing identity for this server action. | ||
| * | ||
| * Server actions POST to the page path, and `/studio` is intentionally public | ||
| * (see auth-paths.ts), so the middleware auth gate does *not* run here. Without | ||
| * an explicit check any anonymous visitor could invoke this action and drive | ||
| * GitHub writes with the server's GITHUB_TOKEN. We rebuild a minimal Request | ||
| * from the incoming cookies so the same trusted-identity resolution used by | ||
| * privileged API routes (session email → signed billing cookie) applies here. | ||
| */ | ||
| async function resolveActionBillingEmail(): Promise<string | null> { | ||
| const cookieHeader = (await headers()).get('cookie') ?? ''; | ||
| const request = new Request('https://studio.internal/studio', { | ||
| headers: { cookie: cookieHeader }, | ||
| }); | ||
| return resolveTrustedBillingEmail(request); | ||
| } | ||
|
|
||
| export async function openGitHubPrsForApprovedSpecs( | ||
| specs: ApprovedSpecPullRequestInput[], | ||
| ): Promise<OpenGitHubPrsResult> { | ||
| // Authorize before touching any server-side GitHub credentials. Opening PRs | ||
| // in the configured repository is a privileged, Pro-gated action — the same | ||
| // entitlement class as agent dispatch — so anonymous/free callers are refused | ||
| // before any GitHub API request is made. | ||
| const billingEmail = await resolveActionBillingEmail(); | ||
| if (!(await isProSubscriber(billingEmail))) { | ||
| return { | ||
| ok: false, | ||
| error: 'Opening GitHub pull requests requires a Pro subscription. Upgrade at /pricing.', | ||
| pullRequests: [], | ||
| }; | ||
| } | ||
|
|
||
| const approvedSpecs = specs.filter((spec) => spec.approved); | ||
| if (approvedSpecs.length === 0) { | ||
| return { | ||
| ok: false, | ||
| error: 'Approve at least one spec before opening GitHub pull requests.', | ||
| pullRequests: [], | ||
| }; | ||
| } | ||
|
|
||
| const token = (process.env.GITHUB_TOKEN || '').trim(); | ||
| const repository = parseRepository(process.env.GITHUB_REPOSITORY || ''); | ||
| if (!token || !repository) { | ||
| return { | ||
| ok: false, | ||
| error: 'GitHub integration is not configured.', | ||
| pullRequests: [], | ||
| }; | ||
| } | ||
|
|
||
| try { | ||
| const pullRequests = await createOrUpdatePullRequestsForApprovedSpecs( | ||
| approvedSpecs, | ||
| { | ||
| owner: repository.owner, | ||
| repo: repository.repo, | ||
| token, | ||
| }, | ||
| ); | ||
| return { ok: true, pullRequests }; | ||
| } catch (error) { | ||
| return { | ||
| ok: false, | ||
| error: error instanceof Error ? error.message : 'Could not open GitHub pull requests.', | ||
| pullRequests: [], | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.