<commit_before>module Ruboty
  module Capistrano
    module Actions
      class Deploy < Ruboty::Actions::Base
        class DeployError < StandardError; end

        attr_reader :repo_path, :env, :branch, :logger

        def initialize(env:, repo_path:, branch:)
          @env = env
          @repo_path = repo_path
          @branch = branch
          @logger = Logger.new(deploy_log_path)
        end

        def call
          deploy
        rescue => e
          logger.error e.message
          raise e, unknown_error
        end

        private

        def deploy
          cmd = "cd #{repo_path} && bundle && bundle exec cap #{env} deploy BRANCH=#{branch}"
          out, err, status = Bundler.with_clean_env { Open3.capture3(cmd) }
          logger.info out
          raise DeployError.new(err) unless err.empty?
        end

        def deploy_log_path
          return STDOUT if Ruboty::Capistrano.config.log_path.to_s.empty?

          File.join(Ruboty::Capistrano.config.log_path.to_s, "#{DateTime.now.strftime('%Y%m%d%H%M')}.log")
        end

        def unknown_error
          <<~TEXT
            :cop:問題が発生しました:cop:
          TEXT
        end
      end
    end
  end
end<commit_msg>Refactor<commit_after>module Ruboty
  module Capistrano
    module Actions
      class Deploy < Ruboty::Actions::Base
        class DeployError < StandardError; end

        attr_reader :repo_path, :env, :branch, :logger

        def initialize(env:, repo_path:, branch:)
          @env = env
          @repo_path = repo_path
          @branch = branch
          @logger = Logger.new(deploy_log_path)
        end

        def call
          deploy
        rescue => e
          logger.error e.message
          raise e, ':cop:問題が発生しました:cop:'
        end

        private

        def deploy
          cmd = "cd #{repo_path} && bundle && bundle exec cap #{env} deploy BRANCH=#{branch}"
          out, err, status = Bundler.with_clean_env { Open3.capture3(cmd) }
          logger.info out
          raise DeployError.new(err) unless err.empty?
        end

        def deploy_log_path
          return STDOUT if Ruboty::Capistrano.config.log_path.to_s.empty?

          File.join(Ruboty::Capistrano.config.log_path.to_s, "#{DateTime.now.strftime('%Y%m%d%H%M')}.log")
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09657860545690775, 'fraction_numerical': 0.0017323516673884798, 'mean_word_length': 1.9166666666666667, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5311054', 'n_tokens_mistral': 738, 'n_tokens_neox': 713, 'n_words': 165}