github_actions_diff.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. ]))
  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 = map(lambda x: os.getenv("TESTLANG") + "/" + x,
  80. filter(lambda x: os.path.isdir(dir + x), os.listdir(dir)))
  81. elif os.getenv("TESTDIR"):
  82. test_dirs = os.getenv("TESTDIR").split(' ')
  83. else:
  84. def get_frameworks(test_lang):
  85. dir = "frameworks/" + test_lang + "/"
  86. return map(lambda x: test_lang + "/" + x,
  87. filter(lambda x: os.path.isdir(dir + x),
  88. os.listdir(dir)))
  89. test_dirs = []
  90. for frameworks in map(get_frameworks, os.listdir("frameworks")):
  91. for framework in frameworks:
  92. test_dirs.append(framework)
  93. # Forced full run
  94. if re.search(r'\[ci run-all\]', last_commit_msg, re.M):
  95. print("All tests have been forced to run from the commit message.")
  96. run_tests = test_dirs
  97. quit_diffing()
  98. # Forced *fw-only* specific tests
  99. if re.search(r'\[ci fw-only .+\]', last_commit_msg, re.M):
  100. tests = re.findall(r'\[ci fw-only (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  101. for test in tests:
  102. if test in test_dirs:
  103. print("{!s} has been forced to run from the commit message.".format(test))
  104. run_tests.append(test)
  105. # quit here because we're using "only"
  106. quit_diffing()
  107. # Forced *lang-only* specific tests
  108. if re.search(r'\[ci lang-only .+\]', last_commit_msg, re.M):
  109. langs = re.findall(r'\[ci lang-only (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  110. for test in test_dirs:
  111. for lang in langs:
  112. if test.startswith(lang + "/"):
  113. print("{!s} has been forced to run from the commit message.".format(test))
  114. run_tests.append(test)
  115. # quit here because we're using "only"
  116. quit_diffing()
  117. # Forced framework run in addition to other tests
  118. if re.search(r'\[ci fw .+\]', last_commit_msg, re.M):
  119. tests = re.findall(r'\[ci fw (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  120. for test in tests:
  121. if test in test_dirs:
  122. print("{!s} has been forced to run from the commit message.".format(test))
  123. run_tests.append(test)
  124. # Forced lang run in addition to other running tests
  125. if re.search(r'\[ci lang .+\]', last_commit_msg, re.M):
  126. langs = re.findall(r'\[ci lang (.+)\]', last_commit_msg, re.M)[0].strip().split(' ')
  127. for test in test_dirs:
  128. for lang in langs:
  129. if test.startswith(lang + "/"):
  130. print("{!s} has been forced to run from the commit message.".format(test))
  131. run_tests.append(test)
  132. # Ignore travis, continuous and scaffolding changes
  133. if re.search(r'^toolset\/(?!(travis\/|continuous\/|scaffolding\/))|^tfb|^Dockerfile|^.github\/workflows\/', changes, re.M) is not None:
  134. print("Found changes to core toolset. Running all tests.")
  135. run_tests = test_dirs
  136. quit_diffing()
  137. for test in test_dirs:
  138. if fw_found_in_changes(test, changes):
  139. print("Found changes that affect {!s}".format(test))
  140. run_tests.append(test)
  141. quit_diffing()