|
| 1 | +"""Validate the YAML files for GitHub Actions workflows. |
| 2 | +
|
| 3 | +TODO: b/359303016 - convert to use unittest |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +import re |
| 8 | + |
| 9 | +import yaml |
| 10 | + |
| 11 | +# Ensure every job is in the list of blocking jobs. |
| 12 | +with open( |
| 13 | + os.path.join(os.path.dirname(__file__), '../workflows/test_runner.yml'), 'r' |
| 14 | +) as f: |
| 15 | + data = yaml.safe_load(f) |
| 16 | + |
| 17 | + # List of all YAML files that are used by jobs in the test_runner.yml file. |
| 18 | + yaml_files = [] |
| 19 | + |
| 20 | + # Get a list of all jobs in the test_runner, except for the blocking job and |
| 21 | + # the tag removal job, which is not always run. |
| 22 | + all_jobs = list(data['jobs'].keys()) |
| 23 | + all_jobs.remove('all-blocking-tests') |
| 24 | + all_jobs.remove('remove-tag') |
| 25 | + |
| 26 | + passed = True |
| 27 | + blocking_jobs = data['jobs']['all-blocking-tests']['needs'] |
| 28 | + |
| 29 | + for job in all_jobs: |
| 30 | + if 'uses' in data['jobs'][job]: |
| 31 | + yaml_files.append( |
| 32 | + os.path.join( |
| 33 | + os.path.dirname(__file__), |
| 34 | + '../workflows', |
| 35 | + os.path.basename(data['jobs'][job]['uses']), |
| 36 | + ) |
| 37 | + ) |
| 38 | + if job not in blocking_jobs: |
| 39 | + passed = False |
| 40 | + raise ValueError('Job %s is not in the list of blocking jobs.' % job) |
| 41 | + |
| 42 | + print('PASSED: All jobs are in the list of blocking jobs.') |
| 43 | + |
| 44 | +# Ensure every job with a continuous prefix conditions every step on whether we |
| 45 | +# are in a continuous run. |
| 46 | +for file in yaml_files: |
| 47 | + with open(file, 'r') as f: |
| 48 | + data = yaml.safe_load(f) |
| 49 | + jobs = data['jobs'] |
| 50 | + for job in jobs: |
| 51 | + if 'steps' not in jobs[job]: |
| 52 | + continue |
| 53 | + continuous_condition = 'inputs.continuous-prefix' in jobs[job]['name'] |
| 54 | + steps = jobs[job]['steps'] |
| 55 | + for step in steps: |
| 56 | + if 'name' in step: |
| 57 | + name = step['name'] |
| 58 | + elif 'with' in step and 'name' in step['with']: |
| 59 | + name = step['with']['name'] |
| 60 | + else: |
| 61 | + raise ValueError( |
| 62 | + 'Step in job %s from file %s does not have a name.' % (job, file) |
| 63 | + ) |
| 64 | + if continuous_condition and 'continuous-run' not in step.get('if', ''): |
| 65 | + raise ValueError( |
| 66 | + 'Step %s in job %s from file %s does not check the continuous-run' |
| 67 | + ' condition' % (name, job, file) |
| 68 | + ) |
| 69 | + if not continuous_condition and 'continuous-run' in step.get('if', ''): |
| 70 | + raise ValueError( |
| 71 | + 'Step %s in job %s from file %s checks the continuous-run' |
| 72 | + ' condition but the job does not contain the continuous-prefix' |
| 73 | + % (name, job, file) |
| 74 | + ) |
| 75 | +print('PASSED: All steps in all jobs check the continuous-run condition.') |
0 commit comments