Skip to content

Commit ef571ce

Browse files
committed
ci: alert on failed automation
1 parent 49ed8ff commit ef571ce

2 files changed

Lines changed: 109 additions & 0 deletions

File tree

‎.github/workflows/alert.yml‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Alert
2+
3+
# Tells the maintainer when a run of the release, release PR or upstream check workflow did not pass. It comments
4+
# `<workflow> <conclusion>: <run url>` on the open issue titled `Automation needs a look`, or opens that issue assigned
5+
# to drptbl. workflow_run matches a workflow by its name:, so the names below are release.yml's, release-pr.yml's and
6+
# upstream.yml's. A pull request runs none of them, and one never alerts. Runs of every workflow that writes the issue
7+
# share the concurrency group automation-alert, wait for each other and are never replaced, so no alert is dropped and
8+
# no second issue is opened.
9+
on:
10+
workflow_run:
11+
workflows: [Release, Release PR, Upstream check]
12+
types: [completed]
13+
14+
permissions: {}
15+
16+
defaults:
17+
run:
18+
shell: bash
19+
20+
jobs:
21+
alert:
22+
if: github.event.workflow_run.conclusion != 'success' &&
23+
github.event.workflow_run.conclusion != 'skipped' &&
24+
github.event.workflow_run.conclusion != 'neutral' &&
25+
github.event.workflow_run.event != 'pull_request'
26+
runs-on: ubuntu-latest
27+
timeout-minutes: 5
28+
permissions:
29+
issues: write
30+
concurrency:
31+
group: automation-alert
32+
cancel-in-progress: false
33+
queue: max
34+
steps:
35+
# No checkout: gh alone, on this repository, with GITHUB_TOKEN. The event's fields reach the shell through env.
36+
# The issue is found by its exact title in the open issues, not by a search, whose index can lag behind an issue
37+
# opened moments ago.
38+
- name: Comment on the issue, or open it
39+
run: |
40+
title='Automation needs a look'
41+
body="$WORKFLOW $CONCLUSION: $RUN_URL"
42+
number=$(TITLE=$title gh issue list --state open --limit 1000 --json number,title \
43+
--jq 'map(select(.title == env.TITLE) | .number) | min // empty')
44+
if [[ -n $number ]]; then
45+
gh issue comment "$number" --body "$body"
46+
else
47+
gh issue create --title "$title" --assignee drptbl --body "$body"
48+
fi
49+
env:
50+
GH_TOKEN: ${{ github.token }}
51+
GH_REPO: ${{ github.repository }}
52+
WORKFLOW: ${{ github.event.workflow_run.name }}
53+
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
54+
RUN_URL: ${{ github.event.workflow_run.html_url }}

‎tests/test_release_pr.py‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,58 @@ def test_downstream_dispatches_with_a_contents_token(self):
693693
downstream = job(self.WORKFLOW, "downstream")
694694
self.assertNotIn("checkout", downstream)
695695
self.assertEqual(1, downstream.count("uses:"))
696+
697+
698+
class TestAlertWorkflow(WorkflowTestCase):
699+
WORKFLOW = "alert.yml"
700+
701+
def test_watches_the_automation_workflows_by_their_exact_names(self):
702+
self.assertIn("""\
703+
on:
704+
workflow_run:
705+
workflows: [Release, Release PR, Upstream check]
706+
types: [completed]
707+
708+
permissions: {}
709+
""", self.text())
710+
names = {path.name: re.search(r"^name: (.+)$", path.read_text(encoding="utf-8"), re.MULTILINE).group(1)
711+
for path in WORKFLOWS.glob("*.yml")}
712+
self.assertEqual("Release", names["release.yml"])
713+
self.assertEqual("Release PR", names["release-pr.yml"])
714+
self.assertEqual("Upstream check", names["upstream.yml"])
715+
716+
def test_comments_on_one_issue_with_github_token(self):
717+
self.assert_in_job("alert", """\
718+
alert:
719+
if: github.event.workflow_run.conclusion != 'success' &&
720+
github.event.workflow_run.conclusion != 'skipped' &&
721+
github.event.workflow_run.conclusion != 'neutral' &&
722+
github.event.workflow_run.event != 'pull_request'
723+
runs-on: ubuntu-latest
724+
timeout-minutes: 5
725+
permissions:
726+
issues: write
727+
concurrency:
728+
group: automation-alert
729+
cancel-in-progress: false
730+
queue: max
731+
""")
732+
self.assert_in_job("alert", """\
733+
run: |
734+
title='Automation needs a look'
735+
body="$WORKFLOW $CONCLUSION: $RUN_URL"
736+
number=$(TITLE=$title gh issue list --state open --limit 1000 --json number,title \\
737+
--jq 'map(select(.title == env.TITLE) | .number) | min // empty')
738+
if [[ -n $number ]]; then
739+
gh issue comment "$number" --body "$body"
740+
else
741+
gh issue create --title "$title" --assignee drptbl --body "$body"
742+
fi
743+
env:
744+
GH_TOKEN: ${{ github.token }}
745+
GH_REPO: ${{ github.repository }}
746+
WORKFLOW: ${{ github.event.workflow_run.name }}
747+
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
748+
RUN_URL: ${{ github.event.workflow_run.html_url }}
749+
""")
750+
self.assertNotIn("uses:", self.text())

0 commit comments

Comments
 (0)