2018-07-18 12:03:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-28 02:26:52 -05:00
|
|
|
module WikiPages
|
2020-03-24 08:09:42 -04:00
|
|
|
# There are 3 notions of 'action' that inheriting classes must implement:
|
|
|
|
#
|
|
|
|
# - external_action: the action we report to external clients with webhooks
|
|
|
|
# - usage_counter_action: the action that we count in out internal counters
|
|
|
|
# - event_action: what we record as the value of `Event#action`
|
2020-05-13 08:07:54 -04:00
|
|
|
class BaseService < ::BaseContainerService
|
2016-02-28 02:26:52 -05:00
|
|
|
private
|
|
|
|
|
2020-03-24 08:09:42 -04:00
|
|
|
def execute_hooks(page)
|
|
|
|
page_data = payload(page)
|
2020-05-13 08:07:54 -04:00
|
|
|
container.execute_hooks(page_data, :wiki_page_hooks)
|
|
|
|
container.execute_services(page_data, :wiki_page_hooks)
|
2020-03-24 08:09:42 -04:00
|
|
|
increment_usage
|
|
|
|
create_wiki_event(page)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Passed to web-hooks, and send to external consumers.
|
|
|
|
def external_action
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
# Passed to the WikiPageCounter to count events.
|
|
|
|
# Must be one of WikiPageCounter::KNOWN_EVENTS
|
|
|
|
def usage_counter_action
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
# Used to create `Event` records.
|
|
|
|
# Must be a valid value for `Event#action`
|
|
|
|
def event_action
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def payload(page)
|
|
|
|
Gitlab::DataBuilder::WikiPage.build(page, current_user, external_action)
|
2019-07-20 21:26:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# This method throws an error if the action is an unanticipated value.
|
2020-03-24 08:09:42 -04:00
|
|
|
def increment_usage
|
|
|
|
Gitlab::UsageDataCounters::WikiPageCounter.count(usage_counter_action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_wiki_event(page)
|
2020-07-20 08:09:34 -04:00
|
|
|
response = WikiPages::EventCreateService
|
|
|
|
.new(current_user)
|
|
|
|
.execute(slug_for_page(page), page, event_action, fingerprint(page))
|
2020-03-24 08:09:42 -04:00
|
|
|
|
2020-05-11 23:09:31 -04:00
|
|
|
log_error(response.message) if response.error?
|
2020-03-24 08:09:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def slug_for_page(page)
|
|
|
|
page.slug
|
2016-02-28 02:26:52 -05:00
|
|
|
end
|
2020-07-20 08:09:34 -04:00
|
|
|
|
|
|
|
def fingerprint(page)
|
|
|
|
page.sha
|
|
|
|
end
|
2016-02-28 02:26:52 -05:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
WikiPages::BaseService.prepend_if_ee('EE::WikiPages::BaseService')
|