Class: Danger::CISource::CircleCI

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

Overview

Instance Attribute Summary

Attributes inherited from CI

#pull_request_id, #repo_slug, #repo_url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CI

available_ci_sources, inherited, #supports?

Constructor Details

#initialize(env) ⇒ CircleCI

Returns a new instance of CircleCI.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/danger/ci_source/circle.rb', line 41

def initialize(env)
  self.repo_url = GitRepo.new.origins # CircleCI doesn't provide a repo url env variable :/

  @circle_token = env["CIRCLE_CI_API_TOKEN"]
  url = pull_request_url(env)

  if URI.parse(url).path.split("/").count == 5
    paths = URI.parse(url).path.split("/")
    # The first one is an extra slash, ignore it
    self.repo_slug = paths[1] + "/" + paths[2]
    self.pull_request_id = paths[4]
  end
end

Class Method Details

.validates?(env) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/danger/ci_source/circle.rb', line 9

def self.validates?(env)
  return false unless env["CIRCLE_BUILD_NUM"]
  return false unless env["CIRCLE_PROJECT_USERNAME"]
  return false unless env["CIRCLE_PROJECT_REPONAME"]

  return true
end

Instance Method Details

#clientObject



21
22
23
# File 'lib/danger/ci_source/circle.rb', line 21

def client
  @client ||= CircleAPI.new(@circle_token)
end

#fetch_pull_request_url(repo_slug, build_number) ⇒ Object



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

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

#pull_request_url(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/danger/ci_source/circle.rb', line 30

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"]
    url = fetch_pull_request_url(repo_slug, env["CIRCLE_BUILD_NUM"])
  end

  url
end

#supported_request_sourcesObject



17
18
19
# File 'lib/danger/ci_source/circle.rb', line 17

def supported_request_sources
  @supported_request_sources ||= [Danger::RequestSources::GitHub]
end