diff --git a/CHANGELOG.md b/CHANGELOG.md index a1b20faa3..fc130bbf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/danger/ci_source/circle.rb b/lib/danger/ci_source/circle.rb index 1dbc2ad22..f4b070226 100644 --- a/lib/danger/ci_source/circle.rb +++ b/lib/danger/ci_source/circle.rb @@ -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] diff --git a/lib/danger/circle_api.rb b/lib/danger/circle_api.rb new file mode 100644 index 000000000..4167c318a --- /dev/null +++ b/lib/danger/circle_api.rb @@ -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 diff --git a/spec/fixtures/circle_build_response.json b/spec/fixtures/circle_build_response.json new file mode 100644 index 000000000..21805fc38 --- /dev/null +++ b/spec/fixtures/circle_build_response.json @@ -0,0 +1 @@ +{"compare":"https://github.com/artsy/eigen/commit/1b064fe71ac7","previous_successful_build":{"build_num":1498,"status":"success","build_time_millis":544632},"build_parameters":null,"oss":true,"committer_date":"2016-02-10T09:30:48Z","steps":[{"name":"Starting the build","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"Starting the build","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:18.526Z","type":"infrastructure","output_url":"https://circle-production-action-output.s3.amazonaws.com/d52d558a91055bdd2630bb65-artsy-eigen-0-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=GvKkVCxkz09%2FT88TAmRkU9N09VU%3D","start_time":"2016-02-10T09:31:14.802Z","exit_code":null,"canceled":null,"step":0,"run_time_millis":3724,"has_output":true}]},{"name":"Start container","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Start container","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:20.274Z","source":"config","type":"infrastructure","output_url":"https://circle-production-action-output.s3.amazonaws.com/e52d558a91055bdd6630bb65-artsy-eigen-1-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=MZQlsp6X9Xu6iBZl5CLkEkcakXk%3D","start_time":"2016-02-10T09:31:18.532Z","exit_code":0,"canceled":null,"step":1,"run_time_millis":1742,"has_output":true}]},{"name":"Enable SSH","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Enable SSH","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:20.921Z","type":"infrastructure","output_url":"https://circle-production-action-output.s3.amazonaws.com/f52d558a91055bdd8630bb65-artsy-eigen-2-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=L%2Bb8L6prU6aiBeXmOMspWEQCB58%3D","start_time":"2016-02-10T09:31:20.280Z","exit_code":null,"canceled":null,"step":2,"run_time_millis":641,"has_output":true}]},{"name":"Restore source cache","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"Restore source cache","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:24.518Z","source":"cache","type":"checkout","output_url":"https://circle-production-action-output.s3.amazonaws.com/062d558a91055bdd8630bb65-artsy-eigen-3-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=aRa%2Fs2%2BI4btp9dAIt7DEuliKjFc%3D","start_time":"2016-02-10T09:31:20.929Z","exit_code":null,"canceled":null,"step":3,"run_time_millis":3589,"has_output":true}]},{"name":"Checkout using deploy key: c1:49:95:9d:84:4e:36:1a:78:d7:bc:bd:e6:6a:4e:2a","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Checkout using deploy key: c1:49:95:9d:84:4e:36:1a:78:d7:bc:bd:e6:6a:4e:2a","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:27.443Z","source":"config","type":"checkout","output_url":"https://circle-production-action-output.s3.amazonaws.com/162d558a91055bddc630bb65-artsy-eigen-4-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=306MVpVbp%2F81mJlm4E56WlWsjks%3D","start_time":"2016-02-10T09:31:24.525Z","exit_code":0,"canceled":null,"step":4,"run_time_millis":2918,"has_output":true}]},{"name":"Configure the build","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"Configure the build","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:29.117Z","source":"cache","type":"machine","output_url":"https://circle-production-action-output.s3.amazonaws.com/262d558a91055bddf630bb65-artsy-eigen-5-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=Tucr%2BPveBtJ%2BbYpqeXm2ZkBzYII%3D","start_time":"2016-02-10T09:31:27.451Z","exit_code":null,"canceled":null,"step":5,"run_time_millis":1666,"has_output":true}]},{"name":"Exporting env vars from circle.yml","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Exporting env vars from circle.yml","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:29.427Z","source":"config","type":"machine","output_url":"https://circle-production-action-output.s3.amazonaws.com/362d558a91055bdd1730bb65-artsy-eigen-6-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=yUtEfy0xtAUoqCZqxb6ztZB46hw%3D","start_time":"2016-02-10T09:31:29.123Z","exit_code":0,"canceled":null,"step":6,"run_time_millis":304,"has_output":true}]},{"name":"Exporting env vars from project settings","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Exporting env vars from project settings","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:29.737Z","source":"db","type":"machine","output_url":"https://circle-production-action-output.s3.amazonaws.com/462d558a91055bdd1730bb65-artsy-eigen-7-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=rb60MLo682KnK%2Fg59UhdomgsxA0%3D","start_time":"2016-02-10T09:31:29.433Z","exit_code":0,"canceled":null,"step":7,"run_time_millis":304,"has_output":true}]},{"name":"Select Xcode Version","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Select Xcode Version","bash_command":"sudo xcode-select --switch /Applications/Xcode-7.2.app","status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:30.056Z","source":"inference","type":"machine","start_time":"2016-02-10T09:31:29.744Z","exit_code":0,"canceled":null,"step":8,"run_time_millis":312,"has_output":false}]},{"name":"Restore cache","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"Restore cache","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:35.580Z","source":"cache","type":"machine","output_url":"https://circle-production-action-output.s3.amazonaws.com/662d558a91055bdd2730bb65-artsy-eigen-9-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=LBFcAZJjt0x1HxXY6yRJ7%2FC4XuY%3D","start_time":"2016-02-10T09:31:30.061Z","exit_code":null,"canceled":null,"step":9,"run_time_millis":5519,"has_output":true}]},{"name":"echo \"machine github.com login $GITHUB_API_KEY\" > ~/.netrc","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"echo \"machine github.com login $GITHUB_API_KEY\" > ~/.netrc","bash_command":"echo \"machine github.com login $GITHUB_API_KEY\" > ~/.netrc","status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:35.901Z","source":"config","type":"machine","start_time":"2016-02-10T09:31:35.588Z","exit_code":0,"canceled":null,"step":10,"run_time_millis":313,"has_output":false}]},{"name":"chmod 600 ~/.netrc","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"chmod 600 ~/.netrc","bash_command":"chmod 600 ~/.netrc","status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:36.209Z","source":"config","type":"machine","start_time":"2016-02-10T09:31:35.906Z","exit_code":0,"canceled":null,"step":11,"run_time_millis":303,"has_output":false}]},{"name":"bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3 --without development","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3 --without development","bash_command":"bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3 --without development","status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:31:48.838Z","source":"config","type":"dependencies","output_url":"https://circle-production-action-output.s3.amazonaws.com/962d558a91055bdd8730bb65-artsy-eigen-12-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=%2F%2Bf7p7FJGLTmOKOtyoxYg%2FC2k6U%3D","start_time":"2016-02-10T09:31:36.216Z","exit_code":0,"canceled":null,"step":12,"run_time_millis":12622,"has_output":true}]},{"name":"bundle exec pod install","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"bundle exec pod install","bash_command":"bundle exec pod install","status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:32:57.758Z","source":"config","type":"dependencies","output_url":"https://circle-production-action-output.s3.amazonaws.com/b62d558a91055bdd4830bb65-artsy-eigen-13-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=a9xEa59TgN2%2Bu8CsB4JiakLxNMY%3D","start_time":"2016-02-10T09:31:48.843Z","exit_code":0,"canceled":null,"step":13,"run_time_millis":68915,"has_output":true}]},{"name":"Save cache","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"Save cache","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:33:07.495Z","source":"cache","type":"database","output_url":"https://circle-production-action-output.s3.amazonaws.com/c72d558a91055bdd9c30bb65-artsy-eigen-14-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=tsAhNUOYU0fIU4QjUtEhlbxHbXk%3D","start_time":"2016-02-10T09:32:57.765Z","exit_code":null,"canceled":null,"step":14,"run_time_millis":9730,"has_output":true}]},{"name":"make ci","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"make ci","bash_command":"make ci","status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:35:21.392Z","source":"config","type":"test","output_url":"https://circle-production-action-output.s3.amazonaws.com/182d558a91055bdd3d30bb65-artsy-eigen-15-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=9i8idO7XM2%2FEW5rheskoFjmSik4%3D","start_time":"2016-02-10T09:33:07.502Z","exit_code":0,"canceled":null,"step":15,"run_time_millis":133890,"has_output":true}]},{"name":"make test","actions":[{"truncated":false,"index":0,"parallel":false,"failed":true,"infrastructure_fail":null,"name":"make test","bash_command":"make test","status":"failed","timedout":null,"continue":null,"end_time":"2016-02-10T09:37:32.913Z","source":"config","type":"test","output_url":"https://circle-production-action-output.s3.amazonaws.com/6b2d558a91055bdd9540bb65-artsy-eigen-16-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=CszrDphn0kXEaGfo5UBlHhV%2F29w%3D","start_time":"2016-02-10T09:35:21.404Z","exit_code":2,"canceled":null,"step":16,"run_time_millis":131509,"has_output":true}]},{"name":"bundle exec danger --verbose --base=master","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"bundle exec danger --verbose --base=master","bash_command":"bundle exec danger --verbose --base=master","status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:37:34.374Z","source":"config","type":"test","output_url":"https://circle-production-action-output.s3.amazonaws.com/cc2d558a91055bdddd40bb65-artsy-eigen-17-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=WmzEzDMc4Ip2mqjAzEZvzbisewg%3D","start_time":"2016-02-10T09:37:33.059Z","exit_code":0,"canceled":null,"step":17,"run_time_millis":1315,"has_output":true}]},{"name":"make deploy_if_beta_branch","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"make deploy_if_beta_branch","bash_command":"make deploy_if_beta_branch","status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:37:34.684Z","source":"config","type":"test","output_url":"https://circle-production-action-output.s3.amazonaws.com/ec2d558a91055bdded40bb65-artsy-eigen-18-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=3Nyvk1c%2BOTOGujbeFOXMG0tMvz8%3D","start_time":"2016-02-10T09:37:34.381Z","exit_code":0,"canceled":null,"step":18,"run_time_millis":303,"has_output":true}]},{"name":"Collect test metadata","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Collect test metadata","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:37:36.011Z","type":"teardown","output_url":"https://circle-production-action-output.s3.amazonaws.com/fc2d558a91055bdded40bb65-artsy-eigen-19-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=5k7FDPElcqsjdJt707U%2Bz2JC5%2F8%3D","start_time":"2016-02-10T09:37:34.692Z","exit_code":null,"canceled":null,"step":19,"run_time_millis":1319,"has_output":true}]},{"name":"Collect artifacts","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Collect artifacts","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:37:48.618Z","type":"teardown","output_url":"https://circle-production-action-output.s3.amazonaws.com/0d2d558a91055bdd0e40bb65-artsy-eigen-20-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=08TVUfEX0hEmhroEerTD5l8JAWs%3D","start_time":"2016-02-10T09:37:36.020Z","exit_code":null,"canceled":null,"step":20,"run_time_millis":12598,"has_output":true}]},{"name":"Ensure caches are uploaded","actions":[{"truncated":false,"index":0,"parallel":false,"failed":null,"infrastructure_fail":null,"name":"Ensure caches are uploaded","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:37:48.638Z","source":"cache","type":"teardown","output_url":"https://circle-production-action-output.s3.amazonaws.com/3d2d558a91055bddce40bb65-artsy-eigen-21-0?AWSAccessKeyId=AKIAIQ65EYQDTMSJK2DQ&Expires=1613026953&Signature=lWnop2qDH%2BLQoXscvzf0SxV%2BsZs%3D","start_time":"2016-02-10T09:37:48.636Z","exit_code":null,"canceled":null,"step":21,"run_time_millis":2,"has_output":true}]},{"name":"Disable SSH","actions":[{"truncated":false,"index":0,"parallel":true,"failed":null,"infrastructure_fail":null,"name":"Disable SSH","bash_command":null,"status":"success","timedout":null,"continue":null,"end_time":"2016-02-10T09:37:48.655Z","type":"teardown","start_time":"2016-02-10T09:37:48.647Z","exit_code":null,"canceled":null,"step":23,"run_time_millis":8,"has_output":false}]}],"body":"","usage_queued_at":"2016-02-10T09:30:59.176Z","retry_of":null,"reponame":"eigen","ssh_users":[],"build_url":"https://circleci.com/gh/artsy/eigen/1500","parallel":1,"failed":true,"branch":"orta-dm_pilot","username":"artsy","author_date":"2016-02-10T09:30:48Z","why":"github","user":{"is_user":true,"login":"orta","name":"Orta","email":"orta@artsymail.com","avatar_url":"https://avatars.githubusercontent.com/u/49038?v=3"},"vcs_revision":"1b064fe71ac7ec073223221889d96dcecb292000","owners":["orta"],"vcs_tag":null,"build_num":1500,"infrastructure_fail":false,"ssh_enabled":false,"committer_email":"orta.therox@gmail.com","has_artifacts":true,"previous":null,"status":"failed","committer_name":"Orta Therox","pull_request_urls":["https://github.com/artsy/eigen/pull/1130"],"retries":[1501],"subject":"Use the latest Danger / pilot","vcs_type":"github","timedout":false,"dont_build":null,"lifecycle":"finished","no_dependency_cache":null,"stop_time":"2016-02-10T09:37:49.133Z","build_time_millis":398863,"circle_yml":{"string":"machine:\n xcode:\n version: \"7.2\"\n environment:\n # Public - Yay\n UPLOAD_IOS_SNAPSHOT_BUCKET_NAME: eigen-ci\n\n # Private - Keeps us in business! Yay\n AWS_ACCESS_KEY_ID: from_admin\n AWS_SECRET_ACCESS_KEY: from_admin\n DANGER_GITHUB_API_TOKEN: from_admin\n GITHUB_API_KEY : from_admin\n MATCH_PASSWORD : from_admin\n HOCKEY_API_TOKEN : from_admin\n FASTLANE_USERNAME : from_admin\n FASTLANE_PASSWORD : from_admin\n ArtsyAPIClientSecret: from_admin\n ArtsyAPIClientKey: from_admin\n ArtsyFacebookAppID: from_admin\n ArtsyTwitterKey: from_admin\n ArtsyTwitterSecret: from_admin\n ArtsyTwitterStagingKey: from_admin\n ArtsyTwitterStagingSecret: from_admin\n SegmentProductionWriteKey: from_admin\n SegmentDevWriteKey: from_admin\n AdjustProductionAppToken: from_admin\n ArtsyEchoProductionToken: from_admin\n\n post:\n - echo \"machine github.com login $GITHUB_API_KEY\" > ~/.netrc\n - chmod 600 ~/.netrc\n\ndependencies:\n override:\n - bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3 --without development\n - bundle exec pod install\n cache_directories:\n - vendor/bundle\n - Pods\n\ntest:\n pre:\n - make ci\n override:\n - make test\n - bundle exec danger --verbose --base=master\n post:\n - make deploy_if_beta_branch\n"},"messages":[],"is_first_green_build":false,"job_name":null,"start_time":"2016-02-10T09:31:10.270Z","canceler":null,"all_commit_details":[{"committer_date":"2016-02-10T09:30:48Z","body":"","branch":"orta-dm_pilot","author_date":"2016-02-10T09:30:48Z","committer_email":"orta.therox@gmail.com","commit":"1b064fe71ac7ec073223221889d96dcecb292000","committer_login":"orta","committer_name":"Orta Therox","subject":"Use the latest Danger / pilot","commit_url":"https://github.com/artsy/eigen/commit/1b064fe71ac7ec073223221889d96dcecb292000","author_login":"orta","author_name":"Orta Therox","author_email":"orta.therox@gmail.com"}],"outcome":"failed","vcs_url":"https://github.com/artsy/eigen","author_name":"Orta Therox","node":[{"public_ip_addr":"162.217.87.35","port":55003,"username":"distiller","image_id":null,"ssh_enabled":null}],"queued_at":"2016-02-10T09:31:10.160Z","canceled":false,"author_email":"orta.therox@gmail.com"} \ No newline at end of file diff --git a/spec/sources/circle_api_spec.rb b/spec/sources/circle_api_spec.rb new file mode 100644 index 000000000..ada27d69f --- /dev/null +++ b/spec/sources/circle_api_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' +require 'danger/circle_api' + +describe Danger::CircleAPI do + api_path = "project/artsy/eigen/1500" + accept_json = { accept: 'application/json' } + + before do + build_response = fixture("circle_build_response") + @mocked_response = Faraday::Response.new(body: build_response, status: 200) + @expected_json_response = JSON.parse(build_response, symbolize_names: true) + end + + it 'has a nil token as default' do + api = Danger::CircleAPI.new + expect(api.circle_token).to be nil + end + + it 'sets the token on initialize' do + api = Danger::CircleAPI.new('123456') + expect(api.circle_token).to eql('123456') + end + + it 'creates a client with the correct base url' do + api = Danger::CircleAPI.new + expect(api.client.url_prefix.to_s).to eql('https://circleci.com/api/v1') + end + + it 'fetches the build info without token' do + api = Danger::CircleAPI.new + allow(api.client).to receive(:get).with(api_path, { :'circle-token' => nil }, accept_json).and_return(@mocked_response) + + expect(api.fetch_build("artsy/eigen", "1500")).to eql(@expected_json_response) + end + + it 'fetches the build info with token' do + api = Danger::CircleAPI.new('123456') + allow(api.client).to receive(:get).with(api_path, { :'circle-token' => '123456' }, accept_json).and_return(@mocked_response) + + expect(api.fetch_build("artsy/eigen", "1500")).to eql(@expected_json_response) + end +end diff --git a/spec/sources/circle_spec.rb b/spec/sources/circle_spec.rb index 9a3b430d8..ae1afaf47 100644 --- a/spec/sources/circle_spec.rb +++ b/spec/sources/circle_spec.rb @@ -15,6 +15,14 @@ expect(Danger::CISource::CircleCI.validates?(env)).to be true end + it 'validates when circle env var is found and it has no PR url' do + env = { "CIRCLE_BUILD_NUM" => "true", + "CIRCLE_PROJECT_USERNAME" => "orta", + "CIRCLE_PROJECT_REPONAME" => "thing" + } + expect(Danger::CISource::CircleCI.validates?(env)).to be true + end + it 'doesnt get a PR id when it has a bad PR url' do env = { "CIRCLE_BUILD_NUM" => "true", "CI_PULL_REQUEST" => not_legit_pr } t = Danger::CISource::CircleCI.new(env) @@ -36,4 +44,34 @@ expect(t.repo_slug).to eql("artsy/eigen") expect(t.pull_request_id).to eql("800") end + + it 'gets out a repo slug, pull request number and commit refs when PR url is not found' do + env = { + "CIRCLE_BUILD_NUM" => "1500", + "CIRCLE_PROJECT_USERNAME" => "artsy", + "CIRCLE_PROJECT_REPONAME" => "eigen", + "CIRCLE_COMPARE_URL" => "https://github.com/artsy/eigen/compare/759adcbd0d8f...13c4dc8bb61d" + } + build_response = JSON.parse(fixture("circle_build_response"), symbolize_names: true) + allow_any_instance_of(Danger::CircleAPI).to receive(:fetch_build).with("artsy/eigen", "1500").and_return(build_response) + + t = Danger::CISource::CircleCI.new(env) + + expect(t.repo_slug).to eql("artsy/eigen") + expect(t.pull_request_id).to eql("1130") + end + + it 'uses Circle CI API token if available' do + env = { + "CIRCLE_BUILD_NUM" => "1500", + "CIRCLE_CI_API_TOKEN" => "token", + "CIRCLE_PROJECT_USERNAME" => "artsy", + "CIRCLE_PROJECT_REPONAME" => "eigen" + } + build_response = JSON.parse(fixture("circle_build_response"), symbolize_names: true) + allow_any_instance_of(Danger::CircleAPI).to receive(:fetch_build).with("artsy/eigen", "1500").and_return(build_response) + + t = Danger::CISource::CircleCI.new(env) + expect(t.client.circle_token).to eql("token") + end end