gitlab-remote-search
SKILL.md
GitLab Remote Code Search Skill
通过 GitLab API 远程搜索代码仓库,无需克隆到本地。支持搜索关键字、文件类型过滤、多仓库批量搜索。
使用场景
- 搜索某个接口在哪些服务中有调用
- 查找某个模板/URL 是否在代码中使用
- 快速定位代码位置,无需下载整个仓库
前置条件
1. GitLab Token
需要 GitLab Personal Access Token,权限要求:
read_api- 读取 APIread_repository- 读取仓库
获取方式:
- 访问 GitLab → Settings → Access Tokens
- 创建新 token,勾选上述权限
- 保存 token(只显示一次)
2. 配置 Token
在 TOOLS.md 中添加:
### GitLab
- Token: `glpat-xxxxxxxxxxxx`
- Base URL: `https://eaglelab-gitlab.tcljd.com`
或在执行时通过环境变量传入:
export GITLAB_TOKEN="glpat-xxxxxxxxxxxx"
使用方法
基本搜索
cd ~/.openclaw/workspace/skills/gitlab-remote-search
python gitlab-search.py --search "keyword" --repos "repo1,repo2,repo3"
完整参数
python gitlab-search.py \
--search "statis-operation-calc" \
--repos "iot3.1/ac-common-service,iot3.1/iot-device-service" \
--extension ".java" \
--ref "release" \
--output "results.txt"
参数说明
| 参数 | 必填 | 说明 |
|---|---|---|
--search |
是 | 搜索关键字 |
--repos |
是 | 仓库列表(逗号分隔,格式:group/project) |
--extension |
否 | 文件扩展名过滤(如 .java, .py, .js) |
--ref |
否 | 分支/标签名,默认 release |
--output |
否 | 输出文件路径,默认打印到终端 |
--token |
否 | GitLab Token(也可用环境变量 GITLAB_TOKEN) |
--base-url |
否 | GitLab 基础 URL,默认 https://eaglelab-gitlab.tcljd.com |
输出格式
========================================
Repository: cloudplatform/business/official/iot3.1/ac-common-service
File: src/main/java/com/tcl/iot/service/ExampleService.java
Line: 45
URL: https://eaglelab-gitlab.tcljd.com/cloudplatform/business/official/iot3.1/ac-common-service/-/blob/release/src/main/java/...#L45
----------------------------------------
[代码片段预览]
========================================
示例
搜索 Java 代码中的关键字
python gitlab-search.py \
--search "getUserInfo" \
--repos "iot3.1/ac-common-service,iot3.1/iot-user-service" \
--extension ".java" \
--ref "release"
搜索所有文件类型
python gitlab-search.py \
--search "TODO" \
--repos "iot3.1/*" \
--ref "main"
导出结果到文件
python gitlab-search.py \
--search "statis-operation-calc" \
--repos "iot3.1/*" \
--output "search-results.txt"
注意事项
- API 速率限制:GitLab API 有速率限制,大量搜索时注意间隔
- Token 安全:不要将 token 提交到代码仓库
- 仓库路径格式:使用
group/subgroup/project格式,不需要.git后缀 - 通配符支持:可以用
*匹配多个仓库(需要额外处理)
故障排查
401 Unauthorized
- Token 无效或过期
- Token 权限不足
404 Not Found
- 仓库路径错误
- Token 没有该仓库的访问权限
搜索结果为空
- 关键字拼写错误
- 分支名称错误
- 文件类型过滤太严格
文件结构
gitlab-remote-search/
├── SKILL.md # 技能说明文档
├── gitlab-search.py # 主搜索脚本
├── repos.txt # 仓库列表文件(可选)
└── requirements.txt # Python 依赖