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

113 lines
3.3 KiB
Ruby
Raw Normal View History

2013-04-12 17:52:56 +01:00
class Api::V1::IssuesController < Api::V1::BaseController
2015-03-19 23:31:41 +00:00
include Api::V1::Issueable
2015-03-04 23:19:19 +00:00
before_action :authenticate_user!
2015-03-19 23:31:41 +00:00
skip_before_action :authenticate_user!, only: %i(index group_index show) if APP_CONFIG['anonymous_access']
2013-04-12 17:52:56 +01:00
2015-03-19 23:31:41 +00:00
before_action :load_group, only: :group_index
before_action :load_project
skip_before_action :load_project, only: %i(all_index user_index group_index)
2015-03-26 00:26:24 +00:00
before_action :load_issue, only: %i(show update index)
2013-04-12 17:52:56 +01:00
def index
@issues = @project.issues
render_issues_list
end
def all_index
2015-03-19 23:31:41 +00:00
authorize :issue, :index?
project_ids = get_all_project_ids membered_projects.pluck(:id)
2014-01-21 04:51:49 +00:00
@issues = Issue.where(project_id: project_ids)
2013-04-12 17:52:56 +01:00
render_issues_list
end
def user_index
2015-03-19 23:31:41 +00:00
authorize :issue, :index?
project_ids = get_all_project_ids current_user.projects.pluck(:id)
2014-01-21 04:51:49 +00:00
@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)
2015-03-19 23:31:41 +00:00
project_ids = membered_projects.where(id: project_ids).pluck(:id)
2014-01-21 04:51:49 +00:00
@issues = Issue.where(project_id: project_ids)
2013-04-12 17:52:56 +01:00
render_issues_list
end
def show
2014-09-10 09:53:39 +01:00
if @issue.pull_request
redirect_to api_v1_project_pull_request_path(@project.id, @issue.serial_id)
else
respond_to :json
end
2013-04-12 17:52:56 +01:00
end
def create
@issue = @project.issues.new
@issue.assign_attributes subject_params(Issue, @issue)
2013-04-15 20:21:11 +01:00
@issue.user = current_user
2013-04-12 17:52:56 +01:00
create_subject @issue
end
def update
@issue.labelings.destroy_all if params[:update_labels] && policy(@project).write?
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.preload(:user, :assignee, :labels, :project).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'
2014-01-21 04:51:49 +00:00
@issues = @issues.where(assigned_id: nil)
2013-04-12 17:52:56 +01:00
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'
2014-01-21 04:51:49 +00:00
@issues = @issues.where(user_id: current_user)
2013-04-12 17:52:56 +01:00
when 'all'
else
2014-01-21 04:51:49 +00:00
@issues = @issues.where(assignee_id: current_user)
2013-04-12 17:52:56 +01:00
end
else
@issues.where('users.uname = ?', params[:creator]) if params[:creator].present?
end
if params[:labels].present?
2015-03-19 23:31:41 +00:00
labels = params[:labels].split(',').map(&:strip).select(&:present?)
2013-04-12 17:52:56 +01:00
@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)
2014-08-28 21:22:11 +01:00
respond_to do |format|
format.json { render :index }
end
2013-04-12 17:52:56 +01:00
end
end