Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions grafana-github/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""

Expand Down Expand Up @@ -35,15 +36,22 @@ 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}


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"]
Expand All @@ -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:
Expand Down
15 changes: 14 additions & 1 deletion grafana-github/test_lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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):
Expand All @@ -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__":
Expand Down
Loading