UNPKG

@candriajs/git-neko-kit

Version:

Github, Gitee, GitCode API封装库

5 lines 4.82 kB
const e=require(`./root-s6IKP4Xz.cjs`),t=require(`./client-Bmkb1ges.cjs`),n=e.__toESM(require(`radash`));var r=class extends t.GitHubClient{constructor(e){super(e),this.userToken=e.userToken,this.api_url=e.api_url,this.base_url=e.base_url}async get_commit_info(e){try{this.setRequestConfig({token:this.userToken});let r,i,a,o;if(`url`in e){a=e.url;let n=t.parse_git_url(a);r=n?.owner,i=n?.repo}else if(`owner`in e&&`repo`in e)r=e.owner,i=e.repo;else throw Error(t.MissingRepoIdentifierMsg);if(e.sha)o=e.sha;else{let e=await this.get_repo(),t=await e.get_repo_default_branch({owner:r,repo:i});o=t}let s=await this.get(`/repos/${r}/${i}/commits/${o}`);switch(s.statusCode){case 401:throw Error(t.RepoOrPermissionDeniedMsg);case 404:throw Error(t.CommitOrRepoNotFoundMsg);case 422:throw Error(t.CommitNotFoundMsg)}if(s.data){let[e,r]=await Promise.all([this.format?await t.format_date(s.data.commit.author.date):s.data.commit.author.date,this.format?await t.format_date(s.data.commit.committer.date):s.data.commit.committer.date]),i=s.data?.commit?.message,[a,...o]=i.split(` `),c={html_url:s.data.html_url,sha:s.data.sha,comments_url:s.data.comments_url,commit:{url:s.data.commit.url,author:{id:s.data.author.id,login:s.data.author.login,name:(0,n.isEmpty)(s.data.commit.author.name)?null:s.data.commit.author.name,email:(0,n.isEmpty)(s.data.commit.author.email)?null:s.data.commit.author.email,avatar_url:s.data.author.avatar_url,html_url:s.data.author.html_url,type:(0,n.capitalize)(String(s.data.author.type).toLowerCase()),date:e},committer:{id:s.data.committer.id,login:s.data.committer.login,name:(0,n.isEmpty)(s.data.commit.committer.name)?null:s.data.commit.committer.name,email:(0,n.isEmpty)(s.data.commit.committer.email)?null:s.data.commit.committer.email,avatar_url:s.data.committer.avatar_url,html_url:s.data.committer.html_url,type:(0,n.capitalize)(String(s.data.committer.type).toLowerCase()),date:r},message:s.data.commit.message,...this.format&&{title:a,body:o.length>0?o.join(` `):null},tree:{url:s.data.commit.tree.url,sha:s.data.commit.tree.sha}},parents:s.data.parents.map(e=>({sha:e.sha,url:e.url})),stats:{additions:s.data.stats.additions,deletions:s.data.stats.deletions,total:s.data.stats.total},files:s.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}))};s.data=c}return s}catch(e){throw Error(`[GitHub] 获取提交信息失败: ${e.message}`)}}async get_commit_list(e){try{this.setRequestConfig({token:this.userToken});let r,i,a;if(`url`in e){a=e.url;let n=t.parse_git_url(a);r=n?.owner,i=n?.repo}else if(`owner`in e&&`repo`in e)r=e.owner,i=e.repo;else throw Error(t.MissingRepoIdentifierMsg);let{...o}=e;if(e.sha)o.sha=e.sha;else{let e=await this.get_repo(),t=await e.get_repo_default_branch({owner:r,repo:i});t&&(o.sha=t)}let s={};o.sha&&(s.sha=o.sha),o.path&&(s.path=o.path),o.author&&(s.author=o.author),o.since&&(s.since=o.since),o.until&&(s.until=o.until),o.per_page&&(s.per_page=o.per_page.toString()),o.page&&(s.page=o.page.toString());let c=`/repos/${r}/${i}/commits`,l=await this.get(c,s);switch(l.statusCode){case 401:throw Error(t.RepoOrPermissionDeniedMsg);case 404:throw Error(t.CommitOrRepoNotFoundMsg)}if(l.data){let e=await Promise.all(l.data.map(async e=>{let[r,...i]=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:(0,n.isEmpty)(e.committer.name)?null:e.committer.name,email:(0,n.isEmpty)(e.committer.email)?null:e.committer.email,avatar_url:e.author.avatar_url,html_url:e.author.html_url,type:(0,n.capitalize)(String(e.author.type).toLowerCase()),date:this.format?await t.format_date(e.commit.author.date):e.commit.author.date},committer:{id:e.committer.id,login:e.committer.login,name:(0,n.isEmpty)(e.committer.name)?null:e.committer.name,email:(0,n.isEmpty)(e.committer.email)?null:e.committer.email,avatar_url:e.committer.avatar_url,html_url:e.committer.html_url,type:(0,n.capitalize)(String(e.committer.type).toLowerCase()),date:this.format?await t.format_date(e.commit.committer.date):e.commit.committer.date},message:e.commit.message,...this.format&&{title:r,body:i.length>0?i.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}))}}));l.data=e}return l}catch(e){throw Error(`[GitHub] 获取提交列表失败: ${e.message}`)}}};Object.defineProperty(exports,`Commit`,{enumerable:!0,get:function(){return r}});