1212 *
1313 * Options:
1414 * --session-dir <path> Path to session directory under $HOME (default: auto-detect ~/.codex, ~/.claude, ~/.qwen, or ~/.gemini)
15- * --pr-number <number> PR number to post comment to (optional, auto-detected from branch)
15+ * --pr-number <number> Open PR number to post comment to (optional, auto-detected from branch)
1616 * --repo <owner/repo> Source repository (optional, auto-detected from git remote)
1717 * --no-comment Skip posting PR comment
1818 * --dry-run Show what would be uploaded without actually uploading
@@ -29,6 +29,7 @@ const fs = require("node:fs");
2929const path = require ( "node:path" ) ;
3030const { execSync, spawnSync } = require ( "node:child_process" ) ;
3131const os = require ( "node:os" ) ;
32+ const GH_MAX_BUFFER_BYTES = 32 * 1024 * 1024 ;
3233
3334const {
3435 buildBlobUrl,
@@ -107,7 +108,7 @@ const parseArgs = () => {
107108
108109Options:
109110 --session-dir <path> Path to session directory under $HOME
110- --pr-number <number> PR number to post comment to
111+ --pr-number <number> Open PR number to post comment to
111112 --repo <owner/repo> Source repository
112113 --no-comment Skip posting PR comment
113114 --dry-run Show what would be uploaded
@@ -142,6 +143,7 @@ const ghCommand = (args, ghEnv) => {
142143 const result = spawnSync ( "gh" , args , {
143144 encoding : "utf8" ,
144145 stdio : [ "pipe" , "pipe" , "pipe" ] ,
146+ maxBuffer : GH_MAX_BUFFER_BYTES ,
145147 env : ghEnv ,
146148 } ) ;
147149
@@ -245,20 +247,24 @@ const getPrNumberFromBranch = (repo, branch, ghEnv) => {
245247 return null ;
246248} ;
247249
248- const prExists = ( repo , prNumber , ghEnv ) => {
250+ const getPrState = ( repo , prNumber , ghEnv ) => {
249251 const result = ghCommand ( [
250252 "pr" ,
251253 "view" ,
252254 prNumber . toString ( ) ,
253255 "--repo" ,
254256 repo ,
255257 "--json" ,
256- "number " ,
258+ "state " ,
257259 "--jq" ,
258- ".number " ,
260+ ".state " ,
259261 ] , ghEnv ) ;
260262
261- return result . success && result . stdout === prNumber . toString ( ) ;
263+ return result . success ? result . stdout : null ;
264+ } ;
265+
266+ const prIsOpen = ( repo , prNumber , ghEnv ) => {
267+ return getPrState ( repo , prNumber , ghEnv ) === "OPEN" ;
262268} ;
263269
264270const getPrNumberFromWorkspaceBranch = ( branch ) => {
@@ -275,9 +281,12 @@ const findPrContext = (repos, branch, verbose, ghEnv) => {
275281 for ( const repo of repos ) {
276282 log ( verbose , `Checking open PR in ${ repo } for branch ${ branch } ` ) ;
277283 const prNumber = getPrNumberFromBranch ( repo , branch , ghEnv ) ;
278- if ( prNumber !== null ) {
284+ if ( prNumber !== null && prIsOpen ( repo , prNumber , ghEnv ) ) {
279285 return { repo, prNumber } ;
280286 }
287+ if ( prNumber !== null ) {
288+ log ( verbose , `Skipping PR #${ prNumber } in ${ repo } : PR is not open` ) ;
289+ }
281290 }
282291
283292 const workspacePrNumber = getPrNumberFromWorkspaceBranch ( branch ) ;
@@ -287,7 +296,7 @@ const findPrContext = (repos, branch, verbose, ghEnv) => {
287296
288297 for ( const repo of repos ) {
289298 log ( verbose , `Checking workspace PR #${ workspacePrNumber } in ${ repo } for branch ${ branch } ` ) ;
290- if ( prExists ( repo , workspacePrNumber , ghEnv ) ) {
299+ if ( prIsOpen ( repo , workspacePrNumber , ghEnv ) ) {
291300 return { repo, prNumber : workspacePrNumber } ;
292301 }
293302 }
@@ -410,7 +419,7 @@ const buildSnapshotReadme = ({ backupRepo, source, manifestUrl, summary, session
410419 "" ,
411420 `- Manifest: ${ manifestUrl } ` ,
412421 "" ,
413- "Generated automatically by the docker-git `pre -push` session backup hook." ,
422+ "Generated automatically by the docker-git `post -push` session backup hook." ,
414423 "" ,
415424 ] . join ( "\n" ) ;
416425
@@ -492,7 +501,11 @@ const main = () => {
492501
493502 let prContext = null ;
494503 if ( args . prNumber !== null ) {
495- prContext = { repo : sourceRepo , prNumber : args . prNumber } ;
504+ if ( prIsOpen ( sourceRepo , args . prNumber , ghEnv ) ) {
505+ prContext = { repo : sourceRepo , prNumber : args . prNumber } ;
506+ } else {
507+ console . log ( `[session-backup] Skipping PR comment: PR #${ args . prNumber } is not open` ) ;
508+ }
496509 } else if ( args . postComment ) {
497510 prContext = findPrContext ( repoCandidates , branch , verbose , ghEnv ) ;
498511 }
0 commit comments