From 01a721bfccdcfe5de267d82ec0c7a965f217fb02 Mon Sep 17 00:00:00 2001 From: Alexander Machehin Date: Fri, 12 Apr 2013 22:52:56 +0600 Subject: [PATCH] [#42] issues api --- app/controllers/api/v1/issues_controller.rb | 100 +++++++++++++++++++ app/views/api/v1/issues/_issue.json.jbuilder | 11 ++ app/views/api/v1/issues/_label.json.jbuilder | 1 + app/views/api/v1/issues/index.json.jbuilder | 11 ++ config/routes.rb | 4 + 5 files changed, 127 insertions(+) create mode 100644 app/controllers/api/v1/issues_controller.rb create mode 100644 app/views/api/v1/issues/_issue.json.jbuilder create mode 100644 app/views/api/v1/issues/_label.json.jbuilder create mode 100644 app/views/api/v1/issues/index.json.jbuilder diff --git a/app/controllers/api/v1/issues_controller.rb b/app/controllers/api/v1/issues_controller.rb new file mode 100644 index 000000000..f575c363d --- /dev/null +++ b/app/controllers/api/v1/issues_controller.rb @@ -0,0 +1,100 @@ +# -*- encoding : utf-8 -*- +class Api::V1::IssuesController < Api::V1::BaseController + + before_filter :authenticate_user! + skip_before_filter :authenticate_user!, :only => [:show] if APP_CONFIG['anonymous_access'] + + load_and_authorize_resource :group, :only => :group_index + load_resource :project + load_and_authorize_resource :issue, :through => :project, :find_by => :serial_id, :only => [:show, :update, :destroy, :create, :index] + + def index + @issues = @project.issues + render_issues_list + end + + def all_index + project_ids = Project.accessible_by(current_ability, :membered).pluck(:id) + @issues = Issue.where(:project_id => project_ids) + render_issues_list + end + + def user_index + project_ids = current_user.projects.pluck(:id) + @issues = Issue.where(:project_id => project_ids) + render_issues_list + end + + def group_index + project_ids = @group.projects.pluck(:id) + @issues = Issue.where(:project_id => project_ids) + render_issues_list + end + + def show + end + + def create + create_subject @issue + end + + def update + update_subject @issue + end + + def destroy + destroy_subject @issue + end + + private + + def render_issues_list + @issues = @issues.includes(:user, :assignee, :labels).without_pull_requests + @issues = @issues.opened if params[:status] == 'open' + @issues = @issues.closed if params[:status] == 'closed' + + if action_name == 'index' && params[:assignee].present? + case params[:assignee] + when 'none' + @issues = @issues.where(:assigned_id => nil) + when '*' + @issues = @issues.where('assigned_id IS NOT NULL') + else + @issues = @issues.where('assignees_issues.uname = ?', params[:assignee]) + 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('created_at >= to_timestamp(?)', params[:since]) if params[:since] =~ /\A\d+\z/ + @issues.paginate(paginate_params) + render 'index' + end + + 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 diff --git a/app/views/api/v1/issues/_issue.json.jbuilder b/app/views/api/v1/issues/_issue.json.jbuilder new file mode 100644 index 000000000..7db24740b --- /dev/null +++ b/app/views/api/v1/issues/_issue.json.jbuilder @@ -0,0 +1,11 @@ +json.number issue.serial_id +json.(issue, :title, :status) +json.labels issue.labels do |json_labels, label| + json.partial! 'label', :label => label, :json => json_labels +end +json.assignee do |json_assignee| + json.partial! 'api/v1/shared/member', :member => issue.assignee, :tag => json_assignee +end if issue.assignee + +json.url api_v1_project_issue_path(issue.project.id, issue.serial_id, :format => :json) + diff --git a/app/views/api/v1/issues/_label.json.jbuilder b/app/views/api/v1/issues/_label.json.jbuilder new file mode 100644 index 000000000..fdee26e0d --- /dev/null +++ b/app/views/api/v1/issues/_label.json.jbuilder @@ -0,0 +1 @@ +json.(label, :name, :color) diff --git a/app/views/api/v1/issues/index.json.jbuilder b/app/views/api/v1/issues/index.json.jbuilder new file mode 100644 index 000000000..85e2ffb00 --- /dev/null +++ b/app/views/api/v1/issues/index.json.jbuilder @@ -0,0 +1,11 @@ +json.issues @issues do |json, issue| + json.partial! 'issue', :issue => issue, :json => json + json.issue issue.body + json.partial! 'api/v1/shared/owner', :owner => issue.user + json.closed_at issue.closed_at.to_i + json.closed_by do |json_user| + json.partial! 'api/v1/shared/member', :member => issue.closer, :tag => json_user + end if issue.closer + json.created_at issue.created_at.to_i + json.updated_at issue.updated_at.to_i +end diff --git a/config/routes.rb b/config/routes.rb index d1fa562ed..aa52742d1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -58,6 +58,7 @@ Rosa::Application.routes.draw do put :update_member } resources :build_lists, :only => :index + resources :issues, :only => [:index, :create, :show, :update] end resources :users, :only => [:show] get 'user' => 'users#show_current_user' @@ -65,6 +66,7 @@ Rosa::Application.routes.draw do member { get :notifiers put :notifiers + get '/issues' => 'issues#user_index' } end resources :groups, :only => [:index, :show, :update, :create, :destroy] do @@ -73,6 +75,7 @@ Rosa::Application.routes.draw do put :add_member delete :remove_member put :update_member + get '/issues' => 'issues#group_index' } end resources :products, :only => [:show, :update, :create, :destroy] do @@ -82,6 +85,7 @@ Rosa::Application.routes.draw do put :cancel, :on => :member end #resources :ssh_keys, :only => [:index, :create, :destroy] + get 'issues' => 'issues#all_index' end end