🌳
pt0/opencodeF/k8sF/epSyncAI.mjs
1import { spawnSync } from 'node:child_process'
2import { syncCodeserv as doSyncCodeserv } from './codeservLibAI.mts'
9const lsRemoteWithRetry = async (gitsshRemoteUrl, maxRetries = 3) => {
10 for (let i = 0; i < maxRetries; i++) {
11 const result = spawnSync('git', ['ls-remote', gitsshRemoteUrl], {stdio: 'pipe', timeout: 10000})
12 if (result.status === 0) return {ok: true, hasContent: !!result.stdout?.toString().trim()}
13 if (i < maxRetries - 1) {
14 console.log(`gitssh not reachable yet, retrying in 3s...`)
15 await sleep(3000)
16 }
17 }
18 return {ok: false, hasContent: false}
21const pushWithRetry = async (gitsshRemoteUrl, maxRetries = 3) => {
22 for (let i = 0; i < maxRetries; i++) {
23 const result = spawnSync('git', ['push', gitsshRemoteUrl, 'HEAD:main'], {stdio: 'inherit', timeout: 120000})
24 if (result.status === 0) return true
25 if (i < maxRetries - 1) {
26 console.log(`Push failed, retrying in 3s...`)
27 await sleep(3000)
28 }
29 }
30 return false
33const ensureGitsshHasContent = async ({action, gitsshRemoteUrl}) => {
34 if (action !== 'apply') return true
36 const {ok, hasContent} = await lsRemoteWithRetry(gitsshRemoteUrl)
37 if (ok && hasContent) return true
39 if (!ok) console.log('\n gitssh not reachable, will push to init')
40 else console.log('\n gitssh repo is empty, pushing current branch...')
41 const pushOk = await pushWithRetry(gitsshRemoteUrl)
42 if (!pushOk) {
43 console.error(`Push failed. Run setup action for gitssh first to init the repo.`)
44 return false
45 }
46 // Verify push propagated (brief delay can occur between push completing and refs being available)
47 const {hasContent: verified} = await lsRemoteWithRetry(gitsshRemoteUrl)
48 if (!verified) {
49 console.error(`Push succeeded but refs not visible yet - unexpected state`)
50 return false
51 }
52 console.log('Push successful!')
53 return true
56export const syncCodeserv = async () => {
57 const {gitName, gitEmail, gitsshRemoteUrl} = getReqCodeservCtx()
58 const action = getAction()
59 assertDefined(gitName)
60 assertDefined(gitEmail)
61 assertDefined(gitsshRemoteUrl)
62 await ensureGitsshHasContent({action, gitsshRemoteUrl})