cli / mcp for bitbucket
at main 48 lines 1.4 kB view raw
1import { execSync } from 'node:child_process'; 2 3export const detectFromGitRemote = (): { workspace: string; repoSlug: string } | null => { 4 try { 5 const remoteUrl = execSync('git remote get-url origin', { 6 encoding: 'utf-8', 7 stdio: ['pipe', 'pipe', 'ignore'], 8 }).trim(); 9 10 const httpsMatch = remoteUrl.match(/https:\/\/[^@]*@?bitbucket\.org\/([^/]+)\/([^/.]+)/); 11 if (httpsMatch) { 12 return { workspace: httpsMatch[1], repoSlug: httpsMatch[2] }; 13 } 14 15 const sshMatch = remoteUrl.match(/git@bitbucket\.org:([^/]+)\/([^/.]+)/); 16 if (sshMatch) { 17 return { workspace: sshMatch[1], repoSlug: sshMatch[2] }; 18 } 19 20 return null; 21 } catch { 22 return null; 23 } 24}; 25 26export const resolveWorkspaceAndRepo = (overrides?: { 27 workspace?: string; 28 repoSlug?: string; 29}): { workspace: string; repoSlug: string } => { 30 const workspace = overrides?.workspace ?? process.env.BITBUCKET_WORKSPACE; 31 const repoSlug = overrides?.repoSlug ?? process.env.BITBUCKET_REPO; 32 33 if (workspace && repoSlug) { 34 return { workspace, repoSlug }; 35 } 36 37 const detected = detectFromGitRemote(); 38 if (detected) { 39 return { 40 workspace: workspace ?? detected.workspace, 41 repoSlug: repoSlug ?? detected.repoSlug, 42 }; 43 } 44 45 throw new Error( 46 'workspace and repoSlug are required. Set BITBUCKET_WORKSPACE/BITBUCKET_REPO env vars or run from a git repo with a Bitbucket remote.' 47 ); 48};