From a6400fb08ae896f5c71989ac47bf5ea803faeb70 Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 11 Mar 2023 00:14:36 +0530 Subject: [PATCH] use intersection of changes per commit and merge ref --- .github/workflows/build.yml | 5 ---- tools/ci_set_matrix.py | 46 ++++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4383153fd1..150c2364ae 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -80,16 +80,11 @@ jobs: run: git cat-file -e $SHA && echo "BASE_SHA=$SHA" >> $GITHUB_ENV || true env: SHA: ${{ github.event.before }} - - name: Get changes - id: get-changes - if: env.BASE_SHA && env.HEAD_SHA - run: echo $(git diff $BASE_SHA...$HEAD_SHA --name-only) | echo "changed_files=[\"$(sed "s/ /\", \"/g")\"]" >> $GITHUB_OUTPUT - name: Set matrix id: set-matrix run: python3 -u ci_set_matrix.py working-directory: tools env: - CHANGED_FILES: ${{ steps.get-changes.outputs.changed_files }} LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.check_runs }} tests: diff --git a/tools/ci_set_matrix.py b/tools/ci_set_matrix.py index b5618aec2f..3cd0dbf56f 100755 --- a/tools/ci_set_matrix.py +++ b/tools/ci_set_matrix.py @@ -68,25 +68,43 @@ PATTERN_WINDOWS = [ "tools/", ] + +def git_diff(pattern: str): + return ( + subprocess.run( + f"git diff {pattern} --name-only", + capture_output=True, + shell=True, + ) + .stdout.decode("utf-8") + .split("\n")[:-1] + ) + + if len(sys.argv) > 1: print("Using files list on commandline") changed_files = sys.argv[1:] - last_failed_jobs = {} +elif os.environ.get("BASE_SHA") and os.environ.get("HEAD_SHA"): + print("Using files list by computing diff") + changed_files = git_diff("$BASE_SHA...$HEAD_SHA") + if os.environ.get("GITHUB_EVENT_NAME") == "pull_request": + changed_files = list(set(changed_files).intersection(git_diff("$HEAD_SHA~...$HEAD_SHA"))) else: - c = os.environ["CHANGED_FILES"] - if c == "": - print("CHANGED_FILES is in environment, but value is empty") - changed_files = [] - else: - print("Using files list in CHANGED_FILES") - changed_files = json.loads(c.replace("\\", "")) + print("Using files list in CHANGED_FILES") + changed_files = json.loads(os.environ.get("CHANGED_FILES") or "[]") - j = os.environ["LAST_FAILED_JOBS"] - if j == "": - print("LAST_FAILED_JOBS is in environment, but value is empty") - last_failed_jobs = {} - else: - last_failed_jobs = json.loads(j) +print("Using jobs list in LAST_FAILED_JOBS") +last_failed_jobs = json.loads(os.environ.get("LAST_FAILED_JOBS") or "{}") + + +def print_enclosed(title, content): + print("::group::" + title) + print(content) + print("::endgroup::") + + +print_enclosed("LOG: changed_files", changed_files) +print_enclosed("LOG: last_failed_jobs", last_failed_jobs) def set_output(name: str, value):