Merge pull request #3 from ewjoachim/iterate
This commit is contained in:
19
action.yml
19
action.yml
@@ -1,5 +1,7 @@
|
|||||||
name: 'Coverage'
|
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:
|
inputs:
|
||||||
GITHUB_TOKEN:
|
GITHUB_TOKEN:
|
||||||
description: A GitHub token to write comments and write the badge to the wiki.
|
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.
|
description: Whether or not a badge will be generated and stored.
|
||||||
default: "true"
|
default: "true"
|
||||||
required: false
|
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:
|
runs:
|
||||||
using: 'docker'
|
using: 'docker'
|
||||||
image: 'Dockerfile'
|
image: 'Dockerfile'
|
||||||
@@ -28,3 +42,6 @@ runs:
|
|||||||
COMMENT_TEMPLATE: ${{ inputs.COMMENT_TEMPLATE }}
|
COMMENT_TEMPLATE: ${{ inputs.COMMENT_TEMPLATE }}
|
||||||
DIFF_COVER_ARGS: ${{ inputs.DIFF_COVER_ARGS }}
|
DIFF_COVER_ARGS: ${{ inputs.DIFF_COVER_ARGS }}
|
||||||
BADGE_ENABLED: ${{ inputs.BADGE_ENABLED }}
|
BADGE_ENABLED: ${{ inputs.BADGE_ENABLED }}
|
||||||
|
BADGE_FILENAME: ${{ inputs.BADGE_FILENAME }}
|
||||||
|
MINIMUM_GREEN: ${{ inputs.MINIMUM_GREEN }}
|
||||||
|
MINIMUM_ORANGE: ${{ inputs.MINIMUM_ORANGE }}
|
||||||
|
|||||||
@@ -26,5 +26,3 @@ then
|
|||||||
|
|
||||||
git push -u origin
|
git push -u origin
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "https://raw.githubusercontent.com/wiki/${repo_name}/${filename}"
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ The branch rate is `{{ branch_coverage }}%`
|
|||||||
<details>
|
<details>
|
||||||
<summary>Diff Coverage details (click to unfold)</summary>
|
<summary>Diff Coverage details (click to unfold)</summary>
|
||||||
|
|
||||||
{% for filename, stats in file_info.items() -%}}
|
{% for filename, stats in file_info.items() -%}
|
||||||
### {{ filename }}
|
### {{ filename }}
|
||||||
`{{ stats.diff_coverage }}%` of new lines are covered
|
`{{ stats.diff_coverage }}%` of new lines are covered
|
||||||
|
|
||||||
|
|||||||
@@ -12,20 +12,19 @@ from typing import List, Optional
|
|||||||
|
|
||||||
import github
|
import github
|
||||||
import jinja2
|
import jinja2
|
||||||
|
import requests
|
||||||
import xmltodict
|
import xmltodict
|
||||||
|
|
||||||
MARKER = """<!-- This comment was produced by coverage-comment-action -->"""
|
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}"
|
SHIELD_URL = "https://img.shields.io/endpoint?url={url}"
|
||||||
|
JSON_URL = "https://raw.githubusercontent.com/wiki/{repo_name}/{filename}"
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
print("Starting action")
|
||||||
config = Config.from_environ(os.environ)
|
config = Config.from_environ(os.environ)
|
||||||
coverage_info = get_coverage_info(config=config)
|
coverage_info = get_coverage_info(config=config)
|
||||||
gh = get_api(config=config)
|
gh = get_api(config=config)
|
||||||
|
|
||||||
if config.GITHUB_PR_NUMBER:
|
if config.GITHUB_PR_NUMBER:
|
||||||
print(f"Commenting on the coverage on PR {config.GITHUB_PR_NUMBER}")
|
print(f"Commenting on the coverage on PR {config.GITHUB_PR_NUMBER}")
|
||||||
diff_coverage_info = get_diff_coverage_info(config=config)
|
diff_coverage_info = get_diff_coverage_info(config=config)
|
||||||
@@ -41,11 +40,14 @@ def main():
|
|||||||
|
|
||||||
if config.BADGE_ENABLED and is_main_branch(gh=gh, config=config):
|
if config.BADGE_ENABLED and is_main_branch(gh=gh, config=config):
|
||||||
print("Running on default branch, saving Badge into the repo wiki")
|
print("Running on default branch, saving Badge into the repo wiki")
|
||||||
badge = compute_badge(coverage_info=coverage_info)
|
badge = compute_badge(coverage_info=coverage_info, config=config)
|
||||||
url = upload_badge(badge=badge, 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 JSON stored at {url}")
|
||||||
print(f"Badge URL: {SHIELD_URL.format(url=url)}")
|
print(f"Badge URL: {SHIELD_URL.format(url=url)}")
|
||||||
|
|
||||||
|
print("Ending action")
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class Config:
|
class Config:
|
||||||
@@ -56,10 +58,13 @@ class Config:
|
|||||||
GITHUB_REPOSITORY: str
|
GITHUB_REPOSITORY: str
|
||||||
GITHUB_HEAD_REF: str
|
GITHUB_HEAD_REF: str
|
||||||
GITHUB_REF: str
|
GITHUB_REF: str
|
||||||
|
BADGE_FILENAME: str = "coverage-comment-badge.json"
|
||||||
COVERAGE_FILE: str = "coverage.xml"
|
COVERAGE_FILE: str = "coverage.xml"
|
||||||
COMMENT_TEMPLATE: Optional[str] = None
|
COMMENT_TEMPLATE: Optional[str] = None
|
||||||
DIFF_COVER_ARGS: List[str] = dataclasses.field(default_factory=list)
|
DIFF_COVER_ARGS: List[str] = dataclasses.field(default_factory=list)
|
||||||
BADGE_ENABLED: bool = True
|
BADGE_ENABLED: bool = True
|
||||||
|
MINIMUM_GREEN: float = 100.0
|
||||||
|
MINIMUM_ORANGE: float = 70.0
|
||||||
|
|
||||||
# Clean methods
|
# Clean methods
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -70,6 +75,14 @@ class Config:
|
|||||||
def clean_badge_enabled(cls, value: str) -> bool:
|
def clean_badge_enabled(cls, value: str) -> bool:
|
||||||
return value.lower() in ("1", "true", "yes")
|
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
|
@property
|
||||||
def GITHUB_PR_NUMBER(self) -> Optional[int]:
|
def GITHUB_PR_NUMBER(self) -> Optional[int]:
|
||||||
# "refs/pull/2/merge"
|
# "refs/pull/2/merge"
|
||||||
@@ -204,12 +217,12 @@ def post_comment(body: str, gh: github.Github, config: Config) -> None:
|
|||||||
issue.create_comment(body=body)
|
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)
|
rate = int(coverage_info["@line-rate"] * 100)
|
||||||
|
|
||||||
if rate >= MINIMUM_GREEN:
|
if rate >= config.MINIMUM_GREEN:
|
||||||
color = "green"
|
color = "green"
|
||||||
elif rate >= MINIMUM_ORANGE:
|
elif rate >= config.MINIMUM_ORANGE:
|
||||||
color = "orange"
|
color = "orange"
|
||||||
else:
|
else:
|
||||||
color = "red"
|
color = "red"
|
||||||
@@ -224,16 +237,28 @@ def compute_badge(coverage_info: dict) -> str:
|
|||||||
return json.dumps(badge)
|
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]:
|
def get_previous_coverage_rate(config: Config) -> Optional[float]:
|
||||||
return 0.42
|
|
||||||
|
|
||||||
|
|
||||||
def upload_badge(badge: str, config: Config) -> str:
|
|
||||||
try:
|
try:
|
||||||
process = call(
|
return float(
|
||||||
|
requests.get(get_badge_json_url(config=config)).json()["value"][:-1]
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
print("Previous coverage results not found, cannot report on evolution.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def upload_badge(badge: str, config: Config) -> None:
|
||||||
|
try:
|
||||||
|
call(
|
||||||
"add-to-wiki",
|
"add-to-wiki",
|
||||||
config.GITHUB_REPOSITORY,
|
config.GITHUB_REPOSITORY,
|
||||||
BADGE_FILENAME,
|
config.BADGE_FILENAME,
|
||||||
"Update badge",
|
"Update badge",
|
||||||
input=badge,
|
input=badge,
|
||||||
)
|
)
|
||||||
@@ -244,7 +269,6 @@ def upload_badge(badge: str, config: Config) -> str:
|
|||||||
"wiki. You may disable it afterwards."
|
"wiki. You may disable it afterwards."
|
||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
return process.stdout.strip()
|
|
||||||
|
|
||||||
|
|
||||||
class Exit(Exception):
|
class Exit(Exception):
|
||||||
|
|||||||
@@ -2,3 +2,4 @@ diff-cover
|
|||||||
jinja2
|
jinja2
|
||||||
PyGithub
|
PyGithub
|
||||||
xmltodict
|
xmltodict
|
||||||
|
requests
|
||||||
|
|||||||
Reference in New Issue
Block a user