|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. |
| 7 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | +# (the "License"); you may not use this file except in compliance with |
| 9 | +# the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +""" |
| 21 | +Validates changelog YAML files in changelog/unreleased/ folder. |
| 22 | +
|
| 23 | +Checks: |
| 24 | +- File is valid YAML |
| 25 | +- Contains required 'title' field (non-empty string) |
| 26 | +- Contains required 'type' field (one of: added, changed, fixed, deprecated, removed, dependency_update, security, other) |
| 27 | +- Contains required 'authors' field with at least one author |
| 28 | +- Each author has a 'name' field (non-empty string) |
| 29 | +""" |
| 30 | + |
| 31 | +import sys |
| 32 | +import yaml |
| 33 | + |
| 34 | + |
| 35 | +def validate_changelog_yaml(file_path): |
| 36 | + """Validate a changelog YAML file.""" |
| 37 | + valid_types = ['added', 'changed', 'fixed', 'deprecated', 'removed', 'dependency_update', 'security', 'other'] |
| 38 | + |
| 39 | + try: |
| 40 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 41 | + data = yaml.safe_load(f) |
| 42 | + |
| 43 | + # Check if file contains a mapping (dictionary) |
| 44 | + if not isinstance(data, dict): |
| 45 | + print(f"::error file={file_path}::File must contain YAML mapping (key-value pairs)") |
| 46 | + return False |
| 47 | + |
| 48 | + # Validate 'title' field |
| 49 | + if 'title' not in data or not data['title']: |
| 50 | + print(f"::error file={file_path}::Missing or empty 'title' field") |
| 51 | + return False |
| 52 | + |
| 53 | + if not isinstance(data['title'], str) or not data['title'].strip(): |
| 54 | + print(f"::error file={file_path}::Field 'title' must be a non-empty string") |
| 55 | + return False |
| 56 | + |
| 57 | + # Validate 'type' field |
| 58 | + if 'type' not in data or not data['type']: |
| 59 | + print(f"::error file={file_path}::Missing or empty 'type' field") |
| 60 | + return False |
| 61 | + |
| 62 | + if data['type'] not in valid_types: |
| 63 | + print(f"::error file={file_path}::Invalid 'type': '{data['type']}'. Must be one of: {', '.join(valid_types)}") |
| 64 | + return False |
| 65 | + |
| 66 | + # Validate 'authors' field |
| 67 | + if 'authors' not in data or not data['authors']: |
| 68 | + print(f"::error file={file_path}::Missing or empty 'authors' field") |
| 69 | + return False |
| 70 | + |
| 71 | + if not isinstance(data['authors'], list): |
| 72 | + print(f"::error file={file_path}::Field 'authors' must be a list") |
| 73 | + return False |
| 74 | + |
| 75 | + if len(data['authors']) == 0: |
| 76 | + print(f"::error file={file_path}::Field 'authors' must contain at least one author") |
| 77 | + return False |
| 78 | + |
| 79 | + # Validate each author |
| 80 | + for i, author in enumerate(data['authors']): |
| 81 | + if not isinstance(author, dict): |
| 82 | + print(f"::error file={file_path}::Author {i} must be a mapping (key-value pairs)") |
| 83 | + return False |
| 84 | + if 'name' not in author or not author['name']: |
| 85 | + print(f"::error file={file_path}::Author {i} missing or empty 'name' field") |
| 86 | + return False |
| 87 | + if not isinstance(author['name'], str) or not author['name'].strip(): |
| 88 | + print(f"::error file={file_path}::Author {i} 'name' must be a non-empty string") |
| 89 | + return False |
| 90 | + |
| 91 | + # All validations passed |
| 92 | + print(f"✓ {file_path} is valid") |
| 93 | + print(f" Title: {data['title']}") |
| 94 | + print(f" Type: {data['type']}") |
| 95 | + print(f" Authors: {', '.join(a['name'] for a in data['authors'])}") |
| 96 | + return True |
| 97 | + |
| 98 | + except yaml.YAMLError as e: |
| 99 | + print(f"::error file={file_path}::Invalid YAML: {e}") |
| 100 | + return False |
| 101 | + except Exception as e: |
| 102 | + print(f"::error file={file_path}::Error validating file: {e}") |
| 103 | + return False |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + if len(sys.argv) < 2: |
| 108 | + print("Usage: validate-changelog-yaml.py <yaml-file>") |
| 109 | + sys.exit(1) |
| 110 | + |
| 111 | + file_path = sys.argv[1] |
| 112 | + if not validate_changelog_yaml(file_path): |
| 113 | + sys.exit(1) |
0 commit comments