Bitbucket拉取请求管理客户端

一个基于Python的Bitbucket Cloud API客户端,用于程序化管理拉取请求。

  • by PratheekRBhat
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 PratheekRBhat/bitbucket-mcp

使用说明

比特桶MCP客户端

一个基于Python的客户端与Bitbucket Cloud API交互,允许您以编程方式管理pull请求。

安装

pip install bitbucket-mcp

配置

您需要具有适当权限的Bitbucket API令牌(存储库:读/写,拉取请求:读/写入)。您可以在Atlassian帐户设置中创建一个。

将其设置为环境变量:

export BITBUCKET_API_TOKEN="your-api-token"

现在,工作区和存储库的slug可以从本地Git存储库中自动发现 origin 远程。不需要额外的配置。

用法

初始化

from bitbucket_client import BitbucketClient

# Initialize with your workspace and repository slug
# Token is read from BITBUCKET_API_TOKEN env var by default
client = BitbucketClient(
    workspace="your-workspace",
    repo_slug="your-repo-slug"
)

列出拉取请求

# List all open pull requests
open_prs = client.pull_requests.list(state="open")

for pr in open_prs:
    print(f"#{pr.id}: {pr.title} ({pr.author.display_name})")

获取拉取请求

pr = client.pull_requests.get(pull_request_id=123)
print(f"Title: {pr.title}")
print(f"Description: {pr.summary.raw}")

创建拉取请求

from bitbucket_client.models import CreatePullRequest, PullRequestSource, PullRequestDestination, PullRequestBranch

new_pr = client.pull_requests.create(
    CreatePullRequest(
        title="My New Feature",
        description="Implements the new feature X",
        source=PullRequestSource(branch=PullRequestBranch(name="feature/new-feature")),
        destination=PullRequestDestination(branch=PullRequestBranch(name="main")),
        close_source_branch=True
    )
)
print(f"Created PR #{new_pr.id}")

合并拉取请求

# Simple merge
client.pull_requests.merge_simple(pull_request_id=123, close_source_branch=True)

# Or with more options
from bitbucket_client.models import MergePullRequest

client.pull_requests.merge(
    pull_request_id=123,
    params=MergePullRequest(
        message="Merging feature X",
        merge_strategy="squash",
        close_source_branch=True
    )
)

拒绝拉取请求

client.pull_requests.decline(pull_request_id=123)

错误处理

from bitbucket_client import BaseClientError

try:
    client.pull_requests.get(99999)
except BaseClientError as e:
    print(f"API Error: {e}")