Add a simple validator for reusable workflows

This commit is contained in:
Mikhail f. Shiryaev 2023-11-10 19:45:57 +01:00
parent a831a648cd
commit 88e04579fd
No known key found for this signature in database
GPG Key ID: 4B02ED204C7D93F4
2 changed files with 60 additions and 1 deletions

View File

@ -2,8 +2,14 @@
set -e
WORKING_DIR=$(dirname "$0")
cd "$WORKING_DIR"
GIT_ROOT=$(git rev-parse --show-cdup)
GIT_ROOT=${GIT_ROOT:-.}
GIT_ROOT=${GIT_ROOT:-../../}
act --list --directory="$GIT_ROOT" 1>/dev/null 2>&1 || act --list --directory="$GIT_ROOT" 2>&1
actionlint -ignore 'reusable workflow call.+' || :
python3 check_reusable_workflows.py

View File

@ -0,0 +1,53 @@
#!/usr/bin/env python3
from pathlib import Path
from typing import Dict, Iterable, List
import yaml
git_root = Path(__file__).absolute().parents[2]
def check_workflows(paths: Iterable[Path]) -> List[str]:
outputs = [] # type: List[str]
for path in paths:
workflow_object = yaml.safe_load(path.read_bytes())
workflow_object["file---name"] = path.name
outputs.extend(check_name_override(workflow_object))
return outputs
def check_name_override(workflow_object: dict) -> List[str]:
outputs = [] # type: List[str]
workflow_file = workflow_object.get("file---name", "") # type: str
jobs = workflow_object.get("jobs", {}) # type: Dict[str, dict]
for name, obj in jobs.items():
header = f"Workflow '{workflow_file}': Job '{name}': "
name_overriden = obj.get("name", "")
env_name_overriden = obj.get("env", {}).get("GITHUB_JOB_OVERRIDDEN", "")
if name_overriden or env_name_overriden:
if not (name_overriden and env_name_overriden):
outputs.append(
f"{header}job has one of 'name' and 'env.GITHUB_JOB_OVERRIDDEN', "
"but not both"
)
elif name_overriden != env_name_overriden:
outputs.append(
f"{header}value of 'name' and 'env.GITHUB_JOB_OVERRIDDEN' are not "
f"equal. name={name_overriden}; "
f"env.GITHUB_JOB_OVERRIDDEN={env_name_overriden}"
)
return outputs
def main() -> None:
reusable_workflow_paths = git_root.glob(".github/workflows/reusable_*.y*ml")
outputs = check_workflows(reusable_workflow_paths)
if outputs:
print("Found next issues for workflows:")
for o in outputs:
print(o)
if __name__ == "__main__":
main()