跳转至

Bitbucket

概览

OpenSRE 会查询 Bitbucket,以获取最近的提交、文件内容和代码搜索结果——帮助追溯 Bitbucket 托管仓库中的哪次变更触发了事故。所有操作均为只读。

前置条件

  • Bitbucket Cloud 账户
  • 具有仓库读取权限的 app password
  • Workspace slug(即 https://bitbucket.org/<workspace>/ 中的标识符)

配置

Bitbucket 通过环境变量或持久化集成存储进行配置。目前没有专门的 opensre integrations setup bitbucket 向导。

选项 1:环境变量

添加到你的 .env:

BITBUCKET_WORKSPACE=your-workspace-slug
BITBUCKET_USERNAME=your-username
BITBUCKET_APP_PASSWORD=your-app-password
# 可选:
# BITBUCKET_BASE_URL=https://api.bitbucket.org/2.0
变量 默认值 说明
BITBUCKET_WORKSPACE — 必填。 Bitbucket workspace slug
BITBUCKET_USERNAME — 必填。 Bitbucket 用户名
BITBUCKET_APP_PASSWORD — 必填。 Bitbucket app password
BITBUCKET_BASE_URL https://api.bitbucket.org/2.0 Bitbucket API 基础 URL

选项 2:持久化存储

{
  "version": 1,
  "integrations": [
    {
      "id": "bitbucket-prod",
      "service": "bitbucket",
      "status": "active",
      "credentials": {
        "workspace": "your-workspace-slug",
        "username": "your-username",
        "app_password": "your-app-password"
      }
    }
  ]
}

凭据

创建 app password

  1. 在 Bitbucket 中,进入 Personal settings → App passwords
  2. 点击 Create app password
  3. 为其设置一个标签(例如 opensre)
  4. 启用以下权限:Repositories: Read
  5. 复制生成的密码

Info

workspace slug 是你的 Bitbucket URL 中的标识符:https://bitbucket.org/<workspace>/

认证使用 HTTP Basic,配合用户名 + app password。

工具

工具 功能
list_bitbucket_commits 仓库的最近提交,可按文件路径筛选
get_bitbucket_file_contents 特定修订版本的文件内容
search_bitbucket_code 在整个 workspace 或特定仓库中搜索代码

验证

opensre integrations verify bitbucket

预期输出:

Service: bitbucket
Status: passed
Detail: Authenticated as Your Name; workspace: your-workspace

故障排查

症状 解决方法
401 Unauthorized 检查用户名与 app password 的组合
Workspace not found 核实 workspace slug——它区分大小写
403 Forbidden 确保 app password 具有 Repositories: Read 权限
Code search unavailable 代码搜索需要 Bitbucket Cloud Standard 或 Premium 套餐

安全

  • 使用 app password 而非你的账户密码——app password 可单独吊销。
  • 权限范围仅限 Repositories: Read。
  • 将凭据存放在 .env 中,而不是源代码中。

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 op/opensre