class CircleCIReporter::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_reporter/client.rb, line 37
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{ |hash| create_artifact(hash) }
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_reporter/client.rb, line 50
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_reporter/client.rb, line 60
def get(url, params = {})
  params['circle-token'] = configuration.circleci_token
  query_string = params.map { |key, value| "#{key}=#{value}" }.join('&')
  connection = Faraday.new do |faraday|
    faraday.use FaradayMiddleware::FollowRedirects
  end
  connection.get("#{url}?#{query_string}")
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_reporter/client.rb, line 21
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_reporter/client.rb, line 78
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_reporter/client.rb, line 72
def configuration
  CircleCIReporter.configuration
end
create_artifact(hash) click to toggle source

@param hash [Hash] @return [Artifact]

# File lib/circleci_reporter/client.rb, line 127
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_reporter/client.rb, line 133
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_reporter/client.rb, line 92
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 { |hash| create_build(hash) }
end
recent_builds_url(branch) click to toggle source

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

# File lib/circleci_reporter/client.rb, line 102
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_reporter/client.rb, line 115
def single_build_url(build_number)
  [
    CIRCLECI_ENDPOINT,
    'project',
    configuration.vcs_type,
    configuration.project,
    build_number
  ].join('/')
end