303.md 3.1 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
# Custom Attributes API

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

*   [List custom attributes](#list-custom-attributes)
*   [Single custom attribute](#single-custom-attribute)
*   [Set custom attribute](#set-custom-attribute)
*   [Delete custom attribute](#delete-custom-attribute)

# Custom Attributes API[](#custom-attributes-api "Permalink")

每个对自定义属性的 API 调用都必须经过管理员身份验证.

自定义属性当前可用于用户,组和项目,在本文档中将其称为"资源".

## List custom attributes[](#list-custom-attributes "Permalink")

获取资源上的所有自定义属性.

```
GET /users/:id/custom_attributes
GET /groups/:id/custom_attributes
GET /projects/:id/custom_attributes 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 资源的 ID |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/42/custom_attributes" 
```

响应示例:

```
[  {  "key":  "location",  "value":  "Antarctica"  },  {  "key":  "role",  "value":  "Developer"  }  ] 
```

## Single custom attribute[](#single-custom-attribute "Permalink")

在资源上获取单个自定义属性.

```
GET /users/:id/custom_attributes/:key
GET /groups/:id/custom_attributes/:key
GET /projects/:id/custom_attributes/:key 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 资源的 ID |
| `key` | string | yes | 自定义属性的键 |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/42/custom_attributes/location" 
```

响应示例:

```
{  "key":  "location",  "value":  "Antarctica"  } 
```

## Set custom attribute[](#set-custom-attribute "Permalink")

在资源上设置自定义属性. 如果该属性已经存在,则将对其进行更新;否则,将重新创建该属性.

```
PUT /users/:id/custom_attributes/:key
PUT /groups/:id/custom_attributes/:key
PUT /projects/:id/custom_attributes/:key 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 资源的 ID |
| `key` | string | yes | 自定义属性的键 |
| `value` | string | yes | 自定义属性的值 |

```
curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" --data "value=Greenland" "https://gitlab.example.com/api/v4/users/42/custom_attributes/location" 
```

响应示例:

```
{  "key":  "location",  "value":  "Greenland"  } 
```

## Delete custom attribute[](#delete-custom-attribute "Permalink")

删除资源上的自定义属性.

```
DELETE /users/:id/custom_attributes/:key
DELETE /groups/:id/custom_attributes/:key
DELETE /projects/:id/custom_attributes/:key 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer | yes | 资源的 ID |
| `key` | string | yes | 自定义属性的键 |

```
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/users/42/custom_attributes/location" 
```