{{< details >}}
- Tier: 基础版,专业版,旗舰版
- Offering: JihuLab.com, 私有化部署
{{< /details >}}
片段可见性级别
在极狐GitLab 中,片段可以是私有、内部或公开的。您可以使用片段中的 visibility
字段来设置它。
片段可见性级别的常量有:
- Private: 片段仅对项目成员可见。
- Internal: 片段对任何经过身份验证的用户可见,但不包括 external users。
- Public: 片段可以在不进行身份验证的情况下访问。
{{< alert type=”note” >}}
从 2019 年 7 月起,Internal
可见性设置在 JihuLab.com 上对新项目、群组和片段禁用。使用 Internal
可见性设置的现有项目、群组和片段保留此设置。
{{< /alert >}}
列出片段
获取项目片段列表。
GET /projects/:id/snippets
参数:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
单个片段
获取单个项目片段。
GET /projects/:id/snippets/:snippet_id
参数:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
snippet_id |
integer | 是 | 项目的片段 ID。 |
{
"id": 1,
"title": "test",
"file_name": "add.rb",
"description": "Ruby test snippet",
"author": {
"id": 1,
"username": "john_smith",
"email": "john@example.com",
"name": "John Smith",
"state": "active",
"created_at": "2012-05-23T08:00:58Z"
},
"updated_at": "2012-06-28T10:52:04Z",
"created_at": "2012-06-28T10:52:04Z",
"imported": false,
"imported_from": "none",
"project_id": 1,
"web_url": "http://example.com/example/example/snippets/1",
"raw_url": "http://example.com/example/example/snippets/1/raw"
}
创建新片段
创建新的项目片段。用户必须有权限创建新片段。
POST /projects/:id/snippets
参数:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
files:content |
string | 是 | 片段文件的内容。 |
files:file_path |
string | 是 | 片段文件的文件路径。 |
title |
string | 是 | 片段的标题。 |
content |
string | 否 | 已弃用:请使用 files 。片段的内容。 |
description |
string | 否 | 片段的描述。 |
file_name |
string | 否 | 已弃用:请使用 files 。片段文件的名称。 |
files |
array of hashes | 否 | 片段文件的数组。 |
visibility |
string | 否 | 片段的 可见性。 |
示例请求:
curl --request POST "https://jihulab.com/api/v4/projects/:id/snippets" \
--header "PRIVATE-TOKEN: <your access token>" \
--header "Content-Type: application/json" \
-d @snippet.json
上述示例请求中使用的 snippet.json
:
{
"title" : "Example Snippet Title",
"description" : "More verbose snippet description",
"visibility" : "private",
"files": [
{
"file_path": "example.txt",
"content" : "source code \n with multiple lines\n"
}
]
}
更新片段
更新现有项目片段。用户必须有权限更改现有片段。
对具有多个文件的片段的更新必须使用 files
属性。
PUT /projects/:id/snippets/:snippet_id
参数:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
files:action |
string | 是 | 要对文件执行的操作类型。可选值:create ,update ,delete ,move 。 |
snippet_id |
integer | 是 | 项目的片段 ID。 |
content |
string | 否 | 已弃用:请使用 files 。片段的内容。 |
description |
string | 否 | 片段的描述。 |
files |
array of hashes | 否 | 片段文件的数组。 |
files:content |
string | 否 | 片段文件的内容。 |
files:file_path |
string | 否 | 片段文件的文件路径。 |
file_name |
string | 否 | 已弃用:请使用 files 。片段文件的名称。 |
files:previous_path |
string | 否 | 片段文件的之前路径。 |
title |
string | 否 | 片段的标题。 |
visibility |
string | 否 | 片段的 可见性。 |
示例请求:
curl --request PUT "https://jihulab.com/api/v4/projects/:id/snippets/:snippet_id" \
--header "PRIVATE-TOKEN: <your_access_token>" \
--header "Content-Type: application/json" \
-d @snippet.json
上述示例请求中使用的 snippet.json
:
{
"title" : "Updated Snippet Title",
"description" : "More verbose snippet description",
"visibility" : "private",
"files": [
{
"action": "update",
"file_path": "example.txt",
"content" : "updated source code \n with multiple lines\n"
}
]
}
删除片段
删除现有项目片段。如果操作成功,则返回 204 No Content
状态码;如果资源未找到,则返回 404
。
DELETE /projects/:id/snippets/:snippet_id
参数:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
snippet_id |
integer | 是 | 项目的片段 ID。 |
示例请求:
curl --request DELETE "https://jihulab.com/api/v4/projects/:id/snippets/:snippet_id" \
--header "PRIVATE-TOKEN: <your_access_token>"
片段内容
返回项目片段的原始内容作为纯文本。
GET /projects/:id/snippets/:snippet_id/raw
参数:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
snippet_id |
integer | 是 | 项目的片段 ID。 |
示例请求:
curl "https://jihulab.com/api/v4/projects/:id/snippets/:snippet_id/raw" \
--header "PRIVATE-TOKEN: <your_access_token>"
片段仓库文件内容
返回文件内容的原始内容作为纯文本。
GET /projects/:id/snippets/:snippet_id/files/:ref/:file_path/raw
参数:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
file_path |
string | 是 | 文件的 URL 编码路径,例如 snippet%2Erb 。 |
ref |
string | 是 | 分支、标签或提交的名称,例如 main 。 |
snippet_id |
integer | 是 | 项目的片段 ID。 |
示例请求:
curl "https://jihulab.com/api/v4/projects/1/snippets/2/files/master/snippet%2Erb/raw" \
--header "PRIVATE-TOKEN: <your_access_token>"
获取用户代理详细信息
仅对具有管理员访问权限的用户可用。
GET /projects/:id/snippets/:snippet_id/user_agent_detail
参数:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
id |
integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
snippet_id |
Integer | 是 | 片段 ID。 |
示例请求:
curl --header "PRIVATE-TOKEN: <your_access_token>" \
--url "https://gitlab.example.com/api/v4/projects/1/snippets/2/user_agent_detail"
示例响应:
{
"user_agent": "AppleWebKit/537.36",
"ip_address": "127.0.0.1",
"akismet_submitted": false
}