Read about our upcoming Code of Conduct on this issue

commit_check.rb 1.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Gitlab
  module Checks
    class CommitCheck
      include Gitlab::Utils::StrongMemoize

      attr_reader :project, :user, :newrev, :oldrev

      def initialize(project, user, newrev, oldrev)
        @project = project
        @user = user
        @newrev = user
        @oldrev = user
        @file_paths = []
      end

      def validate(commit, validations)
        return if validations.empty? && path_validations.empty?

        commit.raw_deltas.each do |diff|
          @file_paths << (diff.new_path || diff.old_path)

          validations.each do |validation|
            if error = validation.call(diff)
              raise ::Gitlab::GitAccess::UnauthorizedError, error
            end
          end
        end
      end

      def validate_file_paths
        path_validations.each do |validation|
          if error = validation.call(@file_paths)
            raise ::Gitlab::GitAccess::UnauthorizedError, error
          end
        end
      end

      private

      def validate_lfs_file_locks?
        strong_memoize(:validate_lfs_file_locks) do
          project.lfs_enabled? && project.lfs_file_locks.any? && newrev && oldrev
        end
      end

      def lfs_file_locks_validation
        lambda do |paths|
          lfs_lock = project.lfs_file_locks.where(path: paths).where.not(user_id: user.id).first

          if lfs_lock
            return "The path '#{lfs_lock.path}' is locked in Git LFS by #{lfs_lock.user.name}"
          end
        end
      end

      def path_validations
        validate_lfs_file_locks? ? [lfs_file_locks_validation] : []
      end
    end
  end
end