rosa-build/app/controllers/api/v1/issues_controller.rb

117 lines
3.7 KiB
Ruby
Raw Normal View History

2013-04-12 17:52:56 +01:00
class Api::V1::IssuesController < Api::V1::BaseController
2013-04-15 13:32:11 +01:00
respond_to :json
2013-04-12 17:52:56 +01:00
before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:index, :group_index, :show] if APP_CONFIG['anonymous_access']
2013-04-12 17:52:56 +01:00
2013-06-18 21:00:01 +01:00
load_and_authorize_resource :group, :only => :group_index, :find_by => :id, :parent => false
load_and_authorize_resource :project
load_and_authorize_resource :issue, :through => :project, :find_by => :serial_id, :only => [:show, :update, :create, :index]
2013-04-12 17:52:56 +01:00
def index
@issues = @project.issues
render_issues_list
end
def all_index
project_ids = get_all_project_ids Project.accessible_by(current_ability, :membered).pluck(:id)
@issues = Issue.where(:project_id => project_ids)
2013-04-12 17:52:56 +01:00
render_issues_list
end
def user_index
project_ids = get_all_project_ids current_user.projects.pluck(:id)
@issues = Issue.where(:project_id => project_ids)
2013-04-12 17:52:56 +01:00
render_issues_list
end
def group_index
project_ids = @group.projects.pluck(:id)
project_ids = Project.accessible_by(current_ability, :membered).where(:id => project_ids).pluck(:id)
2013-04-12 17:52:56 +01:00
@issues = Issue.where(:project_id => project_ids)
render_issues_list
end
def show
redirect_to api_v1_project_pull_request_path(@project.id, @issue.serial_id) if @issue.pull_request
2013-04-12 17:52:56 +01:00
end
def create
2013-04-15 20:21:11 +01:00
@issue.user = current_user
2013-08-19 11:53:12 +01:00
@issue.assignee = nil if cannot?(:write, @project)
2013-04-12 17:52:56 +01:00
create_subject @issue
end
def update
2013-06-18 21:00:01 +01:00
unless can?(:write, @project)
params.delete :update_labels
[:assignee_id, :labelings, :labelings_attributes].each do |k|
params[:issue].delete k
end if params[:issue]
end
@issue.labelings.destroy_all if params[:update_labels]
if params[:issue] && status = params[:issue].delete(:status)
2013-06-18 21:00:01 +01:00
@issue.set_close(current_user) if status == 'closed'
@issue.set_open if status == 'open'
end
2013-04-12 17:52:56 +01:00
update_subject @issue
end
private
def render_issues_list
@issues = @issues.includes(:user, :assignee, :labels).without_pull_requests
if params[:status] == 'closed'
@issues = @issues.closed
else
@issues = @issues.opened
end
2013-04-12 17:52:56 +01:00
if action_name == 'index' && params[:assignee].present?
case params[:assignee]
when 'none'
@issues = @issues.where(:assigned_id => nil)
when '*'
@issues = @issues.where('issues.assigned_id IS NOT NULL')
2013-04-12 17:52:56 +01:00
else
@issues = @issues.where('issues.assignees_issues.uname = ?', params[:assignee])
2013-04-12 17:52:56 +01:00
end
end
if %w[all_index user_index group_index].include?(action_name)
case params[:filter]
when 'created'
@issues = @issues.where(:user_id => current_user)
when 'all'
else
@issues = @issues.where(:assignee_id => current_user)
end
else
@issues.where('users.uname = ?', params[:creator]) if params[:creator].present?
end
if params[:labels].present?
labels = params[:labels].split(',').map {|e| e.strip}.select {|e| e.present?}
@issues = @issues.where('labels.name IN (?)', labels)
end
sort = params[:sort] == 'updated' ? 'issues.updated_at' : 'issues.created_at'
direction = params[:direction] == 'asc' ? 'ASC' : 'DESC'
@issues = @issues.order("#{sort} #{direction}")
@issues = @issues.where('issues.created_at >= to_timestamp(?)', params[:since]) if params[:since] =~ /\A\d+\z/
@issues = @issues.paginate(paginate_params)
2013-06-06 07:59:38 +01:00
render :index
2013-04-12 17:52:56 +01:00
end
def get_all_project_ids default_project_ids
2013-04-17 18:32:04 +01:00
project_ids = []
if ['created', 'all'].include? params[:filter]
# add own issues
project_ids = Project.accessible_by(current_ability, :show).joins(:issues).
where(:issues => {:user_id => current_user.id}).pluck('projects.id')
2013-04-12 17:52:56 +01:00
end
project_ids |= default_project_ids
2013-04-12 17:52:56 +01:00
end
end