UNPKG

eve

Version:

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

1 lines • 7.1 kB
import{isObject}from"#shared/guards.js";import{parseJsonObject}from"#shared/json.js";import{resolveGitHubInstallationToken}from"#public/channels/github/auth.js";var GitHubApiError=class extends Error{body;method;path;status;constructor(e){super(`GitHub ${e.method} ${e.path} failed with HTTP ${e.status}.`),this.name=`GitHubApiError`,this.body=e.body,this.method=e.method,this.path=e.path,this.status=e.status}};async function callGitHubApi(e){let t=e.api?.fetch??fetch,n=e.options?.auth!==!1,r={accept:`application/vnd.github+json`,"x-github-api-version":`2022-11-28`,...e.options?.headers};e.body!==void 0&&(r[`content-type`]=`application/json; charset=utf-8`),n&&(r.authorization=`Bearer ${await resolveGitHubInstallationToken({api:e.api,credentials:e.credentials,installationId:e.options?.installationId??e.installationId})}`);let i=await t(`${e.api?.apiBaseUrl??`https://api.github.com`}${e.path}`,{body:e.body===void 0?void 0:JSON.stringify(parseJsonObject(e.body)),headers:r,method:e.method}),a=await parseResponseBody(i);if(!i.ok)throw new GitHubApiError({body:a,method:e.method,path:e.path,status:i.status});return{body:a,ok:i.ok,status:i.status}}async function createGitHubIssueComment(e){return toPostedComment((await callGitHubApi({api:e.api,body:normalizeCommentBody(e.body),credentials:e.credentials,installationId:e.installationId,method:`POST`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/issues/${e.issueNumber}/comments`})).body)}async function updateGitHubIssueComment(e){return toPostedComment((await callGitHubApi({api:e.api,body:normalizeCommentBody(e.body),credentials:e.credentials,installationId:e.installationId,method:`PATCH`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/issues/comments/${e.commentId}`})).body)}async function createGitHubReviewCommentReply(e){return toPostedComment((await callGitHubApi({api:e.api,body:normalizeCommentBody(e.body),credentials:e.credentials,installationId:e.installationId,method:`POST`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/pulls/${e.pullRequestNumber}/comments/${e.commentId}/replies`})).body)}async function updateGitHubPullRequestReviewComment(e){return toPostedComment((await callGitHubApi({api:e.api,body:normalizeCommentBody(e.body),credentials:e.credentials,installationId:e.installationId,method:`PATCH`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/pulls/comments/${e.commentId}`})).body)}function createGitHubPullRequestReview(e){return callGitHubApi({api:e.api,body:e.body,credentials:e.credentials,installationId:e.installationId,method:`POST`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/pulls/${e.pullRequestNumber}/reviews`})}async function createGitHubPullRequestReviewComment(e){return toPostedComment((await callGitHubApi({api:e.api,body:e.body,credentials:e.credentials,installationId:e.installationId,method:`POST`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/pulls/${e.pullRequestNumber}/comments`})).body)}async function getGitHubPullRequest(e){return toPullRequestDetails((await callGitHubApi({api:e.api,credentials:e.credentials,installationId:e.installationId,method:`GET`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/pulls/${e.pullRequestNumber}`})).body)}async function listGitHubPullRequestFiles(e){let t=e.perPage===void 0?``:`?per_page=${Math.min(e.perPage,100)}`;return(await callGitHubApi({api:e.api,credentials:e.credentials,installationId:e.installationId,method:`GET`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/pulls/${e.pullRequestNumber}/files${t}`})).body.map(toPullRequestFile)}async function listGitHubCompareFiles(e){return((await callGitHubApi({api:e.api,credentials:e.credentials,installationId:e.installationId,method:`GET`,path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/compare/${e.baseSha}...${e.headSha}`})).body.files??[]).map(toPullRequestFile)}function createGitHubReaction(e){let t=e.subject===`issue_comment`?`issues/comments`:`pulls/comments`;return callGitHubApi({api:e.api,body:{content:e.content},credentials:e.credentials,installationId:e.installationId,method:`POST`,options:{headers:{accept:`application/vnd.github+json`}},path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}/${t}/${e.commentId}/reactions`})}async function getGitHubRepository(e){let t=await callGitHubApi({api:e.api,credentials:e.credentials,installationId:e.installationId,method:`GET`,options:{auth:e.auth??e.installationId!==void 0},path:`/repos/${encodePath(e.owner)}/${encodePath(e.repo)}`}),n=isObject(t.body)?t.body:{},r=typeof n.id==`number`?n.id:0;return{defaultBranch:typeof n.default_branch==`string`?n.default_branch:void 0,id:r}}function normalizeCommentBody(e){return{body:typeof e==`string`?e:e.body}}function toPostedComment(e){let t=isObject(e)?e:{};return{htmlUrl:typeof t.html_url==`string`?t.html_url:void 0,id:typeof t.id==`number`?t.id:0,raw:e,url:typeof t.url==`string`?t.url:void 0}}function toPullRequestFile(e){let t=isObject(e)?e:{};return{additions:typeof t.additions==`number`?t.additions:void 0,changes:typeof t.changes==`number`?t.changes:void 0,deletions:typeof t.deletions==`number`?t.deletions:void 0,filename:typeof t.filename==`string`?t.filename:``,patch:typeof t.patch==`string`?t.patch:void 0,status:typeof t.status==`string`?t.status:void 0}}function toPullRequestDetails(e){let t=isObject(e)?e:{},n=toPullRequestRefDetails(t.base);return{additions:typeof t.additions==`number`?t.additions:void 0,author:toPullRequestUser(t.user),base:n,body:typeof t.body==`string`&&t.body.length>0?t.body:void 0,changedFiles:typeof t.changed_files==`number`?t.changed_files:void 0,defaultBranch:readDefaultBranch(t.base),deletions:typeof t.deletions==`number`?t.deletions:void 0,draft:t.draft===!0,head:toPullRequestRefDetails(t.head),htmlUrl:typeof t.html_url==`string`?t.html_url:void 0,mergeable:typeof t.mergeable==`boolean`||t.mergeable===null?t.mergeable:void 0,number:typeof t.number==`number`?t.number:0,raw:e,state:typeof t.state==`string`?t.state:void 0,title:typeof t.title==`string`?t.title:``}}function toPullRequestRefDetails(e){let t=isObject(e)?e:{},n=isObject(t.repo)?t.repo:{};return{ref:typeof t.ref==`string`?t.ref:void 0,repoFullName:typeof n.full_name==`string`?n.full_name:void 0,sha:typeof t.sha==`string`?t.sha:void 0}}function toPullRequestUser(e){if(isObject(e)&&typeof e.login==`string`)return{id:typeof e.id==`number`?e.id:0,login:e.login,type:typeof e.type==`string`?e.type:`User`}}function readDefaultBranch(e){let t=isObject(e)?e:{},n=isObject(t.repo)?t.repo:{};return typeof n.default_branch==`string`?n.default_branch:void 0}async function parseResponseBody(e){let t=await e.text();if(!t)return null;try{return JSON.parse(t)}catch{return t}}function encodePath(e){return encodeURIComponent(e)}export{GitHubApiError,callGitHubApi,createGitHubIssueComment,createGitHubPullRequestReview,createGitHubPullRequestReviewComment,createGitHubReaction,createGitHubReviewCommentReply,getGitHubPullRequest,getGitHubRepository,listGitHubCompareFiles,listGitHubPullRequestFiles,updateGitHubIssueComment,updateGitHubPullRequestReviewComment};