debian-mirror-gitlab/app/services/ci/parse_dotenv_artifact_service.rb

72 lines
1.8 KiB
Ruby
Raw Normal View History

2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
module Ci
class ParseDotenvArtifactService < ::BaseService
2021-12-11 22:18:48 +05:30
include ::Gitlab::Utils::StrongMemoize
2020-04-08 14:13:33 +05:30
SizeLimitError = Class.new(StandardError)
ParserError = Class.new(StandardError)
def execute(artifact)
validate!(artifact)
variables = parse!(artifact)
Ci::JobVariable.bulk_insert!(variables)
success
rescue SizeLimitError, ParserError, ActiveRecord::RecordInvalid => error
Gitlab::ErrorTracking.track_exception(error, job_id: artifact.job_id)
error(error.message, :bad_request)
end
private
def validate!(artifact)
unless artifact&.dotenv?
raise ArgumentError, 'Artifact is not dotenv file type'
end
2021-12-11 22:18:48 +05:30
unless artifact.file.size < dotenv_size_limit
2020-04-08 14:13:33 +05:30
raise SizeLimitError,
2021-12-11 22:18:48 +05:30
"Dotenv Artifact Too Big. Maximum Allowable Size: #{dotenv_size_limit}"
2020-04-08 14:13:33 +05:30
end
end
def parse!(artifact)
variables = []
artifact.each_blob do |blob|
blob.each_line do |line|
key, value = scan_line!(line)
variables << Ci::JobVariable.new(job_id: artifact.job_id,
source: :dotenv, key: key, value: value)
end
end
2021-12-11 22:18:48 +05:30
if variables.size > dotenv_variable_limit
2020-04-08 14:13:33 +05:30
raise SizeLimitError,
2021-12-11 22:18:48 +05:30
"Dotenv files cannot have more than #{dotenv_variable_limit} variables"
2020-04-08 14:13:33 +05:30
end
variables
end
def scan_line!(line)
2020-11-24 15:15:51 +05:30
result = line.scan(/^(.*?)=(.*)$/).last
2020-04-08 14:13:33 +05:30
raise ParserError, 'Invalid Format' if result.nil?
result.each(&:strip!)
end
2021-12-11 22:18:48 +05:30
def dotenv_variable_limit
strong_memoize(:dotenv_variable_limit) { project.actual_limits.dotenv_variables }
end
def dotenv_size_limit
strong_memoize(:dotenv_size_limit) { project.actual_limits.dotenv_size }
end
2020-04-08 14:13:33 +05:30
end
end