Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

understand phpunit coverage txt format #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ But no need to generate any new token, you can use the Github action token (`${{
token: ${{ github.token }}
```

#### Report name

```yml
- uses: devmasx/[email protected]
with:
type: lcov
result_path: coverage/example.lcov
min_coverage: 90
token: ${{ github.token }}
report_name: "My Github Action Check Name"
```

## Screenshots

![Success](./screenshots/success.png)
Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ branding:
color: "green"
inputs:
type:
description: "lcov | simplecov"
description: "lcov | simplecov | phpunit"
required: true
default: "lcov"
token:
Expand All @@ -15,6 +15,9 @@ inputs:
min_coverage:
description: "Minimum coverage"
default: "80"
report_name:
description: "Name of the github action check"
default: "Coverage"
result_path:
description: "Json with coverage result"
required: true
Expand Down
24 changes: 24 additions & 0 deletions lib/coverage_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def generate(type, report_path, data)
simplecov(report_path, data)
elsif type == 'lcov'
lcov(report_path, data)
elsif type == 'phpunit'
phpunit(report_path, data)
else
raise 'InvalidCoverageReportType'
end
Expand All @@ -25,6 +27,12 @@ def lcov(report_path, data)
{ 'lines' => { 'covered_percent' => lcov_covered_percent(lcov_result), 'minumum_percent' => minumum_percent } }
end

def phpunit(report_path, data)
phpunit_result = execute_phpunit_parse(report_path)
minumum_percent = data[:min]
{ 'lines' => { 'covered_percent' => phpunit_covered_percent(phpunit_result), 'minumum_percent' => minumum_percent} }
end

private

def lcov_covered_percent(lcov_result)
Expand All @@ -39,6 +47,22 @@ def execute_lcov_parse(report_path)
JSON.parse(`node #{bin_path}/lcov-parse.js #{report_path}`)
end


def phpunit_covered_percent(phpunit_result)
# example for
# phpunit --coverage-text
# Summary:
# Classes: 10.14% (14/138)
# Methods: 16.67% (107/642)
# Lines: 13.95% (1059/7591)

/Lines: * ([0-9\.]*)%/.match(phpunit_result)[1].to_f
end

def execute_phpunit_parse(report_path)
File.read(report_path)
end

def read_json(path)
JSON.parse(File.read(path))
end
Expand Down
11 changes: 5 additions & 6 deletions lib/github_check_run_service.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# frozen_string_literal: true

class GithubCheckRunService
CHECK_NAME = 'Coverage'

def initialize(report, github_data, report_adapter)
def initialize(report, github_data, report_name, report_adapter)
@report = report
@github_data = github_data
@report_name = report_name
@report_adapter = report_adapter
@client = GithubClient.new(@github_data[:token], user_agent: 'coverage-action')
end
Expand Down Expand Up @@ -34,7 +33,7 @@ def endpoint_url

def create_check_payload
{
name: CHECK_NAME,
name: @report_name,
head_sha: @github_data[:sha],
status: 'in_progress',
started_at: Time.now.iso8601
Expand All @@ -43,13 +42,13 @@ def create_check_payload

def update_check_payload
{
name: CHECK_NAME,
name: @report_name,
head_sha: @github_data[:sha],
status: 'completed',
completed_at: Time.now.iso8601,
conclusion: @conclusion,
output: {
title: "#{CHECK_NAME} #{@percent}%",
title: "#{@report_name} #{@percent}%",
summary: @summary,
annotations: @annotations
}
Expand Down
5 changes: 3 additions & 2 deletions lib/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ def read_json(path)
sha: ENV['GITHUB_SHA'],
token: ENV['INPUT_TOKEN'],
owner: ENV['GITHUB_REPOSITORY_OWNER'] || @event_json.dig('repository', 'owner', 'login'),
repo: ENV['GITHUB_REPOSITORY_NAME'] || @event_json.dig('repository', 'name')
repo: ENV['GITHUB_REPOSITORY_NAME'] || @event_json.dig('repository', 'name'),
}

@coverage_type = ENV['INPUT_TYPE']
@report_path = ENV['INPUT_RESULT_PATH']
@report_name = ENV['INPUT_REPORT_NAME']
@data = { min: ENV['INPUT_MIN_COVERAGE'] }

@report = CoverageReport.generate(@coverage_type, @report_path, @data)

GithubCheckRunService.new(@report, @github_data, ReportAdapter).run
GithubCheckRunService.new(@report, @github_data, @report_name, ReportAdapter).run
3 changes: 2 additions & 1 deletion spec/github_check_run_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
{ 'lines' => { 'covered_percent' => 80, 'minumum_percent' => 80 } }
end
let(:github_data) { { sha: 'sha', token: 'token', owner: 'owner', repo: 'repository_name' } }
let(:service) { GithubCheckRunService.new(report, github_data, ReportAdapter) }
let(:report_name) { 'Coverage' }
let(:service) { GithubCheckRunService.new(report, github_data, report_name, ReportAdapter) }

it '#run' do
stub_request(:any, 'https://api.github.com/repos/owner/repository_name/check-runs/id')
Expand Down