Class: Danger::CircleAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/danger/ci_source/circle_api.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



25
26
27
# File 'lib/danger/ci_source/circle_api.rb', line 25

def client
  @client ||= Faraday.new(url: "https://circleci.com/api/v1")
end

#fetch_build(repo_slug, build_number, token) ⇒ Object

Make the API call, and parse the JSON



36
37
38
39
40
41
42
# File 'lib/danger/ci_source/circle_api.rb', line 36

def fetch_build(repo_slug, build_number, token)
  url = "project/#{repo_slug}/#{build_number}"
  params = { "circle-token" => token }
  response = client.get url, params, accept: "application/json"
  json = JSON.parse(response.body, symbolize_names: true)
  json
end

#fetch_pull_request_url(repo_slug, build_number, token) ⇒ Object

Ask the API if the commit is inside a PR



30
31
32
33
# File 'lib/danger/ci_source/circle_api.rb', line 30

def fetch_pull_request_url(repo_slug, build_number, token)
  build_json = fetch_build(repo_slug, build_number, token)
  build_json[:pull_request_urls].first
end

#pull_request?(env) ⇒ Boolean

Determine if there’s a PR attached to this commit, and return a bool

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/danger/ci_source/circle_api.rb', line 7

def pull_request?(env)
  url = pull_request_url(env)
  return !url.nil?
end

#pull_request_url(env) ⇒ Object

Determine if there’s a PR attached to this commit, and return the url if so



14
15
16
17
18
19
20
21
22
23
# File 'lib/danger/ci_source/circle_api.rb', line 14

def pull_request_url(env)
  url = env["CI_PULL_REQUEST"]

  if url.nil? && !env["CIRCLE_PROJECT_USERNAME"].nil? && !env["CIRCLE_PROJECT_REPONAME"].nil?
    repo_slug = env["CIRCLE_PROJECT_USERNAME"] + "/" + env["CIRCLE_PROJECT_REPONAME"]
    token = env["DANGER_CIRCLE_CI_API_TOKEN"] || env["CIRCLE_CI_API_TOKEN"]
    url = fetch_pull_request_url(repo_slug, env["CIRCLE_BUILD_NUM"], token)
  end
  url
end