This commit is contained in:
Joachim Jablon
2021-07-23 23:53:16 +02:00
parent 2cedb8b908
commit 626fbd3669
3 changed files with 111 additions and 0 deletions

14
Dockerfile Normal file
View File

@@ -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" ]

20
add-to-wiki.sh Executable file
View File

@@ -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

77
entrypoint.py Executable file
View File

@@ -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()