From da31232cdf307639486d189de9efdf88c0e41a77 Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 7 Jan 2023 11:50:31 +0530 Subject: [PATCH 01/13] refactor variable naming --- tools/ci_changes_per_commit.py | 46 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tools/ci_changes_per_commit.py b/tools/ci_changes_per_commit.py index e38c98f40e..e40ee8ae47 100644 --- a/tools/ci_changes_per_commit.py +++ b/tools/ci_changes_per_commit.py @@ -39,7 +39,7 @@ query ($owner: String!, $name: String!, $pullNumber: Int!, $commitsPerPage: Int! } """ -QUERY_CHECKRUNS = """ +QUERY_CHECK_RUNS = """ query ($checkSuiteID: ID!, $afterFailedRun: String, $afterIncompleteRun: String, $includeFailedRuns: Boolean!, $includeIncompleteRuns: Boolean!) { @@ -92,7 +92,7 @@ query_variables_commits = { } -query_variables_checkruns = { +query_variables_check_runs = { "checkSuiteID": "", "afterFailedRun": None, "afterIncompleteRun": None, @@ -141,7 +141,7 @@ def set_output(name, value): print(f"Would set GitHub actions output {name} to '{value}'") -def get_commit_and_checksuite(query_commits): +def get_commit_and_check_suite(query_commits): commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"] if commits["totalCount"] > 0: @@ -150,17 +150,17 @@ def get_commit_and_checksuite(query_commits): commit_sha = commit["oid"] if commit_sha == os.environ["EXCLUDE_COMMIT"]: continue - checksuites = commit["checkSuites"] - if checksuites["totalCount"] > 0: - for checksuite in checksuites["nodes"]: - if checksuite["workflowRun"]["workflow"]["name"] == "Build CI": + check_suites = commit["checkSuites"] + if check_suites["totalCount"] > 0: + for check_suite in check_suites["nodes"]: + if check_suite["workflowRun"]["workflow"]["name"] == "Build CI": return [ commit_sha, - checksuite["id"] if checksuite["conclusion"] != "SUCCESS" else None, + check_suite["id"] if check_suite["conclusion"] != "SUCCESS" else None, ] else: if query_commits.paginate(commits["pageInfo"], "beforeCommit"): - return get_commit_and_checksuite(query_commits) + return get_commit_and_check_suite(query_commits) return [None, None] @@ -180,11 +180,11 @@ def append_runs_to_list(runs, bad_runs_by_matrix): bad_runs_by_matrix[matrix].append(res_board.group()[1:-1]) -def get_bad_checkruns(query_checkruns): +def get_bad_check_runs(query_check_runs): more_pages = True bad_runs_by_matrix = {} while more_pages: - checkruns = query_checkruns.fetch()["data"]["node"] + check_runs = query_check_runs.fetch()["data"]["node"] run_types = ["failed", "incomplete"] more_pages = False @@ -192,10 +192,10 @@ def get_bad_checkruns(query_checkruns): run_type_camel = run_type.capitalize() + "Run" run_type = run_type + "Runs" - append_runs_to_list(checkruns[run_type], bad_runs_by_matrix) + append_runs_to_list(check_runs[run_type], bad_runs_by_matrix) - if query_checkruns.paginate(checkruns[run_type]["pageInfo"], "after" + run_type_camel): - query_checkruns.variables["include" + run_type_camel] = True + if query_check_runs.paginate(check_runs[run_type]["pageInfo"], "after" + run_type_camel): + query_check_runs.variables["include" + run_type_camel] = True more_pages = True return bad_runs_by_matrix @@ -207,26 +207,26 @@ def main(): "/" ) - commit, checksuite = get_commit_and_checksuite(query_commits) + commit, check_suite = get_commit_and_check_suite(query_commits) - if checksuite is None: + if check_suite is None: if commit is None: - print("No checkSuites found -> Abort") + print("Abort: No check suite found") else: set_output("commit", commit) quit() - query_checkruns = Query(QUERY_CHECKRUNS, query_variables_checkruns, headers) - query_checkruns.variables["checkSuiteID"] = checksuite + query_check_runs = Query(QUERY_CHECK_RUNS, query_variables_check_runs, headers) + query_check_runs.variables["checkSuiteID"] = check_suite - checkruns = get_bad_checkruns(query_checkruns) + check_runs = get_bad_check_runs(query_check_runs) - if len(checkruns) == 0: - print("No checkRuns found -> Abort") + if len(check_runs) == 0: + print("Abort: No check runs found") quit() set_output("commit", commit) - set_output("checkruns", json.dumps(checkruns)) + set_output("check_runs", json.dumps(check_runs)) if __name__ == "__main__": From 03d4d63ab56ec93b49a4ce1f53ff36babe4b1499 Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 7 Jan 2023 11:55:40 +0530 Subject: [PATCH 02/13] simplify fetching changed files --- .github/workflows/build.yml | 22 +++++++++----- tools/ci_changes_per_commit.py | 54 +++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c584f5f427..b99dee3edc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,9 +27,9 @@ jobs: boards-aarch: ${{ steps.set-matrix.outputs.boards-aarch }} steps: - name: Dump GitHub context + run: echo "$GITHUB_CONTEXT" env: GITHUB_CONTEXT: ${{ toJson(github) }} - run: echo "$GITHUB_CONTEXT" - uses: actions/checkout@v3 with: submodules: false @@ -135,20 +135,26 @@ jobs: GITHUB_TOKEN: ${{ github.token }} EXCLUDE_COMMIT: ${{ github.event.after }} run: python3 -u ci_changes_per_commit.py + - name: Deepen and convert depth to sha + id: deepen-and-convert-depth-to-sha + run: | + DEEPEN=$((DEPTH - $(git rev-list HEAD --count))) && if((DEEPEN > 0)); then git fetch --no-tags --recurse-submodules=no --deepen=$DEEPEN; fi + echo "commit=$(git rev-list $GITHUB_SHA --skip=$((DEPTH + 1)) --max-count=1)" >> $GITHUB_OUTPUT + env: + DEPTH: ${{ steps.get-last-commit-with-checks.outputs.commit_depth || github.event.pull_request.commits }} - name: Get changes id: get-changes if: github.event_name == 'pull_request' - uses: tj-actions/changed-files@v34 - with: - json: true - sha: ${{ steps.get-last-commit-with-checks.outputs.commit && github.event.after }} - base_sha: ${{ steps.get-last-commit-with-checks.outputs.commit }} + run: echo $(git diff $BASE_SHA...$HEAD_SHA --name-only) | echo "changed_files=[\"$(sed "s/ /\", \"/g")\"]" >> $GITHUB_OUTPUT + env: + BASE_SHA: ${{ steps.deepen-and-convert-depth-to-sha.outputs.commit }} + HEAD_SHA: ${{ github.event.after }} - name: Set matrix id: set-matrix working-directory: tools env: - CHANGED_FILES: ${{ steps.get-changes.outputs.all_changed_and_modified_files }} - LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.checkruns }} + CHANGED_FILES: ${{ steps.get-changes.outputs.changed_files }} + LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.check_runs }} run: python3 -u ci_set_matrix.py diff --git a/tools/ci_changes_per_commit.py b/tools/ci_changes_per_commit.py index e40ee8ae47..b2256c6d45 100644 --- a/tools/ci_changes_per_commit.py +++ b/tools/ci_changes_per_commit.py @@ -18,7 +18,7 @@ query ($owner: String!, $name: String!, $pullNumber: Int!, $commitsPerPage: Int! } nodes { commit { - checkSuites(first: 3) { + checkSuites(first: 100) { nodes { conclusion workflowRun { @@ -141,26 +141,29 @@ def set_output(name, value): print(f"Would set GitHub actions output {name} to '{value}'") -def get_commit_and_check_suite(query_commits): - commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"] +def get_commit_depth_and_check_suite(query_commits): + while True: + commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"] - if commits["totalCount"] > 0: - for commit in reversed(commits["nodes"]): - commit = commit["commit"] - commit_sha = commit["oid"] - if commit_sha == os.environ["EXCLUDE_COMMIT"]: - continue - check_suites = commit["checkSuites"] - if check_suites["totalCount"] > 0: - for check_suite in check_suites["nodes"]: - if check_suite["workflowRun"]["workflow"]["name"] == "Build CI": - return [ - commit_sha, - check_suite["id"] if check_suite["conclusion"] != "SUCCESS" else None, - ] - else: - if query_commits.paginate(commits["pageInfo"], "beforeCommit"): - return get_commit_and_check_suite(query_commits) + if commits["totalCount"] > 0: + nodes = commits["nodes"] + nodes.reverse() + if nodes[0]["commit"]["oid"] == os.environ["EXCLUDE_COMMIT"]: + nodes.pop(0) + for index, commit in enumerate(nodes): + commit = commit["commit"] + commit_sha = commit["oid"] + check_suites = commit["checkSuites"] + if check_suites["totalCount"] > 0: + for check_suite in check_suites["nodes"]: + if check_suite["workflowRun"]["workflow"]["name"] == "Build CI": + return [ + {"sha": commit_sha, "depth": index + 1}, + check_suite["id"] if check_suite["conclusion"] != "SUCCESS" else None, + ] + else: + if not query_commits.paginate(commits["pageInfo"], "beforeCommit"): + break return [None, None] @@ -201,19 +204,24 @@ def get_bad_check_runs(query_check_runs): return bad_runs_by_matrix +def set_commit(commit): + set_output("commit_sha", commit["sha"]) + set_output("commit_depth", commit["depth"]) + + def main(): query_commits = Query(QUERY_COMMITS, query_variables_commits, headers) query_commits.variables["owner"], query_commits.variables["name"] = os.environ["REPO"].split( "/" ) - commit, check_suite = get_commit_and_check_suite(query_commits) + commit, check_suite = get_commit_depth_and_check_suite(query_commits) if check_suite is None: if commit is None: print("Abort: No check suite found") else: - set_output("commit", commit) + set_commit(commit) quit() query_check_runs = Query(QUERY_CHECK_RUNS, query_variables_check_runs, headers) @@ -225,7 +233,7 @@ def main(): print("Abort: No check runs found") quit() - set_output("commit", commit) + set_commit(commit) set_output("check_runs", json.dumps(check_runs)) From 0e46c77c1463e5dadf34ea061b7ac5d97df04c4d Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 7 Jan 2023 17:52:33 +0530 Subject: [PATCH 03/13] fix for bug `fatal: error in object: unshallow` --- tools/ci_fetch_deps.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/ci_fetch_deps.py b/tools/ci_fetch_deps.py index 515c3b198f..e7eb042a1f 100644 --- a/tools/ci_fetch_deps.py +++ b/tools/ci_fetch_deps.py @@ -70,6 +70,8 @@ run( "Fetch back to the start of 2021 to get commit history", f'git fetch --recurse-submodules=no --shallow-since="2021-07-01" origin {ref}', ) +# See https://stackoverflow.com/questions/63878612/git-fatal-error-in-object-unshallow-sha-1#comment118418373_63879454 +run ("Fix for bug \"fatal: error in object: unshallow\"", "git repack -d") run("Init submodules", "git submodule init") run("Submodule status", "git submodule status") From 8096c94e1b580597e13a6d477a4b8dbdd301f07f Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 7 Jan 2023 18:17:48 +0530 Subject: [PATCH 04/13] build docs only when `//|` matches --- .github/workflows/build.yml | 2 ++ tools/ci_set_matrix.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b99dee3edc..660edc1535 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -153,6 +153,8 @@ jobs: id: set-matrix working-directory: tools env: + BASE_SHA: ${{ steps.deepen-and-convert-depth-to-sha.outputs.commit }} + HEAD_SHA: ${{ github.event.after }} CHANGED_FILES: ${{ steps.get-changes.outputs.changed_files }} LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.check_runs }} run: python3 -u ci_set_matrix.py diff --git a/tools/ci_set_matrix.py b/tools/ci_set_matrix.py index 77f58742cd..33dc9af7c5 100755 --- a/tools/ci_set_matrix.py +++ b/tools/ci_set_matrix.py @@ -26,6 +26,7 @@ import os import sys import json import pathlib +import subprocess from concurrent.futures import ThreadPoolExecutor tools_dir = pathlib.Path(__file__).resolve().parent @@ -238,6 +239,15 @@ def set_docs_to_build(build_all): r"^(?:.github/workflows/|docs|extmod/ulab|(?:(?:ports/\w+/bindings|shared-bindings)\S+\.c|conf\.py|tools/extract_pyi\.py|requirements-doc\.txt)$)|(?:-stubs|\.(?:md|MD|rst|RST))$" ) for p in changed_files: + if ( + p.endswith(".c") + and not subprocess.run( + f"git diff -U0 {os.environ.get('BASE_SHA')}...{os.environ.get('HEAD_SHA')} {p} | grep -o -m 1 '^[+-]\/\/|'", + capture_output=True, + shell=True, + ).stdout + ): + continue if doc_pattern.search(p): doc_match = True break From 09adb2428467fe2c5d1b747ff5a2e9b555f7045b Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 7 Jan 2023 18:40:18 +0530 Subject: [PATCH 05/13] slightly refactor ci --- .github/workflows/build.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 660edc1535..2833605f5b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -152,12 +152,12 @@ jobs: - name: Set matrix id: set-matrix working-directory: tools + run: python3 -u ci_set_matrix.py env: BASE_SHA: ${{ steps.deepen-and-convert-depth-to-sha.outputs.commit }} HEAD_SHA: ${{ github.event.after }} CHANGED_FILES: ${{ steps.get-changes.outputs.changed_files }} LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.check_runs }} - run: python3 -u ci_set_matrix.py mpy-cross-mac: @@ -420,14 +420,15 @@ jobs: path: ${{ github.workspace }}/.idf_tools key: ${{ runner.os }}-idf-tools-${{ hashFiles('.git/modules/ports/espressif/esp-idf/HEAD') }}-${{ steps.py3.outputs.python-path }}-20220404 - name: Clone IDF submodules - run: | - (cd $IDF_PATH && git submodule update --init) + run: git submodule update --init $IDF_PATH env: IDF_PATH: ${{ github.workspace }}/ports/espressif/esp-idf - name: Install IDF tools run: | + echo "Installing ESP-IDF tools" $IDF_PATH/tools/idf_tools.py --non-interactive install required $IDF_PATH/tools/idf_tools.py --non-interactive install cmake + echo "Installing Python environment and packages" $IDF_PATH/tools/idf_tools.py --non-interactive install-python-env rm -rf $IDF_TOOLS_PATH/dist env: @@ -445,7 +446,6 @@ jobs: run: | source $IDF_PATH/export.sh gcc --version - xtensa-esp32s2-elf-gcc --version python3 --version ninja --version cmake --version @@ -479,6 +479,7 @@ jobs: AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} if: (github.event_name == 'push' && github.ref == 'refs/heads/main' && github.repository_owner == 'adafruit') || (github.event_name == 'release' && (github.event.action == 'published' || github.event.action == 'rerequested')) + build-aarch: runs-on: ubuntu-20.04 needs: test From 70273316f8bd46b6d5259b7c8574314c270c12e9 Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 7 Jan 2023 19:00:54 +0530 Subject: [PATCH 06/13] run pre-commit formatting --- tools/ci_changes_per_commit.py | 46 ++++++++++++++++++---------------- tools/ci_fetch_deps.py | 2 +- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/tools/ci_changes_per_commit.py b/tools/ci_changes_per_commit.py index b2256c6d45..92c98fe858 100644 --- a/tools/ci_changes_per_commit.py +++ b/tools/ci_changes_per_commit.py @@ -143,27 +143,29 @@ def set_output(name, value): def get_commit_depth_and_check_suite(query_commits): while True: - commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"] + commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"] - if commits["totalCount"] > 0: - nodes = commits["nodes"] - nodes.reverse() - if nodes[0]["commit"]["oid"] == os.environ["EXCLUDE_COMMIT"]: - nodes.pop(0) - for index, commit in enumerate(nodes): - commit = commit["commit"] - commit_sha = commit["oid"] - check_suites = commit["checkSuites"] - if check_suites["totalCount"] > 0: - for check_suite in check_suites["nodes"]: - if check_suite["workflowRun"]["workflow"]["name"] == "Build CI": - return [ - {"sha": commit_sha, "depth": index + 1}, - check_suite["id"] if check_suite["conclusion"] != "SUCCESS" else None, - ] - else: - if not query_commits.paginate(commits["pageInfo"], "beforeCommit"): - break + if commits["totalCount"] > 0: + nodes = commits["nodes"] + nodes.reverse() + if nodes[0]["commit"]["oid"] == os.environ["EXCLUDE_COMMIT"]: + nodes.pop(0) + for index, commit in enumerate(nodes): + commit = commit["commit"] + commit_sha = commit["oid"] + check_suites = commit["checkSuites"] + if check_suites["totalCount"] > 0: + for check_suite in check_suites["nodes"]: + if check_suite["workflowRun"]["workflow"]["name"] == "Build CI": + return [ + {"sha": commit_sha, "depth": index + 1}, + check_suite["id"] + if check_suite["conclusion"] != "SUCCESS" + else None, + ] + else: + if not query_commits.paginate(commits["pageInfo"], "beforeCommit"): + break return [None, None] @@ -197,7 +199,9 @@ def get_bad_check_runs(query_check_runs): append_runs_to_list(check_runs[run_type], bad_runs_by_matrix) - if query_check_runs.paginate(check_runs[run_type]["pageInfo"], "after" + run_type_camel): + if query_check_runs.paginate( + check_runs[run_type]["pageInfo"], "after" + run_type_camel + ): query_check_runs.variables["include" + run_type_camel] = True more_pages = True diff --git a/tools/ci_fetch_deps.py b/tools/ci_fetch_deps.py index e7eb042a1f..04ae6fb0e1 100644 --- a/tools/ci_fetch_deps.py +++ b/tools/ci_fetch_deps.py @@ -71,7 +71,7 @@ run( f'git fetch --recurse-submodules=no --shallow-since="2021-07-01" origin {ref}', ) # See https://stackoverflow.com/questions/63878612/git-fatal-error-in-object-unshallow-sha-1#comment118418373_63879454 -run ("Fix for bug \"fatal: error in object: unshallow\"", "git repack -d") +run('Fix for bug "fatal: error in object: unshallow"', "git repack -d") run("Init submodules", "git submodule init") run("Submodule status", "git submodule status") From f0e7e3115abdf4a75493cb615f01df564400fbbc Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 7 Jan 2023 21:03:16 +0530 Subject: [PATCH 07/13] refactor how base and head commit are set - avoid `github.event.after` as it isn't available during initial CI run of the PR --- .github/workflows/build.yml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2833605f5b..6694a045be 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -135,27 +135,23 @@ jobs: GITHUB_TOKEN: ${{ github.token }} EXCLUDE_COMMIT: ${{ github.event.after }} run: python3 -u ci_changes_per_commit.py - - name: Deepen and convert depth to sha - id: deepen-and-convert-depth-to-sha + - name: Set base sha run: | DEEPEN=$((DEPTH - $(git rev-list HEAD --count))) && if((DEEPEN > 0)); then git fetch --no-tags --recurse-submodules=no --deepen=$DEEPEN; fi - echo "commit=$(git rev-list $GITHUB_SHA --skip=$((DEPTH + 1)) --max-count=1)" >> $GITHUB_OUTPUT + echo "BASE_SHA=$(git rev-list $GITHUB_SHA --skip=$((DEPTH + 1)) --max-count=1)" >> $GITHUB_ENV env: DEPTH: ${{ steps.get-last-commit-with-checks.outputs.commit_depth || github.event.pull_request.commits }} + - name: Set head sha + run: echo "HEAD_SHA=$(git rev-list $GITHUB_SHA --skip=1 --max-count=1)" >> $GITHUB_ENV - name: Get changes id: get-changes if: github.event_name == 'pull_request' run: echo $(git diff $BASE_SHA...$HEAD_SHA --name-only) | echo "changed_files=[\"$(sed "s/ /\", \"/g")\"]" >> $GITHUB_OUTPUT - env: - BASE_SHA: ${{ steps.deepen-and-convert-depth-to-sha.outputs.commit }} - HEAD_SHA: ${{ github.event.after }} - name: Set matrix id: set-matrix working-directory: tools run: python3 -u ci_set_matrix.py env: - BASE_SHA: ${{ steps.deepen-and-convert-depth-to-sha.outputs.commit }} - HEAD_SHA: ${{ github.event.after }} CHANGED_FILES: ${{ steps.get-changes.outputs.changed_files }} LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.check_runs }} From 722a313cc10d7f4cf83161ba1f373e3565d699f2 Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sat, 7 Jan 2023 21:12:39 +0530 Subject: [PATCH 08/13] set base and head commit only when the event is a pr --- .github/workflows/build.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6694a045be..e2c5de08a7 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -136,12 +136,14 @@ jobs: EXCLUDE_COMMIT: ${{ github.event.after }} run: python3 -u ci_changes_per_commit.py - name: Set base sha + if: github.event_name == 'pull_request' run: | DEEPEN=$((DEPTH - $(git rev-list HEAD --count))) && if((DEEPEN > 0)); then git fetch --no-tags --recurse-submodules=no --deepen=$DEEPEN; fi echo "BASE_SHA=$(git rev-list $GITHUB_SHA --skip=$((DEPTH + 1)) --max-count=1)" >> $GITHUB_ENV env: DEPTH: ${{ steps.get-last-commit-with-checks.outputs.commit_depth || github.event.pull_request.commits }} - name: Set head sha + if: github.event_name == 'pull_request' run: echo "HEAD_SHA=$(git rev-list $GITHUB_SHA --skip=1 --max-count=1)" >> $GITHUB_ENV - name: Get changes id: get-changes From 565bbd5002e92cebc7c5a31528f5308d8bd58b2a Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Sun, 8 Jan 2023 13:23:51 +0530 Subject: [PATCH 09/13] fix path in `set_docs_to_build` --- tools/ci_set_matrix.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/ci_set_matrix.py b/tools/ci_set_matrix.py index 33dc9af7c5..cc84cddd28 100755 --- a/tools/ci_set_matrix.py +++ b/tools/ci_set_matrix.py @@ -238,11 +238,13 @@ def set_docs_to_build(build_all): doc_pattern = re.compile( r"^(?:.github/workflows/|docs|extmod/ulab|(?:(?:ports/\w+/bindings|shared-bindings)\S+\.c|conf\.py|tools/extract_pyi\.py|requirements-doc\.txt)$)|(?:-stubs|\.(?:md|MD|rst|RST))$" ) + github_workspace = os.environ.get("GITHUB_WORKSPACE") or "" + github_workspace = github_workspace and github_workspace + "/" for p in changed_files: if ( p.endswith(".c") and not subprocess.run( - f"git diff -U0 {os.environ.get('BASE_SHA')}...{os.environ.get('HEAD_SHA')} {p} | grep -o -m 1 '^[+-]\/\/|'", + f"git diff -U0 $BASE_SHA...$HEAD_SHA {github_workspace + p} | grep -o -m 1 '^[+-]\/\/|'", capture_output=True, shell=True, ).stdout From c94f83f2e6a276d4ec5936e0b967708a45dbea02 Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Mon, 9 Jan 2023 18:17:51 +0530 Subject: [PATCH 10/13] fix pagination and some refactoring --- tools/ci_changes_per_commit.py | 37 ++++++++++++++++------------------ 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/tools/ci_changes_per_commit.py b/tools/ci_changes_per_commit.py index 92c98fe858..4d71f8e38c 100644 --- a/tools/ci_changes_per_commit.py +++ b/tools/ci_changes_per_commit.py @@ -111,13 +111,11 @@ class Query: self.headers = headers def paginate(self, page_info, name): - has_page = ( - page_info["hasNextPage"] if name.startswith("after") else page_info["hasPreviousPage"] - ) + has_page = page_info["hasNextPage" if name.startswith("after") else "hasPreviousPage"] if has_page: - self.variables[name] = ( - page_info["endCursor"] if name.startswith("after") else page_info["startCursor"] - ) + self.variables[name] = page_info[ + "endCursor" if name.startswith("after") else "startCursor" + ] return has_page def fetch(self): @@ -142,15 +140,16 @@ def set_output(name, value): def get_commit_depth_and_check_suite(query_commits): + commit_depth = 0 while True: commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"] - if commits["totalCount"] > 0: nodes = commits["nodes"] nodes.reverse() if nodes[0]["commit"]["oid"] == os.environ["EXCLUDE_COMMIT"]: nodes.pop(0) - for index, commit in enumerate(nodes): + for commit in nodes: + commit_depth += 1 commit = commit["commit"] commit_sha = commit["oid"] check_suites = commit["checkSuites"] @@ -158,16 +157,13 @@ def get_commit_depth_and_check_suite(query_commits): for check_suite in check_suites["nodes"]: if check_suite["workflowRun"]["workflow"]["name"] == "Build CI": return [ - {"sha": commit_sha, "depth": index + 1}, + {"sha": commit_sha, "depth": commit_depth}, check_suite["id"] if check_suite["conclusion"] != "SUCCESS" else None, ] - else: - if not query_commits.paginate(commits["pageInfo"], "beforeCommit"): - break - - return [None, None] + if not query_commits.paginate(commits["pageInfo"], "beforeCommit"): + return [None, None] def append_runs_to_list(runs, bad_runs_by_matrix): @@ -188,9 +184,10 @@ def append_runs_to_list(runs, bad_runs_by_matrix): def get_bad_check_runs(query_check_runs): more_pages = True bad_runs_by_matrix = {} + run_types = ["failed", "incomplete"] + while more_pages: check_runs = query_check_runs.fetch()["data"]["node"] - run_types = ["failed", "incomplete"] more_pages = False for run_type in run_types: @@ -221,11 +218,11 @@ def main(): commit, check_suite = get_commit_depth_and_check_suite(query_commits) - if check_suite is None: - if commit is None: - print("Abort: No check suite found") - else: + if not check_suite: + if commit: set_commit(commit) + else: + print("Abort: No check suite found") quit() query_check_runs = Query(QUERY_CHECK_RUNS, query_variables_check_runs, headers) @@ -233,7 +230,7 @@ def main(): check_runs = get_bad_check_runs(query_check_runs) - if len(check_runs) == 0: + if not check_runs: print("Abort: No check runs found") quit() From 645499e819de6cef7b16d783cf2eb9b03cc0b7ee Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Wed, 11 Jan 2023 11:52:19 +0530 Subject: [PATCH 11/13] check `//|` only after a file matches the doc pattern --- tools/ci_set_matrix.py | 56 +++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/tools/ci_set_matrix.py b/tools/ci_set_matrix.py index cc84cddd28..40c5553ded 100755 --- a/tools/ci_set_matrix.py +++ b/tools/ci_set_matrix.py @@ -83,7 +83,7 @@ else: last_failed_jobs = json.loads(j) -def set_output(name, value): +def set_output(name: str, value): if "GITHUB_OUTPUT" in os.environ: with open(os.environ["GITHUB_OUTPUT"], "at") as f: print(f"{name}={value}", file=f) @@ -91,7 +91,7 @@ def set_output(name, value): print(f"Would set GitHub actions output {name} to '{value}'") -def set_boards_to_build(build_all): +def set_boards_to_build(build_all: bool): # Get boards in json format boards_info_json = build_board_info.get_board_mapping() all_board_ids = set() @@ -229,34 +229,34 @@ def set_boards_to_build(build_all): set_output(f"boards-{arch}", json.dumps(sorted(arch_to_boards[arch]))) -def set_docs_to_build(build_all): - if "build-doc" in last_failed_jobs: - build_all = True - - doc_match = build_all - if not build_all: - doc_pattern = re.compile( - r"^(?:.github/workflows/|docs|extmod/ulab|(?:(?:ports/\w+/bindings|shared-bindings)\S+\.c|conf\.py|tools/extract_pyi\.py|requirements-doc\.txt)$)|(?:-stubs|\.(?:md|MD|rst|RST))$" - ) - github_workspace = os.environ.get("GITHUB_WORKSPACE") or "" - github_workspace = github_workspace and github_workspace + "/" - for p in changed_files: - if ( - p.endswith(".c") - and not subprocess.run( - f"git diff -U0 $BASE_SHA...$HEAD_SHA {github_workspace + p} | grep -o -m 1 '^[+-]\/\/|'", - capture_output=True, - shell=True, - ).stdout - ): - continue - if doc_pattern.search(p): - doc_match = True - break +def set_docs_to_build(build_doc: bool): + if not build_doc: + if "build-doc" in last_failed_jobs: + build_doc = True + else: + doc_pattern = re.compile( + r"^(?:.github/workflows/|docs|extmod/ulab|(?:(?:ports/\w+/bindings|shared-bindings)\S+\.c|conf\.py|tools/extract_pyi\.py|requirements-doc\.txt)$)|(?:-stubs|\.(?:md|MD|rst|RST))$" + ) + github_workspace = os.environ.get("GITHUB_WORKSPACE") or "" + github_workspace = github_workspace and github_workspace + "/" + for p in changed_files: + if doc_pattern.search(p) and ( + ( + subprocess.run( + f"git diff -U0 $BASE_SHA...$HEAD_SHA {github_workspace + p} | grep -o -m 1 '^[+-]\/\/|'", + capture_output=True, + shell=True, + ).stdout + ) + if p.endswith(".c") + else True + ): + build_doc = True + break # Set the step outputs - print("Building docs:", doc_match) - set_output("build-doc", doc_match) + print("Building docs:", build_doc) + set_output("build-doc", build_doc) def check_changed_files(): From ad4357d1529aca1cafa32479846d77ad4fd33b01 Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Wed, 11 Jan 2023 19:22:07 +0530 Subject: [PATCH 12/13] use pr head ref and not the merge ref --- .github/workflows/build.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e2c5de08a7..b1f9698d4a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -135,16 +135,16 @@ jobs: GITHUB_TOKEN: ${{ github.token }} EXCLUDE_COMMIT: ${{ github.event.after }} run: python3 -u ci_changes_per_commit.py + - name: Set head sha + if: github.event_name == 'pull_request' + run: echo "HEAD_SHA=$(git show -s --format=%s $GITHUB_SHA | grep -o -P "(?<=Merge ).*(?= into)")" >> $GITHUB_ENV - name: Set base sha if: github.event_name == 'pull_request' run: | - DEEPEN=$((DEPTH - $(git rev-list HEAD --count))) && if((DEEPEN > 0)); then git fetch --no-tags --recurse-submodules=no --deepen=$DEEPEN; fi - echo "BASE_SHA=$(git rev-list $GITHUB_SHA --skip=$((DEPTH + 1)) --max-count=1)" >> $GITHUB_ENV + git fetch --no-tags --no-recurse-submodules --depth=$((DEPTH + 1)) origin $HEAD_SHA + echo "BASE_SHA=$(git rev-list $HEAD_SHA --skip=$DEPTH --max-count=1)" >> $GITHUB_ENV env: DEPTH: ${{ steps.get-last-commit-with-checks.outputs.commit_depth || github.event.pull_request.commits }} - - name: Set head sha - if: github.event_name == 'pull_request' - run: echo "HEAD_SHA=$(git rev-list $GITHUB_SHA --skip=1 --max-count=1)" >> $GITHUB_ENV - name: Get changes id: get-changes if: github.event_name == 'pull_request' From 4a9d9340082c8942f1c03a05292f4362369b9288 Mon Sep 17 00:00:00 2001 From: MicroDev <70126934+MicroDev1@users.noreply.github.com> Date: Wed, 11 Jan 2023 20:56:53 +0530 Subject: [PATCH 13/13] escape `.` and `/` in regex --- tools/ci_set_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/ci_set_matrix.py b/tools/ci_set_matrix.py index 40c5553ded..bf899127ae 100755 --- a/tools/ci_set_matrix.py +++ b/tools/ci_set_matrix.py @@ -235,7 +235,7 @@ def set_docs_to_build(build_doc: bool): build_doc = True else: doc_pattern = re.compile( - r"^(?:.github/workflows/|docs|extmod/ulab|(?:(?:ports/\w+/bindings|shared-bindings)\S+\.c|conf\.py|tools/extract_pyi\.py|requirements-doc\.txt)$)|(?:-stubs|\.(?:md|MD|rst|RST))$" + r"^(?:\.github\/workflows\/|docs|extmod\/ulab|(?:(?:ports\/\w+\/bindings|shared-bindings)\S+\.c|conf\.py|tools\/extract_pyi\.py|requirements-doc\.txt)$)|(?:-stubs|\.(?:md|MD|rst|RST))$" ) github_workspace = os.environ.get("GITHUB_WORKSPACE") or "" github_workspace = github_workspace and github_workspace + "/"