@candriajs/git-neko-kit
Version:
Github, Gitee, GitCode API封装库
5 lines • 4.49 kB
JavaScript
import{CommitNotFoundMsg as e,CommitOrRepoNotFoundMsg as t,GitHubClient as n,MissingRepoIdentifierMsg as r,RepoOrPermissionDeniedMsg as i,format_date as a,parse_git_url as o}from"./client-sGV7POG-.mjs";import{capitalize as s,isEmpty as c}from"radash";var l=class extends n{constructor(e){super(e),this.userToken=e.userToken,this.api_url=e.api_url,this.base_url=e.base_url}async get_commit_info(n){try{this.setRequestConfig({token:this.userToken});let l,u,d,f;if(`url`in n){d=n.url;let e=o(d);l=e?.owner,u=e?.repo}else if(`owner`in n&&`repo`in n)l=n.owner,u=n.repo;else throw Error(r);if(n.sha)f=n.sha;else{let e=await this.get_repo(),t=await e.get_repo_default_branch({owner:l,repo:u});f=t}let p=await this.get(`/repos/${l}/${u}/commits/${f}`);switch(p.statusCode){case 401:throw Error(i);case 404:throw Error(t);case 422:throw Error(e)}if(p.data){let[e,t]=await Promise.all([this.format?await a(p.data.commit.author.date):p.data.commit.author.date,this.format?await a(p.data.commit.committer.date):p.data.commit.committer.date]),n=p.data?.commit?.message,[r,...i]=n.split(`
`),o={html_url:p.data.html_url,sha:p.data.sha,comments_url:p.data.comments_url,commit:{url:p.data.commit.url,author:{id:p.data.author.id,login:p.data.author.login,name:c(p.data.commit.author.name)?null:p.data.commit.author.name,email:c(p.data.commit.author.email)?null:p.data.commit.author.email,avatar_url:p.data.author.avatar_url,html_url:p.data.author.html_url,type:s(String(p.data.author.type).toLowerCase()),date:e},committer:{id:p.data.committer.id,login:p.data.committer.login,name:c(p.data.commit.committer.name)?null:p.data.commit.committer.name,email:c(p.data.commit.committer.email)?null:p.data.commit.committer.email,avatar_url:p.data.committer.avatar_url,html_url:p.data.committer.html_url,type:s(String(p.data.committer.type).toLowerCase()),date:t},message:p.data.commit.message,...this.format&&{title:r,body:i.length>0?i.join(`
`):null},tree:{url:p.data.commit.tree.url,sha:p.data.commit.tree.sha}},parents:p.data.parents.map(e=>({sha:e.sha,url:e.url})),stats:{additions:p.data.stats.additions,deletions:p.data.stats.deletions,total:p.data.stats.total},files:p.data.files.map(e=>({sha:e.sha,filename:e.filename,status:e.status,additions:e.additions,deletions:e.deletions,changes:e.changes,blob_url:e.blob_url,raw_url:e.raw_url}))};p.data=o}return p}catch(e){throw Error(`[GitHub] 获取提交信息失败: ${e.message}`)}}async get_commit_list(e){try{this.setRequestConfig({token:this.userToken});let n,l,u;if(`url`in e){u=e.url;let t=o(u);n=t?.owner,l=t?.repo}else if(`owner`in e&&`repo`in e)n=e.owner,l=e.repo;else throw Error(r);let{...d}=e;if(e.sha)d.sha=e.sha;else{let e=await this.get_repo(),t=await e.get_repo_default_branch({owner:n,repo:l});t&&(d.sha=t)}let f={};d.sha&&(f.sha=d.sha),d.path&&(f.path=d.path),d.author&&(f.author=d.author),d.since&&(f.since=d.since),d.until&&(f.until=d.until),d.per_page&&(f.per_page=d.per_page.toString()),d.page&&(f.page=d.page.toString());let p=`/repos/${n}/${l}/commits`,m=await this.get(p,f);switch(m.statusCode){case 401:throw Error(i);case 404:throw Error(t)}if(m.data){let e=await Promise.all(m.data.map(async e=>{let[t,...n]=e.commit.message.split(`
`);return{html_url:e.html_url,sha:e.sha,comments_url:e.comments_url,commit:{url:e.commit.url,author:{id:e.author.id,login:e.author.login,name:c(e.committer.name)?null:e.committer.name,email:c(e.committer.email)?null:e.committer.email,avatar_url:e.author.avatar_url,html_url:e.author.html_url,type:s(String(e.author.type).toLowerCase()),date:this.format?await a(e.commit.author.date):e.commit.author.date},committer:{id:e.committer.id,login:e.committer.login,name:c(e.committer.name)?null:e.committer.name,email:c(e.committer.email)?null:e.committer.email,avatar_url:e.committer.avatar_url,html_url:e.committer.html_url,type:s(String(e.committer.type).toLowerCase()),date:this.format?await a(e.commit.committer.date):e.commit.committer.date},message:e.commit.message,...this.format&&{title:t,body:n.length>0?n.join(`
`):null},tree:{url:e.commit.tree.url,sha:e.commit.tree.sha}},parents:e.parents.map(e=>({sha:e.sha,url:e.url})),stats:{additions:e.stats.additions,deletions:e.stats.deletions,total:e.stats.total},files:e.files.map(e=>({sha:e.sha,filename:e.filename,status:e.status,additions:e.additions,deletions:e.deletions,changes:e.changes,blob_url:e.blob_url,raw_url:e.raw_url}))}}));m.data=e}return m}catch(e){throw Error(`[GitHub] 获取提交列表失败: ${e.message}`)}}};export{l as Commit};