class CircleCI::CoverageReporter::Client

CircleCI API client

Constants

CIRCLECI_ENDPOINT

Public Instance Methods

artifacts(build_number) click to toggle source

Retrieve artifacts for the build.

@param build_number [Integer] @return [Array<Artifact>] @raise [RequestError] @see circleci.com/docs/api/v1-reference/#build-artifacts

# File lib/circleci/coverage_reporter/client.rb, line 33
def artifacts(build_number)
  resp = get(artifacts_url(build_number))
  body = JSON.parse(resp.body)
  raise RequestError.new(body['message'], resp) unless resp.success?
  body.map(&method(:create_artifact))
end
build_number_by_revision(revision, branch: nil) click to toggle source

Find the latest build number for the given vcs revision.

@param revision [String] @param branch [String, nil] @return [Integer, nil]

# File lib/circleci/coverage_reporter/client.rb, line 45
def build_number_by_revision(revision, branch: nil)
  build = recent_builds(branch).find { |recent_build| recent_build.match?(revision) }
  build ? build.build_number : nil
end
get(url, params = {}) click to toggle source

Raw entry point for GET APIs.

@param url [String] @param params [Hash] @return [Faraday::Response]

# File lib/circleci/coverage_reporter/client.rb, line 55
def get(url, params = {})
  params['circle-token'] = configuration.circleci_token
  Faraday.get(url + '?' + params.map { |key, value| "#{key}=#{value}" }.join('&'))
end
single_build(build_number) click to toggle source

Fetch a build data from API and create a {Build} object for it.

@param build_number [Integer, nil] @return [Build, nil] @raise [RequestError] @see circleci.com/docs/api/v1-reference/#build

# File lib/circleci/coverage_reporter/client.rb, line 19
def single_build(build_number)
  return unless build_number
  resp = get(single_build_url(build_number))
  body = JSON.parse(resp.body)
  raise RequestError.new(body['message'], resp) unless resp.success?
  create_build(body)
end

Private Instance Methods

artifacts_url(build_number) click to toggle source

@param build_number [Integer] @return [String] URL for “Artifacts of a Bulid API”

# File lib/circleci/coverage_reporter/client.rb, line 69
def artifacts_url(build_number)
  [
    CIRCLECI_ENDPOINT,
    'project',
    configuration.vcs_type,
    configuration.project,
    build_number,
    'artifacts'
  ].join('/')
end
configuration() click to toggle source

@return [Configuration]

# File lib/circleci/coverage_reporter/client.rb, line 63
def configuration
  CoverageReporter.configuration
end
create_artifact(hash) click to toggle source

@param hash [Hash] @return [Artifact]

# File lib/circleci/coverage_reporter/client.rb, line 117
def create_artifact(hash)
  Artifact.new(hash['path'], hash['url'], hash['node_index'])
end
create_build(hash) click to toggle source

@param hash [Hash] @return [Build]

# File lib/circleci/coverage_reporter/client.rb, line 123
def create_build(hash)
  Build.new(hash['vcs_revision'], hash['build_num'])
end
recent_builds(branch) click to toggle source

@param branch [String, nil] @return [Array<Build>] @raise [RequestError]

# File lib/circleci/coverage_reporter/client.rb, line 83
def recent_builds(branch)
  resp = get(recent_builds_url(branch), limit: 100)
  body = JSON.parse(resp.body)
  raise RequestError.new(body['message'], resp) unless resp.success?
  body.map(&method(:create_build))
end
recent_builds_url(branch) click to toggle source

@param branch [String, nil] @return [String]

# File lib/circleci/coverage_reporter/client.rb, line 92
def recent_builds_url(branch)
  elements = [
    CIRCLECI_ENDPOINT,
    'project',
    configuration.vcs_type,
    configuration.project
  ]
  elements += ['tree', branch] if branch
  elements.join('/')
end
single_build_url(build_number) click to toggle source

@param build_number [Integer] @return [String]

# File lib/circleci/coverage_reporter/client.rb, line 105
def single_build_url(build_number)
  [
    CIRCLECI_ENDPOINT,
    'project',
    configuration.vcs_type,
    configuration.project,
    build_number
  ].join('/')
end