module Slather::CoverageService::Coveralls

Public Instance Methods

buildkite_build_url() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 138
def buildkite_build_url
  "https://buildkite.com/" + ENV['BUILDKITE_PROJECT_SLUG'] + "/builds/" + ENV['BUILDKITE_BUILD_NUMBER'] + "#"
end
buildkite_git_info() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 126
def buildkite_git_info
  {
    :head => {
      :id => ENV['BUILDKITE_COMMIT'],
      :author_name => (`git log --format=%an -n 1 HEAD`.chomp || ""),
      :author_email => (`git log --format=%ae -n 1 HEAD`.chomp || ""),
      :message => (`git log --format=%s -n 1 HEAD`.chomp || "")
    },
    :branch => ENV['BUILDKITE_BRANCH']
  }
end
post() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 272
def post
  f = File.open('coveralls_json_file', 'w+')
  begin
    f.write(coveralls_coverage_data)
    f.close

    curl_result = `curl -s --form json_file=@#{f.path} #{coveralls_api_jobs_path}`

    if curl_result.is_a? String 
      curl_result_json = JSON.parse(curl_result)          

      if curl_result_json["error"]
        error_message = curl_result_json["message"]
        raise StandardError, "Error while uploading coverage data to Coveralls. CI Service: #{ci_service} Message: #{error_message}"
      end
    end

  rescue StandardError => e
    FileUtils.rm(f)
    raise e
  end
  FileUtils.rm(f)
end

Private Instance Methods

buildkite_job_id() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 74
def buildkite_job_id
  ENV['BUILDKITE_BUILD_NUMBER']
end
buildkite_pull_request() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 79
def buildkite_pull_request
  ENV['BUILDKITE_PULL_REQUEST']
end
circleci_build_url() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 109
def circleci_build_url
  "https://circleci.com/gh/" + ENV['CIRCLE_PROJECT_USERNAME'] || "" + "/" + ENV['CIRCLE_PROJECT_REPONAME'] || "" + "/" + ENV['CIRCLE_BUILD_NUM'] || ""
end
circleci_git_info() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 114
def circleci_git_info
  {
    :head => {
      :id => (ENV['CIRCLE_SHA1'] || ""),
      :author_name => (ENV['CIRCLE_PR_USERNAME'] || ENV['CIRCLE_USERNAME'] || ""),
      :message => (`git log --format=%s -n 1 HEAD`.chomp || "")
    },
    :branch => (ENV['CIRCLE_BRANCH'] || "")
  }
end
circleci_job_id() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 19
def circleci_job_id
  ENV['CIRCLE_BUILD_NUM']
end
circleci_pull_request() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 24
def circleci_pull_request
  ENV['CIRCLE_PR_NUMBER'] || ENV['CI_PULL_REQUEST'] || ""
end
coverage_file_class() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 5
def coverage_file_class
  if input_format == "profdata"
    Slather::ProfdataCoverageFile
  else
    Slather::CoverageFile
  end
end
coveralls_api_jobs_path() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 296
def coveralls_api_jobs_path
  "https://coveralls.io/api/v1/jobs"
end
coveralls_coverage_data() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 159
def coveralls_coverage_data
  if ci_service == :travis_ci || ci_service == :travis_pro
    if travis_job_id
      if ci_service == :travis_ci
        
        if coverage_access_token.to_s.strip.length > 0
          raise StandardError, "Access token is set. Uploading coverage data for public repositories doesn't require an access token."
        end

        {
          :service_job_id => travis_job_id,
          :service_name => "travis-ci",
          :source_files => coverage_files.map(&:as_json)
        }.to_json
      elsif ci_service == :travis_pro              

        if coverage_access_token.to_s.strip.length == 0
          raise StandardError, "Access token is not set. Uploading coverage data for private repositories requires an access token."
        end

        {
          :service_job_id => travis_job_id,
          :service_name => "travis-pro",
          :repo_token => coverage_access_token,
          :source_files => coverage_files.map(&:as_json)
        }.to_json
      end
    else
      raise StandardError, "Environment variable `TRAVIS_JOB_ID` not set. Is this running on a travis build?"
    end
  elsif ci_service == :circleci
    if circleci_job_id
      coveralls_hash = {
        :service_job_id => circleci_job_id,
        :service_name => "circleci",
        :repo_token => coverage_access_token,
        :source_files => coverage_files.map(&:as_json),
        :git => circleci_git_info,
        :service_build_url => circleci_build_url
      }

      if circleci_pull_request != nil && circleci_pull_request.length > 0
        coveralls_hash[:service_pull_request] = circleci_pull_request.split("/").last
      end

      coveralls_hash.to_json
    else
      raise StandardError, "Environment variable `CIRCLE_BUILD_NUM` not set. Is this running on a circleci build?"
    end
  elsif ci_service == :jenkins
    if jenkins_job_id
      {
        service_job_id: jenkins_job_id,
        service_name: "jenkins",
        repo_token: coverage_access_token,
        source_files: coverage_files.map(&:as_json),
        git: jenkins_git_info
      }.to_json
    else
      raise StandardError, "Environment variable `BUILD_ID` not set. Is this running on a jenkins build?"
    end
  elsif ci_service == :buildkite
    if buildkite_job_id
      {
        :service_job_id => buildkite_job_id,
        :service_name => "buildkite",
        :repo_token => coverage_access_token,
        :source_files => coverage_files.map(&:as_json),
        :git => buildkite_git_info,
        :service_build_url => buildkite_build_url,
        :service_pull_request => buildkite_pull_request
      }.to_json
    else
      raise StandardError, "Environment variable `BUILDKITE_BUILD_NUMBER` not set. Is this running on a buildkite build?"
    end
  elsif ci_service == :teamcity
    if teamcity_job_id
      {
        :service_job_id => teamcity_job_id,
        :service_name => "teamcity",
        :repo_token => coverage_access_token,
        :source_files => coverage_files.map(&:as_json),
        :git => teamcity_git_info
      }.to_json
    else
      raise StandardError, "Environment variable `TC_BUILD_NUMBER` not set. Is this running on a teamcity build?"
    end
  elsif ci_service == :github

    if coverage_access_token.to_s.strip.length == 0
      raise StandardError, "Access token is not set. Uploading coverage data for private repositories requires an access token."
    end

    if github_job_id
      {
        :service_job_id => github_job_id,
        :service_name => "github",
        :repo_token => coverage_access_token,
        :repo_name => github_repo_name,
        :source_files => coverage_files.map(&:as_json),
        :service_build_url => github_build_url,
        :service_pull_request => github_pull_request,
        :git => github_git_info
      }.to_json
    else
      raise StandardError, "Environment variable `GITHUB_RUN_ID` not set.  Is this running on github build?"
    end
  else
    raise StandardError, "No support for ci named #{ci_service}"
  end
end
github_branch_name() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 69
def github_branch_name
  ENV['GIT_BRANCH'] || `git ls-remote --heads origin | grep $(git rev-parse HEAD) | cut -d / -f 3-`.chomp
end
github_build_url() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 154
def github_build_url
  "https://github.com/" + ENV['GITHUB_REPOSITORY'] + "/actions/runs/" + ENV['GITHUB_RUN_ID']
end
github_git_info() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 142
def github_git_info
  {
    :head => {
      :id => ENV['GITHUB_SHA'],
      :author_name => ENV['GITHUB_ACTOR'],
      :message => (`git log --format=%s -n 1 HEAD`.chomp || "")
    },
    :branch => github_branch_name
  }
end
github_job_id() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 39
def github_job_id
  ENV['GITHUB_RUN_ID']
end
github_pull_request() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 44
def github_pull_request
  ENV['CI_PULL_REQUEST'] || ""
end
github_repo_name() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 49
def github_repo_name
  ENV['GITHUB_REPOSITORY'] || ""
end
jenkins_branch_name() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 54
def jenkins_branch_name
  branch_name = ENV['GIT_BRANCH'] || ENV['BRANCH_NAME']
  if branch_name.include? 'origin/'
    branch_name[7...branch_name.length]
  else
    branch_name
  end
end
jenkins_git_info() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 84
def jenkins_git_info
  {
    head: {
      id: ENV['sha1'],
      author_name: ENV['ghprbActualCommitAuthor'],
      message: ENV['ghprbPullTitle']
    },
    branch: jenkins_branch_name
  }
end
jenkins_job_id() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 34
def jenkins_job_id
  ENV['BUILD_ID']
end
teamcity_branch_name() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 64
def teamcity_branch_name
  ENV['GIT_BRANCH'] || `git ls-remote --heads origin | grep $(git rev-parse HEAD) | cut -d / -f 3-`.chomp
end
teamcity_git_info() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 96
def teamcity_git_info
  {
    head: {
      :id => (`git log --format=%H -n 1 HEAD`.chomp || ""),
      :author_name => (`git log --format=%an -n 1 HEAD`.chomp || ""),
      :author_email => (`git log --format=%ae -n 1 HEAD`.chomp || ""),
      :message => (`git log --format=%s -n 1 HEAD`.chomp || "")
    },
    :branch => teamcity_branch_name
  }
end
teamcity_job_id() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 29
def teamcity_job_id
  ENV['TC_BUILD_NUMBER']
end
travis_job_id() click to toggle source
# File lib/slather/coverage_service/coveralls.rb, line 14
def travis_job_id
  ENV['TRAVIS_JOB_ID']
end