Class: Takeoff::Stage::VerifyCircleCiStatus

Inherits:
Base
  • Object
show all
Defined in:
lib/takeoff/stage/verify_circle_ci_status.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Helpers

#branches_up_to_date?, #diff, #execute, #file_has_changed_locally?, #files_have_changed?, #latest_commit, #log

Constructor Details

This class inherits a constructor from Takeoff::Stage::Base

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/takeoff/stage/verify_circle_ci_status.rb', line 10

def call(env)
  unless env[:github_repo] && ENV["GITHUB_OAUTH_TOKEN"]
    log "WARNING: Skipping verification of Circle CI status because GitHub repo or OAuth token isn't set."

    return @app.call(env)
  end

  raise "A GitHub OAuth token is required to check the Circle CI status." unless ENV["GITHUB_OAUTH_TOKEN"]

  sha = latest_commit(env[:development_branch])

  uri = URI.parse("https://api.github.com/repos/#{env[:github_repo]}/statuses/#{sha}")

  connection = Net::HTTP.new(uri.host, uri.port)
  connection.use_ssl = true

  request = Net::HTTP::Get.new(uri.request_uri)
  request["Authorization"] = "token #{ENV["GITHUB_OAUTH_TOKEN"]}"
  response = connection.request(request)

  statuses = JSON.parse(response.body)

  if statuses.find { |s| s["state"] == "success" }
    # Success
  elsif status = statuses.find { |s| %w(failure error).include?(s["state"]) }
    raise "The Circle CI tests for branch '#{env[:development_branch]}' (commit #{sha}) failed. Fix them and try again. See #{status["target_url"]}"
  elsif status = statuses.find { |s| s["state"] == "pending"}
    raise "The Circle CI tests for branch '#{env[:development_branch]}' (commit #{sha}) are still running. Wait for them to finish successfully. See #{status["target_url"]}"
  else
    raise "The Circle CI tests for branch '#{env[:development_branch]}' (commit #{sha}) have not run yet. Wait for them to start and finish successfully."
  end

  @app.call(env)
end