如何批量下载Bitbucket指定条件文件并自动化处理提交PR
Bitbucket批量文件操作实现方案
1. 提取搜索结果的仓库与文件信息
Bitbucket网页搜索没有直接导出功能,可通过以下两种方式获取结果:
- 浏览器控制台提取:打开搜索结果页面(
ext:lhs "Configuration"),按F12打开控制台,执行以下脚本提取仓库URL和文件路径,结果会复制到剪贴板:
const results = Array.from(document.querySelectorAll('.search-result-item')).map(item => ({ repoUrl: item.querySelector('.repo-link').href, filePath: item.querySelector('.file-path').textContent.trim() })); copy(JSON.stringify(results)); // 把结果保存为results.json
- API调用(需权限):调用Bitbucket代码搜索API,请求地址为
GET /rest/api/1.0/search/code,携带参数ext=lhs和q="Configuration",解析返回JSON中的values字段获取仓库及文件信息。
2. 批量下载.lhs文件
用Shell脚本配合jq工具遍历结果,克隆仓库并归档目标文件:
# 确保已安装jq:sudo apt install jq(Linux)或brew install jq(Mac) cat results.json | jq -r '.[] | "\(.repoUrl) \(.filePath)"' | while read repoUrl filePath; do repoName=$(basename "$repoUrl" .git) # 克隆仓库(已克隆则跳过,改用git pull更新) if [ ! -d "$repoName" ]; then git clone "$repoUrl" --depth 1 # 浅克隆加快速度 fi # 创建归档目录并复制文件 mkdir -p downloaded_lhs cp "$repoName/$filePath" "downloaded_lhs/${repoName}_$(basename $filePath)" done
如果不想克隆整个仓库,可通过Bitbucket原始文件API直接下载:GET /rest/api/1.0/projects/{项目KEY}/repos/{仓库SLUG}/raw/{文件路径},需携带认证信息。
3. 批量处理.launch文件
编写Python脚本遍历克隆的仓库,查找并处理.launch文件:
import os # 遍历本地克隆的仓库目录 for repo_dir in os.listdir('.'): repo_path = os.path.abspath(repo_dir) # 跳过非仓库目录和归档目录 if not os.path.isdir(repo_path) or repo_dir == 'downloaded_lhs': continue # 递归查找所有.launch文件 for root, _, files in os.walk(repo_path): for file in files: if file.endswith('.launch'): launch_file = os.path.join(root, file) # 读取原文件内容,替换为你的处理逻辑 with open(launch_file, 'r', encoding='utf-8') as f: content = f.read() # 示例:添加自定义配置段 new_content = content + '\n<!-- Auto-generated Configuration Section -->' # 生成新文件(可覆盖原文件或创建新文件) new_file = os.path.join(root, 'processed_config.launch') with open(new_file, 'w', encoding='utf-8') as f: f.write(new_content)
4. 自动推送修改并创建PR
将Git操作和Bitbucket API调用整合到脚本中,完成自动化PR创建:
4.1 提交并推送修改
在Shell脚本中添加以下逻辑(承接步骤2的循环):
cd "$repoName" # 创建新分支 branch_name="feature/auto-process-launch-config" git checkout -b "$branch_name" # 添加修改的文件(这里假设处理后生成了processed_config.launch) git add processed_config.launch git commit -m "Auto-generate config from .launch file processing" git push origin "$branch_name" cd ..
4.2 调用API创建PR
需要生成Bitbucket个人访问令牌(PAT),权限勾选pull-request:write和repository:write。然后用curl调用API:
# 替换以下变量 PROJECT_KEY="你的项目KEY" REPO_SLUG="$repoName" PAT="你的个人访问令牌" BRANCH_NAME="feature/auto-process-launch-config" curl -X POST "https://bitbucket.org/rest/api/1.0/projects/$PROJECT_KEY/repos/$REPO_SLUG/pull-requests" \ -H "Authorization: Bearer $PAT" \ -H "Content-Type: application/json" \ -d '{ "title": "Auto-generated config from .launch processing", "description": "This PR is created automatically by batch processing script", "fromRef": { "id": "refs/heads/'"$BRANCH_NAME"'", "repository": { "slug": "'"$REPO_SLUG"'", "project": { "key": "'"$PROJECT_KEY"'" } } }, "toRef": { "id": "refs/heads/main", # 替换为目标分支,如master "repository": { "slug": "'"$REPO_SLUG"'", "project": { "key": "'"$PROJECT_KEY"'" } } } }'
可以将这部分逻辑整合到循环中,自动为每个仓库创建PR。
内容的提问来源于stack exchange,提问作者nick
相关产品推荐
相关产品推荐

