Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Dylan Baker | e991c4d | 2023-12-13 11:38:41 -0800 | [diff] [blame] | 2 | # SPDX-License-Identifier: Apache-2.0 |
Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 3 | # Copyright 2018 The Meson development team |
| 4 | |
Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 5 | import argparse |
| 6 | import os |
| 7 | import subprocess |
| 8 | import sys |
| 9 | import traceback |
| 10 | |
| 11 | |
| 12 | def check_pr(is_pr_env): |
| 13 | if is_pr_env not in os.environ: |
Eli Schwartz | 6a0fabc | 2021-03-04 17:16:11 -0500 | [diff] [blame] | 14 | print(f'This is not pull request: {is_pr_env} is not set') |
Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 15 | sys.exit() |
| 16 | elif os.environ[is_pr_env] == 'false': |
Eli Schwartz | 6a0fabc | 2021-03-04 17:16:11 -0500 | [diff] [blame] | 17 | print(f'This is not pull request: {is_pr_env} is false') |
Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 18 | sys.exit() |
| 19 | |
| 20 | |
| 21 | def get_base_branch(base_env): |
| 22 | if base_env not in os.environ: |
Eli Schwartz | 6a0fabc | 2021-03-04 17:16:11 -0500 | [diff] [blame] | 23 | print(f'Unable to determine base branch: {base_env} is not set') |
Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 24 | sys.exit() |
| 25 | return os.environ[base_env] |
| 26 | |
| 27 | |
| 28 | def get_git_files(base): |
| 29 | diff = subprocess.check_output(['git', 'diff', '--name-only', base + '...HEAD']) |
| 30 | return diff.strip().split(b'\n') |
| 31 | |
| 32 | |
| 33 | def is_documentation(filename): |
| 34 | return filename.startswith(b'docs/') |
| 35 | |
| 36 | |
| 37 | def main(): |
| 38 | try: |
| 39 | parser = argparse.ArgumentParser(description='CI Skipper') |
| 40 | parser.add_argument('--base-branch-env', required=True, |
| 41 | help='Branch push is targeted to') |
| 42 | parser.add_argument('--is-pull-env', required=True, |
| 43 | help='Variable set if it is a PR') |
Jon Turney | 7bdb396 | 2018-10-06 18:09:56 +0100 | [diff] [blame] | 44 | parser.add_argument('--base-branch-origin', action='store_true', |
| 45 | help='Base branch reference is only in origin remote') |
Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 46 | args = parser.parse_args() |
| 47 | check_pr(args.is_pull_env) |
| 48 | base = get_base_branch(args.base_branch_env) |
Jon Turney | 7bdb396 | 2018-10-06 18:09:56 +0100 | [diff] [blame] | 49 | if args.base_branch_origin: |
| 50 | base = 'origin/' + base |
Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 51 | if all(is_documentation(f) for f in get_git_files(base)): |
Guilherme Janczak | 3c66be0 | 2022-05-27 00:05:55 +0000 | [diff] [blame] | 52 | print("Documentation change, CI skipped.") |
Aleksey Filippov | aa3b4d5 | 2018-03-22 22:32:35 +0000 | [diff] [blame] | 53 | sys.exit(1) |
| 54 | except Exception: |
| 55 | # If this script fails we want build to proceed. |
| 56 | # Failure likely means some corner case we did not consider or bug. |
| 57 | # Either case this should not prevent CI from running if it is needed, |
| 58 | # and we tolerate it if it is run where it is not required. |
| 59 | traceback.print_exc() |
| 60 | print('There is a BUG in skip_ci.py, exiting.') |
| 61 | sys.exit() |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | main() |