Fix bugs, add parameters

This commit is contained in:
Joachim Jablon
2021-09-26 00:13:50 +02:00
parent ef7c542912
commit 385964faab
5 changed files with 56 additions and 19 deletions

View File

@@ -1,5 +1,7 @@
name: 'Coverage'
description: 'Publish diff coverage report as PR comment'
description: >
Publish diff coverage report as PR comment, and create a coverage badge
to display on the readme.
inputs:
GITHUB_TOKEN:
description: A GitHub token to write comments and write the badge to the wiki.
@@ -19,6 +21,18 @@ inputs:
description: Whether or not a badge will be generated and stored.
default: "true"
required: false
BADGE_FILENAME:
description: Name of the json file containing badge informations stored in the repo wiki
default: coverage-comment-badge.json
required: false
MINIMUM_GREEN:
description:
default: 100
required: false
MINIMUM_ORANGE:
description:
default: 70
required: false
runs:
using: 'docker'
image: 'Dockerfile'
@@ -28,3 +42,6 @@ runs:
COMMENT_TEMPLATE: ${{ inputs.COMMENT_TEMPLATE }}
DIFF_COVER_ARGS: ${{ inputs.DIFF_COVER_ARGS }}
BADGE_ENABLED: ${{ inputs.BADGE_ENABLED }}
BADGE_FILENAME: ${{ inputs.BADGE_FILENAME }}
MINIMUM_GREEN: ${{ inputs.MINIMUM_GREEN }}
MINIMUM_ORANGE: ${{ inputs.MINIMUM_ORANGE }}

View File

@@ -26,5 +26,3 @@ then
git push -u origin
fi
echo "https://raw.githubusercontent.com/wiki/${repo_name}/${filename}"

View File

@@ -19,7 +19,7 @@ The branch rate is `{{ branch_coverage }}%`
<details>
<summary>Diff Coverage details (click to unfold)</summary>
{% for filename, stats in file_info.items() -%}}
{% for filename, stats in file_info.items() -%}
### {{ filename }}
`{{ stats.diff_coverage }}%` of new lines are covered

View File

@@ -12,13 +12,12 @@ from typing import List, Optional
import github
import jinja2
import requests
import xmltodict
MARKER = """<!-- This comment was produced by coverage-comment-action -->"""
BADGE_FILENAME = "coverage-comment-badge.json"
MINIMUM_GREEN = 100
MINIMUM_ORANGE = 70
SHIELD_URL = "https://img.shields.io/endpoint?url={url}"
JSON_URL = "https://raw.githubusercontent.com/wiki/{repo_name}/{filename}"
def main():
@@ -41,8 +40,9 @@ def main():
if config.BADGE_ENABLED and is_main_branch(gh=gh, config=config):
print("Running on default branch, saving Badge into the repo wiki")
badge = compute_badge(coverage_info=coverage_info)
url = upload_badge(badge=badge, config=config)
badge = compute_badge(coverage_info=coverage_info, config=config)
upload_badge(badge=badge, config=config)
url = get_badge_json_url(config=config)
print(f"Badge JSON stored at {url}")
print(f"Badge URL: {SHIELD_URL.format(url=url)}")
@@ -56,10 +56,13 @@ class Config:
GITHUB_REPOSITORY: str
GITHUB_HEAD_REF: str
GITHUB_REF: str
BADGE_FILENAME: str = "coverage-comment-badge.json"
COVERAGE_FILE: str = "coverage.xml"
COMMENT_TEMPLATE: Optional[str] = None
DIFF_COVER_ARGS: List[str] = dataclasses.field(default_factory=list)
BADGE_ENABLED: bool = True
MINIMUM_GREEN: float = 100.0
MINIMUM_ORANGE: float = 70.0
# Clean methods
@classmethod
@@ -70,6 +73,14 @@ class Config:
def clean_badge_enabled(cls, value: str) -> bool:
return value.lower() in ("1", "true", "yes")
@classmethod
def clean_minimum_green(cls, value: str) -> float:
return float(value)
@classmethod
def clean_minimum_orange(cls, value: str) -> float:
return float(value)
@property
def GITHUB_PR_NUMBER(self) -> Optional[int]:
# "refs/pull/2/merge"
@@ -204,12 +215,12 @@ def post_comment(body: str, gh: github.Github, config: Config) -> None:
issue.create_comment(body=body)
def compute_badge(coverage_info: dict) -> str:
def compute_badge(coverage_info: dict, config: Config) -> str:
rate = int(coverage_info["@line-rate"] * 100)
if rate >= MINIMUM_GREEN:
if rate >= config.MINIMUM_GREEN:
color = "green"
elif rate >= MINIMUM_ORANGE:
elif rate >= config.MINIMUM_ORANGE:
color = "orange"
else:
color = "red"
@@ -224,16 +235,27 @@ def compute_badge(coverage_info: dict) -> str:
return json.dumps(badge)
def get_badge_json_url(config: Config) -> str:
return JSON_URL.format(
repo_name=config.GITHUB_REPOSITORY, filename=config.BADGE_FILENAME
)
def get_previous_coverage_rate(config: Config) -> Optional[float]:
return 0.42
def upload_badge(badge: str, config: Config) -> str:
try:
process = call(
return float(
requests.get(get_badge_json_url(config=config)).json()["value"][:-1]
)
except Exception:
return None
def upload_badge(badge: str, config: Config) -> None:
try:
call(
"add-to-wiki",
config.GITHUB_REPOSITORY,
BADGE_FILENAME,
config.BADGE_FILENAME,
"Update badge",
input=badge,
)
@@ -244,7 +266,6 @@ def upload_badge(badge: str, config: Config) -> str:
"wiki. You may disable it afterwards."
)
raise
return process.stdout.strip()
class Exit(Exception):

View File

@@ -2,3 +2,4 @@ diff-cover
jinja2
PyGithub
xmltodict
requests