dify可通过bitbucket api实现私有代码库自动合规扫描。需创建仅含“repositories: read”和“account: read”权限的app password,配置custom tool并填写openapi schema与api url,在function中编写python扫描逻辑(轻量版或对接内部合规引擎),最后构建含条件判断的审查工作流。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 多模态理解力帮你轻松跨越从0到1的创作门槛☜☜☜

在Dify中接入Bitbucket API,让AI自动扫描企业私有仓库代码,识别硬编码密钥、敏感路径、未授权API调用等合规风险项,无需手动导出代码或切换平台。
准备Bitbucket应用令牌与权限配置
登录Bitbucket Cloud控制台 → 左侧导航栏点击「Personal settings」→ 「App passwords」→ 点击「Create app password」。
勾选「Repositories: Read」和「Account: Read」两项权限,其他全部取消——【勾选过多权限会导致Dify后续调用被Bitbucket拒绝】。命名填入“dify-compliance-scan”,点击创建后立即复制生成的令牌字符串,该字符串仅显示一次,关闭页面即无法找回。
将令牌保存至安全位置,后续将作为HTTP请求头中的Bearer凭证使用。
在Dify中配置自定义工具(Custom Tool)
进入Dify工作区 → 点击左侧「Tools」→ 右上角「+ Add tool」→ 选择「Custom tool」→ 填写名称为“bitbucket_code_scanner”。
在「Schema」区域粘贴以下OpenAPI 3.0格式定义(注意替换YOUR_WORKSPACE和YOUR_REPO占位符):
{"type":"object","properties":{"repo_slug":{"type":"string","description":"目标仓库短名,如'payment-service'"},"commit_hash":{"type":"string","description":"指定提交哈希,留空则扫描最新main分支"}}}
在「API URL」填写:https://api.bitbucket.org/2.0/repositories/YOUR_WORKSPACE/{repo_slug}/src/{commit_hash}/,其中YOUR_WORKSPACE需替换为企业实际的Bitbucket工作区ID。
在「Authorization」选择「Bearer Token」,Token字段填入上一步复制的app password字符串。
编写Python函数实现代码片段提取与规则匹配
方法一:使用Dify内置Code Interpreter执行轻量扫描
在Custom Tool的「Function」字段中,粘贴如下Python代码:
def main(repo_slug: str, commit_hash: str = ""):
import requests
url = f"https://api.bitbucket.org/2.0/repositories/YOUR_WORKSPACE/{repo_slug}/src/{commit_hash or 'main'}/"
headers = {"Authorization": "Bearer YOUR_APP_PASSWORD"}
res = requests.get(url, headers=headers)
if res.status_code != 200:
return {"error": f"Failed to fetch repo: {res.status_code}"}
files = [f["path"] for f in res.json().get("values", []) if f["path"].endswith((".py", ".js", ".java"))]
results = []
for fp in files[:5]: # 限制首次扫描最多5个文件,避免超时
content_url = f"{url}{fp}"
content = requests.get(content_url, headers=headers).text
if "os.environ.get('API_KEY')" in content or "sk-live-" in content:
results.append({"file": fp, "risk": "hardcoded_api_key"})
return {"findings": results}
方法二:对接企业已有合规引擎(推荐生产环境使用)
将上述函数中requests.get部分替换为调用内部合规服务API:requests.post("https://compliance.internal/api/scan", json={"repo": repo_slug, "ref": commit_hash}, timeout=30)。此方式绕过Dify沙箱限制,支持正则、AST解析、上下文语义判断等深度检测能力。
构建合规审查工作流
第一步:新建一个「Application」→ 选择「Chatbot」类型 → 在「Orchestration」中拖入「Tool calling」节点。
第二步:将「bitbucket_code_scanner」工具拖入该节点,并设置参数映射:用户提问中的「仓库名」字段绑定到repo_slug,「分支或提交」绑定到commit_hash。
第三步:添加条件分支节点,判断返回结果中findings是否非空。若存在风险项,则触发「Send message」节点,用红字高亮输出:⚠ 发现硬编码密钥:{file} 第{line}行;否则输出绿色确认消息。
第四步:发布应用,将嵌入代码贴入企业内部IM群机器人配置页,即可通过@机器人发送“扫描 payment-service main分支”触发全自动审查。
大量免费API接口:立即使用
涵盖生活服务API、金融科技API、企业工商API、等相关的API接口服务。免费API接口可安全、合规地连接上下游,为数据API应用能力赋能!











