cli / mcp for bitbucket
1import { declinePullRequest, type Pullrequest, updatePullRequest } from '@bitbucket-tool/core';
2import { defineCommand, unwrapOrExit } from '../utils/command-builder';
3
4export const registerPrUpdateCommand = defineCommand({
5 name: 'pr:update',
6 description: 'Update or close a pull request',
7 arguments: [{ syntax: '<pr-id>', description: 'Pull request ID', parser: parseInt }],
8 options: [
9 {
10 flag: '-t, --title <title>',
11 description: 'Update PR title',
12 },
13 {
14 flag: '-d, --description <description>',
15 description: 'Update PR description',
16 },
17 {
18 flag: '--destination <branch>',
19 description: 'Update destination branch',
20 },
21 {
22 flag: '--close',
23 description: 'Close/decline the pull request',
24 },
25 ],
26 action: async ({ workspace, repoSlug, args, options }) => {
27 const [prId] = args;
28
29 if (options.close) {
30 unwrapOrExit(await declinePullRequest({ workspace, repoSlug, prId }));
31 console.log(`\nPR #${prId} declined\n`);
32 return;
33 }
34
35 const updates: Partial<Pick<Pullrequest, 'title' | 'description' | 'destination'>> = {
36 ...(options.title !== undefined && { title: options.title }),
37 ...(options.description !== undefined && { description: options.description }),
38 ...(options.destination !== undefined && {
39 destination: { branch: { name: options.destination } },
40 }),
41 };
42
43 if (Object.keys(updates).length === 0) {
44 console.error(
45 'Error: No updates specified. Use --title, --description, --destination, or --close'
46 );
47 process.exit(1);
48 }
49
50 const pr = unwrapOrExit(await updatePullRequest({ workspace, repoSlug, prId, updates }));
51
52 console.log(`\nPR #${prId} updated successfully`);
53 console.log(`Title: ${pr.title}`);
54 console.log(`Branch: ${pr.source?.branch?.name} → ${pr.destination?.branch?.name}`);
55 console.log(`URL: ${pr.links?.html?.href}\n`);
56 },
57});