Commit 140b51ce authored by Kamil Trzcinski's avatar Kamil Trzcinski

Introduce tests for pipeline triggers

parent ab972295
module TriggersHelper module TriggersHelper
def builds_trigger_url(project_id, ref: nil) def builds_trigger_url(project_id, ref: nil)
if ref.nil? if ref.nil?
"#{Settings.gitlab.url}/api/v3/projects/#{project_id}/trigger/builds" "#{Settings.gitlab.url}/api/v4/projects/#{project_id}/trigger/pipeline"
else else
"#{Settings.gitlab.url}/api/v3/projects/#{project_id}/ref/#{ref}/trigger/builds" "#{Settings.gitlab.url}/api/v4/projects/#{project_id}/ref/#{ref}/trigger/pipeline"
end end
end end
......
---
title: Introduce Pipeline Triggers that are user-aware
merge_request:
author:
...@@ -36,7 +36,7 @@ it will not trigger a job. ...@@ -36,7 +36,7 @@ it will not trigger a job.
To trigger a job you need to send a `POST` request to GitLab's API endpoint: To trigger a job you need to send a `POST` request to GitLab's API endpoint:
``` ```
POST /projects/:id/trigger/builds POST /projects/:id/trigger/pipeline
``` ```
The required parameters are the trigger's `token` and the Git `ref` on which The required parameters are the trigger's `token` and the Git `ref` on which
...@@ -71,7 +71,7 @@ To trigger a job from webhook of another project you need to add the following ...@@ -71,7 +71,7 @@ To trigger a job from webhook of another project you need to add the following
webhook url for Push and Tag push events: webhook url for Push and Tag push events:
``` ```
https://gitlab.example.com/api/v4/projects/:id/ref/:ref/trigger/builds?token=TOKEN https://gitlab.example.com/api/v4/projects/:id/ref/:ref/trigger/pipeline?token=TOKEN
``` ```
> **Note**: > **Note**:
...@@ -105,7 +105,7 @@ Using cURL you can trigger a rebuild with minimal effort, for example: ...@@ -105,7 +105,7 @@ Using cURL you can trigger a rebuild with minimal effort, for example:
curl --request POST \ curl --request POST \
--form token=TOKEN \ --form token=TOKEN \
--form ref=master \ --form ref=master \
https://gitlab.example.com/api/v4/projects/9/trigger/builds https://gitlab.example.com/api/v4/projects/9/trigger/pipeline
``` ```
In this case, the project with ID `9` will get rebuilt on `master` branch. In this case, the project with ID `9` will get rebuilt on `master` branch.
...@@ -114,7 +114,7 @@ Alternatively, you can pass the `token` and `ref` arguments in the query string: ...@@ -114,7 +114,7 @@ Alternatively, you can pass the `token` and `ref` arguments in the query string:
```bash ```bash
curl --request POST \ curl --request POST \
"https://gitlab.example.com/api/v4/projects/9/trigger/builds?token=TOKEN&ref=master" "https://gitlab.example.com/api/v4/projects/9/trigger/pipeline?token=TOKEN&ref=master"
``` ```
### Triggering a job within `.gitlab-ci.yml` ### Triggering a job within `.gitlab-ci.yml`
...@@ -128,7 +128,7 @@ need to add in project's A `.gitlab-ci.yml`: ...@@ -128,7 +128,7 @@ need to add in project's A `.gitlab-ci.yml`:
build_docs: build_docs:
stage: deploy stage: deploy
script: script:
- "curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v4/projects/9/trigger/builds" - "curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v4/projects/9/trigger/pipeline"
only: only:
- tags - tags
``` ```
...@@ -187,7 +187,7 @@ curl --request POST \ ...@@ -187,7 +187,7 @@ curl --request POST \
--form token=TOKEN \ --form token=TOKEN \
--form ref=master \ --form ref=master \
--form "variables[UPLOAD_TO_S3]=true" \ --form "variables[UPLOAD_TO_S3]=true" \
https://gitlab.example.com/api/v4/projects/9/trigger/builds https://gitlab.example.com/api/v4/projects/9/trigger/pipeline
``` ```
### Using webhook to trigger job ### Using webhook to trigger job
...@@ -195,7 +195,7 @@ curl --request POST \ ...@@ -195,7 +195,7 @@ curl --request POST \
You can add the following webhook to another project in order to trigger a job: You can add the following webhook to another project in order to trigger a job:
``` ```
https://gitlab.example.com/api/v4/projects/9/ref/master/trigger/builds?token=TOKEN&variables[UPLOAD_TO_S3]=true https://gitlab.example.com/api/v4/projects/9/ref/master/trigger/pipeline?token=TOKEN&variables[UPLOAD_TO_S3]=true
``` ```
### Using cron to trigger nightly jobs ### Using cron to trigger nightly jobs
...@@ -205,7 +205,7 @@ in conjunction with cron. The example below triggers a job on the `master` ...@@ -205,7 +205,7 @@ in conjunction with cron. The example below triggers a job on the `master`
branch of project with ID `9` every night at `00:30`: branch of project with ID `9` every night at `00:30`:
```bash ```bash
30 0 * * * curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v4/projects/9/trigger/builds 30 0 * * * curl --request POST --form token=TOKEN --form ref=master https://gitlab.example.com/api/v4/projects/9/trigger/pipeline
``` ```
[ci-229]: https://gitlab.com/gitlab-org/gitlab-ci/merge_requests/229 [ci-229]: https://gitlab.com/gitlab-org/gitlab-ci/merge_requests/229
...@@ -639,7 +639,9 @@ module API ...@@ -639,7 +639,9 @@ module API
end end
class Trigger < Grape::Entity class Trigger < Grape::Entity
expose :token, :created_at, :updated_at, :deleted_at, :last_used expose :token, :description
expose :created_at, :updated_at, :deleted_at, :last_used
expose :owner, using: Entities::UserBasic
end end
class Variable < Grape::Entity class Variable < Grape::Entity
......
...@@ -7,7 +7,7 @@ module API ...@@ -7,7 +7,7 @@ module API
end end
resource :projects do resource :projects do
desc 'Trigger a GitLab project pipeline' do desc 'Trigger a GitLab project pipeline' do
success Entities::TriggerRequest success Entities::Pipeline
end end
params do params do
requires :ref, type: String, desc: 'The commit sha or name of a branch or tag' requires :ref, type: String, desc: 'The commit sha or name of a branch or tag'
...@@ -31,7 +31,7 @@ module API ...@@ -31,7 +31,7 @@ module API
if trigger_request if trigger_request
present trigger_request.pipeline, with: Entities::Pipeline present trigger_request.pipeline, with: Entities::Pipeline
else else
errors = 'No pipeline create' errors = 'No pipeline created'
render_api_error!(errors, 400) render_api_error!(errors, 400)
end end
end end
...@@ -61,7 +61,7 @@ module API ...@@ -61,7 +61,7 @@ module API
authenticate! authenticate!
authorize! :admin_build, user_project authorize! :admin_build, user_project
trigger = user_project.triggers.find(params[:trigger_id]) trigger = user_project.triggers.find(params.delete(:trigger_id))
return not_found!('Trigger') unless trigger return not_found!('Trigger') unless trigger
present trigger, with: Entities::Trigger present trigger, with: Entities::Trigger
...@@ -94,15 +94,18 @@ module API ...@@ -94,15 +94,18 @@ module API
requires :trigger_id, type: Integer, desc: 'The trigger ID' requires :trigger_id, type: Integer, desc: 'The trigger ID'
optional :description, type: String, desc: 'The trigger description' optional :description, type: String, desc: 'The trigger description'
end end
delete ':id/triggers/:trigger_id' do put ':id/triggers/:trigger_id' do
authenticate! authenticate!
authorize! :admin_build, user_project authorize! :admin_build, user_project
trigger = user_project.triggers.find(params[:trigger_id]) trigger = user_project.triggers.find(params.delete(:trigger_id))
return not_found!('Trigger') unless trigger return not_found!('Trigger') unless trigger
trigger = trigger.update(declared_params(include_missing: false)) if trigger.update(declared_params(include_missing: false))
present trigger, with: Entities::Trigger present trigger, with: Entities::Trigger
else
render_validation_error!(trigger)
end
end end
desc 'Take ownership of trigger' do desc 'Take ownership of trigger' do
...@@ -115,10 +118,11 @@ module API ...@@ -115,10 +118,11 @@ module API
authenticate! authenticate!
authorize! :admin_build, user_project authorize! :admin_build, user_project
trigger = user_project.triggers.find(params[:trigger_id]) trigger = user_project.triggers.find(params.delete(:trigger_id))
return not_found!('Trigger') unless trigger return not_found!('Trigger') unless trigger
if trigger.update(owner: current_user) if trigger.update(owner: current_user)
status :ok
present trigger, with: Entities::Trigger present trigger, with: Entities::Trigger
else else
render_validation_error!(trigger) render_validation_error!(trigger)
...@@ -135,7 +139,7 @@ module API ...@@ -135,7 +139,7 @@ module API
authenticate! authenticate!
authorize! :admin_build, user_project authorize! :admin_build, user_project
trigger = user_project.triggers.find(params[:trigger_id]) trigger = user_project.triggers.find(params.delete(:trigger_id))
return not_found!('Trigger') unless trigger return not_found!('Trigger') unless trigger
trigger.destroy trigger.destroy
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment