Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

module Wikis
module Adapters
module Providers
module XWiki
module Queries
module Concerns
module XWikiQuery
ACCEPT_HEADERS = { "Accept" => "application/json" }.freeze

def authenticated(auth_strategy)
Adapters::Authentication[auth_strategy].call do |http|
yield http.with(headers: ACCEPT_HEADERS)
end
end

def rest_url(path, query: nil)
url = "#{provider.url.chomp('/')}/rest/#{path.delete_prefix("/")}"

Check notice on line 47 in modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/concerns/xwiki_query.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/concerns/xwiki_query.rb#L47 <Style/StringLiteralsInInterpolation>

Prefer single-quoted strings inside interpolations.
Raw output
modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/concerns/xwiki_query.rb:47:77: C: Style/StringLiteralsInInterpolation: Prefer single-quoted strings inside interpolations.
return url if query.nil?

"#{url}?#{query.to_query}"
end

def handle_response(response)
return failure(code: :connection_error) if response.is_a?(HTTPX::ErrorResponse)

case response
in { status: 200..299 }
json = begin
response.json
rescue MultiJson::ParseError
return failure(code: :invalid_response)

Check warning on line 61 in modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/concerns/xwiki_query.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/concerns/xwiki_query.rb#L61 <Lint/NoReturnInBeginEndBlocks>

Do not `return` in `begin..end` blocks in assignment contexts.
Raw output
modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/concerns/xwiki_query.rb:61:21: W: Lint/NoReturnInBeginEndBlocks: Do not `return` in `begin..end` blocks in assignment contexts.
end

yield json
in { status: 401 | 403 }
failure(code: :unauthorized)
in { status: 404 }
failure(code: :not_found)
else
failure(code: :request_failed)
end
end
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -34,51 +34,25 @@ module Providers
module XWiki
module Queries
class PageInfo < BaseQuery
ACCEPT_HEADERS = { "Accept" => "application/json" }.freeze
include Concerns::XWikiQuery

def call(input_data:, auth_strategy:)
ref = PageReference.parse(input_data.identifier)
return failure(code: :not_found) unless ref

url = "#{provider.url.chomp('/')}/rest#{ref.rest_path}"
Adapters::Authentication[auth_strategy].call do |http|
handle_response(
http.with(headers: ACCEPT_HEADERS).get(url),
identifier: input_data.identifier
)
authenticated(auth_strategy) do |http|
handle_response(http.get(rest_url(ref.rest_path))) do |data|
success(
Results::PageInfo.new(
identifier: input_data.identifier,
title: data["title"],
href: data["xwikiAbsoluteUrl"],
provider:
)
)
end
end
end

private

def handle_response(response, identifier:)
return failure(code: :connection_error) if response.is_a?(HTTPX::ErrorResponse)

case response
in { status: 200..299 }
handle_success_response(response, identifier:)
in { status: 401 | 403 }
failure(code: :unauthorized)
in { status: 404 }
failure(code: :not_found)
else
failure(code: :request_failed)
end
end

def handle_success_response(response, identifier:)
data = response.json
success(
Results::PageInfo.new(
identifier:,
title: data["title"],
href: data["xwikiAbsoluteUrl"],
provider:
)
)
rescue MultiJson::ParseError
failure(code: :invalid_response)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,24 @@
module XWiki
module Queries
class SearchPages < BaseQuery
def call(input_data:, **)
# TODO: use real API endpoints once available
include Concerns::XWikiQuery

titles = [
"#{input_data.query} makes XWiki special",
"API documentation of #{input_data.query}",
"A brief introduction on configuring your own #{input_data.query}."
]
MAXIMUM_RESULTS = 50

success(titles.map { Results::PageInfo.new(identifier: "1338", title: it, href: "#", provider:) })
def call(input_data:, auth_strategy:)
query = { q: input_data.query, number: MAXIMUM_RESULTS }

authenticated(auth_strategy) do |http|
handle_response(http.get(rest_url("wikis/query", query:))) do |json|
# TODO: href is missing is from page infos
# TODO: we should switch all our responses to the stable identifier offered from the OP extension in XWiki (incl. PageInfo)

Check notice on line 47 in modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/search_pages.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/search_pages.rb#L47 <Layout/LineLength>

Line is too long. [141/130]
Raw output
modules/wikis/app/services/wikis/adapters/providers/xwiki/queries/search_pages.rb:47:131: C: Layout/LineLength: Line is too long. [141/130]
success(
json.fetch("searchResults")
.uniq { |r| r.fetch("id") }
.map { |r| Results::PageInfo.new(identifier: r.fetch("id"), title: r.fetch("title"), href: "#", provider:) }
)
end
end
end
end
end
Expand Down
Loading