UNPKG

eve

Version:

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

1 lines 6.8 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 r=e.api?.fetch??fetch,i=e.options?.auth!==!1,a={accept:`application/vnd.github+json`,"x-github-api-version":`2022-11-28`,...e.options?.headers};e.body!==void 0&&(a[`content-type`]=`application/json; charset=utf-8`),i&&(a.authorization=`Bearer ${await resolveGitHubInstallationToken({api:e.api,credentials:e.credentials,installationId:e.options?.installationId??e.installationId})}`);let o=await r(`${e.api?.apiBaseUrl??`https://api.github.com`}${e.path}`,{body:e.body===void 0?void 0:JSON.stringify(parseJsonObject(e.body)),headers:a,method:e.method}),s=await parseResponseBody(o);if(!o.ok)throw new GitHubApiError({body:s,method:e.method,path:e.path,status:o.status});return{body:s,ok:o.ok,status:o.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)}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(t){let n=await callGitHubApi({api:t.api,credentials:t.credentials,installationId:t.installationId,method:`GET`,options:{auth:t.auth??t.installationId!==void 0},path:`/repos/${encodePath(t.owner)}/${encodePath(t.repo)}`}),r=isObject(n.body)?n.body:{},i=typeof r.id==`number`?r.id:0;return{defaultBranch:typeof r.default_branch==`string`?r.default_branch:void 0,id:i}}function normalizeCommentBody(e){return{body:typeof e==`string`?e:e.body}}function toPostedComment(t){let n=isObject(t)?t:{};return{htmlUrl:typeof n.html_url==`string`?n.html_url:void 0,id:typeof n.id==`number`?n.id:0,raw:t,url:typeof n.url==`string`?n.url:void 0}}function toPullRequestFile(t){let n=isObject(t)?t:{};return{additions:typeof n.additions==`number`?n.additions:void 0,changes:typeof n.changes==`number`?n.changes:void 0,deletions:typeof n.deletions==`number`?n.deletions:void 0,filename:typeof n.filename==`string`?n.filename:``,patch:typeof n.patch==`string`?n.patch:void 0,status:typeof n.status==`string`?n.status:void 0}}function toPullRequestDetails(t){let n=isObject(t)?t:{},r=toPullRequestRefDetails(n.base);return{additions:typeof n.additions==`number`?n.additions:void 0,author:toPullRequestUser(n.user),base:r,body:typeof n.body==`string`&&n.body.length>0?n.body:void 0,changedFiles:typeof n.changed_files==`number`?n.changed_files:void 0,defaultBranch:readDefaultBranch(n.base),deletions:typeof n.deletions==`number`?n.deletions:void 0,draft:n.draft===!0,head:toPullRequestRefDetails(n.head),htmlUrl:typeof n.html_url==`string`?n.html_url:void 0,mergeable:typeof n.mergeable==`boolean`||n.mergeable===null?n.mergeable:void 0,number:typeof n.number==`number`?n.number:0,raw:t,state:typeof n.state==`string`?n.state:void 0,title:typeof n.title==`string`?n.title:``}}function toPullRequestRefDetails(t){let n=isObject(t)?t:{},r=isObject(n.repo)?n.repo:{};return{ref:typeof n.ref==`string`?n.ref:void 0,repoFullName:typeof r.full_name==`string`?r.full_name:void 0,sha:typeof n.sha==`string`?n.sha:void 0}}function toPullRequestUser(t){if(!(!isObject(t)||typeof t.login!=`string`))return{id:typeof t.id==`number`?t.id:0,login:t.login,type:typeof t.type==`string`?t.type:`User`}}function readDefaultBranch(t){let n=isObject(t)?t:{},r=isObject(n.repo)?n.repo:{};return typeof r.default_branch==`string`?r.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,listGitHubPullRequestFiles,updateGitHubIssueComment,updateGitHubPullRequestReviewComment};