Class: Danger::CircleCI

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

Overview

### CI Setup

For setting up Circle CI, we recommend turning on “Only Build pull requests.” in “Advanced Setting.” Without this enabled, it is really tricky for Danger to know whether you are in a pull request or not, as the environment metadata isn’t reliable.

With that set up, you can you add ‘bundle exec danger` to your `circle.yml`. If you override the default `test:` section, then add it as an extra step. Otherwise add a new `pre` section to the test:

``` ruby
 test:
   override:
      - bundle exec danger
```

### Token Setup

There is no difference here for OSS vs Closed, add your ‘DANGER_GITHUB_API_TOKEN` to the Environment variable settings page.

### I still want to run commit builds

OK, alright. So, if you add a ‘DANGER_CIRCLE_CI_API_TOKEN` then Danger will use the Circle API to look up the status of whether a commit is inside a PR or not. You can generate a token from inside the project set_trace_func then go to Permissions > “API Permissions” and generate a token with access to Status. Take that token and add it to Build Settings > “Environment Variables”.

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/danger/ci_source/circle.rb', line 57

def initialize(env)
  self.repo_url = env["CIRCLE_REPOSITORY_URL"]
  pr_url = env["CI_PULL_REQUEST"]

  # If it's not a real URL, use the Circle API
  unless pr_url && URI.parse(pr_url).kind_of?(URI::HTTP)
    api = CircleAPI.new
    pr_url = api.pull_request_url(env)
  end

  # We should either have got it via the API, or
  # an ENV var.
  pr_path = URI.parse(pr_url).path.split("/")
  if pr_path.count == 5
    # The first one is an extra slash, ignore it
    self.repo_slug = pr_path[1] + "/" + pr_path[2]
    self.pull_request_id = pr_path[4]

  else
    message = "Danger::Circle.rb considers this a PR, " \
              "but did not get enough information to get a repo slug" \
              "and PR id.\n\n" \
              "PR path: #{pr_url}\n" \
              "Keys: #{env.keys}"
    raise message.red
  end
end

Class Method Details

.validates_as_ci?(env) ⇒ Boolean

Side note: CircleCI is complicated. The env vars for PRs are not guaranteed to exist if the build was triggered from a commit, to look at examples of the different types of CI states, see this repo: github.com/orta/show_circle_env

Returns:

  • (Boolean)


37
38
39
# File 'lib/danger/ci_source/circle.rb', line 37

def self.validates_as_ci?(env)
  env.key? "CIRCLE_BUILD_NUM"
end

.validates_as_pr?(env) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.validates_as_pr?(env)
  # This will get used if it's available, instead of the API faffing.
  return true unless env["CI_PULL_REQUEST"].empty?

  # Real-world talk, it should be worrying if none of these are in the environment
  return false unless ["CIRCLE_CI_API_TOKEN", "CIRCLE_PROJECT_USERNAME", "CIRCLE_PROJECT_REPONAME", "CIRCLE_BUILD_NUM"].all? { |x| env[x] && !env[x].empty? }

  # Uses the Circle API to determine if it's a PR otherwise
  api = CircleAPI.new
  api.pull_request?(env)
end

Instance Method Details

#supported_request_sourcesObject



53
54
55
# File 'lib/danger/ci_source/circle.rb', line 53

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