Commit d9da81f7 authored by Tomasz Maczukin's avatar Tomasz Maczukin

Add triggers feature to API

parent 0a62d3b5
......@@ -365,5 +365,15 @@ module API
class TriggerRequest < Grape::Entity
expose :id, :variables
end
class Trigger < Grape::Entity
expose :id, :token, :created_at, :updated_at, :deleted_at
expose :last_used do |repo_obj, _options|
if repo_obj.respond_to?(:last_trigger_request)
request = repo_obj.last_trigger_request
request.created_at if request
end
end
end
end
end
......@@ -43,6 +43,24 @@ module API
render_api_error!(errors, 400)
end
end
# Get triggers list
#
# Parameters:
# id (required) - The ID of a project
# page (optional) - The page number for pagination
# per_page (optional) - The value of items per page to show
# Example Request:
# GET /projects/:id/triggers
get ':id/triggers' do
authenticate!
authorize_admin_project
triggers = user_project.triggers.includes(:trigger_requests)
triggers = paginate(triggers)
present triggers, with: Entities::Trigger
end
end
end
end
......@@ -3,6 +3,8 @@
FactoryGirl.define do
factory :ci_trigger_request, class: Ci::TriggerRequest do
factory :ci_trigger_request_with_variables do
trigger :ci_trigger
variables do
{
TRIGGER_KEY: 'TRIGGER_VALUE'
......
......@@ -3,11 +3,19 @@ require 'spec_helper'
describe API::API do
include ApiHelpers
describe 'POST /projects/:project_id/trigger' do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let!(:trigger_token) { 'secure token' }
let!(:project) { FactoryGirl.create(:project) }
let!(:project2) { FactoryGirl.create(:empty_project) }
let!(:trigger) { FactoryGirl.create(:ci_trigger, project: project, token: trigger_token) }
let!(:trigger_token_2) { 'secure token 2' }
let!(:project) { create(:project, creator_id: user.id) }
let!(:master) { create(:project_member, user: user, project: project, access_level: ProjectMember::MASTER) }
let!(:developer) { create(:project_member, user: user2, project: project, access_level: ProjectMember::DEVELOPER) }
let!(:trigger) { create(:ci_trigger, project: project, token: trigger_token) }
let!(:trigger2) { create(:ci_trigger, project: project, token: trigger_token_2) }
let!(:trigger_request) { create(:ci_trigger_request, trigger: trigger, created_at: '2015-01-01 12:13:14') }
describe 'POST /projects/:project_id/trigger' do
let!(:project2) { create(:empty_project) }
let(:options) do
{
token: trigger_token
......@@ -77,4 +85,33 @@ describe API::API do
end
end
end
describe 'GET /projects/:id/triggets' do
context 'authenticated user with valid permissions' do
it 'should return list of triggers' do
get api("/projects/#{project.id}/triggers", user)
expect(response.status).to eq(200)
expect(json_response).to be_a(Array)
expect(json_response[0]['token']).to eq(trigger_token)
expect(json_response[1]['token']).to eq(trigger_token_2)
end
end
context 'authenticated user with invalid permissions' do
it 'should not return triggers list' do
get api("/projects/#{project.id}/triggers", user2)
expect(response.status).to eq(403)
end
end
context 'unauthentikated user' do
it 'should not return triggers list' do
get api("/projects/#{project.id}/triggers")
expect(response.status).to eq(401)
end
end
end
end
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