blob: 44408266fe2d3eb991634abec622044b9bb1a914 [file] [log] [blame]
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +00001#!/usr/bin/env python3
Dylan Bakere991c4d2023-12-13 11:38:41 -08002# SPDX-License-Identifier: Apache-2.0
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +00003# Copyright 2018 The Meson development team
4
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +00005import argparse
6import os
7import subprocess
8import sys
9import traceback
10
11
12def check_pr(is_pr_env):
13 if is_pr_env not in os.environ:
Eli Schwartz6a0fabc2021-03-04 17:16:11 -050014 print(f'This is not pull request: {is_pr_env} is not set')
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +000015 sys.exit()
16 elif os.environ[is_pr_env] == 'false':
Eli Schwartz6a0fabc2021-03-04 17:16:11 -050017 print(f'This is not pull request: {is_pr_env} is false')
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +000018 sys.exit()
19
20
21def get_base_branch(base_env):
22 if base_env not in os.environ:
Eli Schwartz6a0fabc2021-03-04 17:16:11 -050023 print(f'Unable to determine base branch: {base_env} is not set')
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +000024 sys.exit()
25 return os.environ[base_env]
26
27
28def get_git_files(base):
29 diff = subprocess.check_output(['git', 'diff', '--name-only', base + '...HEAD'])
30 return diff.strip().split(b'\n')
31
32
33def is_documentation(filename):
34 return filename.startswith(b'docs/')
35
36
37def 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 Turney7bdb3962018-10-06 18:09:56 +010044 parser.add_argument('--base-branch-origin', action='store_true',
45 help='Base branch reference is only in origin remote')
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +000046 args = parser.parse_args()
47 check_pr(args.is_pull_env)
48 base = get_base_branch(args.base_branch_env)
Jon Turney7bdb3962018-10-06 18:09:56 +010049 if args.base_branch_origin:
50 base = 'origin/' + base
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +000051 if all(is_documentation(f) for f in get_git_files(base)):
Guilherme Janczak3c66be02022-05-27 00:05:55 +000052 print("Documentation change, CI skipped.")
Aleksey Filippovaa3b4d52018-03-22 22:32:35 +000053 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
63if __name__ == '__main__':
64 main()