gitlab
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitLab API
GitLab API
Use the GitLab REST API via direct calls to manage projects, issues, merge requests, and pipelines.
curlOfficial docs:https://docs.gitlab.com/ee/api/
通过直接调用来使用GitLab REST API,以管理项目、议题、合并请求和流水线。
curl官方文档:https://docs.gitlab.com/ee/api/
When to Use
适用场景
Use this skill when you need to:
- Manage projects - list, create, get project details
- Handle issues - create, update, close, list issues
- Work with merge requests - create, list, merge MRs
- Check pipelines - list jobs, view pipeline status
- Manage users - search users, get user info
当你需要以下操作时,可以使用本技能:
- 管理项目 - 列出、创建、获取项目详情
- 处理议题 - 创建、更新、关闭、列出议题
- 操作合并请求 - 创建、列出、合并MR
- 查看流水线 - 列出任务、查看流水线状态
- 管理用户 - 搜索用户、获取用户信息
Prerequisites
前置条件
- Go to GitLab → User Settings → Access Tokens
- Create a personal access token with scope
api - Copy the generated token
bash
export GITLAB_HOST="gitlab.com" # Or your self-hosted GitLab domain
export GITLAB_TOKEN="glpat-xxxxxxxxxxxx" # Personal access token with api scope- 进入GitLab → 用户设置 → 访问令牌
- 创建一个拥有权限范围的个人访问令牌
api - 复制生成的令牌
bash
export GITLAB_HOST="gitlab.com" # 或者你的自托管GitLab域名
export GITLAB_TOKEN="glpat-xxxxxxxxxxxx" # 拥有api权限范围的个人访问令牌Rate Limits
请求速率限制
GitLab.com has rate limits of ~2000 requests per minute for authenticated users. Self-hosted instances may vary.
Important: When usingin a command that pipes to another command, wrap the command containing$VARin$VAR. Due to a Claude Code bug, environment variables are silently cleared when pipes are used directly.bash -c '...'bashbash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"' | jq .
GitLab.com对已认证用户的请求速率限制为每分钟约2000次。自托管实例的限制可能有所不同。
重要提示: 当在包含管道的命令中使用时,请将包含$VAR的命令用$VAR包裹。由于Claude Code的一个bug,直接使用管道时环境变量会被静默清除。bash -c '...'bashbash -c 'curl -s "https://api.example.com" -H "Authorization: Bearer $API_KEY"' | jq .
How to Use
使用方法
All examples below assume and are set.
GITLAB_HOSTGITLAB_TOKENBase URL:
https://${GITLAB_HOST}/api/v4Note: Project IDs can be numeric (e.g.,) or URL-encoded paths (e.g.,123).mygroup%2Fmyproject
以下所有示例均假设已设置和环境变量。
GITLAB_HOSTGITLAB_TOKEN基础URL:
https://${GITLAB_HOST}/api/v4注意:项目ID可以是数字(例如:),也可以是URL编码的路径(例如:123)。mygroup%2Fmyproject
1. Get Current User
1. 获取当前用户
Verify your authentication:
bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/user" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{id, username, name, email, state}'验证你的身份认证:
bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/user" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{id, username, name, email, state}'2. List Projects
2. 列出项目
Get projects accessible to you:
bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects?membership=true&per_page=20" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {id, path_with_namespace, visibility, default_branch}'Filter options:
- - Only projects you're a member of
membership=true - - Only projects you own
owned=true - - Search by name
search=keyword - - Filter by visibility
visibility=public|internal|private
获取你有权访问的项目:
bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects?membership=true&per_page=20" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {id, path_with_namespace, visibility, default_branch}'过滤选项:
- - 仅显示你作为成员的项目
membership=true - - 仅显示你拥有的项目
owned=true - - 按名称搜索
search=keyword - - 按可见性过滤
visibility=public|internal|private
3. Get Project Details
3. 获取项目详情
Get details for a specific project. Replace with the numeric project ID or URL-encoded path (e.g., ):
<project-id>mygroup%2Fmyprojectbash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{id, name, path_with_namespace, default_branch, visibility, web_url}获取特定项目的详情。将替换为数字项目ID或URL编码的路径(例如:):
<project-id>mygroup%2Fmyprojectbash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{id, name, path_with_namespace, default_branch, visibility, web_url}4. List Project Issues
4. 列出项目议题
Get issues for a project. Replace with the numeric project ID or URL-encoded path:
<project-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues?state=opened&per_page=20" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {iid, title, state, author: .author.username, labels, web_url}'Filter options:
- - Filter by state
state=opened|closed|all - - Filter by labels
labels=bug,urgent - - Filter by assignee
assignee_id=123 - - Search in title/description
search=keyword
获取项目的议题。将替换为数字项目ID或URL编码的路径:
<project-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues?state=opened&per_page=20" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {iid, title, state, author: .author.username, labels, web_url}'过滤选项:
- - 按状态过滤
state=opened|closed|all - - 按标签过滤
labels=bug,urgent - - 按经办人过滤
assignee_id=123 - - 在标题/描述中搜索
search=keyword
5. Get Issue Details
5. 获取议题详情
Get a specific issue. Replace and with actual values:
<project-id><issue-iid>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{iid, title, description, state, author: .author.username, assignees: [.assignees[].username], labels, created_at, web_url}'获取特定议题的详情。将和替换为实际值:
<project-id><issue-iid>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{iid, title, description, state, author: .author.username, assignees: [.assignees[].username], labels, created_at, web_url}'6. Create Issue
6. 创建议题
Create a new issue in a project. Replace with the actual project ID:
<project-id>Write to :
/tmp/gitlab_request.jsonjson
{
"title": "Bug: Login page not loading",
"description": "The login page shows a blank screen on mobile devices.",
"labels": "bug,frontend"
}Then run:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, web_url}'在项目中创建新议题。将替换为实际项目ID:
<project-id>写入:
/tmp/gitlab_request.jsonjson
{
"title": "Bug: 登录页面无法加载",
"description": "移动端设备上登录页面显示空白屏幕。",
"labels": "bug,frontend"
}然后运行:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, web_url}'7. Create Issue with Assignee and Milestone
7. 创建带经办人和里程碑的议题
Create issue with additional fields. Replace , , and with actual IDs:
<project-id><assignee-id><milestone-id>Write to :
/tmp/gitlab_request.jsonjson
{
"title": "Implement user profile page",
"description": "Create a user profile page with avatar and bio.",
"assignee_ids": [<assignee-id>],
"milestone_id": <milestone-id>,
"labels": "feature,frontend"
}Then run:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, web_url}'创建包含额外字段的议题。将、和替换为实际ID:
<project-id><assignee-id><milestone-id>写入:
/tmp/gitlab_request.jsonjson
{
"title": "实现用户个人资料页面",
"description": "创建包含头像和个人简介的用户个人资料页面。",
"assignee_ids": [<assignee-id>],
"milestone_id": <milestone-id>,
"labels": "feature,frontend"
}然后运行:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, web_url}'8. Update Issue
8. 更新议题
Update an existing issue. Replace and with actual values:
<project-id><issue-iid>Write to :
/tmp/gitlab_request.jsonjson
{
"title": "Updated: Bug fix for login page",
"labels": "bug,frontend,in-progress"
}Then run:
bash
bash -c 'curl -s -X PUT "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, labels, updated_at}'更新现有议题。将和替换为实际值:
<project-id><issue-iid>写入:
/tmp/gitlab_request.jsonjson
{
"title": "更新:修复登录页面Bug",
"labels": "bug,frontend,in-progress"
}然后运行:
bash
bash -c 'curl -s -X PUT "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, labels, updated_at}'9. Close Issue
9. 关闭议题
Close an issue. Replace and with actual values:
<project-id><issue-iid>Write to :
/tmp/gitlab_request.jsonjson
{
"state_event": "close"
}Then run:
bash
bash -c 'curl -s -X PUT "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, state}'Use to reopen a closed issue.
"state_event": "reopen"关闭议题。将和替换为实际值:
<project-id><issue-iid>写入:
/tmp/gitlab_request.jsonjson
{
"state_event": "close"
}然后运行:
bash
bash -c 'curl -s -X PUT "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, state}'使用可以重新打开已关闭的议题。
"state_event": "reopen"10. Add Comment to Issue
10. 为议题添加评论
Add a note/comment to an issue. Replace and with actual values:
<project-id><issue-iid>Write to :
/tmp/gitlab_request.jsonjson
{
"body": "Investigating this issue. Will update soon."
}Then run:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>/notes" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{id, body, author: .author.username, created_at}'为议题添加备注/评论。将和替换为实际值:
<project-id><issue-iid>写入:
/tmp/gitlab_request.jsonjson
{
"body": "正在调查该议题,稍后更新。"
}然后运行:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>/notes" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{id, body, author: .author.username, created_at}'11. List Merge Requests
11. 列出合并请求
Get merge requests for a project. Replace with the actual project ID:
<project-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/merge_requests?state=opened&per_page=20" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {iid, title, state, source_branch, target_branch, author: .author.username, web_url}'Filter options:
- - Filter by state
state=opened|closed|merged|all - - Filter by involvement
scope=created_by_me|assigned_to_me|all - - Filter by labels
labels=review-needed
获取项目的合并请求。将替换为实际项目ID:
<project-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/merge_requests?state=opened&per_page=20" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {iid, title, state, source_branch, target_branch, author: .author.username, web_url}'过滤选项:
- - 按状态过滤
state=opened|closed|merged|all - - 按参与角色过滤
scope=created_by_me|assigned_to_me|all - - 按标签过滤
labels=review-needed
12. Get Merge Request Details
12. 获取合并请求详情
Get a specific merge request. Replace and with actual values:
<project-id><mr-iid>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/merge_requests/<mr-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{iid, title, state, source_branch, target_branch, author: .author.username, merge_status, has_conflicts, web_url}'获取特定合并请求的详情。将和替换为实际值:
<project-id><mr-iid>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/merge_requests/<mr-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{iid, title, state, source_branch, target_branch, author: .author.username, merge_status, has_conflicts, web_url}'13. Create Merge Request
13. 创建合并请求
Create a new merge request. Replace with the actual project ID:
<project-id>Write to :
/tmp/gitlab_request.jsonjson
{
"source_branch": "feature/user-profile",
"target_branch": "main",
"title": "Add user profile page",
"description": "This MR adds a new user profile page with avatar support."
}Then run:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects/<project-id>/merge_requests" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, web_url}'创建新的合并请求。将替换为实际项目ID:
<project-id>写入:
/tmp/gitlab_request.jsonjson
{
"source_branch": "feature/user-profile",
"target_branch": "main",
"title": "添加用户个人资料页面",
"description": "该MR添加了一个支持头像的新用户个人资料页面。"
}然后运行:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects/<project-id>/merge_requests" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, web_url}'14. Merge a Merge Request
14. 合并合并请求
Merge an MR (if it's ready). Replace and with actual values:
<project-id><mr-iid>Write to :
/tmp/gitlab_request.jsonjson
{
"merge_when_pipeline_succeeds": true
}Then run:
bash
bash -c 'curl -s -X PUT "https://${GITLAB_HOST}/api/v4/projects/<project-id>/merge_requests/<mr-iid>/merge" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, state, merged_by: .merged_by.username}'Options:
- - Auto-merge when pipeline passes
merge_when_pipeline_succeeds=true - - Squash commits before merging
squash=true - - Delete source branch after merge
should_remove_source_branch=true
合并MR(如果已准备好)。将和替换为实际值:
<project-id><mr-iid>写入:
/tmp/gitlab_request.jsonjson
{
"merge_when_pipeline_succeeds": true
}然后运行:
bash
bash -c 'curl -s -X PUT "https://${GITLAB_HOST}/api/v4/projects/<project-id>/merge_requests/<mr-iid>/merge" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{iid, title, state, merged_by: .merged_by.username}'可选参数:
- - 流水线通过后自动合并
merge_when_pipeline_succeeds=true - - 合并前压缩提交
squash=true - - 合并后删除源分支
should_remove_source_branch=true
15. List Pipelines
15. 列出流水线
Get pipelines for a project. Replace with the actual project ID:
<project-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/pipelines?per_page=10" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {id, status, ref, sha: .sha[0:8], created_at, web_url}'获取项目的流水线。将替换为实际项目ID:
<project-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/pipelines?per_page=10" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {id, status, ref, sha: .sha[0:8], created_at, web_url}'16. Get Pipeline Details
16. 获取流水线详情
Get details of a specific pipeline. Replace and with actual values:
<project-id><pipeline-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/pipelines/<pipeline-id>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{id, status, ref, duration, finished_at, web_url}'获取特定流水线的详情。将和替换为实际值:
<project-id><pipeline-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/pipelines/<pipeline-id>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '{id, status, ref, duration, finished_at, web_url}'17. List Pipeline Jobs
17. 列出流水线任务
Get jobs in a pipeline. Replace and with actual values:
<project-id><pipeline-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/pipelines/<pipeline-id>/jobs" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {id, name, stage, status, duration}'获取流水线中的任务。将和替换为实际值:
<project-id><pipeline-id>bash
bash -c 'curl -s "https://${GITLAB_HOST}/api/v4/projects/<project-id>/pipelines/<pipeline-id>/jobs" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}"' | jq '.[] | {id, name, stage, status, duration}'18. Search Users
18. 搜索用户
Search for users:
Write to :
/tmp/gitlab_search.txtjohnbash
bash -c 'curl -s -G "https://${GITLAB_HOST}/api/v4/users" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --data-urlencode "search@/tmp/gitlab_search.txt"' | jq '.[] | {id, username, name, state}'搜索用户:
写入:
/tmp/gitlab_search.txtjohnbash
bash -c 'curl -s -G "https://${GITLAB_HOST}/api/v4/users" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --data-urlencode "search@/tmp/gitlab_search.txt"' | jq '.[] | {id, username, name, state}'19. Create Project
19. 创建项目
Create a new project:
Write to :
/tmp/gitlab_request.jsonjson
{
"name": "my-new-project",
"visibility": "private",
"initialize_with_readme": true
}Then run:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{id, path_with_namespace, web_url}'创建新项目:
写入:
/tmp/gitlab_request.jsonjson
{
"name": "my-new-project",
"visibility": "private",
"initialize_with_readme": true
}然后运行:
bash
bash -c 'curl -s -X POST "https://${GITLAB_HOST}/api/v4/projects" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" -d @/tmp/gitlab_request.json' | jq '{id, path_with_namespace, web_url}'20. Delete Issue
20. 删除议题
Delete an issue (requires admin or owner permissions). Replace and with actual values:
<project-id><issue-iid>bash
bash -c 'curl -s -X DELETE "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" -w "\nHTTP Status: %{http_code}"'Returns 204 No Content on success.
删除议题(需要管理员或所有者权限)。将和替换为实际值:
<project-id><issue-iid>bash
bash -c 'curl -s -X DELETE "https://${GITLAB_HOST}/api/v4/projects/<project-id>/issues/<issue-iid>" --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" -w "\nHTTP状态码: %{http_code}"'成功时返回204 No Content。
Project ID Encoding
项目ID编码
When using project paths instead of numeric IDs, URL-encode the path:
- →
mygroup/myprojectmygroup%2Fmyproject - →
mygroup/subgroup/myprojectmygroup%2Fsubgroup%2Fmyproject
bash
undefined当使用项目路径而非数字ID时,需要对路径进行URL编码:
- →
mygroup/myprojectmygroup%2Fmyproject - →
mygroup/subgroup/myprojectmygroup%2Fsubgroup%2Fmyproject
bash
undefinedUsing numeric ID
使用数字ID
PROJECT_ID="123"
PROJECT_ID="123"
Using encoded path
使用编码后的路径
PROJECT_ID="mygroup%2Fmyproject"
---PROJECT_ID="mygroup%2Fmyproject"
---Guidelines
注意事项
- Use IID for issues/MRs: Within a project, use (internal ID like #1, #2) not the global
iidid - URL-encode project paths: If using paths instead of numeric IDs, encode slashes as
%2F - Handle pagination: Use and
per_pageparams for large result setspage - Check merge status: Before merging, verify is
merge_statuscan_be_merged - Rate limiting: Implement backoff if you receive 429 responses
- Self-hosted GitLab: Set to your instance domain (without https://)
GITLAB_HOST
- 使用IID操作议题/MR:在项目内,使用(如#1、#2这样的内部ID)而非全局
iidid - URL编码项目路径:如果使用路径而非数字ID,需将斜杠编码为
%2F - 处理分页:对于大量结果集,使用和
per_page参数page - 检查合并状态:合并前需验证为
merge_statuscan_be_merged - 速率限制处理:如果收到429响应,需实现重试机制
- 自托管GitLab:将设置为你的实例域名(无需https://)
GITLAB_HOST