Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 0b33dae

Browse files
committed
refactor(github): 优化提交信息获取功能
- 添加参数校验和错误处理,提高代码健壮性 - 新增格式化提交信息的功能,优化数据结构 - 引入通用错误消息,提升用户体验
1 parent 1323b13 commit 0b33dae

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/models/github/commit.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
import { parse_git_url } from '@/common'
1+
import {
2+
NotCommitOrRepoMsg,
3+
NotParamMsg,
4+
NotPerrmissionMsg,
5+
parse_git_url
6+
} from '@/common'
27
import { GitHub } from '@/models/github/github'
38
import { Repo } from '@/models/github/repo'
4-
import { ApiResponseType, CommitInfoParamType, CommitInfoResponseType } from '@/types'
9+
import {
10+
ApiResponseType,
11+
CommitInfoParamType,
12+
CommitInfoResponseType
13+
} from '@/types'
514

615
/**
716
* GitHub 提交操作类
@@ -30,6 +39,15 @@ export class Commit {
3039
this.repo = new Repo(options)
3140
}
3241

42+
/**
43+
* 获取一个提交信息
44+
* @param options - 提交信息参数
45+
* @param options.owner - 仓库的拥有者
46+
* @param options.repo - 仓库的名称
47+
* @param options.url - 仓库的URL (与owner/repo二选一)
48+
* @param options.sha - 提交的SHA值,默认为仓库的默认分支 @default 仓库的默认分支
49+
* @returns 提交信息
50+
*/
3351
public async get_commit_info (options: CommitInfoParamType):
3452
Promise<ApiResponseType<CommitInfoResponseType>> {
3553
try {
@@ -43,7 +61,7 @@ export class Commit {
4361
owner = options.owner
4462
repo = options.repo
4563
} else {
46-
throw new Error('参数错误')
64+
throw new Error(NotParamMsg)
4765
}
4866
if (!options.sha) {
4967
const req = await this.repo.get_repo_info({ owner, repo })
@@ -52,6 +70,23 @@ export class Commit {
5270
sha = options.sha
5371
}
5472
const req = await this.get(`/repos/${owner}/${repo}/commits/${sha}`)
73+
if (req.statusCode === 404) {
74+
throw new Error(NotCommitOrRepoMsg)
75+
} else if (req.statusCode === 401) {
76+
throw new Error(NotPerrmissionMsg)
77+
}
78+
if (options.format) {
79+
const message = req.data?.commit?.message ?? ''
80+
const [title, ...bodyParts] = message.split('\n')
81+
req.data = {
82+
...req.data,
83+
commit: {
84+
...req.data.commit,
85+
title: title.trim(),
86+
body: bodyParts.join('\n').trim()
87+
}
88+
}
89+
}
5590
return req
5691
} catch (error) {
5792
throw new Error(`获取提交信息失败: ${(error as Error).message}`)

0 commit comments

Comments
 (0)