skip_ci.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. from __future__ import print_function
  13. import argparse
  14. import os
  15. import subprocess
  16. import sys
  17. import traceback
  18. def check_pr(is_pr_env):
  19. if is_pr_env not in os.environ:
  20. print('This is not pull request: {} is not set'.format(is_pr_env))
  21. sys.exit()
  22. elif os.environ[is_pr_env] == 'false':
  23. print('This is not pull request: {} is false'.format(is_pr_env))
  24. sys.exit()
  25. def get_base_branch(base_env):
  26. if base_env not in os.environ:
  27. print('Unable to determine base branch: {} is not set'.format(base_env))
  28. sys.exit()
  29. return os.environ[base_env]
  30. def get_git_files(base):
  31. diff = subprocess.check_output(['git', 'diff', '--name-only', base + '...HEAD'])
  32. return diff.strip().split(b'\n')
  33. def is_documentation(filename):
  34. return filename.startswith(b'docs/')
  35. def main():
  36. try:
  37. parser = argparse.ArgumentParser(description='CI Skipper')
  38. parser.add_argument('--base-branch-env', required=True,
  39. help='Branch push is targeted to')
  40. parser.add_argument('--is-pull-env', required=True,
  41. help='Variable set if it is a PR')
  42. parser.add_argument('--base-branch-origin', action='store_true',
  43. help='Base branch reference is only in origin remote')
  44. args = parser.parse_args()
  45. check_pr(args.is_pull_env)
  46. base = get_base_branch(args.base_branch_env)
  47. if args.base_branch_origin:
  48. base = 'origin/' + base
  49. if all(is_documentation(f) for f in get_git_files(base)):
  50. print("Don't run CI for documentation-only changes, add '[skip ci]' to commit title.")
  51. print('See http://mesonbuild.com/Contributing.html#skipping-integration-tests')
  52. sys.exit(1)
  53. except Exception:
  54. # If this script fails we want build to proceed.
  55. # Failure likely means some corner case we did not consider or bug.
  56. # Either case this should not prevent CI from running if it is needed,
  57. # and we tolerate it if it is run where it is not required.
  58. traceback.print_exc()
  59. print('There is a BUG in skip_ci.py, exiting.')
  60. sys.exit()
  61. if __name__ == '__main__':
  62. main()