Snippets API

Snippets API

在 GitLab 8.15 中引入 .

片段 API 在运行.

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

代码段可见性级别的有效值为:

List all snippets for a user

获取当前用户的片段列表.

请求示例:

  1. curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets"

响应示例:

  1. [ { "id": 42, "title": "Voluptatem iure ut qui aut et consequatur quaerat.", "file_name": "mclaughlin.rb", "description": null, "visibility": "internal", "author": { "id": 22, "name": "User 0", "username": "user0", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon", "web_url": "http://example.com/user0" }, "updated_at": "2018-09-18T01:12:26.383Z", "created_at": "2018-09-18T01:12:26.383Z", "project_id": null, "web_url": "http://example.com/snippets/42", "raw_url": "http://example.com/snippets/42/raw" }, { "id": 41, "title": "Ut praesentium non et atque.", "file_name": "ondrickaemard.rb", "description": null, "visibility": "internal", "author": { "id": 22, "name": "User 0", "username": "user0", "state": "active", "avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon", "web_url": "http://example.com/user0" }, "updated_at": "2018-09-18T01:12:26.360Z", "created_at": "2018-09-18T01:12:26.360Z", "project_id": 1, "web_url": "http://example.com/gitlab-org/gitlab-test/snippets/41", "raw_url": "http://example.com/gitlab-org/gitlab-test/snippets/41/raw" } ]

Get a single snippet

取得一个摘要.

  1. GET /snippets/:id

Parameters:

Attribute Type Required Description
id integer yes 要检索的代码段的 ID.

请求示例:

  1. curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"

响应示例:

  1. { "id": 1, "title": "test", "file_name": "add.rb", "description": "Ruby test snippet", "visibility": "private", "author": { "id": 1, "username": "john_smith", "email": "john@example.com", "name": "John Smith", "state": "active", "created_at": "2012-05-23T08:00:58Z" }, "expires_at": null, "updated_at": "2012-06-28T10:52:04Z", "created_at": "2012-06-28T10:52:04Z", "project_id": null, "web_url": "http://example.com/snippets/1", "raw_url": "http://example.com/snippets/1/raw" }

获取单个代码段的原始内容.

  1. GET /snippets/:id/raw

Parameters:

Attribute Type Required Description
id integer yes 要检索的代码段的 ID.
  1. curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1/raw"

响应示例:

Snippet repository file content

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

  1. GET /snippets/:id/files/:ref/:file_path/raw

Parameters:

请求示例:

响应示例:

  1. Hello World snippet

Create new snippet

创建一个新的代码段.

注意:用户必须具有创建新代码段的权限.

    Parameters:

    Attribute Type Required Description
    title string yes 摘要的标题.
    file_name string yes 片段文件的名称.
    content string yes 摘要的内容.
    description string no 片段说明.
    visibility string no Snippet’s visibility.

    请求示例:

    1. curl --request POST \
    2. --data '{"title": "This is a snippet", "content": "Hello world", "description": "Hello World snippet", "file_name": "test.txt", "visibility": "internal" }' \
    3. --header 'Content-Type: application/json' \
    4. --header "PRIVATE-TOKEN: <your_access_token>" \
    5. "https://gitlab.example.com/api/v4/snippets"

    响应示例:

    1. { "id": 1, "title": "This is a snippet", "file_name": "test.txt", "description": "Hello World snippet", "visibility": "internal", "author": { "id": 1, "username": "john_smith", "email": "john@example.com", "name": "John Smith", "state": "active", "created_at": "2012-05-23T08:00:58Z" }, "expires_at": null, "updated_at": "2012-06-28T10:52:04Z", "created_at": "2012-06-28T10:52:04Z", "project_id": null, "web_url": "http://example.com/snippets/1", "raw_url": "http://example.com/snippets/1/raw" }

    更新现有代码段.

    注意:用户必须具有更改现有代码段的权限.

    1. PUT /snippets/:id

    Parameters:

    Attribute Type Required Description
    id integer yes 要更新的代码段 ID.
    title string no 摘要的标题.
    string no 片段文件的名称.
    description string no 片段说明.
    content string no 摘要的内容.
    visibility string no Snippet’s .

    响应示例:

    1. { "id": 1, "title": "test", "file_name": "add.rb", "description": "description of snippet", "visibility": "internal", "author": { "id": 1, "username": "john_smith", "email": "john@example.com", "name": "John Smith", "state": "active", "created_at": "2012-05-23T08:00:58Z" }, "expires_at": null, "updated_at": "2012-06-28T10:52:04Z", "created_at": "2012-06-28T10:52:04Z", "project_id": null, "web_url": "http://example.com/snippets/1", "raw_url": "http://example.com/snippets/1/raw" }

    Delete snippet

    删除现有的代码段.

    1. DELETE /snippets/:id

    Parameters:

    请求示例:

    1. curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/1"

    以下是可能的返回码:

    Code Description
    204 删除成功. 没有数据返回.
    404 找不到该代码段.

    List all public snippets

    列出所有公共摘要.

    1. GET /snippets/public

    Parameters:

    Attribute Type Required Description
    per_page integer no 每页要返回的代码段数.
    page integer no 要检索的页面.

    请求示例:

    1. curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/snippets/public?per_page=2&page=1"

    响应示例:

    1. [ { "author": { "avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon", "id": 12, "name": "Libby Rolfson", "state": "active", "username": "elton_wehner", "web_url": "http://example.com/elton_wehner" }, "created_at": "2016-11-25T16:53:34.504Z", "file_name": "oconnerrice.rb", "id": 49, "title": "Ratione cupiditate et laborum temporibus.", "updated_at": "2016-11-25T16:53:34.504Z", "project_id": null, "web_url": "http://example.com/snippets/49", "raw_url": "http://example.com/snippets/49/raw" }, { "author": { "avatar_url": "http://www.gravatar.com/avatar/36583b28626de71061e6e5a77972c3bd?s=80&d=identicon", "id": 16, "name": "Llewellyn Flatley", "state": "active", "username": "adaline", "web_url": "http://example.com/adaline" }, "created_at": "2016-11-25T16:53:34.479Z", "file_name": "muellershields.rb", "id": 48, "title": "Minus similique nesciunt vel fugiat qui ullam sunt.", "updated_at": "2016-11-25T16:53:34.479Z", "project_id": null, "web_url": "http://example.com/snippets/48", "raw_url": "http://example.com/snippets/49/raw", "visibility": "public" } ]

    在 GitLab 9.4 中引入 .

    注意:仅适用于管理员.

    1. GET /snippets/:id/user_agent_detail

    请求示例:

    响应示例: