Sha256: 6bd814ea683d14914b5f38734e52b3fd0163cb8cac46e67adb42d944e865afa7

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module CiToolkit
  # allows to have a combined build status for all builds that are triggered for a pull request
  # it uses the description of the status check on Github to parse the number of builds remaining and total
  class BuildStatus
    def initialize(
      context = "Builds",
      github = CiToolkit::DvcsPrFactory.create(CiToolkit::BitriseEnv.new),
      env = CiToolkit::BitriseEnv.new
    )
      @context = context
      @github = github
      @env = env
    end

    def start(num_builds)
      state = CiToolkit::DvcsPrUtil.status_state_pending
      target_url = @env.app_url
      desc = "Finished building 0/#{num_builds}"
      if num_builds.zero?
        state = "success"
        desc = "No builds assigned"
        target_url = @env.build_url
      end

      @github.create_status(state, @context, target_url, desc)
    end

    def increment(status = CiToolkit::DvcsPrUtil.status_state_pending)
      counter = load_counter
      return if counter.nil?

      num_finished = counter[:num_finished] + 1
      num_total = counter[:num_total]

      state = status
      state = "success" if num_finished == num_total

      @github.create_status(state, @context, @env.app_url, "Finished building #{num_finished}/#{num_total}")
    end

    def error(state = CiToolkit::DvcsPrUtil.status_state_error)
      @github.create_status(state, @context, @env.app_url, "Building failed")
    end

    private

    def load_counter
      description = @github.get_status_description(@context)
      return if description.nil?

      build_counter = description[%r{(\d/\d)}] || "0/0"
      { num_finished: build_counter.split("/")[0].to_i, num_total: build_counter.split("/")[1].to_i }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ci_toolkit-1.5.20 lib/ci_toolkit/build_status.rb