leejunyoung5819 gitgit-mcp
공개gitgit.kr MCP 도구
| 1 | import { z } from "zod"; |
| 2 | import type { GitGitClient } from "../client.ts"; |
| 3 | import { repositoryPath } from "../client.ts"; |
| 4 | import { before, cursor, defined, limit, number, oid, owner, repo, type Registrar } from "../tooling.ts"; |
| 5 | import { diffPaths, filterDiff } from "./code.ts"; |
| 6 | |
| 7 | type DiffFile = { path: string; change: string; status: string; patch: string | null }; |
| 8 | type PullDiff = { baseOid: string | null; headOid: string | null; mergeBaseOid: string | null; diff: { status: string; files: DiffFile[] } | null }; |
| 9 | |
| 10 | const title = z.string().min(1).max(200); |
| 11 | const body = z.string().max(8000); |
| 12 | const comment = z.string().min(1).max(8000).describe("Markdown 본문"); |
| 13 | const branch = z.string().min(1).max(1024); |
| 14 | const issueNumbers = z.array(z.number().int().positive()).max(20).describe("이 PR 과 연결할 Issue 번호"); |
| 15 | const side = z.enum(["old", "new"]).describe("old: 삭제/변경 전 줄, new: 추가/변경 후 줄"); |
| 16 | const pullPath = (o: string, r: string, n: number, suffix = "") => repositoryPath(o, r, `/pulls/${n}${suffix}`); |
| 17 | const beforeCursor = z.string().max(8192).optional().describe("이전 응답의 nextCursor"); |
| 18 | |
| 19 | const diffOf = (client: GitGitClient, o: string, r: string, n: number) => client.get<PullDiff>(pullPath(o, r, n, "/diff")); |
| 20 | |
| 21 | export function registerPullTools(register: Registrar) { |
| 22 | register("list_pulls", { |
| 23 | title: "PR 목록", |
| 24 | description: "저장소의 Pull Request 를 번호 역순으로 봅니다. 다음 쪽은 nextCursor 를 before 에 넣습니다.", |
| 25 | kind: "read", |
| 26 | input: { owner, repo, state: z.enum(["open", "closed", "merged", "all"]).default("open"), limit, before }, |
| 27 | run: ({ owner: o, repo: r, ...query }, { client }) => client.get(repositoryPath(o, r, "/pulls"), query), |
| 28 | }); |
| 29 | |
| 30 | register("get_pull", { |
| 31 | title: "PR 상세", |
| 32 | description: "PR 의 제목, 본문, 상태, 브랜치, 연결된 Issue 와 함께 현재 baseOid/headOid/mergeBaseOid 및 변경 파일 목록(Diff 본문 제외)을 봅니다. 리뷰나 병합 전에 부르세요. Diff 본문은 get_pull_diff 로 받습니다.", |
| 33 | kind: "read", |
| 34 | input: { owner, repo, number }, |
| 35 | run: async ({ owner: o, repo: r, number: n }, { client }) => { |
| 36 | const [detail, diff] = await Promise.all([client.get<{ pull: unknown }>(pullPath(o, r, n)), diffOf(client, o, r, n)]); |
| 37 | return { ...detail, baseOid: diff.baseOid, headOid: diff.headOid, mergeBaseOid: diff.mergeBaseOid, |
| 38 | diffStatus: diff.diff?.status ?? null, files: filterDiff(diff.diff, undefined, false)?.files ?? [] }; |
| 39 | }, |
| 40 | }); |
| 41 | |
| 42 | register("get_pull_diff", { |
| 43 | title: "PR Diff", |
| 44 | description: "PR 의 변경 파일별 Diff 를 봅니다. 큰 PR 은 paths 로 파일을 나눠 받으세요.", |
| 45 | kind: "read", |
| 46 | input: { owner, repo, number, paths: diffPaths }, |
| 47 | run: async ({ owner: o, repo: r, number: n, paths }, { client }) => { |
| 48 | const diff = await diffOf(client, o, r, n); |
| 49 | return { ...diff, diff: filterDiff(diff.diff, paths) }; |
| 50 | }, |
| 51 | }); |
| 52 | |
| 53 | register("list_pull_commits", { |
| 54 | title: "PR 커밋 목록", |
| 55 | description: "PR 작업 브랜치에서 기준 브랜치에 없는 커밋 목록을 봅니다.", |
| 56 | kind: "read", |
| 57 | input: { owner, repo, number, limit, cursor }, |
| 58 | run: ({ owner: o, repo: r, number: n, ...page }, { client }) => client.get(pullPath(o, r, n, "/commits"), page), |
| 59 | }); |
| 60 | |
| 61 | register("list_pull_comments", { |
| 62 | title: "PR 댓글 목록", |
| 63 | description: "PR 의 일반 댓글과 줄 댓글(anchor 포함)을 봅니다. 줄이 바뀐 줄 댓글은 이전 변경분 표시가 붙습니다.", |
| 64 | kind: "read", |
| 65 | input: { owner, repo, number, limit, before: beforeCursor }, |
| 66 | run: ({ owner: o, repo: r, number: n, ...page }, { client }) => client.get(pullPath(o, r, n, "/comments"), page), |
| 67 | }); |
| 68 | |
| 69 | register("list_pull_reviews", { |
| 70 | title: "PR 리뷰 목록", |
| 71 | description: "PR 에 남은 리뷰(승인, 변경 요청, 의견)와 각 리뷰가 본 커밋을 봅니다.", |
| 72 | kind: "read", |
| 73 | input: { owner, repo, number, limit, before: beforeCursor }, |
| 74 | run: ({ owner: o, repo: r, number: n, ...page }, { client }) => client.get(pullPath(o, r, n, "/reviews"), page), |
| 75 | }); |
| 76 | |
| 77 | register("create_pull", { |
| 78 | title: "PR 열기", |
| 79 | description: "같은 저장소의 headBranch 를 baseBranch 로 병합하자는 Pull Request 를 엽니다. pulls:write 범위가 필요합니다. 먼저 compare_branches 로 차이를 확인하면 좋습니다.", |
| 80 | kind: "write", |
| 81 | input: { |
| 82 | owner, repo, title, |
| 83 | body: body.optional().describe("Markdown 본문"), |
| 84 | baseBranch: branch.describe("병합 대상 브랜치 (예: main)"), |
| 85 | headBranch: branch.describe("변경이 있는 작업 브랜치"), |
| 86 | linkedIssueNumbers: issueNumbers.optional(), |
| 87 | }, |
| 88 | run: ({ owner: o, repo: r, ...fields }, { client }) => |
| 89 | client.request("POST", repositoryPath(o, r, "/pulls"), { body: defined(fields) }), |
| 90 | }); |
| 91 | |
| 92 | register("update_pull", { |
| 93 | title: "PR 수정, 닫기, 다시 열기", |
| 94 | description: "PR 의 제목, 본문, 상태(open/closed), 연결 Issue 를 바꿉니다. 준 필드만 바뀝니다. 병합은 merge_pull 을 쓰세요.", |
| 95 | kind: "write", |
| 96 | idempotent: true, |
| 97 | input: { |
| 98 | owner, repo, number, |
| 99 | title: title.optional(), body: body.optional(), |
| 100 | state: z.enum(["open", "closed"]).optional(), |
| 101 | linkedIssueNumbers: issueNumbers.optional(), |
| 102 | }, |
| 103 | run: ({ owner: o, repo: r, number: n, ...fields }, { client }) => |
| 104 | client.request("PATCH", pullPath(o, r, n), { body: defined(fields) }), |
| 105 | }); |
| 106 | |
| 107 | register("create_pull_comment", { |
| 108 | title: "PR 댓글 달기", |
| 109 | description: "PR 에 일반 댓글을 남기거나, path/line/side 를 주면 특정 줄에 줄 댓글을 남깁니다. commitOid 를 생략하면 현재 headOid 를 씁니다. replyToId 로 기존 댓글에 답글을 달 수 있습니다.", |
| 110 | kind: "write", |
| 111 | input: { |
| 112 | owner, repo, number, body: comment, |
| 113 | path: z.string().min(1).max(1024).optional().describe("줄 댓글을 달 파일 경로"), |
| 114 | line: z.number().int().positive().optional().describe("줄 번호 (side 기준)"), |
| 115 | side: side.optional().describe("기본 new"), |
| 116 | commitOid: oid.optional().describe("줄 댓글이 가리킬 커밋. 생략하면 현재 headOid"), |
| 117 | replyToId: z.uuid().optional().describe("답글을 달 댓글 id"), |
| 118 | }, |
| 119 | run: async ({ owner: o, repo: r, number: n, body: text, path, line, side: at, commitOid, replyToId }, { client }) => { |
| 120 | if ((path === undefined) !== (line === undefined)) throw new Error("줄 댓글에는 path 와 line 을 함께 주세요."); |
| 121 | let anchor: Record<string, unknown> | undefined; |
| 122 | if (path !== undefined && line !== undefined) { |
| 123 | const commit = commitOid ?? (await diffOf(client, o, r, n)).headOid; |
| 124 | if (!commit) throw new Error("PR 의 headOid 를 확인하지 못했습니다. commitOid 를 직접 주세요."); |
| 125 | anchor = { commitOid: commit, path, side: at ?? "new", line }; |
| 126 | } |
| 127 | return client.request("POST", pullPath(o, r, n, "/comments"), { body: defined({ body: text, anchor, replyToId }) }); |
| 128 | }, |
| 129 | }); |
| 130 | |
| 131 | register("submit_pull_review", { |
| 132 | title: "PR 리뷰 제출", |
| 133 | description: "PR 을 승인(approved), 변경 요청(changes_requested) 또는 의견(commented)으로 리뷰합니다. 줄 댓글을 최대 20개까지 함께 남길 수 있습니다. headOid 는 리뷰한 커밋이며, 생략하면 현재 headOid 를 씁니다. 리뷰 뒤 새 커밋이 올라오면 이전 승인은 승인 수에서 빠집니다.", |
| 134 | kind: "write", |
| 135 | input: { |
| 136 | owner, repo, number, |
| 137 | state: z.enum(["approved", "changes_requested", "commented"]), |
| 138 | body: body.optional().describe("리뷰 요약 (Markdown)"), |
| 139 | headOid: oid.optional().describe("리뷰한 커밋 OID. 생략하면 현재 headOid (리뷰 도중 새 커밋이 올라왔다면 직접 주는 편이 안전)"), |
| 140 | comments: z.array(z.object({ |
| 141 | body: comment, path: z.string().min(1).max(1024), line: z.number().int().positive(), side: side.optional(), |
| 142 | })).max(20).optional().describe("줄 댓글 목록"), |
| 143 | }, |
| 144 | run: async ({ owner: o, repo: r, number: n, state, body: text, headOid, comments }, { client }) => { |
| 145 | const commit = headOid ?? (await diffOf(client, o, r, n)).headOid; |
| 146 | if (!commit) throw new Error("PR 의 headOid 를 확인하지 못했습니다."); |
| 147 | return client.request("POST", pullPath(o, r, n, "/reviews"), { body: defined({ |
| 148 | state, headOid: commit, body: text, |
| 149 | comments: comments?.map((item) => ({ |
| 150 | body: item.body, anchor: { commitOid: commit, path: item.path, side: item.side ?? "new", line: item.line }, |
| 151 | })), |
| 152 | }) }); |
| 153 | }, |
| 154 | }); |
| 155 | |
| 156 | register("merge_pull", { |
| 157 | title: "PR 병합", |
| 158 | description: "PR 을 Merge Commit(merge) 또는 Squash(squash)로 병합합니다. 확인한 변경분만 병합되도록 get_pull 로 받은 baseOid/headOid 를 expectedBaseOid/expectedHeadOid 에 그대로 넣어야 하며, 그 사이 브랜치가 바뀌면 서버가 거부합니다. 보호 브랜치 규칙(승인 수, 병합 역할)과 pulls:merge 범위가 필요합니다.", |
| 159 | kind: "destructive", |
| 160 | input: { |
| 161 | owner, repo, number, |
| 162 | method: z.enum(["merge", "squash"]).default("merge"), |
| 163 | expectedBaseOid: oid.describe("get_pull 의 baseOid"), |
| 164 | expectedHeadOid: oid.describe("get_pull 의 headOid"), |
| 165 | deleteHeadBranch: z.boolean().optional().describe("병합 후 보호되지 않은 작업 브랜치 삭제"), |
| 166 | commitTitle: title.optional(), |
| 167 | commitBody: body.optional(), |
| 168 | }, |
| 169 | run: ({ owner: o, repo: r, number: n, ...fields }, { client }) => |
| 170 | client.request("POST", pullPath(o, r, n, "/merge"), { body: defined(fields) }), |
| 171 | }); |
| 172 | } |