This repository has been archived on 2023-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
coverage-check-action/lib/github_check_run_service.rb
2021-04-14 12:10:17 +02:00

57 lines
1.3 KiB
Ruby

# frozen_string_literal: true
class GithubCheckRunService
def initialize(report, github_data, report_adapter)
@report = report
@github_data = github_data
@report_adapter = report_adapter
@client = GithubClient.new(@github_data[:token], user_agent: 'coverage-action')
end
def run
id = @client.post(
endpoint_url,
create_check_payload
)['id']
@summary = @report_adapter.summary(@report)
@annotations = @report_adapter.annotations(@report)
@conclusion = @report_adapter.conslusion(@report)
@percent = @report_adapter.lines_covered_percent(@report)
@client.patch(
"#{endpoint_url}/#{id}",
update_check_payload
)
end
private
def endpoint_url
"/repos/#{@github_data[:owner]}/#{@github_data[:repo]}/check-runs"
end
def create_check_payload
{
name: @report_name,
head_sha: @github_data[:sha],
status: 'in_progress',
started_at: Time.now.iso8601
}
end
def update_check_payload
{
name: @report_name,
head_sha: @github_data[:sha],
status: 'completed',
completed_at: Time.now.iso8601,
conclusion: @conclusion,
output: {
title: "#{@report_name} #{@percent}%",
summary: @summary,
annotations: @annotations
}
}
end
end