lib/danger/ci_source/circle.rb in danger-2.1.4 vs lib/danger/ci_source/circle.rb in danger-2.1.5
- old
+ new
@@ -20,66 +20,66 @@
#
# ### 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".
+ #
class CircleCI < CI
+ # 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: https://github.com/orta/show_circle_env
+
def self.validates_as_ci?(env)
env.key? "CIRCLE_BUILD_NUM"
end
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] }
+ 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 otherwose
- @circle_token = env["CIRCLE_CI_API_TOKEN"]
- !pull_request_url(env).nil?
+ # Uses the Circle API to determine if it's a PR otherwise
+ api = CircleAPI.new
+ api.pull_request?(env)
end
def supported_request_sources
@supported_request_sources ||= [Danger::RequestSources::GitHub]
end
- 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
-
- def client
- @client ||= CircleAPI.new(@circle_token)
- end
-
- 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
-
def initialize(env)
- self.repo_url = GitRepo.new.origins # CircleCI doesn't provide a repo url env variable :/
-
+ 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)
- @circle_token = env["CIRCLE_CI_API_TOKEN"]
- pr_url = pull_request_url(env)
+ 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
end
end