From 4555e1b21c365ed8303ffb7a3325d773c9b8bf31 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Wed, 19 May 2021 15:44:42 +0000 Subject: Add latest changes from gitlab-org/gitlab@13-12-stable-ee --- lib/api/helpers/award_emoji.rb | 40 +++++++++++++++ lib/api/helpers/caching.rb | 65 ++++++++++++++++++++++-- lib/api/helpers/common_helpers.rb | 2 +- lib/api/helpers/discussions_helpers.rb | 2 +- lib/api/helpers/groups_helpers.rb | 2 +- lib/api/helpers/headers_helpers.rb | 2 +- lib/api/helpers/internal_helpers.rb | 2 +- lib/api/helpers/issues_helpers.rb | 6 ++- lib/api/helpers/label_helpers.rb | 17 +++++-- lib/api/helpers/members_helpers.rb | 4 +- lib/api/helpers/notes_helpers.rb | 2 +- lib/api/helpers/performance_bar_helpers.rb | 6 +-- lib/api/helpers/project_snapshots_helpers.rb | 2 +- lib/api/helpers/projects_helpers.rb | 2 +- lib/api/helpers/protected_branches_helpers.rb | 2 +- lib/api/helpers/related_resources_helpers.rb | 4 +- lib/api/helpers/resource_label_events_helpers.rb | 2 +- lib/api/helpers/runner.rb | 6 ++- lib/api/helpers/search_helpers.rb | 2 +- lib/api/helpers/services_helpers.rb | 57 ++++----------------- lib/api/helpers/settings_helpers.rb | 2 +- lib/api/helpers/users_helpers.rb | 2 +- lib/api/helpers/variables_helpers.rb | 2 +- lib/api/helpers/wikis_helpers.rb | 2 +- 24 files changed, 154 insertions(+), 81 deletions(-) create mode 100644 lib/api/helpers/award_emoji.rb (limited to 'lib/api/helpers') diff --git a/lib/api/helpers/award_emoji.rb b/lib/api/helpers/award_emoji.rb new file mode 100644 index 00000000000..5b659c4dde7 --- /dev/null +++ b/lib/api/helpers/award_emoji.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +module API + module Helpers + module AwardEmoji + def self.awardables + [ + { type: 'issue', resource: :projects, find_by: :iid, feature_category: :issue_tracking }, + { type: 'merge_request', resource: :projects, find_by: :iid, feature_category: :code_review }, + { type: 'snippet', resource: :projects, find_by: :id, feature_category: :snippets } + ] + end + + def self.awardable_id_desc + "The ID of an Issue, Merge Request or Snippet" + end + + # rubocop: disable CodeReuse/ActiveRecord + def awardable + @awardable ||= + begin + if params.include?(:note_id) + note_id = params.delete(:note_id) + + awardable.notes.find(note_id) + elsif params.include?(:issue_iid) + user_project.issues.find_by!(iid: params[:issue_iid]) + elsif params.include?(:merge_request_iid) + user_project.merge_requests.find_by!(iid: params[:merge_request_iid]) + elsif params.include?(:snippet_id) + user_project.snippets.find(params[:snippet_id]) + end + end + end + # rubocop: enable CodeReuse/ActiveRecord + end + end +end + +API::Helpers::AwardEmoji.prepend_mod_with('API::Helpers::AwardEmoji') diff --git a/lib/api/helpers/caching.rb b/lib/api/helpers/caching.rb index d0f22109879..f24ac7302c1 100644 --- a/lib/api/helpers/caching.rb +++ b/lib/api/helpers/caching.rb @@ -11,6 +11,11 @@ module API # @return [ActiveSupport::Duration] DEFAULT_EXPIRY = 1.day + # @return [Hash] + DEFAULT_CACHE_OPTIONS = { + race_condition_ttl: 5.seconds + }.freeze + # @return [ActiveSupport::Cache::Store] def cache Rails.cache @@ -40,7 +45,7 @@ module API # @param expires_in [ActiveSupport::Duration, Integer] an expiry time for the cache entry # @param presenter_args [Hash] keyword arguments to be passed to the entity # @return [Gitlab::Json::PrecompiledJson] - def present_cached(obj_or_collection, with:, cache_context: -> (_) { current_user.cache_key }, expires_in: DEFAULT_EXPIRY, **presenter_args) + def present_cached(obj_or_collection, with:, cache_context: -> (_) { current_user&.cache_key }, expires_in: DEFAULT_EXPIRY, **presenter_args) json = if obj_or_collection.is_a?(Enumerable) cached_collection( @@ -63,8 +68,59 @@ module API body Gitlab::Json::PrecompiledJson.new(json) end + # Action caching implementation + # + # This allows you to wrap an entire API endpoint call in a cache, useful + # for short TTL caches to effectively rate-limit an endpoint. The block + # will be converted to JSON and cached, and returns a + # `Gitlab::Json::PrecompiledJson` object which will be exported without + # secondary conversion. + # + # @param key [Object] any object that can be converted into a cache key + # @param expires_in [ActiveSupport::Duration, Integer] an expiry time for the cache entry + # @return [Gitlab::Json::PrecompiledJson] + def cache_action(key, **cache_opts) + json = cache.fetch(key, **apply_default_cache_options(cache_opts)) do + response = yield + + if response.is_a?(Gitlab::Json::PrecompiledJson) + response.to_s + else + Gitlab::Json.dump(response.as_json) + end + end + + body Gitlab::Json::PrecompiledJson.new(json) + end + + # Conditionally cache an action + # + # Perform a `cache_action` only if the conditional passes + def cache_action_if(conditional, *opts, **kwargs) + if conditional + cache_action(*opts, **kwargs) do + yield + end + else + yield + end + end + + # Conditionally cache an action + # + # Perform a `cache_action` unless the conditional passes + def cache_action_unless(conditional, *opts, **kwargs) + cache_action_if(!conditional, *opts, **kwargs) do + yield + end + end + private + def apply_default_cache_options(opts = {}) + DEFAULT_CACHE_OPTIONS.merge(opts) + end + # Optionally uses a `Proc` to add context to a cache key # # @param object [Object] must respond to #cache_key @@ -119,8 +175,11 @@ module API objs.flatten! map = multi_key_map(objs, context: context) - cache.fetch_multi(*map.keys, **kwargs) do |key| - yield map[key] + # TODO: `contextual_cache_key` should be constructed based on the guideline https://docs.gitlab.com/ee/development/redis.html#multi-key-commands. + Gitlab::Instrumentation::RedisClusterValidator.allow_cross_slot_commands do + cache.fetch_multi(*map.keys, **kwargs) do |key| + yield map[key] + end end end diff --git a/lib/api/helpers/common_helpers.rb b/lib/api/helpers/common_helpers.rb index 8940cf87f82..02942820982 100644 --- a/lib/api/helpers/common_helpers.rb +++ b/lib/api/helpers/common_helpers.rb @@ -40,4 +40,4 @@ module API end end -API::Helpers::CommonHelpers.prepend_if_ee('EE::API::Helpers::CommonHelpers') +API::Helpers::CommonHelpers.prepend_mod_with('API::Helpers::CommonHelpers') diff --git a/lib/api/helpers/discussions_helpers.rb b/lib/api/helpers/discussions_helpers.rb index 3c0db1d0ea9..cb2feeda1e1 100644 --- a/lib/api/helpers/discussions_helpers.rb +++ b/lib/api/helpers/discussions_helpers.rb @@ -17,4 +17,4 @@ module API end end -API::Helpers::DiscussionsHelpers.prepend_if_ee('EE::API::Helpers::DiscussionsHelpers') +API::Helpers::DiscussionsHelpers.prepend_mod_with('API::Helpers::DiscussionsHelpers') diff --git a/lib/api/helpers/groups_helpers.rb b/lib/api/helpers/groups_helpers.rb index ba07a70ee32..5c5109f3d21 100644 --- a/lib/api/helpers/groups_helpers.rb +++ b/lib/api/helpers/groups_helpers.rb @@ -48,4 +48,4 @@ module API end end -API::Helpers::GroupsHelpers.prepend_if_ee('EE::API::Helpers::GroupsHelpers') +API::Helpers::GroupsHelpers.prepend_mod_with('API::Helpers::GroupsHelpers') diff --git a/lib/api/helpers/headers_helpers.rb b/lib/api/helpers/headers_helpers.rb index 908c57bb04e..56445ccbd0d 100644 --- a/lib/api/helpers/headers_helpers.rb +++ b/lib/api/helpers/headers_helpers.rb @@ -8,7 +8,7 @@ module API def set_http_headers(header_data) header_data.each do |key, value| if value.is_a?(Enumerable) - raise ArgumentError.new("Header value should be a string") + raise ArgumentError, "Header value should be a string" end header "X-Gitlab-#{key.to_s.split('_').collect(&:capitalize).join('-')}", value.to_s diff --git a/lib/api/helpers/internal_helpers.rb b/lib/api/helpers/internal_helpers.rb index 9a1ff2ba8ce..e03f029a6ef 100644 --- a/lib/api/helpers/internal_helpers.rb +++ b/lib/api/helpers/internal_helpers.rb @@ -65,7 +65,7 @@ module API result = Gitlab::Redis::SharedState.with { |redis| redis.ping } result == 'PONG' - rescue => e + rescue StandardError => e Gitlab::AppLogger.warn("GitLab: An unexpected error occurred in pinging to Redis: #{e}") false end diff --git a/lib/api/helpers/issues_helpers.rb b/lib/api/helpers/issues_helpers.rb index 2b1ed479692..b1954f8ece9 100644 --- a/lib/api/helpers/issues_helpers.rb +++ b/lib/api/helpers/issues_helpers.rb @@ -28,7 +28,8 @@ module API :remove_labels, :milestone_id, :state_event, - :title + :title, + :issue_type ] end @@ -47,6 +48,7 @@ module API args[:not][:label_name] ||= args[:not]&.delete(:labels) args[:scope] = args[:scope].underscore if args[:scope] args[:sort] = "#{args[:order_by]}_#{args[:sort]}" + args[:issue_types] ||= args.delete(:issue_type) IssuesFinder.new(current_user, args) end @@ -74,4 +76,4 @@ module API end end -API::Helpers::IssuesHelpers.prepend_if_ee('EE::API::Helpers::IssuesHelpers') +API::Helpers::IssuesHelpers.prepend_mod_with('API::Helpers::IssuesHelpers') diff --git a/lib/api/helpers/label_helpers.rb b/lib/api/helpers/label_helpers.rb index 4018f2dec21..796b8928243 100644 --- a/lib/api/helpers/label_helpers.rb +++ b/lib/api/helpers/label_helpers.rb @@ -5,27 +5,34 @@ module API module LabelHelpers extend Grape::API::Helpers + params :optional_label_params do + optional :description, type: String, desc: 'The description of the label' + optional :remove_on_close, type: Boolean, desc: 'Whether the label should be removed from an issue when the issue is closed' + end + params :label_create_params do requires :name, type: String, desc: 'The name of the label to be created' requires :color, type: String, desc: "The color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB) or one of the allowed CSS color names" - optional :description, type: String, desc: 'The description of label to be created' + + use :optional_label_params end params :label_update_params do optional :new_name, type: String, desc: 'The new name of the label' optional :color, type: String, desc: "The new color of the label given in 6-digit hex notation with leading '#' sign (e.g. #FFAABB) or one of the allowed CSS color names" - optional :description, type: String, desc: 'The new description of label' + + use :optional_label_params end params :project_label_update_params do use :label_update_params optional :priority, type: Integer, desc: 'The priority of the label', allow_blank: true - at_least_one_of :new_name, :color, :description, :priority + at_least_one_of :new_name, :color, :description, :priority, :remove_on_close end params :group_label_update_params do use :label_update_params - at_least_one_of :new_name, :color, :description + at_least_one_of :new_name, :color, :description, :remove_on_close end def find_label(parent, id_or_title, params = { include_ancestor_groups: true }) @@ -117,7 +124,7 @@ module API else render_api_error!('Failed to promote project label to group label', 400) end - rescue => error + rescue StandardError => error render_api_error!(error.to_s, 400) end end diff --git a/lib/api/helpers/members_helpers.rb b/lib/api/helpers/members_helpers.rb index 2de077b5a3b..bd0c2501220 100644 --- a/lib/api/helpers/members_helpers.rb +++ b/lib/api/helpers/members_helpers.rb @@ -18,7 +18,7 @@ module API # rubocop: disable CodeReuse/ActiveRecord def retrieve_members(source, params:, deep: false) - members = deep ? find_all_members(source) : source_members(source).where.not(user_id: nil) + members = deep ? find_all_members(source) : source_members(source).connected_to_user members = members.includes(:user) members = members.references(:user).merge(User.search(params[:query])) if params[:query].present? members = members.where(user_id: params[:user_ids]) if params[:user_ids].present? @@ -65,4 +65,4 @@ module API end end -API::Helpers::MembersHelpers.prepend_if_ee('EE::API::Helpers::MembersHelpers') +API::Helpers::MembersHelpers.prepend_mod_with('API::Helpers::MembersHelpers') diff --git a/lib/api/helpers/notes_helpers.rb b/lib/api/helpers/notes_helpers.rb index cb938bc8a14..356e4a98c97 100644 --- a/lib/api/helpers/notes_helpers.rb +++ b/lib/api/helpers/notes_helpers.rb @@ -151,4 +151,4 @@ module API end end -API::Helpers::NotesHelpers.prepend_if_ee('EE::API::Helpers::NotesHelpers') +API::Helpers::NotesHelpers.prepend_mod_with('API::Helpers::NotesHelpers') diff --git a/lib/api/helpers/performance_bar_helpers.rb b/lib/api/helpers/performance_bar_helpers.rb index 8430e889dff..0b7fb4308fc 100644 --- a/lib/api/helpers/performance_bar_helpers.rb +++ b/lib/api/helpers/performance_bar_helpers.rb @@ -4,17 +4,17 @@ module API module Helpers module PerformanceBarHelpers def set_peek_enabled_for_current_request - Gitlab::SafeRequestStore.fetch(:peek_enabled) { perf_bar_cookie_enabled? && perf_bar_enabled_for_user? } + Gitlab::SafeRequestStore.fetch(:peek_enabled) { perf_bar_cookie_enabled? && perf_bar_allowed_for_user? } end def perf_bar_cookie_enabled? cookies[:perf_bar_enabled] == 'true' end - def perf_bar_enabled_for_user? + def perf_bar_allowed_for_user? # We cannot use `current_user` here because that method raises an exception when the user # is unauthorized and some API endpoints require that `current_user` is not called. - Gitlab::PerformanceBar.enabled_for_user?(find_user_from_sources) + Gitlab::PerformanceBar.allowed_for_user?(find_user_from_sources) end end end diff --git a/lib/api/helpers/project_snapshots_helpers.rb b/lib/api/helpers/project_snapshots_helpers.rb index e708dbf0156..0b10641571a 100644 --- a/lib/api/helpers/project_snapshots_helpers.rb +++ b/lib/api/helpers/project_snapshots_helpers.rb @@ -3,7 +3,7 @@ module API module Helpers module ProjectSnapshotsHelpers - prepend_if_ee('::EE::API::Helpers::ProjectSnapshotsHelpers') # rubocop: disable Cop/InjectEnterpriseEditionModule + prepend_mod_with('API::Helpers::ProjectSnapshotsHelpers') # rubocop: disable Cop/InjectEnterpriseEditionModule def authorize_read_git_snapshot! authenticated_with_can_read_all_resources! diff --git a/lib/api/helpers/projects_helpers.rb b/lib/api/helpers/projects_helpers.rb index cf2bcace33b..d9c0b4f67c8 100644 --- a/lib/api/helpers/projects_helpers.rb +++ b/lib/api/helpers/projects_helpers.rb @@ -170,4 +170,4 @@ module API end end -API::Helpers::ProjectsHelpers.prepend_if_ee('EE::API::Helpers::ProjectsHelpers') +API::Helpers::ProjectsHelpers.prepend_mod_with('API::Helpers::ProjectsHelpers') diff --git a/lib/api/helpers/protected_branches_helpers.rb b/lib/api/helpers/protected_branches_helpers.rb index 970a3687214..4a968ad1d60 100644 --- a/lib/api/helpers/protected_branches_helpers.rb +++ b/lib/api/helpers/protected_branches_helpers.rb @@ -12,4 +12,4 @@ module API end end -API::Helpers::ProtectedBranchesHelpers.prepend_if_ee('EE::API::Helpers::ProtectedBranchesHelpers') +API::Helpers::ProtectedBranchesHelpers.prepend_mod_with('API::Helpers::ProtectedBranchesHelpers') diff --git a/lib/api/helpers/related_resources_helpers.rb b/lib/api/helpers/related_resources_helpers.rb index 9cdde25fe4e..d0eda68bf52 100644 --- a/lib/api/helpers/related_resources_helpers.rb +++ b/lib/api/helpers/related_resources_helpers.rb @@ -23,10 +23,10 @@ module API # Using a blank component at the beginning of the join we ensure # that the resulted path will start with '/'. If the resulted path - # does not start with '/', URI::Generic#build will fail + # does not start with '/', URI::Generic#new will fail path_with_script_name = File.join('', [script_name, path].select(&:present?)) - URI::Generic.build(scheme: protocol, host: host, port: port, path: path_with_script_name).to_s + URI::Generic.new(protocol, nil, host, port, nil, path_with_script_name, nil, nil, nil, URI::RFC3986_PARSER, true).to_s end private diff --git a/lib/api/helpers/resource_label_events_helpers.rb b/lib/api/helpers/resource_label_events_helpers.rb index ad2733baffc..7e641130062 100644 --- a/lib/api/helpers/resource_label_events_helpers.rb +++ b/lib/api/helpers/resource_label_events_helpers.rb @@ -15,4 +15,4 @@ module API end end -API::Helpers::ResourceLabelEventsHelpers.prepend_if_ee('EE::API::Helpers::ResourceLabelEventsHelpers') +API::Helpers::ResourceLabelEventsHelpers.prepend_mod_with('API::Helpers::ResourceLabelEventsHelpers') diff --git a/lib/api/helpers/runner.rb b/lib/api/helpers/runner.rb index 688cd2da994..6f25cf507bc 100644 --- a/lib/api/helpers/runner.rb +++ b/lib/api/helpers/runner.rb @@ -5,7 +5,7 @@ module API module Runner include Gitlab::Utils::StrongMemoize - prepend_if_ee('EE::API::Helpers::Runner') # rubocop: disable Cop/InjectEnterpriseEditionModule + prepend_mod_with('API::Helpers::Runner') # rubocop: disable Cop/InjectEnterpriseEditionModule JOB_TOKEN_HEADER = 'HTTP_JOB_TOKEN' JOB_TOKEN_PARAM = :token @@ -87,6 +87,10 @@ module API project: -> { current_job.project } ) end + + def track_ci_minutes_usage!(_build, _runner) + # noop: overridden in EE + end end end end diff --git a/lib/api/helpers/search_helpers.rb b/lib/api/helpers/search_helpers.rb index cb5f92fa62a..66321306496 100644 --- a/lib/api/helpers/search_helpers.rb +++ b/lib/api/helpers/search_helpers.rb @@ -25,4 +25,4 @@ module API end end -API::Helpers::SearchHelpers.prepend_if_ee('EE::API::Helpers::SearchHelpers') +API::Helpers::SearchHelpers.prepend_mod_with('API::Helpers::SearchHelpers') diff --git a/lib/api/helpers/services_helpers.rb b/lib/api/helpers/services_helpers.rb index 2f2ad88c942..d123db8e3df 100644 --- a/lib/api/helpers/services_helpers.rb +++ b/lib/api/helpers/services_helpers.rb @@ -420,44 +420,6 @@ module API }, chat_notification_events ].flatten, - 'hipchat' => [ - { - required: true, - name: :token, - type: String, - desc: 'The room token' - }, - { - required: false, - name: :room, - type: String, - desc: 'The room name or ID' - }, - { - required: false, - name: :color, - type: String, - desc: 'The room color' - }, - { - required: false, - name: :notify, - type: Boolean, - desc: 'Enable notifications' - }, - { - required: false, - name: :api_version, - type: String, - desc: 'Leave blank for default (v2)' - }, - { - required: false, - name: :server, - type: String, - desc: 'Leave blank for default. https://hipchat.example.com' - } - ], 'irker' => [ { required: true, @@ -803,7 +765,7 @@ module API required: true, name: :webhook, type: String, - desc: 'The Webex Teams webhook. e.g. https://api.ciscospark.com/v1/webhooks/incoming/…' + desc: 'The Webex Teams webhook. For example, https://api.ciscospark.com/v1/webhooks/incoming/...' }, chat_notification_events ].flatten @@ -812,23 +774,22 @@ module API def self.service_classes [ - ::AsanaService, - ::AssemblaService, - ::BambooService, + ::Integrations::Asana, + ::Integrations::Assembla, + ::Integrations::Bamboo, + ::Integrations::Campfire, + ::Integrations::Confluence, + ::Integrations::Datadog, + ::Integrations::EmailsOnPush, ::BugzillaService, ::BuildkiteService, - ::ConfluenceService, - ::CampfireService, ::CustomIssueTrackerService, - ::DatadogService, ::DiscordService, ::DroneCiService, - ::EmailsOnPushService, ::EwmService, ::ExternalWikiService, ::FlowdockService, ::HangoutsChatService, - ::HipchatService, ::IrkerService, ::JenkinsService, ::JiraService, @@ -858,4 +819,4 @@ module API end end -API::Helpers::ServicesHelpers.prepend_if_ee('EE::API::Helpers::ServicesHelpers') +API::Helpers::ServicesHelpers.prepend_mod_with('API::Helpers::ServicesHelpers') diff --git a/lib/api/helpers/settings_helpers.rb b/lib/api/helpers/settings_helpers.rb index 451e578fdd6..a3ea1057bc8 100644 --- a/lib/api/helpers/settings_helpers.rb +++ b/lib/api/helpers/settings_helpers.rb @@ -19,4 +19,4 @@ module API end end -API::Helpers::SettingsHelpers.prepend_if_ee('EE::API::Helpers::SettingsHelpers') +API::Helpers::SettingsHelpers.prepend_mod_with('API::Helpers::SettingsHelpers') diff --git a/lib/api/helpers/users_helpers.rb b/lib/api/helpers/users_helpers.rb index 2d7b22e66b3..1a019283bc6 100644 --- a/lib/api/helpers/users_helpers.rb +++ b/lib/api/helpers/users_helpers.rb @@ -22,4 +22,4 @@ module API end end -API::Helpers::UsersHelpers.prepend_if_ee('EE::API::Helpers::UsersHelpers') +API::Helpers::UsersHelpers.prepend_mod_with('API::Helpers::UsersHelpers') diff --git a/lib/api/helpers/variables_helpers.rb b/lib/api/helpers/variables_helpers.rb index e2b3372fc33..edbdcb257e7 100644 --- a/lib/api/helpers/variables_helpers.rb +++ b/lib/api/helpers/variables_helpers.rb @@ -24,4 +24,4 @@ module API end end -API::Helpers::VariablesHelpers.prepend_if_ee('EE::API::Helpers::VariablesHelpers') +API::Helpers::VariablesHelpers.prepend_mod_with('API::Helpers::VariablesHelpers') diff --git a/lib/api/helpers/wikis_helpers.rb b/lib/api/helpers/wikis_helpers.rb index 49da1e317ab..4a14dc1f40a 100644 --- a/lib/api/helpers/wikis_helpers.rb +++ b/lib/api/helpers/wikis_helpers.rb @@ -32,4 +32,4 @@ module API end end -API::Helpers::WikisHelpers.prepend_if_ee('EE::API::Helpers::WikisHelpers') +API::Helpers::WikisHelpers.prepend_mod_with('API::Helpers::WikisHelpers') -- cgit v1.2.3