github_actions_diff.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env python
  2. # @file: toolset/github_actions/github_actions_diff.py
  3. # @author: Nate Brady
  4. #
  5. # @description: This script is only for use within Github Actions. It is meant
  6. # to look through the commit history and determine whether or not the current
  7. # framework test directory needs to be run. It compares the state of the PR
  8. # branch against the target branch.
  9. #
  10. # Any changes found in the toolset/* directory other than continuous/*,
  11. # github_actions/* and scaffolding/* will cause all tests to be run.
  12. #
  13. # The following commands can be put in commit messages to affect which tests
  14. # will run:
  15. #
  16. # [ci skip] - Provided by Travis. Travis won't trigger any builds.
  17. # [ci run-all] - This will force all tests to run.
  18. # [ci fw-only Java/gemini JavaScript/nodejs] - Ensures that only Java/gemini and
  19. # JavaScript/nodejs tests are run despite the detected changes.
  20. # [ci fw Java/gemini] - Forces Java/gemini to run in addition to detected changes.
  21. # [ci lang-only Java C++] - Ensures that only Java and C++ run despite detected changes.
  22. # [ci lang Java C++] - Forces Java and C++ tests to run in addition to detected changes.
  23. #
  24. # If only a single test within a language group is forced to run, none of the
  25. # other tests in that language group will run.
  26. #
  27. # The master branch will run the full suite of tests.
  28. #
  29. # IMPORTANT: the [ci *] commands must be added to every commit message. We do
  30. # not look at previous commit messages. Make sure to keep your PR branch
  31. # up-to-date with the target branch to avoid running unwanted tests.
  32. import subprocess
  33. import os
  34. import re
  35. def fw_found_in_changes(test, changes_output):
  36. return re.search(
  37. r"frameworks/" + re.escape(test) + "/",
  38. changes_output, re.M)
  39. # Cleans up diffing and grep output and into an array of strings
  40. def clean_output(output):
  41. return os.linesep.join([s for s in output.splitlines() if s])
  42. def quit_diffing():
  43. if len(run_tests):
  44. print("github-actions-run-tests {!s}".format(" ".join(set(run_tests))))
  45. else:
  46. print("No tests to run.")
  47. exit(0)
  48. curr_branch = ""
  49. is_PR = (os.getenv("PR_NUMBER") != "")
  50. # BRANCH_NAME is the the name of the branch
  51. is_master = os.getenv("BRANCH_NAME") == "master"
  52. if is_PR:
  53. curr_branch = "HEAD"
  54. # Also fetch master to compare against
  55. subprocess.check_output(['bash', '-c', 'git fetch origin master:master'])
  56. elif not is_master:
  57. curr_branch = os.getenv("GITHUB_SHA")
  58. # https://stackoverflow.com/questions/25071579/list-all-files-changed-in-a-pull-request-in-git-github
  59. changes = clean_output(
  60. subprocess.check_output([
  61. 'bash', '-c',
  62. 'git --no-pager diff --name-only {0} $(git merge-base {0} master)'
  63. .format(curr_branch)
  64. ]))
  65. print("Determining what to run based on the following file changes: \n{!s}"
  66. .format('\n'.join(changes.split('\n')[0:10])))
  67. if len(changes.split('\n')) > 10:
  68. print("Too many files to show.")
  69. # COMMIT MESSAGES:
  70. # Before any complicated diffing, check for forced runs from the commit message
  71. # Use -2 because travis now inserts a merge commit as the last commit
  72. last_commit_msg = os.getenv("COMMIT_MESSAGE")
  73. test_dirs = []
  74. run_tests = []
  75. # Break the test env variable down into test directories
  76. if os.getenv("TESTLANG"):
  77. dir = "frameworks/" + os.getenv("TESTLANG") + "/"
  78. test_dirs = map(lambda x: os.getenv("TESTLANG") + "/" + x,
  79. filter(lambda x: os.path.isdir(dir + x), os.listdir(dir)))
  80. elif os.getenv("TESTDIR"):
  81. test_dirs = os.getenv("TESTDIR").split(' ')
  82. else:
  83. def get_frameworks(test_lang):
  84. dir = "frameworks/" + test_lang + "/"
  85. return map(lambda x: test_lang + "/" + x,
  86. filter(lambda x: os.path.isdir(dir + x),
  87. os.listdir(dir)))
  88. test_dirs = []
  89. for frameworks in map(get_frameworks, os.listdir("frameworks")):
  90. for framework in frameworks:
  91. test_dirs.append(framework)
  92. # Forced full run
  93. if (not is_PR and is_master) or re.search(r'\[ci run-all\]', last_commit_msg, re.M):
  94. print("All tests have been forced to run from the commit message.")
  95. run_tests = test_dirs
  96. quit_diffing()
  97. # Forced *fw-only* specific tests
  98. if re.search(r'\[ci fw-only .+\]', last_commit_msg, re.M):
  99. tests = re.findall(r'\[ci fw-only (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  100. for test in tests:
  101. if test in test_dirs:
  102. print("{!s} has been forced to run from the commit message.".format(test))
  103. run_tests.append(test)
  104. # quit here because we're using "only"
  105. quit_diffing()
  106. # Forced *lang-only* specific tests
  107. if re.search(r'\[ci lang-only .+\]', last_commit_msg, re.M):
  108. langs = re.findall(r'\[ci lang-only (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  109. for test in test_dirs:
  110. for lang in langs:
  111. if test.startswith(lang + "/"):
  112. print("{!s} has been forced to run from the commit message.".format(test))
  113. run_tests.append(test)
  114. # quit here because we're using "only"
  115. quit_diffing()
  116. # Forced framework run in addition to other tests
  117. if re.search(r'\[ci fw .+\]', last_commit_msg, re.M):
  118. tests = re.findall(r'\[ci fw (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  119. for test in tests:
  120. if test in test_dirs:
  121. print("{!s} has been forced to run from the commit message.".format(test))
  122. run_tests.append(test)
  123. # Forced lang run in addition to other running tests
  124. if re.search(r'\[ci lang .+\]', last_commit_msg, re.M):
  125. langs = re.findall(r'\[ci lang (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  126. for test in test_dirs:
  127. for lang in langs:
  128. if test.startswith(lang + "/"):
  129. print("{!s} has been forced to run from the commit message.".format(test))
  130. run_tests.append(test)
  131. # Ignore travis, github_actions, continuous and scaffolding changes
  132. if re.search(r'^toolset\/(?!(travis\/|github_actions\/|continuous\/|scaffolding\/))', changes, re.M) is not None:
  133. print("Found changes to core toolset. Running all tests.")
  134. run_tests = test_dirs
  135. quit_diffing()
  136. for test in test_dirs:
  137. if fw_found_in_changes(test, changes):
  138. print("Found changes that affect {!s}".format(test))
  139. run_tests.append(test)
  140. quit_diffing()