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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
---
stage: Verify
group: Pipeline Execution
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---
# Merge Trains API
DETAILS:
**Tier:** Premium, Ultimate
**Offering:** GitLab.com, Self-managed, GitLab Dedicated
Every API call for [merge train](../ci/pipelines/merge_trains.md) must be authenticated with at least the Developer [role](../user/permissions.md).
If a user is not a member of a project and the project is private, a `GET` request on that project returns a `404` status code.
If Merge Trains is not available for the project, a `403` status code is returned.
## Merge Trains API pagination
By default, `GET` requests return 20 results at a time because the API results
are paginated.
For more information, see [pagination](rest/index.md#pagination).
## List Merge Trains for a project
Get all Merge Trains of the requested project:
```plaintext
GET /projects/:id/merge_trains
GET /projects/:id/merge_trains?scope=complete
```
| Attribute | Type | Required | Description |
|-----------|----------------|----------|-------------|
| `id` | integer/string | Yes | The ID or [URL-encoded path of the project](rest/index.md#namespaced-paths). |
| `scope` | string | No | Return Merge Trains filtered by the given scope. Available scopes are `active` (to be merged) and `complete` (have been merged). |
| `sort` | string | No | Return Merge Trains sorted in `asc` or `desc` order. Default: `desc`. |
```shell
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/merge_trains"
```
Example response:
```json
[
{
"id": 110,
"merge_request": {
"id": 126,
"iid": 59,
"project_id": 20,
"title": "Test MR 1580978354",
"description": "",
"state": "merged",
"created_at": "2020-02-06T08:39:14.883Z",
"updated_at": "2020-02-06T08:40:57.038Z",
"web_url": "http://local.gitlab.test:8181/root/merge-train-race-condition/-/merge_requests/59"
},
"user": {
"id": 1,
"name": "Administrator",
"username": "root",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
"web_url": "http://local.gitlab.test:8181/root"
},
"pipeline": {
"id": 246,
"sha": "bcc17a8ffd51be1afe45605e714085df28b80b13",
"ref": "refs/merge-requests/59/train",
"status": "success",
"created_at": "2020-02-06T08:40:42.410Z",
"updated_at": "2020-02-06T08:40:46.912Z",
"web_url": "http://local.gitlab.test:8181/root/merge-train-race-condition/pipelines/246"
},
"created_at": "2020-02-06T08:39:47.217Z",
"updated_at": "2020-02-06T08:40:57.720Z",
"target_branch": "feature-1580973432",
"status": "merged",
"merged_at": "2020-02-06T08:40:57.719Z",
"duration": 70
}
]
```
## List merge requests in a merge train
Get all merge requests added to a merge train for the requested target branch.
```plaintext
GET /projects/:id/merge_trains/:target_branch
```
Supported attributes:
| Attribute | Type | Required | Description |
|-----------------|----------------|----------|-------------|
| `id` | integer/string | Yes | The ID or [URL-encoded path of the project](rest/index.md#namespaced-paths). |
| `target_branch` | string | Yes | The target branch of the merge train. |
| `scope` | string | No | Return Merge Trains filtered by the given scope. Available scopes are `active` (to be merged) and `complete` (have been merged). |
| `sort` | string | No | Return Merge Trains sorted in `asc` or `desc` order. Default: `desc`. |
Example request:
```shell
curl --request GET --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/597/merge_trains/main"
```
Example response:
```json
[
{
"id": 267,
"merge_request": {
"id": 273,
"iid": 1,
"project_id": 597,
"title": "My title 9",
"description": null,
"state": "opened",
"created_at": "2022-10-31T19:06:05.725Z",
"updated_at": "2022-10-31T19:06:05.725Z",
"web_url": "http://localhost/namespace18/project21/-/merge_requests/1"
},
"user": {
"id": 933,
"username": "user12",
"name": "Sidney Jones31",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon",
"web_url": "http://localhost/user12"
},
"pipeline": {
"id": 273,
"iid": 1,
"project_id": 598,
"sha": "b83d6e391c22777fca1ed3012fce84f633d7fed0",
"ref": "main",
"status": "pending",
"source": "push",
"created_at": "2022-10-31T19:06:06.231Z",
"updated_at": "2022-10-31T19:06:06.231Z",
"web_url": "http://localhost/namespace19/project22/-/pipelines/273"
},
"created_at": "2022-10-31T19:06:06.237Z",
"updated_at":"2022-10-31T19:06:06.237Z",
"target_branch":"main",
"status":"idle",
"merged_at":null,
"duration":null
}
]
```
## Get the status of a merge request on a merge train
Get merge train information for the requested merge request.
```plaintext
GET /projects/:id/merge_trains/merge_requests/:merge_request_iid
```
Supported attributes:
| Attribute | Type | Required | Description |
|---------------------|----------------|----------|-------------|
| `id` | integer/string | Yes | The ID or [URL-encoded path of the project](rest/index.md#namespaced-paths). |
| `merge_request_iid` | integer | Yes | The internal ID of the merge request. |
Example request:
```shell
curl --request GET --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/597/merge_trains/merge_requests/1"
```
Example response:
```json
{
"id": 267,
"merge_request": {
"id": 273,
"iid": 1,
"project_id": 597,
"title": "My title 9",
"description": null,
"state": "opened",
"created_at": "2022-10-31T19:06:05.725Z",
"updated_at": "2022-10-31T19:06:05.725Z",
"web_url": "http://localhost/namespace18/project21/-/merge_requests/1"
},
"user": {
"id": 933,
"username": "user12",
"name": "Sidney Jones31",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon",
"web_url": "http://localhost/user12"
},
"pipeline": {
"id": 273,
"iid": 1,
"project_id": 598,
"sha": "b83d6e391c22777fca1ed3012fce84f633d7fed0",
"ref": "main",
"status": "pending",
"source": "push",
"created_at": "2022-10-31T19:06:06.231Z",
"updated_at": "2022-10-31T19:06:06.231Z",
"web_url": "http://localhost/namespace19/project22/-/pipelines/273"
},
"created_at": "2022-10-31T19:06:06.237Z",
"updated_at":"2022-10-31T19:06:06.237Z",
"target_branch":"main",
"status":"idle",
"merged_at":null,
"duration":null
}
```
## Add a merge request to a merge train
Add a merge request to the merge train targeting the merge request's target branch.
```plaintext
POST /projects/:id/merge_trains/merge_requests/:merge_request_iid
```
Supported attributes:
| Attribute | Type | Required | Description |
|--------------------------|----------------|----------|-------------|
| `id` | integer/string | Yes | The ID or [URL-encoded path of the project](rest/index.md#namespaced-paths). |
| `merge_request_iid` | integer | Yes | The internal ID of the merge request. |
| `sha` | string | No | If present, the SHA must match the `HEAD` of the source branch, otherwise the merge fails. |
| `squash` | boolean | No | If true, the commits are squashed into a single commit on merge. |
| `when_pipeline_succeeds` | boolean | No | If true, the merge request is added to the merge train when the pipeline succeeds. When false or unspecified, the merge request is added directly to the merge train. |
Example request:
```shell
curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/597/merge_trains/merge_requests/1"
```
Example response:
```json
[
{
"id": 267,
"merge_request": {
"id": 273,
"iid": 1,
"project_id": 597,
"title": "My title 9",
"description": null,
"state": "opened",
"created_at": "2022-10-31T19:06:05.725Z",
"updated_at": "2022-10-31T19:06:05.725Z",
"web_url": "http://localhost/namespace18/project21/-/merge_requests/1"
},
"user": {
"id": 933,
"username": "user12",
"name": "Sidney Jones31",
"state": "active",
"avatar_url": "https://www.gravatar.com/avatar/6c8365de387cb3db10ecc7b1880203c4?s=80\u0026d=identicon",
"web_url": "http://localhost/user12"
},
"pipeline": {
"id": 273,
"iid": 1,
"project_id": 598,
"sha": "b83d6e391c22777fca1ed3012fce84f633d7fed0",
"ref": "main",
"status": "pending",
"source": "push",
"created_at": "2022-10-31T19:06:06.231Z",
"updated_at": "2022-10-31T19:06:06.231Z",
"web_url": "http://localhost/namespace19/project22/-/pipelines/273"
},
"created_at": "2022-10-31T19:06:06.237Z",
"updated_at":"2022-10-31T19:06:06.237Z",
"target_branch":"main",
"status":"idle",
"merged_at":null,
"duration":null
}
]
```
|