Sha256: c1b68094949e8af5c6862af0a9fc5b30e2f3736c1aad646e38e5f98881068d50

Contents?: true

Size: 1.79 KB

Versions: 20

Compression:

Stored size: 1.79 KB

Contents

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
shipit-engine-0.5.2 app/models/commit_checks.rb
shipit-engine-0.5.1 app/models/commit_checks.rb
shipit-engine-0.5.0 app/models/commit_checks.rb
shipit-engine-0.4.10 app/models/commit_checks.rb
shipit-engine-0.4.9 app/models/commit_checks.rb
shipit-engine-0.4.8 app/models/commit_checks.rb
shipit-engine-0.4.7 app/models/commit_checks.rb
shipit-engine-0.4.6 app/models/commit_checks.rb
shipit-engine-0.4.5 app/models/commit_checks.rb
shipit-engine-0.4.4 app/models/commit_checks.rb
shipit-engine-0.4.3 app/models/commit_checks.rb
shipit-engine-0.4.2 app/models/commit_checks.rb
shipit-engine-0.4.1 app/models/commit_checks.rb
shipit-engine-0.4.0 app/models/commit_checks.rb
shipit-engine-0.3.1 app/models/commit_checks.rb
shipit-engine-0.3.0 app/models/commit_checks.rb
shipit-engine-0.2.3 app/models/commit_checks.rb
shipit-engine-0.2.2 app/models/commit_checks.rb
shipit-engine-0.2.1 app/models/commit_checks.rb
shipit-engine-0.2.0 app/models/commit_checks.rb