|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +import requests |
| 5 | +from dotenv import load_dotenv |
| 6 | + |
| 7 | +try: |
| 8 | + |
| 9 | + load_dotenv() # load variables |
| 10 | + load_dotenv(".secrets") # load variables from the ".secrets" file |
| 11 | +except: |
| 12 | + pass |
| 13 | + |
| 14 | +# has been propagated from repo vars to env vars |
| 15 | +try: |
| 16 | + current_scores = json.loads(os.getenv("SNIPPET_SCORE", '{"snippet_score": 0}')) |
| 17 | +except json.decoder.JSONDecodeError: |
| 18 | + current_scores = {"snippet_score": 0} |
| 19 | + |
| 20 | +# set by pytest in custom conftest reporting |
| 21 | +new_scores = {} |
| 22 | +with open("results/snippet_score.json", "r") as f: |
| 23 | + new_scores = json.load(f) |
| 24 | + |
| 25 | + |
| 26 | +# Compare the scores and update the repository variable if necessary |
| 27 | +def add_summary(msg, current_scores: dict, new_scores: dict): |
| 28 | + if os.getenv("GITHUB_STEP_SUMMARY") is None: |
| 29 | + print(f"The environment variable GITHUB_STEP_SUMMARY does not exist.") |
| 30 | + return |
| 31 | + with open(os.getenv("GITHUB_STEP_SUMMARY", 0), "a") as f: |
| 32 | + f.write("# Snippets\n") |
| 33 | + f.write(msg) |
| 34 | + f.write("\n```json\n") |
| 35 | + json.dump({"current": new_scores}, f) |
| 36 | + f.write("\n") |
| 37 | + json.dump({"previous": current_scores}, f) |
| 38 | + f.write("\n```\n") |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +def update_var(var_name: str, value: str): |
| 43 | + repo = os.getenv("GITHUB_REPOSITORY", "Josverl/micropython-stubs") |
| 44 | + gh_token_vars = os.getenv("GH_TOKEN_VARS", os.getenv("GH_TOKEN", "-")) |
| 45 | + if gh_token_vars == "-": |
| 46 | + print("No token available to update the repository variable") |
| 47 | + return |
| 48 | + # update the repository variable |
| 49 | + url = f"https://api.github.com/repos/{repo}/actions/variables/{var_name}" |
| 50 | + headers = { |
| 51 | + "Authorization": f"token {gh_token_vars}", |
| 52 | + "Content-Type": "application/json", |
| 53 | + "User-Agent": "josverl", |
| 54 | + } |
| 55 | + data = {"name": str(var_name), "value": str(value)} |
| 56 | + response = requests.patch(url, headers=headers, json=data) |
| 57 | + response.raise_for_status() |
| 58 | + |
| 59 | + |
| 60 | +if new_scores["snippet_score"] < current_scores["snippet_score"]: |
| 61 | + msg = f"The snippet_score has decreased from {current_scores['snippet_score']} to {new_scores['snippet_score']}" |
| 62 | + print(msg) |
| 63 | + add_summary(msg, current_scores, new_scores) |
| 64 | + exit(1) # Fail the test |
| 65 | +elif new_scores["snippet_score"] == current_scores["snippet_score"]: |
| 66 | + msg = f"The snippet_score has not changed from {current_scores['snippet_score']}" |
| 67 | + print(msg) |
| 68 | + add_summary(msg, current_scores, new_scores) |
| 69 | +elif new_scores["snippet_score"] > current_scores["snippet_score"]: |
| 70 | + msg = f"The snippet_score has improved to {new_scores['snippet_score']}" |
| 71 | + print(msg) |
| 72 | + add_summary(msg, current_scores, new_scores) |
| 73 | + if os.getenv("GITHUB_REF_NAME", "main") == "main": |
| 74 | + update_var(var_name="SNIPPET_SCORE", value=json.dumps(new_scores, skipkeys=True, indent=4)) |
| 75 | + |
| 76 | +print("Done") |
| 77 | +exit(0) |
0 commit comments