Class: Danger::CircleCI
Overview
### CI Setup
For setting up CircleCI, we recommend turning on “Only build pull requests” in “Advanced Settings.” Without this enabled, it’s trickier for Danger to determine whether you’re in a pull request or not, as the environment metadata isn’t as reliable.
A common scenario is when CircleCI begins building a commit before the commit becomes associated with a PR (e.g. a developer pushes their branch to the remote repo for the first time. CircleCI spins up and begins building. Moments later the developer creates a PR on GitHub. Since the build process started before the PR existed, Danger won’t be able to use the Circle-provided environment variables to retrieve PR metadata.)
With “Only build pull requests” enabled, you can add ‘bundle exec danger` to your `config.yml` (Circle 2.0).
e.g.
```yaml
- run: bundle exec danger --verbose
```
And that should be it!
### Token Setup
If “Only build pull requests” can’t be enabled for your project, Danger can still work by relying on CircleCI’s API to retrieve PR metadata, which will require an API token.
-
Go to your project > Settings > API Permissions. Create a token with scope “view-builds” and a label like “DANGER_CIRCLE_CI_API_TOKEN”.
-
Settings > Environment Variables. Add the token as a CircleCI environment variable, which exposes it to the Danger process.
There is no difference here for OSS vs Closed, both scenarios will need this environment variable.
With these pieces in place, Danger should be able to work as expected.
Instance Attribute Summary
Attributes inherited from CI
#pull_request_id, #repo_slug, #repo_url
Class Method Summary collapse
-
.validates_as_ci?(env) ⇒ Boolean
Side note: CircleCI is complicated.
- .validates_as_pr?(env) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(env) ⇒ CircleCI
constructor
A new instance of CircleCI.
- #supported_request_sources ⇒ Object
Methods inherited from CI
available_ci_sources, inherited, #supports?
Constructor Details
#initialize(env) ⇒ CircleCI
Returns a new instance of CircleCI.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/danger/ci_source/circle.rb', line 66 def initialize(env) self.repo_url = env["CIRCLE_REPOSITORY_URL"] pr_url = env["CI_PULL_REQUEST"] || env["CIRCLE_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 = "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 .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
45 46 47 |
# File 'lib/danger/ci_source/circle.rb', line 45 def self.validates_as_ci?(env) env.key? "CIRCLE_BUILD_NUM" end |
.validates_as_pr?(env) ⇒ Boolean
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/danger/ci_source/circle.rb', line 49 def self.validates_as_pr?(env) # This will get used if it's available, instead of the API faffing. return true if env["CI_PULL_REQUEST"] && !env["CI_PULL_REQUEST"].empty? return true if env["CIRCLE_PULL_REQUEST"] && !env["CIRCLE_PULL_REQUEST"].empty? # Real-world talk, it should be worrying if none of these are in the environment return false unless ["DANGER_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_sources ⇒ Object
62 63 64 |
# File 'lib/danger/ci_source/circle.rb', line 62 def supported_request_sources @supported_request_sources ||= [Danger::RequestSources::GitHub, Danger::RequestSources::BitbucketCloud] end |