项目代码片段

  • Tier: 基础版,专业版,旗舰版
  • Offering: JihuLab.com, 私有化部署

片段可见性级别#

在极狐GitLab 中,片段可以是私有、内部或公开的。您可以使用片段中的 visibility 字段来设置它。

片段可见性级别的常量有:

  • Private: 片段仅对项目成员可见。
  • Internal: 片段对任何经过身份验证的用户可见,但不包括 external users
  • Public: 片段可以在不进行身份验证的情况下访问。
从 2019 年 7 月起,`Internal` 可见性设置在 JihuLab.com 上对新项目、群组和片段禁用。使用 `Internal` 可见性设置的现有项目、群组和片段保留此设置。

列出片段#

获取项目片段列表。

plaintext
GET /projects/:id/snippets

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径

单个片段#

获取单个项目片段。

plaintext
GET /projects/:id/snippets/:snippet_id

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
snippet_idinteger项目的片段 ID。
json
1{ 2 "id": 1, 3 "title": "test", 4 "file_name": "add.rb", 5 "description": "Ruby test snippet", 6 "author": { 7 "id": 1, 8 "username": "john_smith", 9 "email": "john@example.com", 10 "name": "John Smith", 11 "state": "active", 12 "created_at": "2012-05-23T08:00:58Z" 13 }, 14 "updated_at": "2012-06-28T10:52:04Z", 15 "created_at": "2012-06-28T10:52:04Z", 16 "imported": false, 17 "imported_from": "none", 18 "project_id": 1, 19 "web_url": "http://example.com/example/example/snippets/1", 20 "raw_url": "http://example.com/example/example/snippets/1/raw" 21}

创建新片段#

创建新的项目片段。用户必须有权限创建新片段。

plaintext
POST /projects/:id/snippets

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
files:contentstring片段文件的内容。
files:file_pathstring片段文件的文件路径。
titlestring片段的标题。
contentstring已弃用:请使用 files。片段的内容。
descriptionstring片段的描述。
file_namestring已弃用:请使用 files。片段文件的名称。
filesarray of hashes片段文件的数组。
visibilitystring片段的 可见性

示例请求:

shell
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

json
1{ 2 "title" : "Example Snippet Title", 3 "description" : "More verbose snippet description", 4 "visibility" : "private", 5 "files": [ 6 { 7 "file_path": "example.txt", 8 "content" : "source code \n with multiple lines\n" 9 } 10 ] 11}

更新片段#

更新现有项目片段。用户必须有权限更改现有片段。

对具有多个文件的片段的更新必须使用 files 属性。

plaintext
PUT /projects/:id/snippets/:snippet_id

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
files:actionstring要对文件执行的操作类型。可选值:createupdatedeletemove
snippet_idinteger项目的片段 ID。
contentstring已弃用:请使用 files。片段的内容。
descriptionstring片段的描述。
filesarray of hashes片段文件的数组。
files:contentstring片段文件的内容。
files:file_pathstring片段文件的文件路径。
file_namestring已弃用:请使用 files。片段文件的名称。
files:previous_pathstring片段文件的之前路径。
titlestring片段的标题。
visibilitystring片段的 可见性

示例请求:

shell
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

json
1{ 2 "title" : "Updated Snippet Title", 3 "description" : "More verbose snippet description", 4 "visibility" : "private", 5 "files": [ 6 { 7 "action": "update", 8 "file_path": "example.txt", 9 "content" : "updated source code \n with multiple lines\n" 10 } 11 ] 12}

删除片段#

删除现有项目片段。如果操作成功,则返回 204 No Content 状态码;如果资源未找到,则返回 404

plaintext
DELETE /projects/:id/snippets/:snippet_id

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
snippet_idinteger项目的片段 ID。

示例请求:

shell
curl --request DELETE "https://jihulab.com/api/v4/projects/:id/snippets/:snippet_id" \ --header "PRIVATE-TOKEN: <your_access_token>"

片段内容#

返回项目片段的原始内容作为纯文本。

plaintext
GET /projects/:id/snippets/:snippet_id/raw

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
snippet_idinteger项目的片段 ID。

示例请求:

shell
curl "https://jihulab.com/api/v4/projects/:id/snippets/:snippet_id/raw" \ --header "PRIVATE-TOKEN: <your_access_token>"

片段仓库文件内容#

返回文件内容的原始内容作为纯文本。

plaintext
GET /projects/:id/snippets/:snippet_id/files/:ref/:file_path/raw

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
file_pathstring文件的 URL 编码路径,例如 snippet%2Erb
refstring分支、标签或提交的名称,例如 main
snippet_idinteger项目的片段 ID。

示例请求:

shell
curl "https://jihulab.com/api/v4/projects/1/snippets/2/files/master/snippet%2Erb/raw" \ --header "PRIVATE-TOKEN: <your_access_token>"

获取用户代理详细信息#

仅对具有管理员访问权限的用户可用。

plaintext
GET /projects/:id/snippets/:snippet_id/user_agent_detail

参数:

属性类型必需描述
idinteger 或 string项目的 ID 或 URL 编码路径
snippet_idInteger片段 ID。

示例请求:

shell
curl --header "PRIVATE-TOKEN: <your_access_token>" \ --url "https://gitlab.example.com/api/v4/projects/1/snippets/2/user_agent_detail"

示例响应:

json
{ "user_agent": "AppleWebKit/537.36", "ip_address": "127.0.0.1", "akismet_submitted": false }