Commit b6e8a0cd authored by alinamihaila's avatar alinamihaila

Move regex in constant

parent 3b9fb330
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
module Gitlab module Gitlab
module Redis module Redis
class HLL class HLL
KEY_REGEX = %r{\A(\w|-|:)*\{\w*\}(\w|-|:)*\z}.freeze
KeyFormatError = Class.new(StandardError) KeyFormatError = Class.new(StandardError)
def self.count(params) def self.count(params)
...@@ -25,8 +26,10 @@ module Gitlab ...@@ -25,8 +26,10 @@ module Gitlab
# project:{1}:set_a # project:{1}:set_a
# project:{1}:set_b # project:{1}:set_b
# project:{2}:set_c # project:{2}:set_c
# 2020-216-{project_action}
# i_{analytics}_dev_ops_score-2020-32
def add(key:, value:, expiry:) def add(key:, value:, expiry:)
unless %r{\A(\w|-|:)*\{\w*\}(\w|-|:)*\z}.match?(key) unless KEY_REGEX.match?(key)
raise KeyFormatError.new("Invalid key format. #{key} key should have changeable parts in curly braces. See https://docs.gitlab.com/ee/development/redis.html#multi-key-commands") raise KeyFormatError.new("Invalid key format. #{key} key should have changeable parts in curly braces. See https://docs.gitlab.com/ee/development/redis.html#multi-key-commands")
end end
......
...@@ -14,6 +14,8 @@ RSpec.describe Gitlab::Redis::HLL, :clean_gitlab_redis_shared_state do ...@@ -14,6 +14,8 @@ RSpec.describe Gitlab::Redis::HLL, :clean_gitlab_redis_shared_state do
expect { described_class.add(key: 'test-{metric}', value: 1, expiry: 1.day) }.not_to raise_error expect { described_class.add(key: 'test-{metric}', value: 1, expiry: 1.day) }.not_to raise_error
expect { described_class.add(key: 'test-{metric}-1', value: 1, expiry: 1.day) }.not_to raise_error expect { described_class.add(key: 'test-{metric}-1', value: 1, expiry: 1.day) }.not_to raise_error
expect { described_class.add(key: 'test:{metric}-1', value: 1, expiry: 1.day) }.not_to raise_error expect { described_class.add(key: 'test:{metric}-1', value: 1, expiry: 1.day) }.not_to raise_error
expect { described_class.add(key: '2020-216-{project_action}', value: 1, expiry: 1.day) }.not_to raise_error
expect { described_class.add(key: 'i_{analytics}_dev_ops_score-2020-32', value: 1, expiry: 1.day) }.not_to raise_error
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment