371.md 7.3 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
# Project snippets

> 原文:[https://docs.gitlab.com/ee/api/project_snippets.html](https://docs.gitlab.com/ee/api/project_snippets.html)

*   [Snippet visibility level](#snippet-visibility-level)
*   [List snippets](#list-snippets)
*   [Single snippet](#single-snippet)
*   [Create new snippet](#create-new-snippet)
*   [Update snippet](#update-snippet)
*   [Delete snippet](#delete-snippet)
*   [Snippet content](#snippet-content)
*   [Snippet repository file content](#snippet-repository-file-content)
*   [Get user agent details](#get-user-agent-details)

# Project snippets[](#project-snippets "Permalink")

## Snippet visibility level[](#snippet-visibility-level "Permalink")

GitLab 中的片段可以是私有的,内部的或公共的. 您可以使用代码段中的`visibility`字段进行设置.

摘要可见性级别的常量为:

| visibility | Description |
| --- | --- |
| `private` | 该代码段仅在代码段创建者中可见 |
| `internal` | 所有已登录的用户都可以看到该代码段 |
| `public` | 无需任何身份验证即可访问该代码段 |

**注意:**从 2019 年 7 月开始,GitLab.com 上的新项目,组和摘要的`` `Internal`可见性''设置被禁用. 使用" `Internal`可见性"设置的现有项目,组和摘录保留此设置. 您可以在[相关问题中](https://gitlab.com/gitlab-org/gitlab/-/issues/12388)阅读有关更改的更多信息.

## List snippets[](#list-snippets "Permalink")

获取项目摘要列表.

```
GET /projects/:id/snippets 
```

Parameters:

*   `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)

## Single snippet[](#single-snippet "Permalink")

获得一个项目片段.

```
GET /projects/:id/snippets/:snippet_id 
```

Parameters:

*   `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `snippet_id` (必填)-项目代码段的 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",  "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[](#create-new-snippet "Permalink")

创建一个新的项目片段. 用户必须具有创建新代码段的权限.

```
POST /projects/:id/snippets 
```

Parameters:

*   `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `title` (required) - The title of a snippet
*   `file_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[](#update-snippet "Permalink")

更新现有的项目代码段. 用户必须具有更改现有代码段的权限.

```
PUT /projects/:id/snippets/:snippet_id 
```

Parameters:

*   `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `snippet_id` (必填)-项目代码段的 ID
*   `title` (可选)-摘要的标题
*   `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[](#delete-snippet "Permalink")

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

```
DELETE /projects/:id/snippets/:snippet_id 
```

Parameters:

*   `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `snippet_id` (必填)-项目代码段的 ID

请求示例:

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

## Snippet content[](#snippet-content "Permalink")

以纯文本形式返回原始项目代码段.

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

Parameters:

*   `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `snippet_id` (必填)-项目代码段的 ID

请求示例:

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

## Snippet repository file content[](#snippet-repository-file-content "Permalink")

以纯文本形式返回原始文件的内容.

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

Parameters:

*   `id` (必填)-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `snippet_id` (必填)-项目代码段的 ID
*   `ref` (必填)-分支,标记或提交的名称,例如 master
*   `file_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[](#get-user-agent-details "Permalink")

在 GitLab 9.4 中[引入](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/29508) .

仅适用于管理员.

```
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  } 
```