UNPKG

cnb-mcp-server

Version:

MCP Server for the cnb API, enabling file operations, repository management, search functionality, and more.

70 lines (69 loc) 2.6 kB
/** * 分支操作 */ import { z } from 'zod'; import { makeRequest, getQueryParams } from '../common/utils.js'; import { BranchSchema } from '../common/types.js'; // 创建分支参数Schema export const CreateBranchSchema = z.object({ owner: z.string().describe('仓库拥有者'), repo: z.string().describe('仓库名称'), branch: z.string().describe('新分支名称'), from_branch: z.string().describe('源分支名称') }); /** * 创建分支 */ export async function createBranchFromRef(owner, repo, branch, fromBranch, accessToken) { // 获取源分支的SHA const branchData = await makeRequest(`/repos/${owner}/${repo}/branches/${fromBranch}`, {}, accessToken); const sha = branchData.commit.sha; // 创建新分支引用 const response = await makeRequest(`/repos/${owner}/${repo}/git/refs`, { method: 'POST', body: JSON.stringify({ ref: `refs/heads/${branch}`, sha }) }, accessToken); // 获取新分支信息 return await getBranch(owner, repo, branch, accessToken); } /** * 获取分支信息 */ export async function getBranch(owner, repo, branch, accessToken) { const response = await makeRequest(`/repos/${owner}/${repo}/branches/${branch}`, {}, accessToken); return BranchSchema.parse(response); } /** * 列出仓库的所有分支 */ export async function listBranches(owner, repo, page = 1, perPage = 30, accessToken) { const params = getQueryParams({ page, per_page: perPage }); const response = await makeRequest(`/repos/${owner}/${repo}/branches${params}`, {}, accessToken); return response.map((branch) => BranchSchema.parse(branch)); } /** * 设置分支保护 */ export async function protectBranch(owner, repo, branch, requiredStatusChecks = false, enforceAdmins = false, requiredPullRequestReviews = false, accessToken) { const response = await makeRequest(`/repos/${owner}/${repo}/branches/${branch}/protection`, { method: 'PUT', body: JSON.stringify({ required_status_checks: requiredStatusChecks ? { strict: true, contexts: [] } : null, enforce_admins: enforceAdmins, required_pull_request_reviews: requiredPullRequestReviews ? { dismissal_restrictions: {} } : null, restrictions: null }) }, accessToken); return response; } /** * 移除分支保护 */ export async function removeProtection(owner, repo, branch, accessToken) { await makeRequest(`/repos/${owner}/${repo}/branches/${branch}/protection`, { method: 'DELETE' }, accessToken); }