From 518a4f213361416c261f106c2f405593faac6daa Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Sun, 26 Sep 2021 00:52:19 +0200 Subject: [PATCH 1/5] Add debugging --- src/entrypoint | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/entrypoint b/src/entrypoint index 392a635..7b36c1d 100755 --- a/src/entrypoint +++ b/src/entrypoint @@ -38,6 +38,7 @@ def main(): ) post_comment(body=comment, gh=gh, config=config) + print(config.BADGE_ENABLED, 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") badge = compute_badge(coverage_info=coverage_info, config=config) @@ -193,6 +194,7 @@ def get_markdown_comment( def is_main_branch(gh: github.Github, config: Config) -> bool: repo = gh.get_repo(config.GITHUB_REPOSITORY) + print(repo, repo.default_branch, config.GITHUB_HEAD_REF) return repo.default_branch == config.GITHUB_HEAD_REF From 77d040cbed43fc83cbeade91a68a1899e4dd32a5 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Sun, 26 Sep 2021 00:59:54 +0200 Subject: [PATCH 2/5] Fix detection of current branch --- src/entrypoint | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/entrypoint b/src/entrypoint index 7b36c1d..1176cc4 100755 --- a/src/entrypoint +++ b/src/entrypoint @@ -57,7 +57,6 @@ class Config: GITHUB_BASE_REF: str GITHUB_TOKEN: str GITHUB_REPOSITORY: str - GITHUB_HEAD_REF: str GITHUB_REF: str BADGE_FILENAME: str = "coverage-comment-badge.json" COVERAGE_FILE: str = "coverage.xml" @@ -194,8 +193,11 @@ def get_markdown_comment( def is_main_branch(gh: github.Github, config: Config) -> bool: repo = gh.get_repo(config.GITHUB_REPOSITORY) - print(repo, repo.default_branch, config.GITHUB_HEAD_REF) - return repo.default_branch == config.GITHUB_HEAD_REF + # refs/heads/ + if not config.GITHUB_REF.startswith("refs/heads/"): + return False + branch = config.GITHUB_REF.split("/")[-1] + return repo.default_branch == branch def post_comment(body: str, gh: github.Github, config: Config) -> None: From 724ba0557ac1c685247471b72f36dda541a71380 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Sun, 26 Sep 2021 02:30:25 +0200 Subject: [PATCH 3/5] Fix diff detection on wiki --- src/add-to-wiki | 8 +++++--- src/entrypoint | 15 ++++++++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/add-to-wiki b/src/add-to-wiki index 72bde66..18bcfaa 100755 --- a/src/add-to-wiki +++ b/src/add-to-wiki @@ -16,13 +16,15 @@ cd $dir git clone "https://${GITHUB_TOKEN}@github.com/${repo_name}.wiki.git" . echo $stdin > "${filename}" -if ! git diff --exit-code -then - git add "${filename}" +git add "${filename}" +if ! git diff --staged --exit-code +then git config --global user.email "coverage-comment-action" git config --global user.name "Coverage Comment Action" git commit -m "$commit_message" git push -u origin +else + echo "No change detected, skipping." fi diff --git a/src/entrypoint b/src/entrypoint index 1176cc4..14e444d 100755 --- a/src/entrypoint +++ b/src/entrypoint @@ -25,6 +25,7 @@ def main(): config = Config.from_environ(os.environ) coverage_info = get_coverage_info(config=config) gh = get_api(config=config) + print(f"Operating on {config.GITHUB_REF}") 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) @@ -38,7 +39,6 @@ def main(): ) post_comment(body=comment, gh=gh, config=config) - print(config.BADGE_ENABLED, 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") badge = compute_badge(coverage_info=coverage_info, config=config) @@ -90,6 +90,13 @@ class Config: return int(self.GITHUB_REF.split("/")[2]) return None + @property + def GITHUB_BRANCH_NAME(self) -> Optional[str]: + # "refs/pull/2/merge" + if self.GITHUB_REF.startswith("refs/heads/"): + return self.GITHUB_REF.split("/")[-1] + return None + @classmethod def from_environ(cls, environ): possible_variables = [e for e in inspect.signature(cls).parameters] @@ -193,10 +200,8 @@ def get_markdown_comment( def is_main_branch(gh: github.Github, config: Config) -> bool: repo = gh.get_repo(config.GITHUB_REPOSITORY) - # refs/heads/ - if not config.GITHUB_REF.startswith("refs/heads/"): - return False - branch = config.GITHUB_REF.split("/")[-1] + + branch = config.GITHUB_BRANCH_NAME return repo.default_branch == branch From 8733456a669262f72d378d5151fb5d4022358cb1 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Sun, 26 Sep 2021 03:04:09 +0200 Subject: [PATCH 4/5] hm ? --- src/add-to-wiki | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/add-to-wiki b/src/add-to-wiki index 18bcfaa..bc7c28c 100755 --- a/src/add-to-wiki +++ b/src/add-to-wiki @@ -13,7 +13,7 @@ commit_message="${3}" dir="$(mktemp -d)" cd $dir -git clone "https://${GITHUB_TOKEN}@github.com/${repo_name}.wiki.git" . +git clone "https://x-access-token:${GITHUB_TOKEN}@github.com/${repo_name}.wiki.git" . echo $stdin > "${filename}" git add "${filename}" From de76db594cd88e9394e7437a886aba99764801c4 Mon Sep 17 00:00:00 2001 From: Joachim Jablon Date: Sun, 26 Sep 2021 03:13:10 +0200 Subject: [PATCH 5/5] Fix retrieving of previous results --- src/entrypoint | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/entrypoint b/src/entrypoint index 14e444d..7994abc 100755 --- a/src/entrypoint +++ b/src/entrypoint @@ -254,9 +254,8 @@ def get_badge_json_url(config: Config) -> str: def get_previous_coverage_rate(config: Config) -> Optional[float]: try: - return float( - requests.get(get_badge_json_url(config=config)).json()["value"][:-1] - ) + response = requests.get(get_badge_json_url(config=config)) + return float(response.json()["message"][:-1]) / 100 except Exception: print("Previous coverage results not found, cannot report on evolution.") return None