|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Determine the CI type based on the change list and commit message. |
| 5 | +
|
| 6 | +Prints "quick" if (explicity required by user): |
| 7 | +- the *last* commit message contains 'ZFS-CI-Type: quick' |
| 8 | +or if (heuristics): |
| 9 | +- the files changed are not in the list of specified directories, and |
| 10 | +- all commit messages do not contain 'ZFS-CI-Type: full' |
| 11 | +
|
| 12 | +Otherwise prints "full". |
| 13 | +""" |
| 14 | + |
| 15 | +import sys |
| 16 | +import subprocess |
| 17 | +import re |
| 18 | + |
| 19 | +""" |
| 20 | +Patterns of files that are not considered to trigger full CI. |
| 21 | +Note: not using pathlib.Path.match() because it does not support '**' |
| 22 | +""" |
| 23 | +FULL_RUN_IGNORE_REGEX = list(map(re.compile, [ |
| 24 | + r'.*\.md', |
| 25 | + r'.*\.gitignore' |
| 26 | +])) |
| 27 | + |
| 28 | +""" |
| 29 | +Patterns of files that are considered to trigger full CI. |
| 30 | +""" |
| 31 | +FULL_RUN_REGEX = list(map(re.compile, [ |
| 32 | + r'cmd.*', |
| 33 | + r'configs/.*', |
| 34 | + r'META', |
| 35 | + r'.*\.am', |
| 36 | + r'.*\.m4', |
| 37 | + r'autogen\.sh', |
| 38 | + r'configure\.ac', |
| 39 | + r'copy-builtin', |
| 40 | + r'contrib', |
| 41 | + r'etc', |
| 42 | + r'include', |
| 43 | + r'lib/.*', |
| 44 | + r'module/.*', |
| 45 | + r'scripts/.*', |
| 46 | + r'tests/.*', |
| 47 | + r'udev/.*' |
| 48 | +])) |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + |
| 52 | + prog = sys.argv[0] |
| 53 | + |
| 54 | + if len(sys.argv) != 3: |
| 55 | + print(f'Usage: {prog} <head_ref> <base_ref>') |
| 56 | + sys.exit(1) |
| 57 | + |
| 58 | + head, base = sys.argv[1:3] |
| 59 | + |
| 60 | + def output_type(type, reason): |
| 61 | + print(f'{prog}: will run {type} CI: {reason}', file=sys.stderr) |
| 62 | + print(type) |
| 63 | + sys.exit(0) |
| 64 | + |
| 65 | + # check last (HEAD) commit message |
| 66 | + last_commit_message_raw = subprocess.run([ |
| 67 | + 'git', 'show', '-s', '--format=%B', 'HEAD' |
| 68 | + ], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 69 | + |
| 70 | + for line in last_commit_message_raw.stdout.decode().splitlines(): |
| 71 | + if line.strip().lower() == 'zfs-ci-type: quick': |
| 72 | + output_type('quick', f'explicitly requested by HEAD commit {head}') |
| 73 | + |
| 74 | + # check all commit messages |
| 75 | + all_commit_message_raw = subprocess.run([ |
| 76 | + 'git', 'show', '-s', |
| 77 | + '--format=ZFS-CI-Commit: %H%n%B', f'{head}...{base}' |
| 78 | + ], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 79 | + all_commit_message = all_commit_message_raw.stdout.decode().splitlines() |
| 80 | + |
| 81 | + commit_ref = head |
| 82 | + for line in all_commit_message: |
| 83 | + if line.startswith('ZFS-CI-Commit:'): |
| 84 | + commit_ref = line.lstrip('ZFS-CI-Commit:').rstrip() |
| 85 | + if line.strip().lower() == 'zfs-ci-type: full': |
| 86 | + output_type('full', f'explicitly requested by commit {commit_ref}') |
| 87 | + |
| 88 | + # check changed files |
| 89 | + changed_files_raw = subprocess.run([ |
| 90 | + 'git', 'diff', '--name-only', head, base |
| 91 | + ], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 92 | + changed_files = changed_files_raw.stdout.decode().splitlines() |
| 93 | + |
| 94 | + for f in changed_files: |
| 95 | + for r in FULL_RUN_IGNORE_REGEX: |
| 96 | + if r.match(f): |
| 97 | + break |
| 98 | + else: |
| 99 | + for r in FULL_RUN_REGEX: |
| 100 | + if r.match(f): |
| 101 | + output_type( |
| 102 | + 'full', |
| 103 | + f'changed file "{f}" matches pattern "{r.pattern}"' |
| 104 | + ) |
| 105 | + |
| 106 | + # catch-all |
| 107 | + output_type('quick', 'no changed file matches full CI patterns') |
0 commit comments