Skip to content

Commit be85362

Browse files
authored
Merge pull request #3372 from pateljannat/ci/branch-tests-event-driven
ci: start the branch tests on each push, and alert when they finish
2 parents ba615df + e93123b commit be85362

3 files changed

Lines changed: 75 additions & 29 deletions

File tree

‎.github/helper/branch_status.py‎

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
2-
Run the full server, UI and frontend suites on develop and version-16 every hour, and post a status on the
2+
Run the full server, UI and frontend suites on develop and version-16 after each push to develop and every
3+
three hours, and post a status on the
34
pull requests waiting to land there. Pull requests skip those suites, so this status is how they
45
see the branch: failing while the latest full run on it failed, unless the pull request carries
56
the fix label and the failing suites passed on it.
@@ -12,7 +13,7 @@
1213
python branch_status.py pr <number> # one pull request
1314
python branch_status.py commit <sha> # the pull requests whose head is this commit
1415
python branch_status.py alert <run> # tell Raven when this full run failed, and which pull requests may have caused it
15-
python branch_status.py alerts # tell Raven of every full run that failed since the last hourly check
16+
python branch_status.py alerts # wait for the full runs, then tell Raven of each suite whose latest run failed
1617
1718
The alert posts to the Raven incoming webhook in RAVEN_WEBHOOK_URL, or prints the message when it is unset.
1819
"""
@@ -223,7 +224,9 @@ def start(force):
223224
for workflow in SUITES:
224225
last = next(iter(branch_runs(workflow, branch)), None)
225226
if not force and last:
226-
if last["status"] != "completed" or last["head_sha"] == head:
227+
# Full runs of a suite on a branch share a concurrency group: a new one waits for the
228+
# running one, so a commit that lands mid-run is not left for the next check.
229+
if last["head_sha"] == head:
227230
continue
228231
changes = gh(f"repos/{REPO}/compare/{last['head_sha']}...{head}")["files"]
229232
files = [
@@ -283,7 +286,7 @@ def alert(run_id):
283286
if run["event"] != "workflow_dispatch" or branch not in BRANCHES or workflow not in SUITES:
284287
return
285288
state, job_url = suite_result(run, workflow)
286-
if state != "failure":
289+
if state != "failure" or told(run):
287290
return
288291

289292
earlier = [r for r in branch_runs(workflow, branch) if r["created_at"] < run["created_at"]]
@@ -323,25 +326,56 @@ def alert(run_id):
323326
)
324327
with urllib.request.urlopen(request, timeout=30):
325328
print(f"Raven told: {suite} failed on {branch}")
329+
mark_told(run, job_url)
330+
331+
332+
# How long a check waits for the full runs to finish. The UI suite alone may take an hour.
333+
ALERT_WAIT = 80 * 60
326334

327335

328336
def alert_failures():
329-
"""Tell Raven of the full runs that finished failing since the previous hourly check. Runs this
330-
workflow starts with its own token finish without a workflow_run event, so the next check finds
331-
them instead. Each run falls between two checks once, so it is told once."""
332-
checks = gh(
333-
f"repos/{REPO}/actions/workflows/branch-status.yml/runs", params={"event": "schedule", "per_page": 5}
334-
)["workflow_runs"]
335-
this = os.environ.get("GITHUB_RUN_ID")
336-
previous = next((check for check in checks if str(check["id"]) != this), None)
337-
if not previous:
338-
return
339-
since = previous["run_started_at"]
337+
"""Wait for the full runs on the tested branches to finish, then tell Raven of each suite whose
338+
latest run failed. Runs this workflow starts with its own token finish without a workflow_run
339+
event, so the check that started them waits for them instead."""
340+
deadline = time.time() + ALERT_WAIT
341+
time.sleep(30) # A run just dispatched takes a moment to show in the list.
342+
while time.time() < deadline and any(
343+
run["status"] != "completed"
344+
for branch in BRANCHES
345+
for workflow in SUITES
346+
for run in branch_runs(workflow, branch)[:5]
347+
):
348+
time.sleep(60)
340349
for branch in BRANCHES:
341350
for workflow in SUITES:
342-
for run in branch_runs(workflow, branch):
343-
if run["status"] == "completed" and run["updated_at"] >= since:
344-
alert(run["id"])
351+
run = latest_finished(workflow, branch)
352+
if run:
353+
alert(run["id"])
354+
355+
356+
def latest_finished(workflow, branch):
357+
return next((r for r in branch_runs(workflow, branch) if suite_result(r, workflow)[0] != "pending"), None)
358+
359+
360+
def told_context(run):
361+
return f"Raven alert: {SUITES[os.path.basename(run['path'])]}"
362+
363+
364+
def told(run):
365+
"""Was Raven told of this suite failing on this commit? Each check looks at the latest runs
366+
again, so a failure would be told once per check without this mark."""
367+
statuses = gh(f"repos/{REPO}/commits/{run['head_sha']}/status")["statuses"]
368+
return any(status["context"] == told_context(run) for status in statuses)
369+
370+
371+
def mark_told(run, job_url):
372+
fields = {
373+
"state": "failure",
374+
"context": told_context(run),
375+
"description": "The team was told on Raven",
376+
"target_url": job_url,
377+
}
378+
gh(f"repos/{REPO}/statuses/{run['head_sha']}", method="POST", body=fields)
345379

346380

347381
def refresh():

‎.github/workflows/branch-status.yml‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
name: Branch Tests
22

33
# Pull requests into develop and the hotfix branches skip the server, UI and frontend suites. They
4-
# run here instead, every hour on develop and version-16 when new commits need them, and each open pull
4+
# run here instead, on develop and version-16 when new commits need them, and each open pull
55
# request gets a status showing whether its branch passes. A pull request labelled fixes-develop
66
# runs the suites itself, and its status turns green once the suites failing on its branch pass.
7-
# Scheduled workflows run only from the default branch, so this file lives on develop alone.
8-
# The hourly check also tells the Raven channel behind the RAVEN_BRANCH_TESTS_WEBHOOK secret of each full
9-
# run that failed since the previous check, with the pull requests merged since the suite last
10-
# passed and their authors.
7+
# A check runs after each push to develop. GitHub often delays or drops scheduled runs, so the
8+
# schedule is only a backstop, and it is what picks up version-16. Scheduled workflows run only
9+
# from the default branch, so this file lives on develop alone, and a push to version-16 starts nothing.
10+
# Each check waits for the full runs it started, then tells the Raven channel behind the
11+
# RAVEN_BRANCH_TESTS_WEBHOOK secret of each suite whose latest run failed, with the pull requests
12+
# merged since the suite last passed and their authors.
1113

1214
on:
1315
schedule:
14-
- cron: "23 * * * *"
16+
- cron: "23 */3 * * *"
17+
push:
18+
branches: [develop]
1519
pull_request_target:
1620
branches: [develop, version-16-hotfix]
1721
types: [opened, synchronize, reopened, labeled, unlabeled, closed]
@@ -30,25 +34,35 @@ jobs:
3034
run-suites:
3135
name: Run Full Suites
3236
if: >-
33-
${{ github.event_name == 'schedule' || github.event_name == 'workflow_dispatch'
37+
${{ contains(fromJSON('["schedule", "push", "workflow_dispatch"]'), github.event_name)
3438
|| (github.event.action == 'closed' && github.event.pull_request.merged
3539
&& contains(github.event.pull_request.labels.*.name, 'fixes-develop')) }}
3640
runs-on: frappe-arc
41+
# Two checks at once could both start the suites on the same commit.
42+
concurrency:
43+
group: branch-suites-start
44+
cancel-in-progress: false
3745
steps:
3846
# Only the helper is checked out, from the default branch: pull request code never runs here.
3947
- uses: actions/checkout@v4
4048
with:
4149
ref: ${{ github.event.repository.default_branch }}
4250
sparse-checkout: .github/helper
43-
- run: python3 .github/helper/branch_status.py start ${{ github.event_name != 'schedule' && 'all' || '' }}
51+
- run: python3 .github/helper/branch_status.py start ${{ !contains(fromJSON('["schedule", "push"]'), github.event_name) && 'all' || '' }}
4452
env:
4553
GH_TOKEN: ${{ github.token }}
4654

4755
alert:
4856
name: Tell Raven of Failed Runs
4957
# Its own job on GitHub's runners: it holds the webhook secret, which stays off frappe-arc.
50-
if: ${{ github.event_name == 'schedule' }}
58+
needs: run-suites
59+
if: ${{ contains(fromJSON('["schedule", "push", "workflow_dispatch"]'), github.event_name) }}
5160
runs-on: ubuntu-latest
61+
timeout-minutes: 90
62+
# A newer check waits for the newer runs, so the older wait is no longer needed.
63+
concurrency:
64+
group: branch-suites-alert
65+
cancel-in-progress: true
5266
steps:
5367
- uses: actions/checkout@v4
5468
with:

‎README.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
**Easy to use, open source, Learning Management System**
77

8-
![Tests](https://img.shields.io/endpoint?url=https://dashboard.cypress.io/badge/simple/vandxn/main&style=flat&logo=cypress)
9-
108
</div>
119

1210

0 commit comments

Comments
 (0)