Sha256: f344da4be4686b76ca03058b9b731282e18845685950f446920a8f0b876c50bc

Contents?: true

Size: 1.95 KB

Versions: 18

Compression:

Stored size: 1.95 KB

Contents

module Shipit
  class CommitChecks
    OUTPUT_TTL = 10.minutes.to_i
    FINAL_STATUSES = %w(failed error success)

    def initialize(commit)
      @commit = commit
    end

    def run
      self.status = 'running'
      commands = StackCommands.new(stack)
      commands.with_temporary_working_directory(commit: commit) do |directory|
        deploy_spec = DeploySpec::FileSystem.new(directory, stack.environment)
        Bundler.with_clean_env do
          capture_all(build_commands(deploy_spec.dependencies_steps, chdir: directory))
          capture_all(build_commands(deploy_spec.review_checks, chdir: directory))
        end
      end
    rescue Command::Error
      self.status = 'failed'
    rescue
      self.status = 'error'
      raise
    else
      self.status = 'success'
    end

    def schedule
      if redis.set('output', '', ex: OUTPUT_TTL, nx: true)
        self.status = 'scheduled'
        PerformCommitChecksJob.perform_later(commit: commit)
      end
    end

    def status
      @status ||= redis.get('status')
    end

    def status=(status)
      redis.set('status', status, ex: OUTPUT_TTL)
      @status = status
    end

    def finished?
      FINAL_STATUSES.include?(status)
    end

    def output(since: 0)
      redis.getrange('output', since, -1)
    end

    def write(output)
      redis.append('output', output)
    end

    private

    def build_commands(commands, chdir:)
      commands.map { |c| Command.new(c, env: Shipit.env, chdir: chdir) }
    end

    def capture_all(commands)
      commands.map { |c| capture(c) }
    end

    def capture(command)
      command.start
      write("$ #{command}\n")
      command.stream! do |line|
        write(line)
      end
    rescue Command::Error => error
      write(error.message)
      raise
    ensure
      write("\n")
    end

    attr_reader :commit

    def redis
      @redis ||= Shipit.redis("commit:#{commit.id}:checks")
    end

    def stack
      @stack ||= commit.stack
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
shipit-engine-0.10.0 app/models/shipit/commit_checks.rb
shipit-engine-0.9.0 app/models/shipit/commit_checks.rb
shipit-engine-0.8.9 app/models/shipit/commit_checks.rb
shipit-engine-0.8.8 app/models/shipit/commit_checks.rb
shipit-engine-0.8.7 app/models/shipit/commit_checks.rb
shipit-engine-0.8.6 app/models/shipit/commit_checks.rb
shipit-engine-0.8.5 app/models/shipit/commit_checks.rb
shipit-engine-0.8.4 app/models/shipit/commit_checks.rb
shipit-engine-0.8.3 app/models/shipit/commit_checks.rb
shipit-engine-0.8.2 app/models/shipit/commit_checks.rb
shipit-engine-0.8.1 app/models/shipit/commit_checks.rb
shipit-engine-0.8.0 app/models/shipit/commit_checks.rb
shipit-engine-0.7.0 app/models/shipit/commit_checks.rb
shipit-engine-0.6.4 app/models/shipit/commit_checks.rb
shipit-engine-0.6.3 app/models/shipit/commit_checks.rb
shipit-engine-0.6.2 app/models/shipit/commit_checks.rb
shipit-engine-0.6.1 app/models/shipit/commit_checks.rb
shipit-engine-0.6.0 app/models/shipit/commit_checks.rb