diff options
Diffstat (limited to 'lib/api/helpers.rb')
-rw-r--r-- | lib/api/helpers.rb | 111 |
1 files changed, 58 insertions, 53 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 54cd4cd9cdb..596c7e8b534 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -5,20 +5,24 @@ module API include Gitlab::Utils include Helpers::Pagination - SUDO_HEADER = "HTTP_SUDO".freeze + SUDO_HEADER = "HTTP_SUDO" SUDO_PARAM = :sudo - API_USER_ENV = 'gitlab.api.user'.freeze + API_USER_ENV = "gitlab.api.user" def declared_params(options = {}) - options = { include_parent_namespaces: false }.merge(options) + options = {include_parent_namespaces: false}.merge(options) declared(params, options).to_h.symbolize_keys end def check_unmodified_since!(last_modified) - if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) rescue nil + if_unmodified_since = begin + Time.parse(headers["If-Unmodified-Since"]) + rescue + nil + end if if_unmodified_since && last_modified && last_modified > if_unmodified_since - render_api_error!('412 Precondition Failed', 412) + render_api_error!("412 Precondition Failed", 412) end end @@ -59,7 +63,7 @@ module API # rubocop:enable Gitlab/ModuleWithInstanceVariables def save_current_user_in_env(user) - env[API_USER_ENV] = { user_id: user.id, username: user.username } + env[API_USER_ENV] = {user_id: user.id, username: user.username} end def sudo? @@ -81,16 +85,17 @@ module API def wiki_page page = ProjectWiki.new(user_project, current_user).find_page(params[:slug]) - page || not_found!('Wiki Page') + page || not_found!("Wiki Page") end def available_labels_for(label_parent, include_ancestor_groups: true) - search_params = { include_ancestor_groups: include_ancestor_groups } + search_params = {include_ancestor_groups: include_ancestor_groups} if label_parent.is_a?(Project) search_params[:project_id] = label_parent.id else - search_params.merge!(group_id: label_parent.id, only_group_labels: true) + search_params[:group_id] = label_parent.id + search_params[:only_group_labels] = true end LabelsFinder.new(current_user, search_params).execute @@ -118,13 +123,13 @@ module API if can?(current_user, :read_project, project) project else - not_found!('Project') + not_found!("Project") end end # rubocop: disable CodeReuse/ActiveRecord def find_group(id) - if id.to_s =~ /^\d+$/ + if /^\d+$/.match?(id.to_s) Group.find_by(id: id) else Group.find_by_full_path(id) @@ -138,13 +143,13 @@ module API if can?(current_user, :read_group, group) group else - not_found!('Group') + not_found!("Group") end end # rubocop: disable CodeReuse/ActiveRecord def find_namespace(id) - if id.to_s =~ /^\d+$/ + if /^\d+$/.match?(id.to_s) Namespace.find_by(id: id) else Namespace.find_by_full_path(id) @@ -158,15 +163,15 @@ module API if can?(current_user, :read_namespace, namespace) namespace else - not_found!('Namespace') + not_found!("Namespace") end end def find_branch!(branch_name) if Gitlab::GitRefValidator.validate(branch_name) - user_project.repository.find_branch(branch_name) || not_found!('Branch') + user_project.repository.find_branch(branch_name) || not_found!("Branch") else - render_api_error!('The branch refname is invalid', 400) + render_api_error!("The branch refname is invalid", 400) end end @@ -187,7 +192,7 @@ module API end def find_project_snippet(id) - finder_params = { project: user_project } + finder_params = {project: user_project} SnippetsFinder.new(current_user, finder_params).find(id) end @@ -212,7 +217,7 @@ module API end def authenticate_by_gitlab_shell_token! - input = params['secret_token'].try(:chomp) + input = params["secret_token"].try(:chomp) unless Devise.secure_compare(secret_token, input) unauthorized! end @@ -249,8 +254,8 @@ module API end def require_gitlab_workhorse! - unless env['HTTP_GITLAB_WORKHORSE'].present? - forbidden!('Request should be executed via GitLab Workhorse') + unless env["HTTP_GITLAB_WORKHORSE"].present? + forbidden!("Request should be executed via GitLab Workhorse") end end @@ -300,72 +305,72 @@ module API end def order_options_with_tie_breaker - order_options = { params[:order_by] => params[:sort] } - order_options['id'] ||= 'desc' + order_options = {params[:order_by] => params[:sort]} + order_options["id"] ||= "desc" order_options end # error helpers def forbidden!(reason = nil) - message = ['403 Forbidden'] + message = ["403 Forbidden"] message << " - #{reason}" if reason - render_api_error!(message.join(' '), 403) + render_api_error!(message.join(" "), 403) end def bad_request!(attribute) message = ["400 (Bad request)"] message << "\"" + attribute.to_s + "\" not given" if attribute - render_api_error!(message.join(' '), 400) + render_api_error!(message.join(" "), 400) end def not_found!(resource = nil) message = ["404"] message << resource if resource message << "Not Found" - render_api_error!(message.join(' '), 404) + render_api_error!(message.join(" "), 404) end def unauthorized! - render_api_error!('401 Unauthorized', 401) + render_api_error!("401 Unauthorized", 401) end def not_allowed! - render_api_error!('405 Method Not Allowed', 405) + render_api_error!("405 Method Not Allowed", 405) end def conflict!(message = nil) - render_api_error!(message || '409 Conflict', 409) + render_api_error!(message || "409 Conflict", 409) end def file_to_large! - render_api_error!('413 Request Entity Too Large', 413) + render_api_error!("413 Request Entity Too Large", 413) end def not_modified! - render_api_error!('304 Not Modified', 304) + render_api_error!("304 Not Modified", 304) end def no_content! - render_api_error!('204 No Content', 204) + render_api_error!("204 No Content", 204) end def accepted! - render_api_error!('202 Accepted', 202) + render_api_error!("202 Accepted", 202) end def render_validation_error!(model) if model.errors.any? - render_api_error!(model.errors.messages || '400 Bad Request', 400) + render_api_error!(model.errors.messages || "400 Bad Request", 400) end end def render_spam_error! - render_api_error!({ error: 'Spam detected' }, 400) + render_api_error!({error: "Spam detected"}, 400) end def render_api_error!(message, status) - error!({ 'message' => message }, status, header) + error!({"message" => message}, status, header) end def handle_api_exception(exception) @@ -389,10 +394,10 @@ module API if Rails.env.test? message else - '500 Internal Server Error' + "500 Internal Server Error" end - rack_response({ 'message' => response_message }.to_json, 500) + rack_response({"message" => response_message}.to_json, 500) end # project helpers @@ -404,7 +409,7 @@ module API # rubocop: enable CodeReuse/ActiveRecord def project_finder_params - finder_params = { without_deleted: true } + finder_params = {without_deleted: true} finder_params[:owned] = true if params[:owned].present? finder_params[:non_public] = true if params[:membership].present? finder_params[:starred] = true if params[:starred].present? @@ -419,16 +424,16 @@ module API # file helpers - def present_disk_file!(path, filename, content_type = 'application/octet-stream') + def present_disk_file!(path, filename, content_type = "application/octet-stream") filename ||= File.basename(path) - header['Content-Disposition'] = ::Gitlab::ContentDisposition.format(disposition: 'attachment', filename: filename) - header['Content-Transfer-Encoding'] = 'binary' + header["Content-Disposition"] = ::Gitlab::ContentDisposition.format(disposition: "attachment", filename: filename) + header["Content-Transfer-Encoding"] = "binary" content_type content_type # Support download acceleration - case headers['X-Sendfile-Type'] - when 'X-Sendfile' - header['X-Sendfile'] = path + case headers["X-Sendfile-Type"] + when "X-Sendfile" + header["X-Sendfile"] = path body else file path @@ -469,11 +474,11 @@ module API unauthorized! unless initial_current_user unless initial_current_user.admin? - forbidden!('Must be admin to use sudo') + forbidden!("Must be admin to use sudo") end unless access_token - forbidden!('Must be authenticated using an OAuth or Personal Access Token to use sudo') + forbidden!("Must be authenticated using an OAuth or Personal Access Token to use sudo") end validate_access_token!(scopes: [:sudo]) @@ -493,9 +498,9 @@ module API end def send_git_blob(repository, blob) - env['api.format'] = :txt - content_type 'text/plain' - header['Content-Disposition'] = ::Gitlab::ContentDisposition.format(disposition: 'inline', filename: blob.name) + env["api.format"] = :txt + content_type "text/plain" + header["Content-Disposition"] = ::Gitlab::ContentDisposition.format(disposition: "inline", filename: blob.name) # Let Workhorse examine the content and determine the better content disposition header[Gitlab::Workhorse::DETECT_HEADER] = "true" @@ -515,8 +520,8 @@ module API # `request`. We workaround this by defining methods that returns the right # values. def define_params_for_grape_middleware - self.define_singleton_method(:request) { ActionDispatch::Request.new(env) } - self.define_singleton_method(:params) { request.params.symbolize_keys } + define_singleton_method(:request) { ActionDispatch::Request.new(env) } + define_singleton_method(:params) { request.params.symbolize_keys } end # We could get a Grape or a standard Ruby exception. We should only report anything that @@ -528,7 +533,7 @@ module API end def archived_param - return 'only' if params[:archived] + return "only" if params[:archived] params[:archived] end |