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. previous_commit = os.getenv("PREVIOUS_COMMIT")
  51. diff_target = os.getenv("TARGET_BRANCH_NAME") if is_PR else previous_commit
  52. if is_PR:
  53. curr_branch = "HEAD"
  54. # Also fetch master to compare against
  55. subprocess.check_output(['bash', '-c', 'git fetch origin {0}:{0}'
  56. .format(diff_target)])
  57. else:
  58. curr_branch = os.getenv("GITHUB_SHA")
  59. # https://stackoverflow.com/questions/25071579/list-all-files-changed-in-a-pull-request-in-git-github
  60. changes = clean_output(
  61. subprocess.check_output([
  62. 'bash', '-c',
  63. 'git --no-pager diff --name-only {0} $(git merge-base {0} {1})'
  64. .format(curr_branch, diff_target)
  65. ], text=True))
  66. print("Determining what to run based on the following file changes: \n{!s}"
  67. .format('\n'.join(changes.split('\n')[0:10])))
  68. if len(changes.split('\n')) > 10:
  69. print("Too many files to show.")
  70. # COMMIT MESSAGES:
  71. # Before any complicated diffing, check for forced runs from the commit message
  72. # Use -2 because travis now inserts a merge commit as the last commit
  73. last_commit_msg = os.getenv("COMMIT_MESSAGE")
  74. test_dirs = []
  75. run_tests = []
  76. # Break the test env variable down into test directories
  77. if os.getenv("TESTLANG"):
  78. dir = "frameworks/" + os.getenv("TESTLANG") + "/"
  79. test_dirs = [os.getenv("TESTLANG") + "/" + x for x in [x for x in os.listdir(dir) if os.path.isdir(dir + x)]]
  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 [test_lang + "/" + x for x in [x for x in os.listdir(dir) if os.path.isdir(dir + x)]]
  86. test_dirs = []
  87. for frameworks in map(get_frameworks, os.listdir("frameworks")):
  88. for framework in frameworks:
  89. test_dirs.append(framework)
  90. # Forced full run
  91. if re.search(r'\[ci run-all\]', last_commit_msg, re.M):
  92. print("All tests have been forced to run from the commit message.")
  93. run_tests = test_dirs
  94. quit_diffing()
  95. # Forced *fw-only* specific tests
  96. if re.search(r'\[ci fw-only .+\]', last_commit_msg, re.M):
  97. tests = re.findall(r'\[ci fw-only (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  98. for test in tests:
  99. if test in test_dirs:
  100. print("{!s} has been forced to run from the commit message.".format(test))
  101. run_tests.append(test)
  102. # quit here because we're using "only"
  103. quit_diffing()
  104. # Forced *lang-only* specific tests
  105. if re.search(r'\[ci lang-only .+\]', last_commit_msg, re.M):
  106. langs = re.findall(r'\[ci lang-only (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  107. for test in test_dirs:
  108. for lang in langs:
  109. if test.startswith(lang + "/"):
  110. print("{!s} has been forced to run from the commit message.".format(test))
  111. run_tests.append(test)
  112. # quit here because we're using "only"
  113. quit_diffing()
  114. # Forced framework run in addition to other tests
  115. if re.search(r'\[ci fw .+\]', last_commit_msg, re.M):
  116. tests = re.findall(r'\[ci fw (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  117. for test in tests:
  118. if test in test_dirs:
  119. print("{!s} has been forced to run from the commit message.".format(test))
  120. run_tests.append(test)
  121. # Forced lang run in addition to other running tests
  122. if re.search(r'\[ci lang .+\]', last_commit_msg, re.M):
  123. langs = re.findall(r'\[ci lang (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  124. for test in test_dirs:
  125. for lang in langs:
  126. if test.startswith(lang + "/"):
  127. print("{!s} has been forced to run from the commit message.".format(test))
  128. run_tests.append(test)
  129. # Ignore travis, continuous and scaffolding changes
  130. if re.search(r'^toolset\/(?!(travis\/|continuous\/|scaffolding\/))|^tfb|^Dockerfile|^.github\/workflows\/', changes, re.M) is not None:
  131. print("Found changes to core toolset. Running all tests.")
  132. run_tests = test_dirs
  133. quit_diffing()
  134. for test in test_dirs:
  135. if fw_found_in_changes(test, changes):
  136. print("Found changes that affect {!s}".format(test))
  137. run_tests.append(test)
  138. quit_diffing()