Skip to content

Commit

Permalink
Merge pull request #57 from marcelofabri/circle-ci-workaround
Browse files Browse the repository at this point in the history
Calling CircleCI API when CI_PULL_REQUEST is not set
  • Loading branch information
orta committed Feb 12, 2016
2 parents f97888c + 79ecd15 commit ebf78ff
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Master

* Calling CircleCI API when `CI_PULL_REQUEST` is not set - marcelofabri
* Look inside PR JSON for the commit range (instead of getting from CI providers) - marcelofabri
* Adds `pr_labels` to DSL - marcelofabri
* Makes the CircleCI provider validate, but not run on non-PR builds - orta
Expand Down
33 changes: 30 additions & 3 deletions lib/danger/ci_source/circle.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
# https://circleci.com/docs/environment-variables
require 'uri'
require 'danger/circle_api'

module Danger
module CISource
class CircleCI < CI
def self.validates?(env)
return !env["CIRCLE_BUILD_NUM"].nil? && !env["CI_PULL_REQUEST"].nil?
return false if env["CIRCLE_BUILD_NUM"].nil?
return true unless env["CI_PULL_REQUEST"].nil?

return !env["CIRCLE_PROJECT_USERNAME"].nil? && !env["CIRCLE_PROJECT_REPONAME"].nil?
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 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 initialize(env)
if URI.parse(env["CI_PULL_REQUEST"]).path.split("/").count == 5
paths = URI.parse(env["CI_PULL_REQUEST"]).path.split("/")
@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]
Expand Down
23 changes: 23 additions & 0 deletions lib/danger/circle_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'faraday'

module Danger
class CircleAPI
attr_accessor :circle_token

def initialize(circle_token = nil)
self.circle_token = circle_token
end

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

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

0 comments on commit ebf78ff

Please sign in to comment.