From b2f3d9428cbfb3e6e2634a46de3cdb182b6c6742 Mon Sep 17 00:00:00 2001 From: Jos Verlinde Date: Sat, 18 Nov 2023 13:01:40 +0100 Subject: [PATCH] Add compare_score.py script for comparing and updating snippet scores Signed-off-by: Jos Verlinde --- .github/workflows/compare_score.py | 78 +++++++++++++++++++++++++ .github/workflows/test_stub_quality.yml | 2 +- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/compare_score.py diff --git a/.github/workflows/compare_score.py b/.github/workflows/compare_score.py new file mode 100644 index 000000000..7347d1857 --- /dev/null +++ b/.github/workflows/compare_score.py @@ -0,0 +1,78 @@ +import json +import os + +import requests +from dotenv import load_dotenv +from dotenv import load_dotenv + +try: + + load_dotenv() # load variables + load_dotenv(".secrets") # load variables from the ".secrets" file +except: + pass + +# has been propagated from repo vars to env vars +try: + current_scores = json.loads(os.getenv("SNIPPET_SCORE", '{"snippet_score": 0}')) +except json.decoder.JSONDecodeError: + current_scores = {"snippet_score": 0} + +# set by pytest in custom conftest reporting +new_scores = {} +with open("coverage/snippet_score.json", "r") as f: + new_scores = json.load(f) + + +# Compare the scores and update the repository variable if necessary +def add_summary(msg, current_scores: dict, new_scores: dict): + if os.getenv("GITHUB_STEP_SUMMARY") is None: + print(f"The environment variable GITHUB_STEP_SUMMARY does not exist.") + return + with open(os.getenv("GITHUB_STEP_SUMMARY", 0), "a") as f: + f.write("# Snippets\n") + f.write(msg) + f.write("\n```json\n") + json.dump({"current": new_scores}, f) + f.write("\n") + json.dump({"previous": current_scores}, f) + f.write("\n```\n") + + + +def update_var(var_name: str, value: str): + repo = os.getenv("GITHUB_REPOSITORY", "Josverl/micropython-stubs") + gh_token_vars = os.getenv("GH_TOKEN_VARS", os.getenv("GH_TOKEN", "-")) + if gh_token_vars == "-": + print("No token available to update the repository variable") + return + # update the repository variable + url = f"https://api.github.com/repos/{repo}/actions/variables/{var_name}" + headers = { + "Authorization": f"token {gh_token_vars}", + "Content-Type": "application/json", + "User-Agent": "josverl", + } + data = {"name": str(var_name), "value": str(value)} + response = requests.patch(url, headers=headers, json=data) + response.raise_for_status() + + +if new_scores["snippet_score"] < current_scores["snippet_score"]: + msg = f"The snippet_score has decreased from {current_scores['snippet_score']} to {new_scores['snippet_score']}" + print(msg) + add_summary(msg, current_scores, new_scores) + exit(1) # Fail the test +elif new_scores["snippet_score"] == current_scores["snippet_score"]: + msg = f"The snippet_score has not changed from {current_scores['snippet_score']}" + print(msg) + add_summary(msg, current_scores, new_scores) +elif new_scores["snippet_score"] > current_scores["snippet_score"]: + msg = f"The snippet_score has improved to {new_scores['snippet_score']}" + print(msg) + add_summary(msg, current_scores, new_scores) + if os.getenv("GITHUB_REF_NAME", "main") == "main": + update_var(var_name="SNIPPET_SCORE", value=json.dumps(new_scores, skipkeys=True, indent=4)) + +print("Done") +exit(0) diff --git a/.github/workflows/test_stub_quality.yml b/.github/workflows/test_stub_quality.yml index 4368aefa5..9665c55cf 100644 --- a/.github/workflows/test_stub_quality.yml +++ b/.github/workflows/test_stub_quality.yml @@ -55,4 +55,4 @@ jobs: - name: compare and update run: | - poetry run python .github/workflows/compare_score.py + python .github/workflows/compare_score.py