BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
c33f09d2
Unverified
Commit
c33f09d2
authored
Mar 05, 2017
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update triggers API
parent
8a5557ce
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
311 additions
and
17 deletions
+311
-17
entities.rb
lib/api/entities.rb
+0
-4
triggers.rb
lib/api/triggers.rb
+57
-11
entities.rb
lib/api/v3/entities.rb
+8
-0
triggers.rb
lib/api/v3/triggers.rb
+75
-2
triggers_spec.rb
spec/requests/api/v3/triggers_spec.rb
+171
-0
No files found.
lib/api/entities.rb
View file @
c33f09d2
...
...
@@ -592,10 +592,6 @@ module API
end
end
class
TriggerRequest
<
Grape
::
Entity
expose
:id
,
:variables
end
class
Runner
<
Grape
::
Entity
expose
:id
expose
:description
...
...
lib/api/triggers.rb
View file @
c33f09d2
...
...
@@ -6,7 +6,7 @@ module API
requires
:id
,
type:
String
,
desc:
'The ID of a project'
end
resource
:projects
do
desc
'Trigger a GitLab project
build
'
do
desc
'Trigger a GitLab project
pipeline
'
do
success
Entities
::
TriggerRequest
end
params
do
...
...
@@ -14,7 +14,7 @@ module API
requires
:token
,
type:
String
,
desc:
'The unique token of trigger'
optional
:variables
,
type:
Hash
,
desc:
'The list of variables to be injected into build'
end
post
":id/(ref/:ref/)trigger/
builds
"
do
post
":id/(ref/:ref/)trigger/
pipeline
"
do
project
=
find_project
(
params
[
:id
])
trigger
=
Ci
::
Trigger
.
find_by_token
(
params
[
:token
].
to_s
)
not_found!
unless
project
&&
trigger
...
...
@@ -29,9 +29,9 @@ module API
# create request and trigger builds
trigger_request
=
Ci
::
CreateTriggerRequestService
.
new
.
execute
(
project
,
trigger
,
params
[
:ref
].
to_s
,
variables
)
if
trigger_request
present
trigger_request
,
with:
Entities
::
TriggerRequest
present
trigger_request
.
pipeline
,
with:
Entities
::
Pipeline
else
errors
=
'No
builds created
'
errors
=
'No
pipeline create
'
render_api_error!
(
errors
,
400
)
end
end
...
...
@@ -55,13 +55,13 @@ module API
success
Entities
::
Trigger
end
params
do
requires
:t
oken
,
type:
String
,
desc:
'The unique token of trigger
'
requires
:t
rigger_id
,
type:
Integer
,
desc:
'The trigger ID
'
end
get
':id/triggers/:t
oken
'
do
get
':id/triggers/:t
rigger_id
'
do
authenticate!
authorize!
:admin_build
,
user_project
trigger
=
user_project
.
triggers
.
find
_by
(
token:
params
[
:token
].
to_s
)
trigger
=
user_project
.
triggers
.
find
(
params
[
:trigger_id
]
)
return
not_found!
(
'Trigger'
)
unless
trigger
present
trigger
,
with:
Entities
::
Trigger
...
...
@@ -70,26 +70,72 @@ module API
desc
'Create a trigger'
do
success
Entities
::
Trigger
end
params
do
requires
:description
,
type:
String
,
desc:
'The trigger description'
end
post
':id/triggers'
do
authenticate!
authorize!
:admin_build
,
user_project
trigger
=
user_project
.
triggers
.
create
trigger
=
user_project
.
triggers
.
create
(
declared_params
(
include_missing:
false
).
merge
(
owner:
current_user
))
if
trigger
.
valid?
present
trigger
,
with:
Entities
::
Trigger
else
render_validation_error!
(
trigger
)
end
end
desc
'Update a trigger'
do
success
Entities
::
Trigger
end
params
do
requires
:trigger_id
,
type:
Integer
,
desc:
'The trigger ID'
optional
:description
,
type:
String
,
desc:
'The trigger description'
end
delete
':id/triggers/:trigger_id'
do
authenticate!
authorize!
:admin_build
,
user_project
trigger
=
user_project
.
triggers
.
find
(
params
[
:trigger_id
])
return
not_found!
(
'Trigger'
)
unless
trigger
trigger
=
trigger
.
update
(
declared_params
(
include_missing:
false
))
present
trigger
,
with:
Entities
::
Trigger
end
desc
'Take ownership of trigger'
do
success
Entities
::
Trigger
end
params
do
requires
:trigger_id
,
type:
Integer
,
desc:
'The trigger ID'
end
post
':id/triggers/:trigger_id/take'
do
authenticate!
authorize!
:admin_build
,
user_project
trigger
=
user_project
.
triggers
.
find
(
params
[
:trigger_id
])
return
not_found!
(
'Trigger'
)
unless
trigger
if
trigger
.
update
(
owner:
current_user
)
present
trigger
,
with:
Entities
::
Trigger
else
render_validation_error!
(
trigger
)
end
end
desc
'Delete a trigger'
do
success
Entities
::
Trigger
end
params
do
requires
:t
oken
,
type:
String
,
desc:
'The unique token of trigger
'
requires
:t
rigger_id
,
type:
Integer
,
desc:
'The trigger ID
'
end
delete
':id/triggers/:t
oken
'
do
delete
':id/triggers/:t
rigger_id
'
do
authenticate!
authorize!
:admin_build
,
user_project
trigger
=
user_project
.
triggers
.
find
_by
(
token:
params
[
:token
].
to_s
)
trigger
=
user_project
.
triggers
.
find
(
params
[
:trigger_id
]
)
return
not_found!
(
'Trigger'
)
unless
trigger
trigger
.
destroy
...
...
lib/api/v3/entities.rb
View file @
c33f09d2
...
...
@@ -186,6 +186,14 @@ module API
class
Environment
<
::
API
::
Entities
::
EnvironmentBasic
expose
:project
,
using:
Entities
::
Project
end
class
Trigger
<
Grape
::
Entity
expose
:token
,
:created_at
,
:updated_at
,
:deleted_at
,
:last_used
end
class
TriggerRequest
<
Grape
::
Entity
expose
:id
,
:variables
end
end
end
end
lib/api/v3/triggers.rb
View file @
c33f09d2
...
...
@@ -7,8 +7,81 @@ module API
requires
:id
,
type:
String
,
desc:
'The ID of a project'
end
resource
:projects
do
desc
'Trigger a GitLab project build'
do
success
::
API
::
V3
::
Entities
::
TriggerRequest
end
params
do
requires
:ref
,
type:
String
,
desc:
'The commit sha or name of a branch or tag'
requires
:token
,
type:
String
,
desc:
'The unique token of trigger'
optional
:variables
,
type:
Hash
,
desc:
'The list of variables to be injected into build'
end
post
":id/(ref/:ref/)trigger/builds"
do
project
=
find_project
(
params
[
:id
])
trigger
=
Ci
::
Trigger
.
find_by_token
(
params
[
:token
].
to_s
)
not_found!
unless
project
&&
trigger
unauthorized!
unless
trigger
.
project
==
project
# validate variables
variables
=
params
[
:variables
].
to_h
unless
variables
.
all?
{
|
key
,
value
|
key
.
is_a?
(
String
)
&&
value
.
is_a?
(
String
)
}
render_api_error!
(
'variables needs to be a map of key-valued strings'
,
400
)
end
# create request and trigger builds
trigger_request
=
Ci
::
CreateTriggerRequestService
.
new
.
execute
(
project
,
trigger
,
params
[
:ref
].
to_s
,
variables
)
if
trigger_request
present
trigger_request
,
with:
::
API
::
V3
::
Entities
::
TriggerRequest
else
errors
=
'No builds created'
render_api_error!
(
errors
,
400
)
end
end
desc
'Get triggers list'
do
success
::
API
::
V3
::
Entities
::
Trigger
end
params
do
use
:pagination
end
get
':id/triggers'
do
authenticate!
authorize!
:admin_build
,
user_project
triggers
=
user_project
.
triggers
.
includes
(
:trigger_requests
)
present
paginate
(
triggers
),
with:
::
API
::
V3
::
Entities
::
Trigger
end
desc
'Get specific trigger of a project'
do
success
::
API
::
V3
::
Entities
::
Trigger
end
params
do
requires
:token
,
type:
String
,
desc:
'The unique token of trigger'
end
get
':id/triggers/:token'
do
authenticate!
authorize!
:admin_build
,
user_project
trigger
=
user_project
.
triggers
.
find_by
(
token:
params
[
:token
].
to_s
)
return
not_found!
(
'Trigger'
)
unless
trigger
present
trigger
,
with:
::
API
::
V3
::
Entities
::
Trigger
end
desc
'Create a trigger'
do
success
::
API
::
V3
::
Entities
::
Trigger
end
post
':id/triggers'
do
authenticate!
authorize!
:admin_build
,
user_project
trigger
=
user_project
.
triggers
.
create
present
trigger
,
with:
::
API
::
V3
::
Entities
::
Trigger
end
desc
'Delete a trigger'
do
success
::
API
::
Entities
::
Trigger
success
::
API
::
V3
::
Entities
::
Trigger
end
params
do
requires
:token
,
type:
String
,
desc:
'The unique token of trigger'
...
...
@@ -22,7 +95,7 @@ module API
trigger
.
destroy
present
trigger
,
with:
::
API
::
Entities
::
Trigger
present
trigger
,
with:
::
API
::
V3
::
Entities
::
Trigger
end
end
end
...
...
spec/requests/api/v3/triggers_spec.rb
View file @
c33f09d2
...
...
@@ -11,6 +11,177 @@ describe API::V3::Triggers do
let!
(
:developer
)
{
create
(
:project_member
,
:developer
,
user:
user2
,
project:
project
)
}
let!
(
:trigger
)
{
create
(
:ci_trigger
,
project:
project
,
token:
trigger_token
)
}
describe
'POST /projects/:project_id/trigger'
do
let!
(
:project2
)
{
create
(
:project
)
}
let
(
:options
)
do
{
token:
trigger_token
}
end
before
do
stub_ci_pipeline_to_return_yaml_file
end
context
'Handles errors'
do
it
'returns bad request if token is missing'
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/trigger/builds"
),
ref:
'master'
expect
(
response
).
to
have_http_status
(
400
)
end
it
'returns not found if project is not found'
do
post
v3_api
(
'/projects/0/trigger/builds'
),
options
.
merge
(
ref:
'master'
)
expect
(
response
).
to
have_http_status
(
404
)
end
it
'returns unauthorized if token is for different project'
do
post
v3_api
(
"/projects/
#{
project2
.
id
}
/trigger/builds"
),
options
.
merge
(
ref:
'master'
)
expect
(
response
).
to
have_http_status
(
401
)
end
end
context
'Have a commit'
do
let
(
:pipeline
)
{
project
.
pipelines
.
last
}
it
'creates builds'
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/trigger/builds"
),
options
.
merge
(
ref:
'master'
)
expect
(
response
).
to
have_http_status
(
201
)
pipeline
.
builds
.
reload
expect
(
pipeline
.
builds
.
pending
.
size
).
to
eq
(
2
)
expect
(
pipeline
.
builds
.
size
).
to
eq
(
5
)
end
it
'creates builds on webhook from other gitlab repository and branch'
do
expect
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/ref/master/trigger/builds?token=
#{
trigger_token
}
"
),
{
ref:
'refs/heads/other-branch'
}
end
.
to
change
(
project
.
builds
,
:count
).
by
(
5
)
expect
(
response
).
to
have_http_status
(
201
)
end
it
'returns bad request with no builds created if there\'s no commit for that ref'
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/trigger/builds"
),
options
.
merge
(
ref:
'other-branch'
)
expect
(
response
).
to
have_http_status
(
400
)
expect
(
json_response
[
'message'
]).
to
eq
(
'No builds created'
)
end
context
'Validates variables'
do
let
(
:variables
)
do
{
'TRIGGER_KEY'
=>
'TRIGGER_VALUE'
}
end
it
'validates variables to be a hash'
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/trigger/builds"
),
options
.
merge
(
variables:
'value'
,
ref:
'master'
)
expect
(
response
).
to
have_http_status
(
400
)
expect
(
json_response
[
'error'
]).
to
eq
(
'variables is invalid'
)
end
it
'validates variables needs to be a map of key-valued strings'
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/trigger/builds"
),
options
.
merge
(
variables:
{
key:
%w(1 2)
},
ref:
'master'
)
expect
(
response
).
to
have_http_status
(
400
)
expect
(
json_response
[
'message'
]).
to
eq
(
'variables needs to be a map of key-valued strings'
)
end
it
'creates trigger request with variables'
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/trigger/builds"
),
options
.
merge
(
variables:
variables
,
ref:
'master'
)
expect
(
response
).
to
have_http_status
(
201
)
pipeline
.
builds
.
reload
expect
(
pipeline
.
builds
.
first
.
trigger_request
.
variables
).
to
eq
(
variables
)
end
end
end
end
describe
'GET /projects/:id/triggers'
do
context
'authenticated user with valid permissions'
do
it
'returns list of triggers'
do
get
v3_api
(
"/projects/
#{
project
.
id
}
/triggers"
,
user
)
expect
(
response
).
to
have_http_status
(
200
)
expect
(
response
).
to
include_pagination_headers
expect
(
json_response
).
to
be_a
(
Array
)
expect
(
json_response
[
0
]).
to
have_key
(
'token'
)
end
end
context
'authenticated user with invalid permissions'
do
it
'does not return triggers list'
do
get
v3_api
(
"/projects/
#{
project
.
id
}
/triggers"
,
user2
)
expect
(
response
).
to
have_http_status
(
403
)
end
end
context
'unauthenticated user'
do
it
'does not return triggers list'
do
get
v3_api
(
"/projects/
#{
project
.
id
}
/triggers"
)
expect
(
response
).
to
have_http_status
(
401
)
end
end
end
describe
'GET /projects/:id/triggers/:token'
do
context
'authenticated user with valid permissions'
do
it
'returns trigger details'
do
get
v3_api
(
"/projects/
#{
project
.
id
}
/triggers/
#{
trigger
.
token
}
"
,
user
)
expect
(
response
).
to
have_http_status
(
200
)
expect
(
json_response
).
to
be_a
(
Hash
)
end
it
'responds with 404 Not Found if requesting non-existing trigger'
do
get
v3_api
(
"/projects/
#{
project
.
id
}
/triggers/abcdef012345"
,
user
)
expect
(
response
).
to
have_http_status
(
404
)
end
end
context
'authenticated user with invalid permissions'
do
it
'does not return triggers list'
do
get
v3_api
(
"/projects/
#{
project
.
id
}
/triggers/
#{
trigger
.
token
}
"
,
user2
)
expect
(
response
).
to
have_http_status
(
403
)
end
end
context
'unauthenticated user'
do
it
'does not return triggers list'
do
get
v3_api
(
"/projects/
#{
project
.
id
}
/triggers/
#{
trigger
.
token
}
"
)
expect
(
response
).
to
have_http_status
(
401
)
end
end
end
describe
'POST /projects/:id/triggers'
do
context
'authenticated user with valid permissions'
do
it
'creates trigger'
do
expect
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/triggers"
,
user
)
end
.
to
change
{
project
.
triggers
.
count
}.
by
(
1
)
expect
(
response
).
to
have_http_status
(
201
)
expect
(
json_response
).
to
be_a
(
Hash
)
end
end
context
'authenticated user with invalid permissions'
do
it
'does not create trigger'
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/triggers"
,
user2
)
expect
(
response
).
to
have_http_status
(
403
)
end
end
context
'unauthenticated user'
do
it
'does not create trigger'
do
post
v3_api
(
"/projects/
#{
project
.
id
}
/triggers"
)
expect
(
response
).
to
have_http_status
(
401
)
end
end
end
describe
'DELETE /projects/:id/triggers/:token'
do
context
'authenticated user with valid permissions'
do
it
'deletes trigger'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment