cli / mcp for bitbucket
at main 75 lines 2.4 kB view raw
1#!/usr/bin/env tsx 2/** 3 * Post-download patch for the Bitbucket OpenAPI spec. 4 * 5 * The official spec is missing pagination query params on many list endpoints 6 * and has incorrect body types on some POST endpoints. This script patches 7 * those deficiencies so the generated client has correct types. 8 */ 9import { readFileSync, writeFileSync } from 'node:fs'; 10 11const SPEC_PATH = process.argv[2]; 12if (!SPEC_PATH) { 13 console.error('Usage: tsx scripts/patch-spec.ts <spec-path>'); 14 process.exit(1); 15} 16 17// biome-ignore lint/suspicious/noExplicitAny: JSON manipulation 18const spec = JSON.parse(readFileSync(SPEC_PATH, 'utf-8')) as any; 19 20const paginationParams = [ 21 { name: 'page', in: 'query', required: false, schema: { type: 'integer' }, description: 'Page number (1-based)' }, 22 { name: 'pagelen', in: 'query', required: false, schema: { type: 'integer', maximum: 100 }, description: 'Items per page' }, 23]; 24 25const endpointsNeedingPagination = [ 26 '/repositories/{workspace}', 27 '/repositories/{workspace}/{repo_slug}/pullrequests', 28 '/repositories/{workspace}/{repo_slug}/pullrequests/{pull_request_id}/comments', 29 '/repositories/{workspace}/{repo_slug}/refs/branches', 30 '/repositories/{workspace}/{repo_slug}/commits', 31]; 32 33endpointsNeedingPagination.forEach((path) => { 34 const endpoint = spec.paths?.[path]?.get; 35 if (!endpoint) { 36 return; 37 } 38 39 endpoint.parameters = endpoint.parameters ?? []; 40 const existing = new Set(endpoint.parameters.map((p: { name: string }) => p.name)); 41 paginationParams.forEach((param) => { 42 if (!existing.has(param.name)) { 43 endpoint.parameters.push(param); 44 } 45 }); 46}); 47 48const branchCreatePath = '/repositories/{workspace}/{repo_slug}/refs/branches'; 49const branchPost = spec.paths?.[branchCreatePath]?.post; 50if (branchPost) { 51 branchPost.requestBody = { 52 required: true, 53 content: { 54 'application/json': { 55 schema: { 56 type: 'object', 57 properties: { 58 name: { type: 'string', description: 'Branch name' }, 59 target: { 60 type: 'object', 61 properties: { 62 hash: { type: 'string', description: 'Commit hash to create the branch from' }, 63 }, 64 required: ['hash'], 65 }, 66 }, 67 required: ['name', 'target'], 68 }, 69 }, 70 }, 71 }; 72} 73 74writeFileSync(SPEC_PATH, JSON.stringify(spec, null, 2)); 75console.log(`Patched spec at ${SPEC_PATH}`);