Skip to content

Commit 03d4d63

Browse files
committed
simplify fetching changed files
1 parent da31232 commit 03d4d63

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ jobs:
2727
boards-aarch: ${{ steps.set-matrix.outputs.boards-aarch }}
2828
steps:
2929
- name: Dump GitHub context
30+
run: echo "$GITHUB_CONTEXT"
3031
env:
3132
GITHUB_CONTEXT: ${{ toJson(github) }}
32-
run: echo "$GITHUB_CONTEXT"
3333
- uses: actions/checkout@v3
3434
with:
3535
submodules: false
@@ -135,20 +135,26 @@ jobs:
135135
GITHUB_TOKEN: ${{ github.token }}
136136
EXCLUDE_COMMIT: ${{ github.event.after }}
137137
run: python3 -u ci_changes_per_commit.py
138+
- name: Deepen and convert depth to sha
139+
id: deepen-and-convert-depth-to-sha
140+
run: |
141+
DEEPEN=$((DEPTH - $(git rev-list HEAD --count))) && if((DEEPEN > 0)); then git fetch --no-tags --recurse-submodules=no --deepen=$DEEPEN; fi
142+
echo "commit=$(git rev-list $GITHUB_SHA --skip=$((DEPTH + 1)) --max-count=1)" >> $GITHUB_OUTPUT
143+
env:
144+
DEPTH: ${{ steps.get-last-commit-with-checks.outputs.commit_depth || github.event.pull_request.commits }}
138145
- name: Get changes
139146
id: get-changes
140147
if: github.event_name == 'pull_request'
141-
uses: tj-actions/changed-files@v34
142-
with:
143-
json: true
144-
sha: ${{ steps.get-last-commit-with-checks.outputs.commit && github.event.after }}
145-
base_sha: ${{ steps.get-last-commit-with-checks.outputs.commit }}
148+
run: echo $(git diff $BASE_SHA...$HEAD_SHA --name-only) | echo "changed_files=[\"$(sed "s/ /\", \"/g")\"]" >> $GITHUB_OUTPUT
149+
env:
150+
BASE_SHA: ${{ steps.deepen-and-convert-depth-to-sha.outputs.commit }}
151+
HEAD_SHA: ${{ github.event.after }}
146152
- name: Set matrix
147153
id: set-matrix
148154
working-directory: tools
149155
env:
150-
CHANGED_FILES: ${{ steps.get-changes.outputs.all_changed_and_modified_files }}
151-
LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.checkruns }}
156+
CHANGED_FILES: ${{ steps.get-changes.outputs.changed_files }}
157+
LAST_FAILED_JOBS: ${{ steps.get-last-commit-with-checks.outputs.check_runs }}
152158
run: python3 -u ci_set_matrix.py
153159

154160

tools/ci_changes_per_commit.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
nodes {
2020
commit {
21-
checkSuites(first: 3) {
21+
checkSuites(first: 100) {
2222
nodes {
2323
conclusion
2424
workflowRun {
@@ -141,26 +141,29 @@ def set_output(name, value):
141141
print(f"Would set GitHub actions output {name} to '{value}'")
142142

143143

144-
def get_commit_and_check_suite(query_commits):
145-
commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"]
146-
147-
if commits["totalCount"] > 0:
148-
for commit in reversed(commits["nodes"]):
149-
commit = commit["commit"]
150-
commit_sha = commit["oid"]
151-
if commit_sha == os.environ["EXCLUDE_COMMIT"]:
152-
continue
153-
check_suites = commit["checkSuites"]
154-
if check_suites["totalCount"] > 0:
155-
for check_suite in check_suites["nodes"]:
156-
if check_suite["workflowRun"]["workflow"]["name"] == "Build CI":
157-
return [
158-
commit_sha,
159-
check_suite["id"] if check_suite["conclusion"] != "SUCCESS" else None,
160-
]
161-
else:
162-
if query_commits.paginate(commits["pageInfo"], "beforeCommit"):
163-
return get_commit_and_check_suite(query_commits)
144+
def get_commit_depth_and_check_suite(query_commits):
145+
while True:
146+
commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"]
147+
148+
if commits["totalCount"] > 0:
149+
nodes = commits["nodes"]
150+
nodes.reverse()
151+
if nodes[0]["commit"]["oid"] == os.environ["EXCLUDE_COMMIT"]:
152+
nodes.pop(0)
153+
for index, commit in enumerate(nodes):
154+
commit = commit["commit"]
155+
commit_sha = commit["oid"]
156+
check_suites = commit["checkSuites"]
157+
if check_suites["totalCount"] > 0:
158+
for check_suite in check_suites["nodes"]:
159+
if check_suite["workflowRun"]["workflow"]["name"] == "Build CI":
160+
return [
161+
{"sha": commit_sha, "depth": index + 1},
162+
check_suite["id"] if check_suite["conclusion"] != "SUCCESS" else None,
163+
]
164+
else:
165+
if not query_commits.paginate(commits["pageInfo"], "beforeCommit"):
166+
break
164167

165168
return [None, None]
166169

@@ -201,19 +204,24 @@ def get_bad_check_runs(query_check_runs):
201204
return bad_runs_by_matrix
202205

203206

207+
def set_commit(commit):
208+
set_output("commit_sha", commit["sha"])
209+
set_output("commit_depth", commit["depth"])
210+
211+
204212
def main():
205213
query_commits = Query(QUERY_COMMITS, query_variables_commits, headers)
206214
query_commits.variables["owner"], query_commits.variables["name"] = os.environ["REPO"].split(
207215
"/"
208216
)
209217

210-
commit, check_suite = get_commit_and_check_suite(query_commits)
218+
commit, check_suite = get_commit_depth_and_check_suite(query_commits)
211219

212220
if check_suite is None:
213221
if commit is None:
214222
print("Abort: No check suite found")
215223
else:
216-
set_output("commit", commit)
224+
set_commit(commit)
217225
quit()
218226

219227
query_check_runs = Query(QUERY_CHECK_RUNS, query_variables_check_runs, headers)
@@ -225,7 +233,7 @@ def main():
225233
print("Abort: No check runs found")
226234
quit()
227235

228-
set_output("commit", commit)
236+
set_commit(commit)
229237
set_output("check_runs", json.dumps(check_runs))
230238

231239

0 commit comments

Comments
 (0)