Merge pull request #875 from warpc/859-actualization-ABF-api

[refs #859]: ABF API: add contrainer create api call, change format of answer for publish, cancel and reject api calls
This commit is contained in:
Vladimir Sharshov 2013-01-31 07:07:49 -08:00
commit 3f3edfe1d9
7 changed files with 140 additions and 63 deletions

View File

@ -5,7 +5,7 @@ class Api::V1::BuildListsController < Api::V1::BaseController
skip_before_filter :authenticate_user!, :only => [:show, :index] if APP_CONFIG['anonymous_access']
load_and_authorize_resource :project, :only => :index
load_and_authorize_resource :build_list, :only => [:show, :create, :cancel, :publish, :reject_publish]
load_and_authorize_resource :build_list, :only => [:show, :create, :cancel, :publish, :reject_publish, :create_container]
def index
filter = BuildList::Filter.new(@project, current_user, params[:filter] || {})
@ -15,26 +15,17 @@ class Api::V1::BuildListsController < Api::V1::BaseController
def create
bl_params = params[:build_list] || {}
project = Project.where(:id => bl_params[:project_id]).first
save_to_repository = Repository.where(:id => bl_params[:save_to_repository_id]).first
if project && save_to_repository
if save_to_repository
bl_params[:save_to_platform_id] = save_to_repository.platform_id
bl_params[:auto_publish] = false unless save_to_repository.publish_without_qa?
@build_list = project.build_lists.build(bl_params)
@build_list.user = current_user
@build_list.priority = current_user.build_priority # User builds more priority than mass rebuild with zero priority
if @build_list.save
render :action => 'show'
else
render :json => {:message => "Validation Failed", :errors => @build_list.errors.messages}.to_json, :status => 422
end
else
render :json => {:message => "Bad Request"}.to_json, :status => 400
end
@build_list = current_user.build_lists.new(bl_params)
@build_list.priority = current_user.build_priority # User builds more priority than mass rebuild with zero priority
create_subject @build_list
end
def cancel
@ -49,17 +40,17 @@ class Api::V1::BuildListsController < Api::V1::BaseController
render_json :reject_publish
end
def create_container
render_json :create_container, :publish_container
end
private
def render_json(action_name)
res, message = if !@build_list.send "can_#{action_name}?"
[false, "Incorrect action for current build list"]
elsif @build_list.send(action_name)
[true, t("layout.build_lists.#{action_name}_success")]
else
[false, t("layout.build_lists.#{action_name}_fail")]
end
render :json => {:"is_#{action_name}ed" => res, :url => api_v1_build_list_path(@build_list, :format => :json), :message => message}
def render_json(action_name, action_method = nil)
if @build_list.try("can_#{action_name}?") && @build_list.send(action_method || action_name)
render_json_response @build_list, t("layout.build_lists.#{action_name}_success")
else
render_validation_error @build_list, t("layout.build_lists.#{action_name}_fail")
end
end
end

View File

@ -90,17 +90,19 @@ class Projects::BuildListsController < Projects::BaseController
end
def create_container
@build_list.publish_container
redirect_to :back, :notice => t('layout.build_lists.container_will_be_created')
if @build_list.publish_container
redirect_to :back, :notice => t('layout.build_lists.create_container_success')
else
redirect_to :back, :notice => t('layout.build_lists.create_container_fail')
end
end
def cancel
notice = if @build_list.cancel
t('layout.build_lists.will_be_canceled')
else
t('layout.build_lists.cancel_fail')
end
redirect_to :back, :notice => notice
if @build_list.cancel
redirect_to :back, :notice => t('layout.build_lists.will_be_canceled')
else
redirect_to :back, :notice => t('layout.build_lists.cancel_fail')
end
end
def log

View File

@ -223,7 +223,7 @@ class BuildList < ActiveRecord::Base
:do => :remove_container
event :publish_container do
transition [:waiting_for_publish, :container_failed_publish] => :container_publish
transition [:waiting_for_publish, :container_failed_publish] => :container_publish, :if => :success?
end
event :published_container do

View File

@ -63,16 +63,15 @@ en:
no_items_data: No data
show: Show
cancel: Cancel build
cancel_success: 'Build canceled'
will_be_canceled: 'Build will be canceled...'
container_will_be_created: 'Container will be created...'
creating: 'creating...'
publish_success: 'Build published'
cancel_fail: 'Errors during build cancelation!'
publish_fail: 'Errors during build publishing!'
create_container_success: 'Container is queued for creating'
create_container_fail: 'Errors during container creating!'
publish_success: 'Build is queued for publishing'
reject_publish_success: 'Publishing rejected'
publish_fail: 'Errors during build publishing!'
cancel_success: 'Build canceled'
cancel_fail: 'Errors during build cancelation!'
reject_publish_success: 'Publishing rejected'
reject_publish_fail: 'Rejecting publishing failed'
container_published: 'Container published in a repository'
action: Action

View File

@ -63,13 +63,14 @@ ru:
show: Просмотр
cancel: Отменить сборку
will_be_canceled: 'Сборка будет отменена...'
container_will_be_created: 'Контейнер будет создан...'
creating: 'создается...'
create_container_success: 'Контейнер поставлен в очередь на создание'
create_container_fail: 'При создании контейнера произошла ошибка!'
cancel_success: 'Сборка отменена.'
cancel_fail: 'При отмене сборки произошла ошибка!'
publish_success: 'Сборка поставлена в очередь на публикацию.'
reject_publish_success: 'Публикация отклонена'
publish_fail: 'При публикации сборки произошла ошибка!'
reject_publish_success: 'Публикация отклонена'
reject_publish_fail: 'Не удалось отклонить публикацию сборки'
container_published: 'Контейнер размещен в репозитории'
action: Действие

View File

@ -19,6 +19,7 @@ Rosa::Application.routes.draw do
put :publish
put :reject_publish
put :cancel
put :create_container
}
end
resources :arches, :only => [:index]

View File

@ -1,10 +1,6 @@
# -*- encoding : utf-8 -*-
require 'spec_helper'
def incorrect_action_message
'Incorrect action for current build list'
end
shared_examples_for 'show build list via api' do
it 'should be able to perform show action' do
get :show, @show_params
@ -39,6 +35,11 @@ shared_examples_for 'create build list via api' do
lambda { post :create, @create_params }.should change{ BuildList.count }.by(1)
end
it 'should return 200 response code' do
post :create, @create_params
response.should be_success
end
it 'should save correct commit_hash for branch based build' do
post :create, @create_params
#@project.build_lists.last.commit_hash.should == @project.repo.commits('master').last.id
@ -71,6 +72,21 @@ shared_examples_for 'not create build list via api' do
it 'should not create one more build list' do
lambda { post :create, @create_params }.should change{ BuildList.count }.by(0)
end
it 'should return 403 response code' do
post :create, @create_params
response.status.should == 403
end
end
shared_examples_for 'validation error via build list api' do |message|
it 'should return 422 response code' do
response.status.should == 422
end
it "should return correct json error message" do
response.body.should == { :build_list => {:id => nil, :message => message} }.to_json
end
end
describe Api::V1::BuildListsController do
@ -116,29 +132,33 @@ describe Api::V1::BuildListsController do
before(:each) {http_login(@owner_user)}
context "if it has :build_pending status" do
it "should return correct json message" do
before do
@build_list.update_column(:status, BuildList::BUILD_PENDING)
do_cancel
response.body.should == {:is_canceled => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.cancel_success')}.to_json
end
it "should return correct json message" do
response.body.should == { :build_list => {:id => @build_list.id, :message => I18n.t('layout.build_lists.cancel_success')} }.to_json
end
it 'should return 200 response code' do
response.should be_success
end
it "should cancel build list" do
@build_list.update_column(:status, BuildList::BUILD_PENDING)
do_cancel
@build_list.reload.status.should == BuildList::BUILD_CANCELING
end
end
context "if it has another status" do
it "should return correct json error message" do
before do
@build_list.update_column(:status, BuildList::PROJECT_VERSION_NOT_FOUND)
do_cancel
response.body.should == {:is_canceled => false, :url => api_v1_build_list_path(@build_list, :format => :json), :message => incorrect_action_message}.to_json
end
it_should_behave_like 'validation error via build list api', I18n.t('layout.build_lists.cancel_fail')
it "should not cancel build list" do
@build_list.update_column(:status, BuildList::PROJECT_VERSION_NOT_FOUND)
do_cancel
@build_list.reload.status.should == BuildList::PROJECT_VERSION_NOT_FOUND
end
end
@ -160,6 +180,65 @@ describe Api::V1::BuildListsController do
end
end
context "do create_container" do
def do_create_container
put :create_container, :id => @build_list, :format => :json
end
before { stub_redis }
context 'if user is project owner' do
before do
http_login(@owner_user)
end
context "if it has :success status" do
before do
@build_list.update_column(:status, BuildList::SUCCESS)
do_create_container
end
it "should return correct json message" do
response.body.should == { :build_list => {:id => @build_list.id, :message => I18n.t('layout.build_lists.create_container_success')} }.to_json
end
it 'should return 200 response code' do
response.should be_success
end
it "should create container" do
@build_list.reload.container_status.should == BuildList::BUILD_PUBLISH
end
end
context "if it has another status" do
before(:each) do
@build_list.update_column(:status, BuildList::BUILD_ERROR)
do_create_container
end
it_should_behave_like 'validation error via build list api', I18n.t('layout.build_lists.create_container_fail')
it "should not create container" do
@build_list.reload.container_status.should == BuildList::WAITING_FOR_RESPONSE
end
end
end
context 'if user is not project owner' do
before(:each) do
@build_list.update_column(:status, BuildList::SUCCESS)
do_create_container
end
it "should return access violation message" do
response.body.should == {"message" => "Access violation to this page!"}.to_json
end
it "should not create container" do
@build_list.reload.container_status.should == BuildList::WAITING_FOR_RESPONSE
end
end
end
context "do publish" do
def do_publish
put :publish, :id => @build_list, :format => :json
@ -174,7 +253,11 @@ describe Api::V1::BuildListsController do
context "if it has :failed_publish status" do
it "should return correct json message" do
response.body.should == {:is_published => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.publish_success')}.to_json
response.body.should == { :build_list => {:id => @build_list.id, :message => I18n.t('layout.build_lists.publish_success')} }.to_json
end
it 'should return 200 response code' do
response.should be_success
end
it "should cancel build list" do
@ -188,9 +271,7 @@ describe Api::V1::BuildListsController do
do_publish
end
it "should return correct json error message" do
response.body.should == {:is_published => false, :url => api_v1_build_list_path(@build_list, :format => :json), :message => incorrect_action_message}.to_json
end
it_should_behave_like 'validation error via build list api', I18n.t('layout.build_lists.publish_fail')
it "should not cancel build list" do
@build_list.reload.status.should == BuildList::PROJECT_VERSION_NOT_FOUND
@ -234,7 +315,11 @@ describe Api::V1::BuildListsController do
context "if it has :success status" do
it "should return correct json message" do
response.body.should == {:is_reject_published => true, :url => api_v1_build_list_path(@build_list, :format => :json), :message => I18n.t('layout.build_lists.reject_publish_success')}.to_json
response.body.should == { :build_list => {:id => @build_list.id, :message => I18n.t('layout.build_lists.reject_publish_success')} }.to_json
end
it 'should return 200 response code' do
response.should be_success
end
it "should reject publish build list" do
@ -248,9 +333,7 @@ describe Api::V1::BuildListsController do
do_reject_publish
end
it "should return correct json error message" do
response.body.should == {:is_reject_published => false, :url => api_v1_build_list_path(@build_list, :format => :json), :message => incorrect_action_message}.to_json
end
it_should_behave_like 'validation error via build list api', I18n.t('layout.build_lists.reject_publish_fail')
it "should not cancel build list" do
@build_list.reload.status.should == BuildList::PROJECT_VERSION_NOT_FOUND