-
Notifications
You must be signed in to change notification settings - Fork 1
feat(B-0581): decompose slice 1 - gh auth refresh wrapper script #3979
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
76f76ab
feat(B-0581): decompose slice 1 - gh auth refresh wrapper script
AceHack f320abc
fix(B-0581): address 3 reviewer threads — buffer prompt across chunks…
AceHack 4aa1e02
fix(B-0581): handle gh's post-code Enter prompt (Codex P1 follow-up a…
AceHack 3b2dc0b
fix(B-0581 wrapper): move TextDecoder inside handleOutput to avoid co…
AceHack 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,93 @@ | ||
| import { spawn } from "bun"; | ||
|
|
||
| export async function main(): Promise<number> { | ||
| const scopes = process.argv.slice(2).join(","); | ||
| if (!scopes) { | ||
| console.error("Usage: bun run tools/auth/gh-auth-refresh-wrapper.ts <scopes>"); | ||
| return 1; | ||
| } | ||
|
|
||
| console.log(`Starting gh auth refresh for scopes: ${scopes}`); | ||
|
|
||
| const proc = spawn({ | ||
| cmd: ["gh", "auth", "refresh", "-h", "github.com", "-s", scopes], | ||
| stdin: "pipe", | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| let codeCaptured = false; | ||
|
|
||
| const handleOutput = async (stream: ReadableStream, isStderr: boolean) => { | ||
| const out = isStderr ? process.stderr : process.stdout; | ||
| // Per-invocation TextDecoder: TextDecoder is stateful (retains partial | ||
| // multi-byte sequences between decode() calls). Two concurrent handleOutput | ||
| // calls (stdout + stderr) sharing one decoder can corrupt cross-stream output. | ||
| const decoder = new TextDecoder(); | ||
| // Buffer text across chunks: gh output may split prompts across | ||
| // arbitrary chunk boundaries, so per-chunk includes() can miss them. | ||
| let buffer = ""; | ||
| const PROMPT = "? Authenticate Git with your GitHub credentials?"; | ||
| let promptHandled = false; | ||
| for await (const chunk of stream) { | ||
| const text = decoder.decode(chunk); | ||
| out.write(text); | ||
| buffer += text; | ||
|
|
||
| // Look for the Y/N prompt in accumulated buffer. | ||
| if (!promptHandled && buffer.includes(PROMPT)) { | ||
| proc.stdin.write("Y\n"); | ||
| proc.stdin.flush(); | ||
| promptHandled = true; | ||
| // Trim consumed prompt to bound buffer growth. | ||
| buffer = buffer.slice(buffer.indexOf(PROMPT) + PROMPT.length); | ||
| } | ||
|
|
||
| // Look for the one-time code in accumulated buffer. | ||
| const codeMatch = buffer.match(/! First copy your one-time code: ([A-Z0-9-]+)/); | ||
| if (codeMatch && !codeCaptured) { | ||
| const code = codeMatch[1]; | ||
| codeCaptured = true; | ||
| console.log(`\n\n`); | ||
| console.log(`========================================================`); | ||
| console.log(`🚀 ONE-TIME CODE CAPTURED: ${code} 🚀`); | ||
| console.log(`========================================================`); | ||
| console.log(`\n`); | ||
| // gh waits at "Press Enter to open github.com in your browser..." | ||
| // after printing the code. Pump a newline so the device flow | ||
| // continues to the token-store step; without this the wrapper | ||
| // hangs indefinitely because the child stdin is piped and the | ||
| // operator can't satisfy the prompt manually. | ||
| proc.stdin.write("\n"); | ||
| proc.stdin.flush(); | ||
| } | ||
|
|
||
| // Keep buffer bounded across long sessions. | ||
| if (buffer.length > 16384) { | ||
| buffer = buffer.slice(-8192); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Capture handler promises and await them before exit; otherwise | ||
| // trailing stdout/stderr (e.g. the one-time code banner) can be | ||
| // dropped when process.exit fires before the output pumps drain. | ||
| const handlersPromise = Promise.all([ | ||
| handleOutput(proc.stdout, false), | ||
| handleOutput(proc.stderr, true), | ||
| ]).catch(console.error); | ||
|
|
||
| const exitCode = await proc.exited; | ||
| await handlersPromise; | ||
| console.log(`\ngh auth refresh exited with code ${exitCode}`); | ||
| return exitCode; | ||
| } | ||
|
|
||
| if (import.meta.main) { | ||
| main() | ||
| .then((code) => process.exit(code)) | ||
| .catch((err) => { | ||
| console.error("Error running wrapper:", err); | ||
| process.exit(1); | ||
| }); | ||
| } | ||
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.