{{< details >}}

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

{{< /details >}}

极狐GitLab 的 Snippets API 操作在代码片段上。相关 API 存在于项目代码片段在存储之间移动代码片段

代码片段可见性级别

在极狐GitLab 中,代码片段可以是私有、内部或公共的。您可以通过代码片段中的 visibility 字段进行设置。

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

可见性 描述
private 代码片段仅对代码片段创建者可见。
internal 代码片段对任何经过身份验证的用户可见,除了外部用户
public 可以在不进行身份验证的情况下访问代码片段。

列出当前用户的所有代码片段

获取当前用户的代码片段列表。

GET /snippets

参数:

属性 类型 必需 描述
per_page integer 每页返回的代码片段数量。
page integer 要检索的页面。
created_after datetime 返回在给定时间之后创建的代码片段。期望的格式为 ISO 8601 (2019-03-15T08:00:00Z)
created_before datetime 返回在给定时间之前创建的代码片段。期望的格式为 ISO 8601 (2019-03-15T08:00:00Z)

示例请求:

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

示例响应:

[
    {
        "id": 42,
        "title": "Voluptatem iure ut qui aut et consequatur quaerat.",
        "file_name": "mclaughlin.rb",
        "description": null,
        "visibility": "internal",
        "imported": false,
        "imported_from": "none",
        "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",
        "imported": false,
        "imported_from": "none",
        "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 /snippets/:id

参数:

属性 类型 必需 描述
id integer 要检索的代码片段 ID。

示例请求:

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

示例响应:

{
  "id": 1,
  "title": "test",
  "file_name": "add.rb",
  "description": "Ruby test snippet",
  "visibility": "private",
  "imported": false,
  "imported_from": "none",
  "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"
}

单个代码片段内容

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

GET /snippets/:id/raw

参数:

属性 类型 必需 描述
id integer 要检索的代码片段 ID。

示例请求:

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

示例响应:

Hello World snippet

代码片段仓库文件内容

以纯文本格式返回原始文件内容。

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

参数:

属性 类型 必需 描述
id integer 要检索的代码片段 ID。
ref string 标签、分支或提交的引用。
file_path string 文件的 URL 编码路径。

示例请求:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/snippets/1/files/main/snippet%2Erb/raw"

示例响应:

Hello World snippet

创建新的代码片段

创建新的代码片段。

{{< alert type=”note” >}}

用户必须有权限创建新的代码片段。

{{< /alert >}}

POST /snippets

参数:

属性 类型 必需 描述
title string 代码片段的标题
file_name string 已弃用:请使用 files。代码片段文件名
content string 已弃用:请使用 files。代码片段内容
description string 代码片段的描述
visibility string 代码片段的可见性
files array of hashes 代码片段文件数组
files:file_path string 代码片段文件的文件路径
files:content string 代码片段文件的内容

示例请求:

curl --request POST "https://gitlab.example.com/api/v4/snippets" \
     --header 'Content-Type: application/json' \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     -d @snippet.json

在上述示例请求中使用的 snippet.json

{
  "title": "This is a snippet",
  "description": "Hello World snippet",
  "visibility": "internal",
  "files": [
    {
      "content": "Hello world",
      "file_path": "test.txt"
    }
  ]
}

示例响应:

{
  "id": 1,
  "title": "This is a snippet",
  "description": "Hello World snippet",
  "visibility": "internal",
  "imported": false,
  "imported_from": "none",
  "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",
  "ssh_url_to_repo": "ssh://git@gitlab.example.com:snippets/1.git",
  "http_url_to_repo": "https://gitlab.example.com/snippets/1.git",
  "file_name": "test.txt",
  "files": [
    {
      "path": "text.txt",
      "raw_url": "https://gitlab.example.com/-/snippets/1/raw/main/renamed.md"
    }
  ]
}

更新代码片段

更新现有代码片段。

{{< alert type=”note” >}}

用户必须有权限更改现有代码片段。

{{< /alert >}}

PUT /snippets/:id

参数:

属性 类型 必需 描述
id integer 要更新的代码片段 ID
title string 代码片段的标题
file_name string 已弃用:请使用 files。代码片段文件名
content string 已弃用:请使用 files。代码片段内容
description string 代码片段的描述
visibility string 代码片段的可见性
files array of hashes 有时 代码片段文件数组。当更新具有多个文件的代码片段时必需。
files:action string 对文件执行的操作类型之一:createupdatedeletemove
files:file_path string 代码片段文件的文件路径
files:previous_path string 代码片段文件的先前路径
files:content string 代码片段文件的内容

示例请求:

curl --request PUT "https://gitlab.example.com/api/v4/snippets/1" \
     --header 'Content-Type: application/json' \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     -d @snippet.json

在上述示例请求中使用的 snippet.json

{
  "title": "foo",
  "files": [
    {
      "action": "move",
      "previous_path": "test.txt",
      "file_path": "renamed.md"
    }
  ]
}

示例响应:

{
  "id": 1,
  "title": "test",
  "description": "description of snippet",
  "visibility": "internal",
  "imported": false,
  "imported_from": "none",
  "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",
  "ssh_url_to_repo": "ssh://git@gitlab.example.com:snippets/1.git",
  "http_url_to_repo": "https://gitlab.example.com/snippets/1.git",
  "file_name": "renamed.md",
  "files": [
    {
      "path": "renamed.md",
      "raw_url": "https://gitlab.example.com/-/snippets/1/raw/main/renamed.md"
    }
  ]
}

删除代码片段

删除现有代码片段。

DELETE /snippets/:id

参数:

属性 类型 必需 描述
id integer 要删除的代码片段 ID。

示例请求:

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

以下是可能的返回代码:

代码 描述
204 删除成功。没有返回数据。
404 找不到代码片段。

列出所有公共代码片段

列出所有公共代码片段。

GET /snippets/public

参数:

属性 类型 必需 描述
per_page integer 每页返回的代码片段数量。
page integer 要检索的页面。
created_after datetime 返回在给定时间之后创建的代码片段。期望的格式为 ISO 8601 (2019-03-15T08:00:00Z)
created_before datetime 返回在给定时间之前创建的代码片段。期望的格式为 ISO 8601 (2019-03-15T08:00:00Z)

示例请求:

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  --url "https://gitlab.example.com/api/v4/snippets/public?per_page=2&page=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"
    }
]

列出所有代码片段

{{< history >}}

  • 在极狐GitLab 16.3 中引入。

{{< /history >}}

列出当前用户有权访问的所有代码片段。具有 管理员审计员 访问级别的用户可以看到所有代码片段(个人和项目)。

GET /snippets/all

参数:

属性 类型 必需 描述
created_after datetime 返回在给定时间之后创建的代码片段。期望的格式为 ISO 8601 (2019-03-15T08:00:00Z)
created_before datetime 返回在给定时间之前创建的代码片段。期望的格式为 ISO 8601 (2019-03-15T08:00:00Z)
page integer 要检索的页面。
per_page integer 每页返回的代码片段数量。
repository_storage string 按代码片段使用的存储库存储进行筛选 (仅限管理员)。在极狐GitLab 16.3 中引入

示例请求:

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

示例响应:

[
  {
    "id": 113,
    "title": "Internal Project Snippet",
    "description": null,
    "visibility": "internal",
    "imported": false,
    "imported_from": "none",
    "author": {
      "id": 17,
      "username": "tim_kreiger",
      "name": "Tim Kreiger",
      "state": "active",
      "avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
      "web_url": "http://example.com/tim_kreiger"
    },
    "created_at": "2023-08-03T10:21:02.480Z",
    "updated_at": "2023-08-03T10:21:02.480Z",
    "project_id": 35,
    "web_url": "http://example.com/tim_kreiger/internal_project/-/snippets/113",
    "raw_url": "http://example.com/tim_kreiger/internal_project/-/snippets/113/raw",
    "file_name": "",
    "files": [],
    "repository_storage": "default"
  },
  {
    "id": 112,
    "title": "Private Personal Snippet",
    "description": null,
    "visibility": "private",
    "imported": false,
    "imported_from": "none",
    "author": {
      "id": 1,
      "username": "root",
      "name": "Administrator",
      "state": "active",
      "avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
      "web_url": "http://example.com/root"
    },
    "created_at": "2023-08-03T10:20:59.994Z",
    "updated_at": "2023-08-03T10:20:59.994Z",
    "project_id": null,
    "web_url": "http://example.com/-/snippets/112",
    "raw_url": "http://example.com/-/snippets/112/raw",
    "file_name": "",
    "files": [],
    "repository_storage": "default"
  },
  {
    "id": 111,
    "title": "Public Personal Snippet",
    "description": null,
    "visibility": "public",
    "imported": false,
    "imported_from": "none",
    "author": {
      "id": 17,
      "username": "tim_kreiger",
      "name": "Tim Kreiger",
      "state": "active",
      "avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon",
      "web_url": "http://example.com/tim_kreiger"
    },
    "created_at": "2023-08-03T10:21:01.312Z",
    "updated_at": "2023-08-03T10:21:01.312Z",
    "project_id": null,
    "web_url": "http://example.com/-/snippets/111",
    "raw_url": "http://example.com/-/snippets/111/raw",
    "file_name": "",
    "files": [],
    "repository_storage": "default"
  },
]

获取用户代理详情

{{< alert type=”note” >}}

仅管理员可用。

{{< /alert >}}

GET /snippets/:id/user_agent_detail
属性 类型 必需 描述
id integer 代码片段 ID。

示例请求:

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

示例响应:

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