UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 5.16 kB
import{resolveGitHubInstallationToken}from"#public/channels/github/auth.js";import{getGitHubPullRequest,getGitHubRepository}from"#public/channels/github/api.js";async function checkoutGitHubRepository(t,n){let r=await resolveCheckoutDescriptor(n),i=t.resolvePath(n.path??`/workspace`),a=resolveCheckoutRef(n.ref,r);if(isFullSha(a)){let e=await readCheckoutHead(t,i);if(e===a)return{baseRef:r.baseRef,path:i,ref:a,sha:e}}let o=normalizeCheckoutDepth(n.depth),s=n.mode===`full`?``:` --depth ${o}`,c=await resolveGitHubInstallationToken({api:n.api,credentials:n.credentials,installationId:r.installationId}),l=publicRemoteUrl({owner:r.owner,repo:r.repo}),u=isFullSha(a)?a:`FETCH_HEAD`;await t.setNetworkPolicy(buildBrokerNetworkPolicy(c)),await runCheckoutCommand({command:`mkdir -p ${shellQuote(i)}`,label:`create checkout directory`,sandbox:t}),await runCheckoutCommand({command:`cd ${shellQuote(i)} && git init`,label:`initialize git repository`,sandbox:t}),await runCheckoutCommand({command:`cd ${shellQuote(i)} && git remote remove origin >/dev/null 2>&1 || true`,label:`reset git remote`,sandbox:t}),await runCheckoutCommand({command:`cd ${shellQuote(i)} && git remote add origin ${shellQuote(l)}`,label:`configure git remote`,sandbox:t}),await runCheckoutCommand({command:`cd ${shellQuote(i)} && GIT_TERMINAL_PROMPT=0 git fetch${s} origin ${shellQuote(a)}`,label:`fetch GitHub ref`,sandbox:t}),await runCheckoutCommand({command:`cd ${shellQuote(i)} && git checkout --detach ${shellQuote(u)}`,label:`checkout GitHub ref`,sandbox:t}),n.includeBase===!0&&r.baseSha!==null&&await runCheckoutCommand({command:`cd ${shellQuote(i)} && GIT_TERMINAL_PROMPT=0 git fetch${s} origin ${shellQuote(r.baseSha)}`,label:`fetch GitHub base ref`,sandbox:t});let d=(await runCheckoutCommand({command:`cd ${shellQuote(i)} && git rev-parse HEAD`,label:`resolve checked out commit`,sandbox:t})).stdout.trim()||r.headSha||a;return{baseRef:r.baseRef,path:i,ref:a,sha:d}}async function resolveCheckoutDescriptor(e){let n=readNonEmptyString(e.owner),r=readNonEmptyString(e.repo);if(n===void 0||r===void 0)throw Error(`GitHub checkout requires a repository owner and name.`);if(e.installationId===void 0||e.installationId===null)throw Error(`GitHub checkout requires a GitHub App installation id.`);let i=e.pullRequestNumber??null,a=readNonEmptyString(e.ref),o=null;i!==null&&(a===void 0&&((e.headSha===void 0||e.headSha===null)&&(e.headRef===void 0||e.headRef===null)||e.defaultBranch===void 0||e.defaultBranch===null)||e.includeBase===!0&&(e.baseSha===void 0||e.baseSha===null)||e.includeBase===!0&&(e.baseRef===void 0||e.baseRef===null))&&(o=await getGitHubPullRequest({api:e.api,credentials:e.credentials,installationId:e.installationId,owner:n,pullRequestNumber:i,repo:r}));let s=readNonEmptyString(e.defaultBranch)??o?.defaultBranch??await resolveRepositoryDefaultBranch(e,{owner:n,pullRequestNumber:i,repo:r});return{baseRef:e.baseRef??o?.base.ref??null,baseSha:e.baseSha??o?.base.sha??null,defaultBranch:s,headRef:e.headRef??o?.head.ref??null,headSha:e.headSha??o?.head.sha??null,installationId:e.installationId,owner:n,pullRequestNumber:i,repo:r}}async function resolveRepositoryDefaultBranch(e,t){return t.pullRequestNumber!==null||readNonEmptyString(e.ref)!==void 0||readNonEmptyString(e.headSha)!==void 0||readNonEmptyString(e.headRef)!==void 0?null:(await getGitHubRepository({api:e.api,credentials:e.credentials,installationId:e.installationId??void 0,owner:t.owner,repo:t.repo})).defaultBranch??null}function resolveCheckoutRef(e,t){if(e!==void 0&&e.trim().length>0)return e.trim();if(t.headSha!==null)return t.headSha;if(t.pullRequestNumber!==null)return`refs/pull/${t.pullRequestNumber}/head`;if(t.headRef!==null)return t.headRef;if(t.defaultBranch!==null)return t.defaultBranch;throw Error(`GitHub checkout could not resolve a ref to fetch.`)}async function readCheckoutHead(e,t){let n=await e.run({command:`cd ${shellQuote(t)} && git rev-parse HEAD 2>/dev/null`});if(n.exitCode!==0)return null;let r=String(n.stdout??``).trim();return isFullSha(r)?r:null}async function runCheckoutCommand(e){let t=await e.sandbox.run({command:e.command}),n=String(t.stderr??``),r=String(t.stdout??``);if(t.exitCode===0)return{stderr:n,stdout:r};throw Error([`GitHub checkout failed during ${e.label} (exit ${t.exitCode}).`,n?`stderr: ${n}`:void 0,r?`stdout: ${r}`:void 0,`Verify the GitHub App installation has access to this repository.`].filter(e=>e!==void 0).join(` `))}function normalizeCheckoutDepth(e){if(e===void 0)return 1;if(!Number.isFinite(e)||e<1)throw Error(`GitHub checkout depth must be a positive number.`);return Math.floor(e)}function publicRemoteUrl(e){return`https://github.com/${e.owner}/${e.repo}.git`}function buildBrokerNetworkPolicy(e){let t=[{transform:[{headers:{Authorization:`Basic ${Buffer.from(`x-access-token:${e}`).toString(`base64`)}`}}]}];return{allow:{"github.com":t,"codeload.github.com":t,"*":[]}}}function readNonEmptyString(e){return typeof e==`string`&&e.trim().length>0?e.trim():void 0}function isFullSha(e){return/^[a-f0-9]{40}$/iu.test(e)}function shellQuote(e){return`'${e.replace(/'/gu,`'\\''`)}'`}export{checkoutGitHubRepository};