Class: CircleCi2

Inherits:
Object
  • Object
show all
Defined in:
lib/codeclimate_circle_ci_coverage/circle_ci_2.rb

Constant Summary collapse

ARTIFACT_PREFIX =
"coverage"

Instance Method Summary collapse

Instance Method Details

#download_filesObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/codeclimate_circle_ci_coverage/circle_ci_2.rb', line 9

def download_files
  if ENV["CIRCLE_TOKEN"].nil?
    puts "You must create a Circle CI Artifacts-API token to use this on Circle CI 2.0"
    puts "Please create that, and store the key as CIRCLE_TOKEN"
    return []
  end
  # rubocop:disable Metrics/LineLength
  api_url = "https://circleci.com/api/v1.1/project/github/#{ENV['CIRCLE_PROJECT_USERNAME']}/#{ENV['CIRCLE_PROJECT_REPONAME']}/#{ENV['CIRCLE_BUILD_NUM']}/artifacts?circle-token=#{ENV['CIRCLE_TOKEN']}"
  # rubocop:enable Metrics/LineLength
  artifacts = open(api_url)

  paths = matching_urls(JSON.load(artifacts))

  if paths.none?
    puts "No Results Found. Did you store the artifacts with 'store_artifacts'?"
    puts "did you use 'prefix: coverage'?"
    return []
  end

  paths.map do |path|
    JSON.load(open("#{path}?circle-token=#{ENV['CIRCLE_TOKEN']}"))
  end
end

#matching_urls(json) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/codeclimate_circle_ci_coverage/circle_ci_2.rb', line 33

def matching_urls(json)
  json.select do |artifact|
    artifact['path'].match("#{ARTIFACT_PREFIX}/.resultset.json")
  end.map do |artifact|
    artifact['url']
  end
end