BigW Consortium Gitlab

action.rb 1.46 KB
Newer Older
1 2 3
module Gitlab
  module Satellite
    class Action
4
      DEFAULT_OPTIONS = { git_timeout: Gitlab.config.satellites.timeout.seconds }
5

6
      attr_accessor :options, :project, :user
7

8 9
      def initialize(user, project, options = {})
        @options = DEFAULT_OPTIONS.merge(options)
10
        @project = project
11
        @user = user
12 13 14 15 16 17 18 19
      end

      protected

      # * Sets a 30s timeout for Git
      # * Locks the satellite repo
      # * Yields the prepared satellite repo
      def in_locked_and_timed_satellite
20 21
        Gitlab::ShellEnv.set_env(user)

22
        Grit::Git.with_timeout(options[:git_timeout]) do
23 24
          project.satellite.lock do
            return yield project.satellite.repo
25 26 27
          end
        end
      rescue Errno::ENOMEM => ex
28
        return handle_exception(ex)
29
      rescue Grit::Git::GitTimeout => ex
30
        return handle_exception(ex)
31 32
      ensure
        Gitlab::ShellEnv.reset_env
33 34
      end

35
      # * Recreates the satellite
36 37 38 39
      # * Sets up Git variables for the user
      #
      # Note: use this within #in_locked_and_timed_satellite
      def prepare_satellite!(repo)
40
        project.satellite.clear_and_update!
41

42 43
        repo.config['user.name'] = user.name
        repo.config['user.email'] = user.email
44 45 46
      end

      def default_options(options = {})
47
        { raise: true, timeout: true }.merge(options)
48
      end
49 50 51 52 53

      def handle_exception(exception)
        Gitlab::GitLogger.error(exception.message)
        false
      end
54 55 56
    end
  end
end