2013-04-12 17:52:56 +01:00
|
|
|
# -*- encoding : utf-8 -*-
|
|
|
|
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!
|
2013-06-26 18:58:32 +01:00
|
|
|
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
|
2013-06-26 18:58:32 +01:00
|
|
|
project_ids = get_all_project_ids Project.accessible_by(current_ability, :membered).pluck(:id)
|
2013-07-17 14:53:14 +01:00
|
|
|
@issues = Issue.where(:project_id => project_ids)
|
2013-04-12 17:52:56 +01:00
|
|
|
render_issues_list
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_index
|
2013-06-26 18:58:32 +01:00
|
|
|
project_ids = get_all_project_ids current_user.projects.pluck(:id)
|
2013-07-17 14:53:14 +01:00
|
|
|
@issues = Issue.where(:project_id => project_ids)
|
2013-04-12 17:52:56 +01:00
|
|
|
render_issues_list
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_index
|
2013-06-26 18:58:32 +01:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
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
|
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]
|
2013-06-28 15:02:02 +01:00
|
|
|
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
|
2013-04-15 20:22:07 +01:00
|
|
|
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 '*'
|
2013-06-06 08:02:25 +01:00
|
|
|
@issues = @issues.where('issues.assigned_id IS NOT NULL')
|
2013-04-12 17:52:56 +01:00
|
|
|
else
|
2013-06-06 08:02:25 +01:00
|
|
|
@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}")
|
|
|
|
|
2013-06-06 08:02:25 +01:00
|
|
|
@issues = @issues.where('issues.created_at >= to_timestamp(?)', params[:since]) if params[:since] =~ /\A\d+\z/
|
2013-06-26 18:58:32 +01:00
|
|
|
@issues = @issues.paginate(paginate_params)
|
2013-06-06 07:59:38 +01:00
|
|
|
render :index
|
2013-04-12 17:52:56 +01:00
|
|
|
end
|
|
|
|
|
2013-04-18 13:31:42 +01:00
|
|
|
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).
|
2013-06-26 18:58:32 +01:00
|
|
|
where(:issues => {:user_id => current_user.id}).pluck('projects.id')
|
2013-04-12 17:52:56 +01:00
|
|
|
end
|
2013-04-18 13:31:42 +01:00
|
|
|
project_ids |= default_project_ids
|
2013-04-12 17:52:56 +01:00
|
|
|
end
|
|
|
|
end
|