Sha256: b47c0c559c49fe5d8b9e6fba8423bb2fc16c408b6db08396fc9bdfe088ea4a70

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require 'uri'
require 'net/http'

module Bubbler
  module Api
    require 'bubbler_ruby/railtie' if defined?(Rails)

    def self.post(simplecov_results)
      body = request_body(simplecov_results.as_json)

      uri = URI('http://bubbler-dev.us-west-2.elasticbeanstalk.com/api/coverage/simplecov')
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = false

      request = Net::HTTP::Post.new(uri.path, 
        {
          'Content-Type' => 'application/json'
        }
      )
      puts body
      if body[:results].nil?
        puts "No results found from SimpleCov"
        return
      end
      request.body = body.to_json
      response = http.request(request)
    end

    def self.request_body(simplecov_results)
      circleci_body(simplecov_results)
    end

    def self.circleci_body(simplecov_results)
      {
        branch: ENV['CIRCLE_BRANCH'],
        commit_sha: ENV['CIRCLE_SHA1'],
        slug: circle_slug,
        pr: circleci_pr_number,
        results: simplecov_results
      }
    end

    # CIRCLE_PR_NUMBER=2  (Only populated on forked PRs)
    # CIRCLE_PULL_REQUEST= https://github.com/mark579/pull/2
    def self.circleci_pr_number
      return unless ENV['CIRCLE_PR_NUMBER'] || ENV['CIRCLE_PULL_REQUEST']
      
      return ENV['CIRCLE_PR_NUMBER'] if ENV['CIRCLE_PR_NUMBER']
      return ENV['CIRCLE_PULL_REQUEST'].split('/').last
    end

    # CIRCLE_PROJECT_REPONAME=rails-bubbler-test
    # CIRCLE_PROJECT_USERNAME=mark579
    # CIRCLE_REPOSITORY_URL=git@github.com:mark579/rails-bubbler-test.git
    def self.circle_slug
      if !ENV['CIRCLE_PROJECT_REPONAME'].nil?
        ENV['CIRCLE_PROJECT_USERNAME'] + '/' + ENV['CIRCLE_PROJECT_REPONAME']
      else
        ENV['CIRCLE_REPOSITORY_URL'].gsub(/^.*:/, '').gsub(/\.git$/, '')
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bubbler_ruby-0.1.3 lib/bubbler_ruby/api.rb