From 19c7228b390946bdce147c1bc70ed489e848bca2 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 2 Oct 2026 12:30:48 +0000 Subject: [PATCH] feat(grafana-github): mention the pager team in each new alert issue Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01Dx1PBYs3bgxaA7jfiN5XXL --- .github/workflows/deploy.yml | 1 + README.md | 1 + grafana-github/lambda_function.py | 14 +++++++++++--- grafana-github/test_lambda_function.py | 15 ++++++++++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index cae8e86..a3d2382 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -123,6 +123,7 @@ jobs: jq -n '{Variables: { GITHUB_APP_CLIENT_ID: "Iv23liaEFekWQaJiQIF6", GITHUB_APP_PRIVATE_KEY: env.GITHUB_APP_PRIVATE_KEY, + GITHUB_MENTION: "@femiwiki/pager", GITHUB_REPOSITORY: "femiwiki/infra", WEBHOOK_TOKEN: env.WEBHOOK_TOKEN }}' > environment.json diff --git a/README.md b/README.md index 5f71be1..9c5eda2 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ rule, and later notifications become comments. `deploy.yml` sets: - `GITHUB_APP_CLIENT_ID`: the alerts GitHub App, installed on femiwiki/infra with Issues read and write - `GITHUB_APP_PRIVATE_KEY`: that App's private key +- `GITHUB_MENTION`: `@femiwiki/pager`, mentioned in each new issue - `GITHUB_REPOSITORY`: `femiwiki/infra` - `WEBHOOK_TOKEN`: the bearer token Grafana sends diff --git a/grafana-github/lambda_function.py b/grafana-github/lambda_function.py index 1051973..5df985b 100644 --- a/grafana-github/lambda_function.py +++ b/grafana-github/lambda_function.py @@ -3,6 +3,7 @@ Grafana's webhook contact point posts its default payload to this function's URL. The first firing opens an issue titled with the rule's name; later notifications for the same rule, repeats and resolves alike, become comments. +A new issue mentions GITHUB_MENTION, a team, so its members get a push. The pull request that fixes the cause closes the issue. """ @@ -35,7 +36,12 @@ def lambda_handler(event: dict[str, Any], context: Any) -> dict[str, int]: bot = f"{app.get_app().slug}[bot]" installation = app.get_repo_installation(owner, name) github = app.get_github_for_installation(installation.id, {"issues": "write"}) - record(github.get_repo(f"{owner}/{name}"), bot, payload) + record( + github.get_repo(f"{owner}/{name}"), + bot, + payload, + os.environ.get("GITHUB_MENTION", ""), + ) return {"statusCode": 204} @@ -43,7 +49,9 @@ def authorized(header: str, token: str) -> bool: return hmac.compare_digest(header.encode(), f"Bearer {token}".encode()) -def record(repo: Repository, bot: str, payload: dict[str, Any]) -> None: +def record( + repo: Repository, bot: str, payload: dict[str, Any], mention: str = "" +) -> None: title = ( payload.get("groupLabels", {}).get("alertname") or payload["commonLabels"]["alertname"] @@ -54,7 +62,7 @@ def record(repo: Repository, bot: str, payload: dict[str, Any]) -> None: issue.create_comment(text) return if payload["status"] == "firing": - repo.create_issue(title=title, body=text) + repo.create_issue(title=title, body=f"{text}\n\n{mention}".strip()) def render(payload: dict[str, Any]) -> str: diff --git a/grafana-github/test_lambda_function.py b/grafana-github/test_lambda_function.py index b0b2256..91c8fa4 100644 --- a/grafana-github/test_lambda_function.py +++ b/grafana-github/test_lambda_function.py @@ -62,6 +62,16 @@ def test_opens_an_issue_on_first_firing(self): self.assertEqual(kwargs["title"], "Disk almost full") self.assertIn("/ is 92% full", kwargs["body"]) + def test_mentions_the_team_only_when_opening(self): + r = repo([]) + record(r, BOT, payload(), "@femiwiki/pager") + self.assertTrue( + r.create_issue.call_args.kwargs["body"].endswith("\n\n@femiwiki/pager") + ) + existing = issue("Disk almost full") + record(repo([existing]), BOT, payload(), "@femiwiki/pager") + self.assertNotIn("@femiwiki/pager", existing.create_comment.call_args.args[0]) + def test_comments_on_the_open_issue_for_the_same_rule(self): existing = issue("Disk almost full") r = repo([existing]) @@ -119,6 +129,7 @@ def test_refuses_a_wrong_token(self): "GITHUB_REPOSITORY": "femiwiki/infra", "GITHUB_APP_CLIENT_ID": "Iv23abc", "GITHUB_APP_PRIVATE_KEY": "unused", + "GITHUB_MENTION": "@femiwiki/pager", }, ) def test_records_as_the_app_on_the_repository(self): @@ -141,7 +152,9 @@ def test_records_as_the_app_on_the_repository(self): app.get_repo_installation.assert_called_once_with("femiwiki", "infra") app.get_github_for_installation.assert_called_once_with(42, {"issues": "write"}) github.get_repo.assert_called_once_with("femiwiki/infra") - recorded.assert_called_once_with(github.get_repo.return_value, BOT, payload()) + recorded.assert_called_once_with( + github.get_repo.return_value, BOT, payload(), "@femiwiki/pager" + ) if __name__ == "__main__":