diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-22 06:09:47 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-22 06:09:47 +0000 |
commit | d17c58402b28c7eabe74df21b57ae31beec56b1f (patch) | |
tree | 5508b8bc06c882dc2aa567d1bd0df2fd491617c7 /app/models/project_services/hipchat_service.rb | |
parent | a6cfee85e9a1fd544ffe0b985a5a2ecb642e6ab3 (diff) | |
download | gitlab-ce-d17c58402b28c7eabe74df21b57ae31beec56b1f.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/project_services/hipchat_service.rb')
-rw-r--r-- | app/models/project_services/hipchat_service.rb | 141 |
1 files changed, 11 insertions, 130 deletions
diff --git a/app/models/project_services/hipchat_service.rb b/app/models/project_services/hipchat_service.rb index cd49c6d253d..93f3d93bd2b 100644 --- a/app/models/project_services/hipchat_service.rb +++ b/app/models/project_services/hipchat_service.rb @@ -1,54 +1,17 @@ # frozen_string_literal: true +# This service is scheduled for removal. All records must +# be deleted before the class can be removed. +# https://gitlab.com/gitlab-org/gitlab/-/issues/27954 class HipchatService < Service - include ActionView::Helpers::SanitizeHelper - - MAX_COMMITS = 3 - HIPCHAT_ALLOWED_TAGS = %w[ - a b i strong em br img pre code - table th tr td caption colgroup col thead tbody tfoot - ul ol li dl dt dd - ].freeze - - prop_accessor :token, :room, :server, :color, :api_version - boolean_accessor :notify_only_broken_pipelines, :notify - validates :token, presence: true, if: :activated? - - def initialize_properties - if properties.nil? - self.properties = {} - self.notify_only_broken_pipelines = true - end - end - - def title - 'HipChat' - end - - def description - 'Private group chat and IM' - end + before_save :prevent_save def self.to_param 'hipchat' end - def fields - [ - { type: 'text', name: 'token', placeholder: 'Room token', required: true }, - { type: 'text', name: 'room', placeholder: 'Room name or ID' }, - { type: 'checkbox', name: 'notify' }, - { type: 'select', name: 'color', choices: %w(yellow red green purple gray random) }, - { type: 'text', name: 'api_version', title: _('API version'), - placeholder: 'Leave blank for default (v2)' }, - { type: 'text', name: 'server', - placeholder: 'Leave blank for default. https://hipchat.example.com' }, - { type: 'checkbox', name: 'notify_only_broken_pipelines' } - ] - end - def self.supported_events - %w(push issue confidential_issue merge_request note confidential_note tag_push pipeline) + [] end def execute(data) @@ -56,96 +19,14 @@ class HipchatService < Service # HipChat is unusable anyway, so do nothing in this method end - def test(data) - begin - result = execute(data) - rescue StandardError => error - return { success: false, result: error } - end - - { success: true, result: result } - end - private - def message_options(data = nil) - { notify: notify.present? && Gitlab::Utils.to_boolean(notify), color: message_color(data) } - end - - def render_line(text) - markdown(text.lines.first.chomp, pipeline: :single_line) if text - end - - def markdown(text, options = {}) - return "" unless text - - context = { - project: project, - pipeline: :email - } - - Banzai.render(text, context) - - context.merge!(options) - - html = Banzai.render_and_post_process(text, context) - sanitized_html = sanitize(html, tags: HIPCHAT_ALLOWED_TAGS, attributes: %w[href title alt]) - - sanitized_html.truncate(200, separator: ' ', omission: '...') - end - - def format_title(title) - "<b>#{render_line(title)}</b>" - end - - def message_color(data) - pipeline_status_color(data) || color || 'yellow' - end - - def pipeline_status_color(data) - return unless data && data[:object_kind] == 'pipeline' - - case data[:object_attributes][:status] - when 'success' - 'green' - else - 'red' - end - end - - def project_name - project.full_name.gsub(/\s/, '') - end - - def project_url - project.web_url - end - - def project_link - "<a href=\"#{project_url}\">#{project_name}</a>" - end - - def update?(data) - data[:object_attributes][:action] == 'update' - end - - def humanized_status(status) - case status - when 'success' - 'passed' - else - status - end - end + def prevent_save + errors.add(:base, _('HipChat endpoint is deprecated and should not be created or modified.')) - def should_pipeline_be_notified?(data) - case data[:object_attributes][:status] - when 'success' - !notify_only_broken_pipelines? - when 'failed' - true - else - false - end + # Stops execution of callbacks and database operation while + # preserving expectations of #save (will not raise) & #save! (raises) + # https://guides.rubyonrails.org/active_record_callbacks.html#halting-execution + throw :abort # rubocop:disable Cop/BanCatchThrow end end |