项目代码片段
- Tier: 基础版,专业版,旗舰版
- Offering: JihuLab.com, 私有化部署
片段可见性级别
在极狐GitLab 中,片段可以是私有、内部或公开的。您可以使用片段中的 visibility 字段来设置它。
片段可见性级别的常量有:
- Private: 片段仅对项目成员可见。
- Internal: 片段对任何经过身份验证的用户可见,但不包括 external users。
- Public: 片段可以在不进行身份验证的情况下访问。
从 2019 年 7 月起,`Internal` 可见性设置在 JihuLab.com 上对新项目、群组和片段禁用。使用 `Internal` 可见性设置的现有项目、群组和片段保留此设置。
列出片段
获取项目片段列表。
plaintextGET /projects/:id/snippets
参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
单个片段
获取单个项目片段。
plaintextGET /projects/:id/snippets/:snippet_id
参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
| snippet_id | integer | 是 | 项目的片段 ID。 |
json1{ 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}
创建新片段
创建新的项目片段。用户必须有权限创建新片段。
plaintextPOST /projects/:id/snippets
参数:
示例请求:
shellcurl --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:
json1{ 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 属性。
plaintextPUT /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 | 否 | 片段的 可见性。 |
示例请求:
shellcurl --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:
json1{ 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。
plaintextDELETE /projects/:id/snippets/:snippet_id
参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
| snippet_id | integer | 是 | 项目的片段 ID。 |
示例请求:
shellcurl --request DELETE "https://jihulab.com/api/v4/projects/:id/snippets/:snippet_id" \ --header "PRIVATE-TOKEN: <your_access_token>"
片段内容
返回项目片段的原始内容作为纯文本。
plaintextGET /projects/:id/snippets/:snippet_id/raw
参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
| snippet_id | integer | 是 | 项目的片段 ID。 |
示例请求:
shellcurl "https://jihulab.com/api/v4/projects/:id/snippets/:snippet_id/raw" \ --header "PRIVATE-TOKEN: <your_access_token>"
片段仓库文件内容
返回文件内容的原始内容作为纯文本。
plaintextGET /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。 |
示例请求:
shellcurl "https://jihulab.com/api/v4/projects/1/snippets/2/files/master/snippet%2Erb/raw" \ --header "PRIVATE-TOKEN: <your_access_token>"
获取用户代理详细信息
仅对具有管理员访问权限的用户可用。
plaintextGET /projects/:id/snippets/:snippet_id/user_agent_detail
参数:
| 属性 | 类型 | 必需 | 描述 |
|---|---|---|---|
| id | integer 或 string | 是 | 项目的 ID 或 URL 编码路径。 |
| snippet_id | Integer | 是 | 片段 ID。 |
示例请求:
shellcurl --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 }