Skip to content

Commit

Permalink
Merge pull request #8346 from adrianschroeter/fix_issue_tracker_api
Browse files Browse the repository at this point in the history
[api] fix editing of issue tracker entries
  • Loading branch information
vpereira authored Feb 28, 2020
2 parents 9a385a3 + c7680aa commit bfb8a1f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 70 deletions.
2 changes: 1 addition & 1 deletion docs/api/api/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ POST /issue_tracker/<name>

Create new issue tracker.

XmlResult: issue_tracker
XmlResult: status

DELETE /issue_tracker/<name>

Expand Down
97 changes: 32 additions & 65 deletions src/api/app/controllers/issue_trackers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ class IssueTrackersController < ApplicationController

validate_action index: { method: :get, response: :issue_trackers }
validate_action show: { method: :get, response: :issue_tracker }
validate_action create: { method: :post, request: :issue_tracker, response: :issue_tracker }
validate_action create: { method: :post, request: :issue_tracker, response: :status }
validate_action update: { method: :put, request: :issue_tracker }

# GET /issue_trackers
# GET /issue_trackers.json
# GET /issue_trackers.xml
def index
@issue_trackers = IssueTracker.all

respond_to do |format|
format.xml { render xml: @issue_trackers.to_xml(IssueTracker::DEFAULT_RENDER_PARAMS) }
format.json { render json: @issue_trackers.to_json(IssueTracker::DEFAULT_RENDER_PARAMS) }
end
end

# GET /issue_trackers/bnc
# GET /issue_trackers/bnc.json
# GET /issue_trackers/bnc.xml
# GET /issue_trackers/<id>
def show
@issue_tracker = IssueTracker.find_by_name(params[:id])
unless @issue_tracker
Expand All @@ -29,92 +24,64 @@ def show

respond_to do |format|
format.xml { render xml: @issue_tracker.to_xml(IssueTracker::DEFAULT_RENDER_PARAMS) }
format.json { render json: @issue_tracker.to_json(IssueTracker::DEFAULT_RENDER_PARAMS) }
end
end

# POST /issue_trackers
# POST /issue_trackers.json
# POST /issue_trackers.xml
def create
begin
@issue_tracker = IssueTracker.new(params)
rescue
# User didn't really upload www-form-urlencoded data but raw XML, try to parse that
xml = Nokogiri::XML(request.raw_post, &:strict).root
@issue_tracker = IssueTracker.create(name: xml.xpath('name[1]/text()').to_s,
kind: xml.xpath('kind[1]/text()').to_s,
description: xml.xpath('description[1]/text()').to_s,
regex: xml.xpath('regex[1]/text()').to_s,
label: xml.xpath('label[1]/text()').to_s,
url: xml.xpath('url[1]/text()').to_s,
enable_fetch: xml.xpath('enable-fetch[1]/text()').to_s,
issues_updated: Time.now,
show_url: xml.xpath('show-url[1]/text()').to_s)
end

xml = Nokogiri::XML(request.raw_post, &:strict).root
@issue_tracker = IssueTracker.create(name: xml.xpath('name[1]/text()').to_s,
kind: xml.xpath('kind[1]/text()').to_s,
description: xml.xpath('description[1]/text()').to_s,
regex: xml.xpath('regex[1]/text()').to_s,
label: xml.xpath('label[1]/text()').to_s,
url: xml.xpath('url[1]/text()').to_s,
enable_fetch: xml.xpath('enable-fetch[1]/text()').to_s,
issues_updated: Time.now,
show_url: xml.xpath('show-url[1]/text()').to_s)
respond_to do |format|
if @issue_tracker
format.xml { render xml: @issue_tracker.to_xml(IssueTracker::DEFAULT_RENDER_PARAMS), status: :created, location: @issue_tracker }
format.json { render json: @issue_tracker.to_json(IssueTracker::DEFAULT_RENDER_PARAMS), status: :created, location: @issue_tracker }
format.xml { render_ok }
else
format.xml { render xml: @issue_tracker.errors, status: :unprocessable_entity }
format.json { render json: @issue_tracker.errors, status: :unprocessable_entity }
end
end
end

# PUT /issue_trackers/bnc
# PUT /issue_trackers/bnc.json
# PUT /issue_trackers/bnc.xml
def update
@issue_tracker = IssueTracker.find_by_name(params[:id])
unless @issue_tracker
render_error(status: 404, errorcode: 'not_found', message: "Unable to find issue tracker '#{params[:id]}'") && return
end

respond_to do |format|
begin
ret = @issue_tracker.update_attributes(request.request_parameters)
rescue ActiveRecord::UnknownAttributeError, ActiveModel::MassAssignmentSecurity::Error
# User didn't really upload www-form-urlencoded data but raw XML, try to parse that
xml = Nokogiri::XML(request.raw_post, &:strict).root
attribs = {}
attribs[:name] = xml.xpath('name[1]/text()').to_s unless xml.xpath('name[1]/text()').empty?
attribs[:kind] = xml.xpath('kind[1]/text()').to_s unless xml.xpath('kind[1]/text()').empty?
attribs[:description] = xml.xpath('description[1]/text()').to_s unless xml.xpath('description[1]/text()').empty?
attribs[:user] = xml.xpath('user[1]/text()').to_s unless xml.xpath('user[1]/text()').empty?
attribs[:password] = xml.xpath('password[1]/text()').to_s unless xml.xpath('password[1]/text()').empty?
attribs[:regex] = xml.xpath('regex[1]/text()').to_s unless xml.xpath('regex[1]/text()').empty?
attribs[:url] = xml.xpath('url[1]/text()').to_s unless xml.xpath('url[1]/text()').empty?
attribs[:label] = xml.xpath('label[1]/text()').to_s unless xml.xpath('label[1]/text()').empty?
attribs[:enable_fetch] = xml.xpath('enable-fetch[1]/text()').to_s unless xml.xpath('enable-fetch[1]/text()').empty?
attribs[:show_url] = xml.xpath('show-url[1]/text()').to_s unless xml.xpath('show-url[1]/text()').empty?
ret = @issue_tracker.update_attributes(attribs)
end
if ret
format.xml { head :ok }
format.json { head :ok }
xml = Nokogiri::XML(request.raw_post, &:strict).root
attribs = {}
attribs[:name] = xml.xpath('name[1]/text()').to_s unless xml.xpath('name[1]/text()').empty?
attribs[:kind] = xml.xpath('kind[1]/text()').to_s unless xml.xpath('kind[1]/text()').empty?
attribs[:description] = xml.xpath('description[1]/text()').to_s unless xml.xpath('description[1]/text()').empty?
attribs[:user] = xml.xpath('user[1]/text()').to_s unless xml.xpath('user[1]/text()').empty?
attribs[:password] = xml.xpath('password[1]/text()').to_s unless xml.xpath('password[1]/text()').empty?
attribs[:regex] = xml.xpath('regex[1]/text()').to_s unless xml.xpath('regex[1]/text()').empty?
attribs[:url] = xml.xpath('url[1]/text()').to_s unless xml.xpath('url[1]/text()').empty?
attribs[:label] = xml.xpath('label[1]/text()').to_s unless xml.xpath('label[1]/text()').empty?
attribs[:enable_fetch] = xml.xpath('enable-fetch[1]/text()').to_s unless xml.xpath('enable-fetch[1]/text()').empty?
attribs[:show_url] = xml.xpath('show-url[1]/text()').to_s unless xml.xpath('show-url[1]/text()').empty?

issue_tracker = IssueTracker.find_by_name(params[:id])
if issue_tracker
issue_tracker.update_attributes(attribs)
else
format.xml { render xml: @issue_tracker.errors, status: :unprocessable_entity }
format.json { render json: @issue_tracker.errors, status: :unprocessable_entity }
IssueTracker.create(attribs)
end
format.xml { render_ok }
end
end

# DELETE /issue_trackers/bnc
# DELETE /issue_trackers/bnc.xml
# DELETE /issue_trackers/bnc.json
def destroy
@issue_tracker = IssueTracker.find_by_name(params[:id])
unless @issue_tracker
render_error(status: 404, errorcode: 'not_found', message: "Unable to find issue tracker '#{params[:id]}'") && return
end
@issue_tracker.destroy

respond_to do |format|
format.xml { head :ok }
format.json { head :ok }
end
render_ok
end
end
6 changes: 2 additions & 4 deletions src/api/test/functional/issue_trackers_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ def test_create_and_update_new_trackers
assert_xml_tag tag: 'url', content: 'http://example.com'
assert_xml_tag tag: 'show-url', content: 'http://example.com/@@@'
assert_no_xml_tag tag: 'password'
get '/issue_trackers/test.json'
assert_response :success

# FIXME: check backend data

Expand All @@ -69,10 +67,10 @@ def test_create_and_update_new_trackers
</issue-tracker>
EOF
login_adrian
put '/issue_trackers/test', params: issue_tracker_xml
raw_put '/issue_trackers/test', issue_tracker_xml
assert_response 403
login_king
put '/issue_trackers/test', params: issue_tracker_xml
raw_put '/issue_trackers/test', issue_tracker_xml
assert_response :success
get '/issue_trackers/test'
assert_response :success
Expand Down

0 comments on commit bfb8a1f

Please sign in to comment.