Class: CiToolkit::BuildStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/ci_toolkit/build_status.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(context = "Builds", github = CiToolkit::DvcsPrFactory.create(CiToolkit::BitriseEnv.new), env = CiToolkit::BitriseEnv.new) ⇒ BuildStatus

Returns a new instance of BuildStatus.



7
8
9
10
11
12
13
14
15
# File 'lib/ci_toolkit/build_status.rb', line 7

def initialize(
  context = "Builds",
  github = CiToolkit::DvcsPrFactory.create(CiToolkit::BitriseEnv.new),
  env = CiToolkit::BitriseEnv.new
)
  @context = context
  @github = github
  @env = env
end

Instance Method Details

#error(state = CiToolkit::DvcsPrUtil.status_state_error) ⇒ Object



42
43
44
# File 'lib/ci_toolkit/build_status.rb', line 42

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

#increment(status = CiToolkit::DvcsPrUtil.status_state_pending) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ci_toolkit/build_status.rb', line 29

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

#load_counterObject



46
47
48
49
50
51
52
# File 'lib/ci_toolkit/build_status.rb', line 46

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

#start(num_builds, state = "pending") ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ci_toolkit/build_status.rb', line 17

def start(num_builds, 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