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