Skip to content

Commit

Permalink
[api] add routes to get XML view of comments
Browse files Browse the repository at this point in the history
issue 545
  • Loading branch information
coolo authored and adrianschroeter committed Feb 10, 2014
1 parent 1fd5a65 commit ba64d2f
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/api/app/controllers/comments_controller.rb
@@ -0,0 +1,37 @@
class CommentsController < ApplicationController

before_filter :find_request, only: [:show_request_comments]

def find_request
@obj = BsRequest.find(params[:id])
end

def show_request_comments
show_comments
end

before_filter :find_package, only: [:show_package_comments]

def find_package
@obj = Package.get_by_project_and_name(params[:project], params[:package])
end

def show_package_comments
show_comments
end

before_filter :find_project, only: [:show_project_comments]

def find_project
@obj = Project.get_by_name(params[:project])
end

def show_project_comments
show_comments
end

def show_comments
@comments = @obj.comments.order(:id)
end

end
8 changes: 8 additions & 0 deletions src/api/app/models/comment.rb
Expand Up @@ -39,4 +39,12 @@ def check_delete_permissions
User.current == self.user || self.user.is_nobody?
end

def to_xml(builder)
attrs = { who: self.user, when: self.created_at, id: self.id }
attrs[:parent] = self.parent_id if self.parent_id

builder.comment_(attrs) do
builder.text(self.body)
end
end
end
3 changes: 3 additions & 0 deletions src/api/app/views/comments/_comments.xml.builder
@@ -0,0 +1,3 @@
comments.each do |c|
c.to_xml(builder)
end
3 changes: 3 additions & 0 deletions src/api/app/views/comments/show_package_comments.xml.builder
@@ -0,0 +1,3 @@
xml.comments(project: @obj.project.name, package: @obj.name) do
render(partial: 'comments', locals: { builder: xml, comments: @comments })
end
3 changes: 3 additions & 0 deletions src/api/app/views/comments/show_project_comments.xml.builder
@@ -0,0 +1,3 @@
xml.comments(project: @obj.name) do
render(partial: 'comments', locals: { builder: xml, comments: @comments })
end
3 changes: 3 additions & 0 deletions src/api/app/views/comments/show_request_comments.xml.builder
@@ -0,0 +1,3 @@
xml.comments(request: @obj.id) do
render(partial: 'comments', locals: { builder: xml, comments: @comments })
end
8 changes: 8 additions & 0 deletions src/api/config/routes.rb
Expand Up @@ -651,6 +651,14 @@ def self.matches?(request)
delete 'source/:project/:package' => :delete_package, constraints: cons
end

controller :comments do

get 'comments/request/:id' => :show_request_comments, constraints: cons, as: :comments_request
get 'comments/package/:project/:package' => :show_package_comments, constraints: cons, as: :comments_package
get 'comments/project/:project' => :show_project_comments, constraints: cons, as: :comments_project

end

# this can be requested by non browsers (like HA proxies :)
get 'apidocs/:filename' => 'webui/apidocs#file', constraints: {filename: %r{[^\/]*}}, as: 'apidocs_file'

Expand Down
34 changes: 34 additions & 0 deletions src/api/test/functional/comments_controller_test.rb
@@ -0,0 +1,34 @@
require_relative '../test_helper'

class CommentsControllerTest < ActionDispatch::IntegrationTest

test 'package comments' do
get comments_package_path(project: 'BaseDistro3', package: 'pack2')
assert_response 401

login_tom
get comments_package_path(project: 'BaseDistro3', package: 'pack2')
assert_response :success

assert_xml_tag tag: 'comment', attributes: { who: 'tom' }

end

test 'hidden project comments' do
login_tom
get comments_project_path(project: 'HiddenProject')
assert_response 404 # huh? Nothing here

prepare_request_with_user "hidden_homer", "homer"
get comments_project_path(project: 'HiddenProject')
assert_response :success
end

test 'show request comments' do
login_tom
get comments_request_path(id: 4)
assert_response :success
assert_xml_tag tag: 'comment', attributes: { who: 'tom', parent: '300' }

end
end

0 comments on commit ba64d2f

Please sign in to comment.