|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import re |
| 6 | +import subprocess |
| 7 | +import unittest |
| 8 | +import urllib.request |
| 9 | + |
| 10 | +PROJECT_ROOT = pathlib.Path(__file__).parent.parent.parent.resolve() |
| 11 | + |
| 12 | + |
| 13 | +def get_github_token() -> str: |
| 14 | + github_token = os.environ['GITHUB_TOKEN'] |
| 15 | + return github_token |
| 16 | + |
| 17 | + |
| 18 | +# https://docs.github.com/en/graphql/guides/forming-calls-with-graphql |
| 19 | +def compose_gh_api_request(owner="opendatahub-io", repo="notebooks", pull_number=556, per_page=100, cursor=""): |
| 20 | + github_token = get_github_token() |
| 21 | + |
| 22 | + return urllib.request.Request( |
| 23 | + url="https://api.github.com/graphql", |
| 24 | + method="POST", |
| 25 | + headers={ |
| 26 | + "Authorization": f"bearer {github_token}", |
| 27 | + }, |
| 28 | + # https://docs.github.com/en/graphql/guides/using-the-explorer |
| 29 | + data=json.dumps({"query": f""" |
| 30 | +{{ |
| 31 | + repository(owner:"{owner}", name:"{repo}") {{ |
| 32 | + pullRequest(number:{pull_number}) {{ |
| 33 | + files(first:{per_page}, after:"{cursor}") {{ |
| 34 | + edges {{ |
| 35 | + node {{ |
| 36 | + path |
| 37 | + }} |
| 38 | + cursor |
| 39 | + }} |
| 40 | + }} |
| 41 | + }} |
| 42 | + }} |
| 43 | +}} |
| 44 | + """}).encode("utf-8"), |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +def list_changed_files(owner: str, repo: str, pr_number: int, per_page=100) -> list[str]: |
| 49 | + files = [] |
| 50 | + |
| 51 | + logging.debug("Getting list of changed files from GitHub API") |
| 52 | + |
| 53 | + CURSOR = "" |
| 54 | + while CURSOR is not None: |
| 55 | + request = compose_gh_api_request(owner, repo, pull_number=pr_number, per_page=per_page, cursor=CURSOR) |
| 56 | + response = urllib.request.urlopen(request) |
| 57 | + data = json.loads(response.read().decode("utf-8")) |
| 58 | + response.close() |
| 59 | + edges = data["data"]["repository"]["pullRequest"]["files"]["edges"] |
| 60 | + |
| 61 | + CURSOR = None |
| 62 | + for edge in edges: |
| 63 | + files.append(edge["node"]["path"]) |
| 64 | + CURSOR = edge["cursor"] |
| 65 | + |
| 66 | + logging.debug(f"Determined {len(files)} changed files: {files[:5]} (..., printing up to 5)") |
| 67 | + return files |
| 68 | + |
| 69 | + |
| 70 | +def analyze_build_directories(make_target) -> list[str]: |
| 71 | + directories = [] |
| 72 | + |
| 73 | + pattern = re.compile(r"#\*# Image build directory: <(?P<dir>[^>]+)> #\(MACHINE-PARSED LINE\)#\*#\.\.\.") |
| 74 | + try: |
| 75 | + logging.debug(f"Running make in --just-print mode for target {make_target}") |
| 76 | + for line in subprocess.check_output(["make", make_target, "--just-print"], encoding="utf-8", |
| 77 | + cwd=PROJECT_ROOT).splitlines(): |
| 78 | + if m := pattern.match(line): |
| 79 | + directories.append(m["dir"]) |
| 80 | + except subprocess.CalledProcessError as e: |
| 81 | + print(e.stderr, e.stdout) |
| 82 | + raise |
| 83 | + |
| 84 | + logging.debug(f"Running make in --just-print mode for target {make_target}") |
| 85 | + return directories |
| 86 | + |
| 87 | + |
| 88 | +def should_build_target(changed_files: list[str], target_directories: list[str]) -> bool: |
| 89 | + for directory in target_directories: |
| 90 | + if any(changed_file.startswith(directory) for changed_file in changed_files): |
| 91 | + return True |
| 92 | + return False |
| 93 | + |
| 94 | + |
| 95 | +def filter_out_unchanged(owner: str, repo: str, pr_number: int, targets: list[str]) -> list[str]: |
| 96 | + changed_files = list_changed_files(owner, repo, pr_number) |
| 97 | + return [target for target in targets if |
| 98 | + should_build_target(changed_files, target_directories=analyze_build_directories(target))] |
| 99 | + |
| 100 | + |
| 101 | +class SelTestsTest(unittest.TestCase): |
| 102 | + def test_compose_gh_api_request__call_without_asserting(self): |
| 103 | + request = compose_gh_api_request(pull_number=556, per_page=100, cursor="") |
| 104 | + print(request.data) |
| 105 | + |
| 106 | + def test_list_changed_files__pagination_works(self): |
| 107 | + changed_files = list_changed_files(556, per_page=1) |
| 108 | + assert changed_files == ['codeserver/ubi9-python-3.9/Dockerfile', |
| 109 | + 'codeserver/ubi9-python-3.9/run-code-server.sh'] |
| 110 | + |
| 111 | + def test_analyze_build_directories(self): |
| 112 | + directories = analyze_build_directories("jupyter-intel-pytorch-ubi9-python-3.9") |
| 113 | + assert directories == ["base/ubi9-python-3.9", |
| 114 | + "intel/base/gpu/ubi9-python-3.9", |
| 115 | + "jupyter/intel/pytorch/ubi9-python-3.9"] |
0 commit comments