Project snippets
- Snippet visibility level
 - List snippets
 - Single snippet
 - Create new snippet
 - Update snippet
 - Delete snippet
 - Snippet content
 - Snippet repository file content
 - Get user agent details
 
Project snippets
Snippet visibility level
GitLab 中的片段可以是私有的,内部的或公共的. 您可以使用代码段中的visibility字段进行设置.
摘要可见性级别的常量为:
| visibility | Description | 
|---|---|
 private  | 
该代码段仅在代码段创建者中可见 | 
 internal  | 
所有已登录的用户都可以看到该代码段 | 
 public  | 
无需任何身份验证即可访问该代码段 | 
注意:从 2019 年 7 月开始,GitLab.com 上的新项目,组和摘要的` Internal可见性''设置被禁用. 使用"Internal`可见性"设置的现有项目,组和摘录保留此设置. 您可以在相关问题中阅读有关更改的更多信息.
List snippets
获取项目摘要列表.
GET /projects/:id/snippets 
Parameters:
Single snippet
获得一个项目片段.
GET /projects/:id/snippets/:snippet_id 
Parameters:
{  "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",  "project_id":  1,  "web_url":  "http://example.com/example/example/snippets/1",  "raw_url":  "http://example.com/example/example/snippets/1/raw"  } 
Create new snippet
创建一个新的项目片段. 用户必须具有创建新代码段的权限.
POST /projects/:id/snippets 
Parameters:
id(必填)-经过身份验证的用户拥有的项目的 ID 或URL 编码路径title(required) - The title of a snippetfile_name(必填)-代码段文件的名称description(可选)-代码段的说明content(必填)-代码段的内容visibility(必填)-代码段的可见性
请求示例:
curl --request POST "https://gitlab.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",  "file_name"  :  "example.txt",  "content"  :  "source code \n with multiple lines\n",  "visibility"  :  "private"  } 
Update snippet
更新现有的项目代码段. 用户必须具有更改现有代码段的权限.
PUT /projects/:id/snippets/:snippet_id 
Parameters:
id(必填)-经过身份验证的用户拥有的项目的 ID 或URL 编码路径snippet_id(必填)-项目代码段的 IDtitle(可选)-摘要的标题file_name(可选)-代码段文件的名称description(可选)-代码段的说明content(可选)-代码段的内容visibility(可选)-代码段的可见性
请求示例:
curl --request PUT "https://gitlab.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",  "file_name"  :  "new_filename.txt",  "content"  :  "updated source code \n with multiple lines\n",  "visibility"  :  "private"  } 
Delete snippet
删除现有项目片段. 如果操作成功,则返回204 No Content状态代码;如果找不到资源,则返回404 .
DELETE /projects/:id/snippets/:snippet_id 
Parameters:
请求示例:
curl --request DELETE "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id" \
     --header "PRIVATE-TOKEN: <your_access_token>" 
Snippet content
以纯文本形式返回原始项目代码段.
GET /projects/:id/snippets/:snippet_id/raw 
Parameters:
请求示例:
curl "https://gitlab.com/api/v4/projects/:id/snippets/:snippet_id/raw" \
     --header "PRIVATE-TOKEN: <your_access_token>" 
Snippet repository file content
以纯文本形式返回原始文件的内容.
GET /projects/:id/snippets/:snippet_id/files/:ref/:file_path/raw 
Parameters:
id(必填)-经过身份验证的用户拥有的项目的 ID 或URL 编码路径snippet_id(必填)-项目代码段的 IDref(必填)-分支,标记或提交的名称,例如 masterfile_path(必填)-文件的 URL 编码路径,例如 snippet%2Erb
请求示例:
curl "https://gitlab.com/api/v4/projects/1/snippets/2/files/master/snippet%2Erb/raw" \
     --header "PRIVATE-TOKEN: <your_access_token>" 
Get user agent details
在 GitLab 9.4 中引入 .
仅适用于管理员.
GET /projects/:id/snippets/:snippet_id/user_agent_detail 
| Attribute | Type | Required | Description | 
|---|---|---|---|
 id  | 
Integer | yes | 项目 ID | 
 snippet_id  | 
Integer | yes | 片段的 ID | 
请求示例:
curl --header "PRIVATE-TOKEN: <your_access_token>" "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  }