|
| 1 | +import { z } from "zod"; |
| 2 | +import { giteeRequest, validateOwnerName, validateRepositoryName, validateBranchName } from "../common/utils.js"; |
| 3 | +import { GiteePullRequestSchema } from "../common/types.js"; |
| 4 | + |
| 5 | +// Schema 定义 |
| 6 | +export const CreatePullRequestSchema = z.object({ |
| 7 | + owner: z.string().describe("仓库所属空间地址 (企业、组织或个人的地址 path)"), |
| 8 | + repo: z.string().describe("仓库路径 (path)"), |
| 9 | + title: z.string().describe("Pull Request 标题"), |
| 10 | + head: z.string().describe("源分支的名称"), |
| 11 | + base: z.string().describe("目标分支的名称"), |
| 12 | + body: z.string().optional().describe("Pull Request 内容"), |
| 13 | + milestone_number: z.number().optional().describe("里程碑序号"), |
| 14 | + labels: z.array(z.string()).optional().describe("标签"), |
| 15 | + issue: z.string().optional().describe("相关的 Issue,格式为 #xxx"), |
| 16 | + assignees: z.array(z.string()).optional().describe("审查人员"), |
| 17 | + testers: z.array(z.string()).optional().describe("测试人员"), |
| 18 | + prune_source_branch: z.boolean().optional().describe("合并后是否删除源分支"), |
| 19 | +}); |
| 20 | + |
| 21 | +export const ListPullRequestsSchema = z.object({ |
| 22 | + owner: z.string().describe("仓库所属空间地址 (企业、组织或个人的地址 path)"), |
| 23 | + repo: z.string().describe("仓库路径 (path)"), |
| 24 | + state: z.enum(["open", "closed", "merged", "all"]).default("open").optional().describe("Pull Request 状态"), |
| 25 | + sort: z.enum(["created", "updated", "popularity", "long-running"]).default("created").optional().describe("排序字段"), |
| 26 | + direction: z.enum(["asc", "desc"]).default("desc").optional().describe("排序方向"), |
| 27 | + milestone: z.number().optional().describe("里程碑 ID"), |
| 28 | + labels: z.string().optional().describe("标签,多个标签以逗号分隔"), |
| 29 | + page: z.number().int().default(1).optional().describe("当前的页码"), |
| 30 | + per_page: z.number().int().min(1).max(100).optional().describe("每页的数量,最大为 100"), |
| 31 | +}); |
| 32 | + |
| 33 | +export const GetPullRequestSchema = z.object({ |
| 34 | + owner: z.string().describe("仓库所属空间地址 (企业、组织或个人的地址 path)"), |
| 35 | + repo: z.string().describe("仓库路径 (path)"), |
| 36 | + pull_number: z.number().describe("Pull Request 编号"), |
| 37 | +}); |
| 38 | + |
| 39 | +export const UpdatePullRequestSchema = z.object({ |
| 40 | + owner: z.string().describe("仓库所属空间地址 (企业、组织或个人的地址 path)"), |
| 41 | + repo: z.string().describe("仓库路径 (path)"), |
| 42 | + pull_number: z.number().describe("Pull Request 编号"), |
| 43 | + title: z.string().optional().describe("Pull Request 标题"), |
| 44 | + body: z.string().optional().describe("Pull Request 内容"), |
| 45 | + state: z.enum(["open", "closed"]).optional().describe("Pull Request 状态"), |
| 46 | + milestone_number: z.number().optional().describe("里程碑序号"), |
| 47 | + labels: z.array(z.string()).optional().describe("标签"), |
| 48 | + assignees: z.array(z.string()).optional().describe("审查人员"), |
| 49 | + testers: z.array(z.string()).optional().describe("测试人员"), |
| 50 | +}); |
| 51 | + |
| 52 | +export const MergePullRequestSchema = z.object({ |
| 53 | + owner: z.string().describe("仓库所属空间地址 (企业、组织或个人的地址 path)"), |
| 54 | + repo: z.string().describe("仓库路径 (path)"), |
| 55 | + pull_number: z.number().describe("Pull Request 编号"), |
| 56 | + merge_method: z.enum(["merge", "squash", "rebase"]).default("merge").optional().describe("合并方式"), |
| 57 | + prune_source_branch: z.boolean().optional().describe("合并后是否删除源分支"), |
| 58 | +}); |
| 59 | + |
| 60 | +// 类型导出 |
| 61 | +export type CreatePullRequestOptions = z.infer<typeof CreatePullRequestSchema>; |
| 62 | +export type ListPullRequestsOptions = z.infer<typeof ListPullRequestsSchema>; |
| 63 | +export type GetPullRequestOptions = z.infer<typeof GetPullRequestSchema>; |
| 64 | +export type UpdatePullRequestOptions = z.infer<typeof UpdatePullRequestSchema>; |
| 65 | +export type MergePullRequestOptions = z.infer<typeof MergePullRequestSchema>; |
| 66 | + |
| 67 | +// 函数实现 |
| 68 | +export async function createPullRequest(options: CreatePullRequestOptions) { |
| 69 | + const { owner, repo, ...rest } = options; |
| 70 | + const validatedOwner = validateOwnerName(owner); |
| 71 | + const validatedRepo = validateRepositoryName(repo); |
| 72 | + const validatedHead = validateBranchName(rest.head); |
| 73 | + const validatedBase = validateBranchName(rest.base); |
| 74 | + |
| 75 | + const url = `https://gitee.com/api/v5/repos/${validatedOwner}/${validatedRepo}/pulls`; |
| 76 | + const body = { |
| 77 | + ...rest, |
| 78 | + head: validatedHead, |
| 79 | + base: validatedBase, |
| 80 | + }; |
| 81 | + |
| 82 | + const response = await giteeRequest(url, "POST", body); |
| 83 | + |
| 84 | + return GiteePullRequestSchema.parse(response); |
| 85 | +} |
| 86 | + |
| 87 | +export async function listPullRequests( |
| 88 | + owner: string, |
| 89 | + repo: string, |
| 90 | + options: Omit<ListPullRequestsOptions, "owner" | "repo"> |
| 91 | +) { |
| 92 | + owner = validateOwnerName(owner); |
| 93 | + repo = validateRepositoryName(repo); |
| 94 | + |
| 95 | + const url = new URL(`https://gitee.com/api/v5/repos/${owner}/${repo}/pulls`); |
| 96 | + |
| 97 | + // 添加查询参数 |
| 98 | + Object.entries(options).forEach(([key, value]) => { |
| 99 | + if (value !== undefined) { |
| 100 | + url.searchParams.append(key, value.toString()); |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + const response = await giteeRequest(url.toString(), "GET"); |
| 105 | + |
| 106 | + return z.array(GiteePullRequestSchema).parse(response); |
| 107 | +} |
| 108 | + |
| 109 | +export async function getPullRequest( |
| 110 | + owner: string, |
| 111 | + repo: string, |
| 112 | + pullNumber: number |
| 113 | +) { |
| 114 | + owner = validateOwnerName(owner); |
| 115 | + repo = validateRepositoryName(repo); |
| 116 | + |
| 117 | + const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${pullNumber}`; |
| 118 | + const response = await giteeRequest(url, "GET"); |
| 119 | + |
| 120 | + return GiteePullRequestSchema.parse(response); |
| 121 | +} |
| 122 | + |
| 123 | +export async function updatePullRequest( |
| 124 | + owner: string, |
| 125 | + repo: string, |
| 126 | + pullNumber: number, |
| 127 | + options: Omit<UpdatePullRequestOptions, "owner" | "repo" | "pull_number"> |
| 128 | +) { |
| 129 | + owner = validateOwnerName(owner); |
| 130 | + repo = validateRepositoryName(repo); |
| 131 | + |
| 132 | + const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${pullNumber}`; |
| 133 | + const response = await giteeRequest(url, "PATCH", options); |
| 134 | + |
| 135 | + return GiteePullRequestSchema.parse(response); |
| 136 | +} |
| 137 | + |
| 138 | +export async function mergePullRequest( |
| 139 | + owner: string, |
| 140 | + repo: string, |
| 141 | + pullNumber: number, |
| 142 | + options: Omit<MergePullRequestOptions, "owner" | "repo" | "pull_number"> |
| 143 | +) { |
| 144 | + owner = validateOwnerName(owner); |
| 145 | + repo = validateRepositoryName(repo); |
| 146 | + |
| 147 | + const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${pullNumber}/merge`; |
| 148 | + const response = await giteeRequest(url, "PUT", options); |
| 149 | + |
| 150 | + return response; |
| 151 | +} |
0 commit comments