Add dependency computation for scripts in presubmit job (#14443)

* Add dependency computation for scripts in presubmit job

* Fix comment

* format code

* Filter to *test.sh files only

* Rename folder_prefix var
This commit is contained in:
Leonardo Sarra 2024-01-08 16:26:29 +01:00 committed by GitHub
parent e7e26579c9
commit 6acb30e7f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 1 deletions

View File

@ -16,8 +16,37 @@
import argparse
import sys
import os
import re
from github import Github
def get_script_dependencies(script_path, folder_prefix):
dependencies = set()
with open(script_path, 'r') as script_file:
script_content = script_file.read()
# Using regular expression to find 'source' followed by a script path
pattern = r'^source\s+["\']?(.+?)["\']?$'
matches = (match for match in re.findall(pattern, script_content, re.MULTILINE) if match.startswith(folder_prefix))
dependencies.update(matches)
return dependencies
def get_script_dependencies_graph(path_prefix, file_suffix="test.sh"):
matching_files = [os.path.join(root, file_name) for root, _, files in os.walk(path_prefix) for file_name in files if file_name.endswith(file_suffix)]
package_dependencies = {}
for file in matching_files:
dependencies = get_script_dependencies(file, path_prefix)
for dep in dependencies:
dep = os.path.dirname(dep)
package_dependencies.setdefault(dep, set()).add(os.path.dirname(file))
return package_dependencies
istio_doc_repo = "istio/istio.io"
doc_file_prefix = "content/en/docs/"
boilerplate_snip_prefix = "content/en/boilerplates/snips/"
@ -35,7 +64,7 @@ args = parser.parse_args()
pull_number = int(args.pull_number)
repo_name = args.repo if args.repo else istio_doc_repo
access_token = args.token if args.token else None # Warning: Github rate limit is very low (60 req/hr) without access token
script_dependencies = get_script_dependencies_graph(doc_file_prefix)
try:
g = Github(access_token)
repo = g.get_repo(repo_name)
@ -52,6 +81,17 @@ try:
if filename.endswith("test.sh") or filename.endswith("/snips.sh"):
relative_file = filename[len(doc_file_prefix):]
test_paths.add(relative_file.rsplit('/', 1)[0])
# Tentatively add tests that externally imported other module files
checked = set()
to_check = {os.path.dirname(filename)}
while to_check:
el = to_check.pop()
checked.add(el)
if el in script_dependencies:
to_add = script_dependencies[el] - checked
to_check.update(to_add)
test_paths.update(map(lambda file_path: file_path[len(doc_file_prefix):].rsplit('/', 1)[0], to_add))
elif filename == istio_go_dependency or \
filename.startswith(test_framework_pkg) or \
filename.startswith(test_framework_util) or \