2019-06-12 00:35:19 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Git
|
|
|
|
class WikiPushService < ::BaseService
|
2020-05-11 23:09:31 -04:00
|
|
|
# Maximum number of change events we will process on any single push
|
|
|
|
MAX_CHANGES = 100
|
|
|
|
|
2020-09-09 17:08:33 -04:00
|
|
|
attr_reader :wiki
|
|
|
|
|
|
|
|
def initialize(wiki, current_user, params)
|
2021-04-19 17:09:27 -04:00
|
|
|
@wiki = wiki
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params.dup
|
2020-09-09 17:08:33 -04:00
|
|
|
end
|
|
|
|
|
2019-06-12 00:35:19 -04:00
|
|
|
def execute
|
2020-09-09 17:08:33 -04:00
|
|
|
# Execute model-specific callbacks
|
|
|
|
wiki.after_post_receive
|
|
|
|
|
2020-05-11 23:09:31 -04:00
|
|
|
process_changes
|
2021-02-08 07:09:20 -05:00
|
|
|
perform_housekeeping
|
2020-05-11 23:09:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def process_changes
|
|
|
|
return unless can_process_wiki_events?
|
|
|
|
|
|
|
|
push_changes.take(MAX_CHANGES).each do |change| # rubocop:disable CodeReuse/ActiveRecord
|
|
|
|
next unless change.page.present?
|
|
|
|
|
|
|
|
response = create_event_for(change)
|
|
|
|
log_error(response.message) if response.error?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_process_wiki_events?
|
2020-09-09 17:08:33 -04:00
|
|
|
# TODO: Support activity events for group wikis
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/209306
|
2020-09-30 14:09:52 -04:00
|
|
|
wiki.is_a?(ProjectWiki)
|
2020-05-11 23:09:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def push_changes
|
|
|
|
default_branch_changes.flat_map do |change|
|
|
|
|
raw_changes(change).map { |raw| Git::WikiPushService::Change.new(wiki, change, raw) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_changes(change)
|
|
|
|
wiki.repository.raw.raw_changes_between(change[:oldrev], change[:newrev])
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_event_for(change)
|
2020-07-20 08:09:34 -04:00
|
|
|
event_service.execute(
|
|
|
|
change.last_known_slug,
|
|
|
|
change.page,
|
|
|
|
change.event_action,
|
|
|
|
change.sha
|
|
|
|
)
|
2020-05-11 23:09:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def event_service
|
|
|
|
@event_service ||= WikiPages::EventCreateService.new(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_default_branch?(change)
|
2020-09-09 17:08:33 -04:00
|
|
|
wiki.default_branch == ::Gitlab::Git.branch_name(change[:ref])
|
2020-05-11 23:09:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# See: [Gitlab::GitPostReceive#changes]
|
|
|
|
def changes
|
|
|
|
params[:changes] || []
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch_changes
|
|
|
|
@default_branch_changes ||= changes.select { |change| on_default_branch?(change) }
|
2019-06-12 00:35:19 -04:00
|
|
|
end
|
2021-01-26 13:09:30 -05:00
|
|
|
|
|
|
|
def perform_housekeeping
|
|
|
|
housekeeping = Repositories::HousekeepingService.new(wiki)
|
|
|
|
housekeeping.increment!
|
|
|
|
housekeeping.execute if housekeeping.needed?
|
|
|
|
rescue Repositories::HousekeepingService::LeaseTaken
|
|
|
|
# no-op
|
|
|
|
end
|
2019-06-12 00:35:19 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Git::WikiPushService.prepend_mod_with('Git::WikiPushService')
|