summaryrefslogtreecommitdiff
path: root/lib/api/helpers
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-09-19 01:45:44 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-09-19 01:45:44 +0000
commit85dc423f7090da0a52c73eb66faf22ddb20efff9 (patch)
tree9160f299afd8c80c038f08e1545be119f5e3f1e1 /lib/api/helpers
parent15c2c8c66dbe422588e5411eee7e68f1fa440bb8 (diff)
downloadgitlab-ce-85dc423f7090da0a52c73eb66faf22ddb20efff9.tar.gz
Add latest changes from gitlab-org/gitlab@13-4-stable-ee
Diffstat (limited to 'lib/api/helpers')
-rw-r--r--lib/api/helpers/internal_helpers.rb2
-rw-r--r--lib/api/helpers/packages/conan/api_helpers.rb98
-rw-r--r--lib/api/helpers/packages_helpers.rb4
-rw-r--r--lib/api/helpers/packages_manager_clients_helpers.rb22
-rw-r--r--lib/api/helpers/search_helpers.rb4
-rw-r--r--lib/api/helpers/services_helpers.rb27
-rw-r--r--lib/api/helpers/snippets_helpers.rb46
7 files changed, 141 insertions, 62 deletions
diff --git a/lib/api/helpers/internal_helpers.rb b/lib/api/helpers/internal_helpers.rb
index b7ce1eba3f9..69b53ea6c2f 100644
--- a/lib/api/helpers/internal_helpers.rb
+++ b/lib/api/helpers/internal_helpers.rb
@@ -67,7 +67,7 @@ module API
result == 'PONG'
rescue => e
- Rails.logger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}") # rubocop:disable Gitlab/RailsLogger
+ Gitlab::AppLogger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}")
false
end
diff --git a/lib/api/helpers/packages/conan/api_helpers.rb b/lib/api/helpers/packages/conan/api_helpers.rb
index 1161d1386bb..dcbf933a4e1 100644
--- a/lib/api/helpers/packages/conan/api_helpers.rb
+++ b/lib/api/helpers/packages/conan/api_helpers.rb
@@ -5,11 +5,13 @@ module API
module Packages
module Conan
module ApiHelpers
+ include Gitlab::Utils::StrongMemoize
+
def present_download_urls(entity)
authorize!(:read_package, project)
presenter = ::Packages::Conan::PackagePresenter.new(
- recipe,
+ package,
current_user,
project,
conan_package_reference: params[:conan_package_reference]
@@ -31,7 +33,7 @@ module API
def recipe_upload_urls
{ upload_urls: Hash[
file_names.select(&method(:recipe_file?)).map do |file_name|
- [file_name, recipe_file_upload_url(file_name)]
+ [file_name, build_recipe_file_upload_url(file_name)]
end
] }
end
@@ -39,7 +41,7 @@ module API
def package_upload_urls
{ upload_urls: Hash[
file_names.select(&method(:package_file?)).map do |file_name|
- [file_name, package_file_upload_url(file_name)]
+ [file_name, build_package_file_upload_url(file_name)]
end
] }
end
@@ -52,32 +54,58 @@ module API
file_name.in?(::Packages::Conan::FileMetadatum::PACKAGE_FILES)
end
- def package_file_upload_url(file_name)
- expose_url(
- api_v4_packages_conan_v1_files_package_path(
- package_name: params[:package_name],
- package_version: params[:package_version],
- package_username: params[:package_username],
- package_channel: params[:package_channel],
- recipe_revision: '0',
- conan_package_reference: params[:conan_package_reference],
- package_revision: '0',
- file_name: file_name
- )
+ def build_package_file_upload_url(file_name)
+ options = url_options(file_name).merge(
+ conan_package_reference: params[:conan_package_reference],
+ package_revision: ::Packages::Conan::FileMetadatum::DEFAULT_PACKAGE_REVISION
)
+
+ package_file_url(options)
+ end
+
+ def build_recipe_file_upload_url(file_name)
+ recipe_file_url(url_options(file_name))
end
- def recipe_file_upload_url(file_name)
- expose_url(
- api_v4_packages_conan_v1_files_export_path(
- package_name: params[:package_name],
- package_version: params[:package_version],
- package_username: params[:package_username],
- package_channel: params[:package_channel],
- recipe_revision: '0',
- file_name: file_name
+ def url_options(file_name)
+ {
+ package_name: params[:package_name],
+ package_version: params[:package_version],
+ package_username: params[:package_username],
+ package_channel: params[:package_channel],
+ file_name: file_name,
+ recipe_revision: ::Packages::Conan::FileMetadatum::DEFAULT_RECIPE_REVISION
+ }
+ end
+
+ def package_file_url(options)
+ case package_scope
+ when :project
+ expose_url(
+ api_v4_projects_packages_conan_v1_files_package_path(
+ options.merge(id: project.id)
+ )
)
- )
+ when :instance
+ expose_url(
+ api_v4_packages_conan_v1_files_package_path(options)
+ )
+ end
+ end
+
+ def recipe_file_url(options)
+ case package_scope
+ when :project
+ expose_url(
+ api_v4_projects_packages_conan_v1_files_export_path(
+ options.merge(id: project.id)
+ )
+ )
+ when :instance
+ expose_url(
+ api_v4_packages_conan_v1_files_export_path(options)
+ )
+ end
end
def recipe
@@ -86,16 +114,23 @@ module API
def project
strong_memoize(:project) do
- full_path = ::Packages::Conan::Metadatum.full_path_from(package_username: params[:package_username])
- Project.find_by_full_path(full_path)
+ case package_scope
+ when :project
+ find_project!(params[:id])
+ when :instance
+ full_path = ::Packages::Conan::Metadatum.full_path_from(package_username: params[:package_username])
+ find_project!(full_path)
+ end
end
end
def package
strong_memoize(:package) do
project.packages
+ .conan
.with_name(params[:package_name])
.with_version(params[:package_version])
+ .with_conan_username(params[:package_username])
.with_conan_channel(params[:package_channel])
.order_created
.last
@@ -123,7 +158,7 @@ module API
conan_package_reference: params[:conan_package_reference]
).execute!
- track_event('pull_package') if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY
+ package_event('pull_package', category: 'API::ConanPackages') if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY
present_carrierwave_file!(package_file.file)
end
@@ -134,7 +169,7 @@ module API
def track_push_package_event
if params[:file_name] == ::Packages::Conan::FileMetadatum::PACKAGE_BINARY && params[:file].size > 0 # rubocop: disable Style/ZeroLengthPredicate
- track_event('push_package')
+ package_event('push_package', category: 'API::ConanPackages')
end
end
@@ -155,6 +190,7 @@ module API
def upload_package_file(file_type)
authorize_upload!(project)
+ bad_request!('File is too large') if project.actual_limits.exceeded?(:conan_max_file_size, params['file.size'].to_i)
current_package = find_or_create_package
@@ -234,6 +270,10 @@ module API
token
end
+
+ def package_scope
+ params[:id].present? ? :project : :instance
+ end
end
end
end
diff --git a/lib/api/helpers/packages_helpers.rb b/lib/api/helpers/packages_helpers.rb
index c6037d52de9..403f5ea3851 100644
--- a/lib/api/helpers/packages_helpers.rb
+++ b/lib/api/helpers/packages_helpers.rb
@@ -47,6 +47,10 @@ module API
authorize_create_package!(subject)
require_gitlab_workhorse!
end
+
+ def package_event(event_name, **args)
+ track_event(event_name, **args)
+ end
end
end
end
diff --git a/lib/api/helpers/packages_manager_clients_helpers.rb b/lib/api/helpers/packages_manager_clients_helpers.rb
index 955d21cb44f..e7662b03577 100644
--- a/lib/api/helpers/packages_manager_clients_helpers.rb
+++ b/lib/api/helpers/packages_manager_clients_helpers.rb
@@ -6,18 +6,8 @@ module API
extend Grape::API::Helpers
include ::API::Helpers::PackagesHelpers
- params :workhorse_upload_params do
- optional 'file.path', type: String, desc: 'Path to locally stored body (generated by Workhorse)'
- optional 'file.name', type: String, desc: 'Real filename as send in Content-Disposition (generated by Workhorse)'
- optional 'file.type', type: String, desc: 'Real content type as send in Content-Type (generated by Workhorse)'
- optional 'file.size', type: Integer, desc: 'Real size of file (generated by Workhorse)'
- optional 'file.md5', type: String, desc: 'MD5 checksum of the file (generated by Workhorse)'
- optional 'file.sha1', type: String, desc: 'SHA1 checksum of the file (generated by Workhorse)'
- optional 'file.sha256', type: String, desc: 'SHA256 checksum of the file (generated by Workhorse)'
- end
-
def find_job_from_http_basic_auth
- return unless headers
+ return unless request.headers
token = decode_token
@@ -27,7 +17,7 @@ module API
end
def find_deploy_token_from_http_basic_auth
- return unless headers
+ return unless request.headers
token = decode_token
@@ -36,16 +26,10 @@ module API
DeployToken.active.find_by_token(token)
end
- def uploaded_package_file(param_name = :file)
- uploaded_file = UploadedFile.from_params(params, param_name, ::Packages::PackageFileUploader.workhorse_local_upload_path)
- bad_request!('Missing package file!') unless uploaded_file
- uploaded_file
- end
-
private
def decode_token
- encoded_credentials = headers['Authorization'].to_s.split('Basic ', 2).second
+ encoded_credentials = request.headers['Authorization'].to_s.split('Basic ', 2).second
Base64.decode64(encoded_credentials || '').split(':', 2).second
end
end
diff --git a/lib/api/helpers/search_helpers.rb b/lib/api/helpers/search_helpers.rb
index 936684ea1f8..cb5f92fa62a 100644
--- a/lib/api/helpers/search_helpers.rb
+++ b/lib/api/helpers/search_helpers.rb
@@ -17,6 +17,10 @@ module API
# This is a separate method so that EE can redefine it.
%w(issues merge_requests milestones notes wiki_blobs commits blobs users)
end
+
+ def self.search_states
+ %w(all opened closed merged)
+ end
end
end
end
diff --git a/lib/api/helpers/services_helpers.rb b/lib/api/helpers/services_helpers.rb
index ff938358439..4bceda51900 100644
--- a/lib/api/helpers/services_helpers.rb
+++ b/lib/api/helpers/services_helpers.rb
@@ -631,12 +631,26 @@ module API
name: :issues_url,
type: String,
desc: 'The issues URL'
+ }
+ ],
+ 'ewm' => [
+ {
+ required: true,
+ name: :new_issue_url,
+ type: String,
+ desc: 'New Issue URL'
},
{
- required: false,
- name: :description,
+ required: true,
+ name: :project_url,
+ type: String,
+ desc: 'Project URL'
+ },
+ {
+ required: true,
+ name: :issues_url,
type: String,
- desc: 'The description of the tracker'
+ desc: 'Issues URL'
}
],
'youtrack' => [
@@ -651,12 +665,6 @@ module API
name: :issues_url,
type: String,
desc: 'The issues URL'
- },
- {
- required: false,
- name: :description,
- type: String,
- desc: 'The description of the tracker'
}
],
'slack' => [
@@ -747,6 +755,7 @@ module API
::DiscordService,
::DroneCiService,
::EmailsOnPushService,
+ ::EwmService,
::ExternalWikiService,
::FlowdockService,
::HangoutsChatService,
diff --git a/lib/api/helpers/snippets_helpers.rb b/lib/api/helpers/snippets_helpers.rb
index 79367da8d1f..9224381735f 100644
--- a/lib/api/helpers/snippets_helpers.rb
+++ b/lib/api/helpers/snippets_helpers.rb
@@ -27,6 +27,24 @@ module API
exactly_one_of :files, :content
end
+ params :update_file_params do |options|
+ optional :files, type: Array, desc: 'An array of files to update' do
+ requires :action, type: String,
+ values: SnippetInputAction::ACTIONS.map(&:to_s),
+ desc: "The type of action to perform on the file, must be one of: #{SnippetInputAction::ACTIONS.join(", ")}"
+ optional :content, type: String, desc: 'The content of a snippet'
+ optional :file_path, file_path: true, type: String, desc: 'The file path of a snippet file'
+ optional :previous_path, file_path: true, type: String, desc: 'The previous path of a snippet file'
+ end
+
+ mutually_exclusive :files, :content
+ mutually_exclusive :files, :file_name
+ end
+
+ params :minimum_update_params do
+ at_least_one_of :content, :description, :files, :file_name, :title, :visibility
+ end
+
def content_for(snippet)
if snippet.empty_repo?
env['api.format'] = :txt
@@ -53,10 +71,30 @@ module API
end
end
- def process_file_args(args)
- args[:snippet_actions] = args.delete(:files)&.map do |file|
- file[:action] = :create
- file.symbolize_keys
+ def process_create_params(args)
+ with_api_params do |api_params|
+ args[:snippet_actions] = args.delete(:files)&.map do |file|
+ file[:action] = :create
+ file.symbolize_keys
+ end
+
+ args.merge(api_params)
+ end
+ end
+
+ def process_update_params(args)
+ with_api_params do |api_params|
+ args[:snippet_actions] = args.delete(:files)&.map(&:symbolize_keys)
+
+ args.merge(api_params)
+ end
+ end
+
+ def validate_params_for_multiple_files(snippet)
+ return unless params[:content] || params[:file_name]
+
+ if Feature.enabled?(:snippet_multiple_files, current_user) && snippet.multiple_files?
+ render_api_error!({ error: _('To update Snippets with multiple files, you must use the `files` parameter') }, 400)
end
end
end