From 626fbd36699aff484eeb9305891178f3bb77a25f Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Fri, 23 Jul 2021 23:53:16 +0200 Subject: [PATCH] Action --- Dockerfile | 14 +++++++++ add-to-wiki.sh | 20 +++++++++++++ entrypoint.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 Dockerfile create mode 100755 add-to-wiki.sh create mode 100755 entrypoint.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cc0a847 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM python:3-slim + +WORKDIR /tool + +RUN set -eux; \ + apt-get update; \ + apt-get install -y git; \ + rm -rf /var/lib/apt/lists/*; \ + pip install ghapi diff-cover + +COPY entrypoint.py /entrypoint.py +COPY add-to-wiki.sh /add-to-wiki.sh + +CMD [ "/entrypoint.py" ] diff --git a/add-to-wiki.sh b/add-to-wiki.sh new file mode 100755 index 0000000..368e45b --- /dev/null +++ b/add-to-wiki.sh @@ -0,0 +1,20 @@ +#!/bin/sh +# Usage $0 {repo/owner} {filename} {commit_message} +# Stores the content of stdin in a file named {filename} in the wiki of +# the provided repo +# Reads envvar GITHUB_TOKEN + +set -eux + +stdin=$(cat -) +repo_name=${1} +filename=${2} +commit_message=${3} +dir=$(mktemp -d) +cd $dir + +git clone "https://${GITHUB_TOKEN}@github.com/${repo_name}.wiki.git" . +echo $stdin > ${filename} +git add ${filename} +git commit -m $commit_message +git push -u origin diff --git a/entrypoint.py b/entrypoint.py new file mode 100755 index 0000000..f1be0cd --- /dev/null +++ b/entrypoint.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 + +import dataclasses +import inspect +import os +import subprocess +import sys +import tempfile +from typing import List + + +def main(): + config = Config.from_environ(os.environ) + + comment = get_markdown_comment(config=config) + post_comment(comment=comment, config=config) + + if config.badge_enabled: + coverage_percent = get_coverage(config=config) + upload_badge(coverage_percent=coverage_percent, config=config) + + +@dataclasses.dataclass +class Config: + """This object defines the environment variables""" + + GITHUB_TOKEN: str + COVERAGE_FILE: str = "coverage.xml" + DIFF_COVER_ARGS: List[str] = dataclasses.field(default_factory=list) + BADGE_ENABLED: bool = False + + # Clean methods + @classmethod + def clean_diff_cover_args(cls, value: str) -> list: + return [e.strip() for e in value.split("\n")] + + @classmethod + def clean_badge_enabled(cls, value: str) -> bool: + return value.lower() in ("1", "true", "yes") + + @classmethod + def from_environ(cls, environ): + possible_variables = [e for e in inspect.signature(cls).parameters] + config = {k: v for k, v in environ.items() if k in possible_variables} + for key, value in list(config.items()): + if func := getattr(cls, f"clean_{key.lower()}", None): + config[key] = func(value) + + try: + return cls(**config) + except TypeError as exc: + sys.exit(f"{exc} environment variable is mandatory") + + +def get_markdown_comment(config: Config) -> str: + with tempfile.NamedTemporaryFile("r") as f: + subprocess.check_call( + ["diff-cover", "coverage.xml", "--markdown-report", f.name] + + config.DIFF_COVER_ARGS + ) + return f.read() + + +def post_comment(comment: str, config: Config) -> None: + pass + + +def get_coverage(config: Config) -> int: + pass + + +def upload_badge(coverage_percent: int, config: Config) -> None: + pass + + +if __name__ == "__main__": + main()