diff --git a/action.yml b/action.yml index 4ae030b..3735b1b 100644 --- a/action.yml +++ b/action.yml @@ -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 }} diff --git a/src/add-to-wiki b/src/add-to-wiki index 4bac70b..72bde66 100755 --- a/src/add-to-wiki +++ b/src/add-to-wiki @@ -26,5 +26,3 @@ then git push -u origin fi - -echo "https://raw.githubusercontent.com/wiki/${repo_name}/${filename}" diff --git a/src/default.md.j2 b/src/default.md.j2 index 1e374da..0f14cfd 100644 --- a/src/default.md.j2 +++ b/src/default.md.j2 @@ -19,7 +19,7 @@ The branch rate is `{{ branch_coverage }}%`
Diff Coverage details (click to unfold) -{% for filename, stats in file_info.items() -%}} +{% for filename, stats in file_info.items() -%} ### {{ filename }} `{{ stats.diff_coverage }}%` of new lines are covered diff --git a/src/entrypoint b/src/entrypoint index f4016e4..392a635 100755 --- a/src/entrypoint +++ b/src/entrypoint @@ -12,20 +12,19 @@ from typing import List, Optional import github import jinja2 +import requests import xmltodict MARKER = """""" -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(): + print("Starting action") config = Config.from_environ(os.environ) coverage_info = get_coverage_info(config=config) gh = get_api(config=config) - if 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) @@ -41,11 +40,14 @@ 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)}") + print("Ending action") + @dataclasses.dataclass class Config: @@ -56,10 +58,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 +75,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 +217,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 +237,28 @@ 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: + 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", config.GITHUB_REPOSITORY, - BADGE_FILENAME, + config.BADGE_FILENAME, "Update badge", input=badge, ) @@ -244,7 +269,6 @@ def upload_badge(badge: str, config: Config) -> str: "wiki. You may disable it afterwards." ) raise - return process.stdout.strip() class Exit(Exception): diff --git a/src/requirements.txt b/src/requirements.txt index 299e810..3eea122 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -2,3 +2,4 @@ diff-cover jinja2 PyGithub xmltodict +requests