Skip to content

Commit

Permalink
(#14015) linter: improve conandata.yml linter output
Browse files Browse the repository at this point in the history
* improve linter output

#13778

* Drop code fence

* more percent encoding

* Update conandata_yaml_linter.py

* Update conandata_yaml_linter.py

* Update conandata_yaml_linter.py

* Update conandata_yaml_linter.py

* Update conandata_yaml_linter.py

* remove test

* run the schema validation on each patch to report multiple errors

* fix copy paste + fix spelling

* fix libzip errors not being reported + add a message about v2 migration suggestion

* clean up markdown linter

* loop regardless of errors

* error messages are a lin off 😕
  • Loading branch information
Chris Mc authored Dec 20, 2022
1 parent 5712ef9 commit 47279a8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 33 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/linter-yaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ jobs:
- name: Run schema check (conandata.yml)
if: steps.changed_files.outputs.any_changed == 'true' && always()
run: |
err=0
for file in ${{ env.CONANDATA_FILES_PATH }}; do
python3 linter/conandata_yaml_linter.py ${file}
python3 linter/conandata_yaml_linter.py ${file} || err=1
done
if [[ $err == 1 ]]; then
exit 1
fi
lint_pr_files:
# Lint files modified in the pull_request
Expand Down Expand Up @@ -114,5 +118,8 @@ jobs:
echo "::remove-matcher owner=yamllint_matcher::"
for file in ${{ steps.changed_files_conandata.outputs.all_changed_files }}; do
python3 linter/conandata_yaml_linter.py ${file}
python3 linter/conandata_yaml_linter.py ${file} || err=1
done
if [[ $err == 1 ]]; then
exit 1
fi
5 changes: 1 addition & 4 deletions .github/workflows/markdown-links.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
name: Check Markdown links
name: "[linter] Markdown links"

on:
pull_request:
paths:
- '**.md'

env:
PYVER: "3.8"

jobs:
markdown-link-check-pr:
runs-on: ubuntu-latest
Expand Down
86 changes: 59 additions & 27 deletions linter/conandata_yaml_linter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import sys
from strictyaml import (
load,
Map,
Expand All @@ -13,6 +14,9 @@
from yaml_linting import file_path


CONANDATA_YAML_URL = "https://github.com/conan-io/conan-center-index/blob/master/docs/adding_packages/conandata_yml_format.md"


def main():
parser = argparse.ArgumentParser(
description="Validate Conan's 'conandata.yaml' file to ConanCenterIndex's requirements."
Expand All @@ -33,49 +37,77 @@ def main():
["official", "conan", "portability", "bugfix", "vulnerability"]
),
Optional("patch_source"): Str(),
Optional("sha256"): Str(), # Really uncommon
# No longer required for v2 recipes with layouts
Optional("base_path"): Str(),
}
)
schema = Map(
{
"sources": MapPattern(Str(), Any(), minimum_keys=1),
Optional("patches"): MapPattern(Str(), Seq(patch_fields), minimum_keys=1),
Optional("patches"): MapPattern(Str(), Seq(Any()), minimum_keys=1),
}
)

with open(args.path) as f:
with open(args.path, encoding="utf-8") as f:
content = f.read()

try:
parsed = load(content, schema)

if "patches" in parsed:
for version in parsed["patches"]:
patches = parsed["patches"][version]
for i, patch in enumerate(patches):
type = parsed["patches"][version][i]["patch_type"]
if (
type in ["official", "bugfix", "vulnerability"]
and not "patch_source" in patch
):
print(
f"::warning file={args.path},line={type.start_line},endline={type.end_line},"
f"title=conandata.yml schema warning"
"::'patch_type' should have 'patch_source' as per https://github.com/conan-io/conan-center-index/blob/master/docs/conandata_yml_format.md#patches-fields"
" it is expected to have a source (e.g. a URL) to where it originates from to help with reviewing and consumers to evaluate patches\n"
)
except YAMLValidationError as error:
e = error.__str__().replace("\n", "%0A")
print(
f"::error file={args.path},line={error.context_mark.line},endline={error.problem_mark.line},"
f"title=conandata.yml schema error"
f"::{e}\n"
)
pretty_print_yaml_validate_error(args, error)
sys.exit(1)
except BaseException as error:
e = error.__str__().replace("\n", "%0A")
print(f"::error ::{e}")
pretty_print_yaml_validate_error(args, error)
sys.exit(1)

exit_code = 0
if "patches" in parsed:
for version in parsed["patches"]:
patches = parsed["patches"][version]
for i, patch in enumerate(patches):
# Individual report errors for each patch object
try:
parsed["patches"][version][i].revalidate(patch_fields)
except YAMLValidationError as error:
pretty_print_yaml_validate_error(args, error)
exit_code = 1
continue

# Make sure `patch_source` exists where it's encouraged
type = parsed["patches"][version][i]["patch_type"]
if (
type in ["official", "bugfix", "vulnerability"]
and not "patch_source" in patch
):
print(
f"::warning file={args.path},line={type.start_line},endline={type.end_line},"
f"title=conandata.yml schema warning"
f"::'patch_type' should have 'patch_source' as per {CONANDATA_YAML_URL}#patch_type"
" it is expected to have a source (e.g. a URL) to where it originates from to help with"
" reviewing and consumers to evaluate patches"
)

# v2 migrations suggestion
if "base_path" in parsed["patches"][version][i]:
base_path = parsed["patches"][version][i]["base_path"]
print(
f"::notice file={args.path},line={base_path.start_line},endline={base_path.end_line},"
f"title=conandata.yml v2 migration suggestion"
"::'base_path' should not be required once a recipe has been upgraded to take advantage of"
" layouts (see https://docs.conan.io/en/latest/reference/conanfile/tools/layout.html) and"
" the new helper (see https://docs.conan.io/en/latest/reference/conanfile/tools/files/patches.html#conan-tools-files-apply-conandata-patches)"
)

sys.exit(exit_code)


def pretty_print_yaml_validate_error(args, error):
snippet = error.context_mark.get_snippet().replace("\n", "%0A")
print(
f"::error file={args.path},line={error.context_mark.line},endline={error.problem_mark.line+1},"
f"title=conandata.yml schema error"
f"::Schema outlined in {CONANDATA_YAML_URL}#patches-fields is not followed.%0A%0A{error.problem} in %0A{snippet}%0A"
)


if __name__ == "__main__":
Expand Down

0 comments on commit 47279a8

Please sign in to comment.