2018-10-11 16:12:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-01-26 09:28:08 -05:00
|
|
|
module Gitlab
|
|
|
|
module Checks
|
2018-02-02 10:27:30 -05:00
|
|
|
class PostPushMessage
|
2020-03-17 08:09:52 -04:00
|
|
|
def initialize(repository, user, protocol)
|
|
|
|
@repository = repository
|
2018-01-26 09:28:08 -05:00
|
|
|
@user = user
|
|
|
|
@protocol = protocol
|
|
|
|
end
|
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
def self.fetch_message(user, repository)
|
|
|
|
key = message_key(user, repository)
|
|
|
|
|
|
|
|
# Also check for messages in the legacy key
|
|
|
|
# TODO: Remove in the next release
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/292030
|
|
|
|
legacy_key = legacy_message_key(user, repository) if respond_to?(:legacy_message_key)
|
2018-01-26 09:28:08 -05:00
|
|
|
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
|
|
|
message = redis.get(key)
|
|
|
|
redis.del(key)
|
2021-06-22 11:06:55 -04:00
|
|
|
|
|
|
|
if legacy_key
|
|
|
|
legacy_message = redis.get(legacy_key)
|
|
|
|
redis.del(legacy_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
legacy_message || message
|
2018-01-26 09:28:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_message
|
2021-06-22 11:06:55 -04:00
|
|
|
return unless user && repository
|
2018-01-26 09:28:08 -05:00
|
|
|
|
|
|
|
Gitlab::Redis::SharedState.with do |redis|
|
2021-06-22 11:06:55 -04:00
|
|
|
key = self.class.message_key(user, repository)
|
2018-01-26 09:28:08 -05:00
|
|
|
redis.setex(key, 5.minutes, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def message
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2020-03-17 08:09:52 -04:00
|
|
|
attr_reader :repository, :user, :protocol
|
|
|
|
|
|
|
|
delegate :project, to: :repository, allow_nil: true
|
|
|
|
delegate :container, to: :repository, allow_nil: false
|
2018-01-26 09:28:08 -05:00
|
|
|
|
2021-06-22 11:06:55 -04:00
|
|
|
def self.message_key(user, repository)
|
2018-01-26 09:28:08 -05:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_to_repo
|
2020-10-05 11:08:56 -04:00
|
|
|
protocol == 'ssh' ? container.ssh_url_to_repo : container.http_url_to_repo
|
2018-01-26 09:28:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|