openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 5.38 kB
JavaScript
import{i as isMissingPathError}from"./errno-DUBt6SQQ.mjs";import{o as registerSecretValueForRedaction}from"./secret-redaction-registry-DospXE4m.mjs";import{t as createSubsystemLogger}from"./subsystem-DLCDFvCH.mjs";import{r as sha256HexPrefixCore}from"./node-crypto-DPk8agfR.mjs";import"./crypto-digest-T8X5EZni.mjs";import{t as CLEARED_GITHUB_CREDENTIALS}from"./github-host-BIyVIYLv.mjs";import"./github-host-runtime-BU0Fqlvz.mjs";import{_ as writeManagedGitHubProfileFiles,d as removeManagedGitHubProfile,y as managedGitHubIdentityEnvironment}from"./github-tool-identity-ni2dSNkO.mjs";import{t as AGENT_GIT_CONFIG_PARAMETERS}from"./git-maintenance-env-CIA5le3f.mjs";import{a as executeGitCommand}from"./git-exec-BRQo7oSy.mjs";import{n as parseWorkerGitHubLaunchBinding}from"./launch-descriptor-DpXZcP4o.mjs";import path from"node:path";import fs from"node:fs/promises";import os from"node:os";import{inspectPathPermissions}from"@openclaw/fs-safe/permissions";
//!src/worker/github-binding.runtime.ts
const log=createSubsystemLogger(`worker/github`);function disposeWorkerGitHubEnvironment(e,t){return removeManagedGitHubProfile(path.join(e,`github-profiles`,sha256HexPrefixCore(t,16)))}async function bindWorkerGitHubCheckout(e,t,n,r){const git=(t,i=5e3)=>executeGitCommand(e,t,{baseEnv:n,timeoutMs:i,maxOutputBytes:{stdout:1048576,stderr:2048},...r?{signal:r}:{}});const requireGit=async e=>{const t=await git(e);if(t.code!==0||t.stdoutTruncatedBytes)throw new Error(`git ${e[0]} failed or output was truncated (exit ${t.code})`);return t.stdout};try{if((await git([`rev-parse`,`--git-dir`])).code!==0)return;if(t.remoteUrl){const e=await git([`remote`,`get-url`,`origin`]);if(e.code!==0)await requireGit([`remote`,`add`,`origin`,t.remoteUrl]);else if(e.stdout.trim()!==t.remoteUrl)await requireGit([`remote`,`set-url`,`origin`,t.remoteUrl])}const n=await git([`symbolic-ref`,`--quiet`,`HEAD`]);const i=`refs/heads/${t.branch}`;if(n.code!==0||n.stdout.trim()!==i){await requireGit([`update-ref`,i,`HEAD`]);await requireGit([`symbolic-ref`,`HEAD`,i])}if(!t.remoteUrl||r?.aborted)return;const a=await git([`fetch`,`--quiet`,`origin`,t.branch],6e4);if(a.code!==0){if(a.stderr.includes(`couldn't find remote ref`))return;throw new Error(`git fetch failed (exit ${a.code})`)}await requireGit([`update-ref`,`refs/remotes/origin/${t.branch}`,`FETCH_HEAD`]);await requireGit([`branch`,`--set-upstream-to=origin/${t.branch}`,t.branch]);const o=(await requireGit([`rev-parse`,`HEAD`])).trim();const s=(await requireGit([`rev-parse`,`FETCH_HEAD`])).trim();if(o===s)return;if((await git([`merge-base`,`--is-ancestor`,`HEAD`,`FETCH_HEAD`])).code!==0){log.warn(`GitHub checkout fast-forward skipped: ${t.branch} HEAD=${o.slice(0,7)} origin=${s.slice(0,7)}`);return}if(r?.aborted)return;const c=(await requireGit([`diff`,`--name-only`,`--diff-filter=A`,`--no-renames`,`-z`,`HEAD`,`FETCH_HEAD`,`--`])).split(`\0`).filter(Boolean);let l=false;for(const t of c){const n=t.split(`/`);let r=e;for(const[e,t]of n.entries()){r=path.join(r,t);try{const t=await fs.lstat(r);if(e===n.length-1?t.isDirectory():!t.isDirectory()){l=true;break}}catch(e){if(!isMissingPathError(e))throw e;break}}if(l)break}if(l){log.warn(`GitHub checkout fast-forward skipped: local path conflicts with ${t.branch}`);return}const listDeleted=async()=>(await requireGit([`ls-files`,`--deleted`,`-z`])).split(`\0`).filter(Boolean);const u=new Set(await listDeleted());await requireGit([`reset`,`--mixed`,`FETCH_HEAD`]);const d=(await listDeleted()).filter(e=>!u.has(e));if(d.length>0)await requireGit([`--literal-pathspecs`,`checkout`,`--`,...d])}catch(e){log.warn(`GitHub checkout binding failed: ${String(e).slice(0,2048)}`)}}async function prepareWorkerGitHubEnvironment(e){const{binding:t,stateDir:n,turnId:r,cwd:i,signal:a}=e;a?.throwIfAborted();registerSecretValueForRedaction(t.token);const o=path.join(n,`github-profiles`,sha256HexPrefixCore(r,16));try{await removeManagedGitHubProfile(o);await writeManagedGitHubProfileFiles(o,t,{assertCurrent:()=>a?.throwIfAborted()})}catch(e){const t=e instanceof Error?e.message:String(e);throw new Error(`Worker GitHub identity profile could not be written: ${t}`,{cause:e})}const s=managedGitHubIdentityEnvironment({profileDir:o,gitAuthor:t.gitAuthor,gitConfig:[[`credential.helper`,``],[`credential.helper`,`!gh auth git-credential`]]});if(os.platform()===`win32`){const e=await inspectPathPermissions(o);if(!e.ok||e.source!==`windows-acl`||e.ownerTrusted!==true||e.groupReadable||e.worldReadable||e.groupWritable||e.worldWritable){await removeManagedGitHubProfile(o);log.warn(`GitHub binding skipped: profile is not owner-only: ${o}`);return}}await bindWorkerGitHubCheckout(i,t,{...process.env,...s,...CLEARED_GITHUB_CREDENTIALS},a);return{refresh:async(e,n)=>{const assertActive=()=>{n();a?.throwIfAborted()};assertActive();const r=parseWorkerGitHubLaunchBinding({...t,token:e.token});if(!r||e.expiresAtMs!==void 0&&e.expiresAtMs<=Date.now())throw new Error(`Worker GitHub refresh is invalid or expired`);registerSecretValueForRedaction(r.token);await writeManagedGitHubProfileFiles(o,r,{assertCurrent:assertActive});assertActive()},managedLocalIdentity:true,excludedStoreNames:[],credentialScrubEnv:{...CLEARED_GITHUB_CREDENTIALS},localIdentityEnv:s,localGitConfigParameters:AGENT_GIT_CONFIG_PARAMETERS}}export{disposeWorkerGitHubEnvironment,prepareWorkerGitHubEnvironment};