1+ from __future__ import annotations
2+
3+ import argparse
4+ import sys
5+ from collections .abc import Sequence
6+ from typing import Any
7+
8+ import ruamel .yaml
9+
10+ yaml = ruamel .yaml .YAML (typ = "safe" )
11+
12+
13+ def uses_go_fips (melange_cfg : dict [str , Any ]) -> bool :
14+ """Check if package uses go-fips."""
15+ # Check environment packages
16+ env_packages = melange_cfg .get ("environment" , {}).get ("contents" , {}).get ("packages" , [])
17+ if "go-fips" in env_packages :
18+ return True
19+
20+ # Check pipeline steps for go/build with go-package: go-fips
21+ pipelines = melange_cfg .get ("pipeline" , [])
22+ for step in pipelines :
23+ if step .get ("uses" ) == "go/build" :
24+ if step .get ("with" , {}).get ("go-package" ) == "go-fips" :
25+ return True
26+
27+ # Check subpackage pipelines
28+ for subpkg in melange_cfg .get ("subpackages" , []):
29+ subpkg_pipelines = subpkg .get ("pipeline" , [])
30+ for step in subpkg_pipelines :
31+ if step .get ("uses" ) == "go/build" :
32+ if step .get ("with" , {}).get ("go-package" ) == "go-fips" :
33+ return True
34+
35+ return False
36+
37+
38+ def has_go_fips_test (melange_cfg : dict [str , Any ]) -> bool :
39+ """Check if package has go-fips test."""
40+ test_section = melange_cfg .get ("test" , {})
41+ test_pipelines = test_section .get ("pipeline" , [])
42+
43+ for step in test_pipelines :
44+ if step .get ("uses" ) == "test/go-fips-check" :
45+ return True
46+
47+ return False
48+
49+
50+ def main (argv : Sequence [str ] | None = None ) -> int :
51+ parser = argparse .ArgumentParser (
52+ description = "Check that packages using go-fips have corresponding go-fips tests"
53+ )
54+ parser .add_argument ("filenames" , nargs = "*" , help = "Filenames to check" )
55+ args = parser .parse_args (argv )
56+
57+ retval = 0
58+
59+ for filename in args .filenames :
60+ try :
61+ with open (filename ) as f :
62+ melange_cfg = yaml .load (f )
63+ except Exception as e :
64+ print (f"Error loading { filename } : { e } " )
65+ retval = 1
66+ continue
67+
68+ if not melange_cfg :
69+ continue
70+
71+ if uses_go_fips (melange_cfg ):
72+ if not has_go_fips_test (melange_cfg ):
73+ print (
74+ f"{ filename } : Package uses go-fips but does not have "
75+ "a corresponding go-fips test (add '- uses: test/go-fips-check' to test pipeline)"
76+ )
77+ retval = 1
78+
79+ return retval
80+
81+
82+ if __name__ == "__main__" :
83+ sys .exit (main ())
0 commit comments