From aa0b2ed545c62a67f71c815c50710f7b6914f94f Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Thu, 15 Aug 2024 17:40:24 -0600 Subject: [PATCH] Add script to populate required checks list Fixes #4126 --- scripts/add_required_checks.py | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scripts/add_required_checks.py diff --git a/scripts/add_required_checks.py b/scripts/add_required_checks.py new file mode 100644 index 00000000000..6179b15e326 --- /dev/null +++ b/scripts/add_required_checks.py @@ -0,0 +1,46 @@ +from requests import put +from os import environ +from yaml import safe_load +from json import dumps + +job_names = ["EasyCLA"] + +# Check that the files below are all the workflow YAML files that should be +# considered. +for yml_file_name in ["test_0", "test_1", "misc_0", "lint_0", "contrib_0"]: + + with open(f".github/workflows/{yml_file_name}.yml") as yml_file: + job_names.extend( + [job["name"] for job in safe_load(yml_file)["jobs"].values()] + ) + +owner = "open-telemetry" +repo = "opentelemetry-python" +branch = "main" + +response = put( + ( + f"https://api.github.com/repos/{owner}/{repo}/branches/{branch}/" + "protection/required_status_checks/contexts" + ), + headers={ + "Accept": "application/vnd.github.v3+json", + # The token has to be created in Github, and exported to the + # environment variable below. When creating the token, tthe resource + # owner must be open-telemetry, the access must be for the repo above, + # and read and write permissions must be granted for administration + # permissions and read permissions must be granted for metadata + # permissions. + "Authorization": f"token {environ.get('REQUIRED_CHECKS_TOKEN')}" + }, + data=dumps({"contexts": job_names}) +) + +if response.status_code == 200: + print(response.content) +else: + print( + "Failed to update branch protection settings. " + f"Status code: {response.status_code}" + ) + print(response.json())