Refactor checksum code: extract hexdigest from Upload and use in LfsObject and Gitlab::Ci::Trace
Summary
Follow up https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/14477#note_188552032:
This is sprinkled all over the codebase.
Digest::SHA256.file(file_path).hexdigest
I like the idea that each model knows how to calculate its checksum. We can introduce a new concern 'Checksummable' with some shared code?
Improvements
https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
For this iteration, the scope is limited to extracting hexdigest
out of Upload
and reusing it in only LfsObject
and Gitlab::Ci::Trace
. It looks like we'll need to add some tests too.
module Checksummable
extend ActiveSupport::Concern
class_methods do
def hexdigest(path)
Digest::SHA256.file(path).hexdigest
end
end
end
class Upload
include Checksummable
# remove this method
def self.hexdigest(path)
Digest::SHA256.file(path).hexdigest
end
# modify this method to be like
def calculate_checksum!
self.checksum = nil
return unless checksummable?
self.checksum = self.class.hexdigest(absolute_path)
end
end
Risks
Low risk. Just extracting shared code.
Involved components
Optional: Intended side effects
Optional: Missing test coverage
Edited by Rachel Nienaber