skip_ci.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. # Copyright 2018 The Meson development team
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. import argparse
  13. import os
  14. import subprocess
  15. import sys
  16. import traceback
  17. def check_pr(is_pr_env):
  18. if is_pr_env not in os.environ:
  19. print(f'This is not pull request: {is_pr_env} is not set')
  20. sys.exit()
  21. elif os.environ[is_pr_env] == 'false':
  22. print(f'This is not pull request: {is_pr_env} is false')
  23. sys.exit()
  24. def get_base_branch(base_env):
  25. if base_env not in os.environ:
  26. print(f'Unable to determine base branch: {base_env} is not set')
  27. sys.exit()
  28. return os.environ[base_env]
  29. def get_git_files(base):
  30. diff = subprocess.check_output(['git', 'diff', '--name-only', base + '...HEAD'])
  31. return diff.strip().split(b'\n')
  32. def is_documentation(filename):
  33. return filename.startswith(b'docs/')
  34. def main():
  35. try:
  36. parser = argparse.ArgumentParser(description='CI Skipper')
  37. parser.add_argument('--base-branch-env', required=True,
  38. help='Branch push is targeted to')
  39. parser.add_argument('--is-pull-env', required=True,
  40. help='Variable set if it is a PR')
  41. parser.add_argument('--base-branch-origin', action='store_true',
  42. help='Base branch reference is only in origin remote')
  43. args = parser.parse_args()
  44. check_pr(args.is_pull_env)
  45. base = get_base_branch(args.base_branch_env)
  46. if args.base_branch_origin:
  47. base = 'origin/' + base
  48. if all(is_documentation(f) for f in get_git_files(base)):
  49. print("Documentation change, CI skipped.")
  50. sys.exit(1)
  51. except Exception:
  52. # If this script fails we want build to proceed.
  53. # Failure likely means some corner case we did not consider or bug.
  54. # Either case this should not prevent CI from running if it is needed,
  55. # and we tolerate it if it is run where it is not required.
  56. traceback.print_exc()
  57. print('There is a BUG in skip_ci.py, exiting.')
  58. sys.exit()
  59. if __name__ == '__main__':
  60. main()