File size: 2,156 Bytes
0e57036
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Send a GitHub Actions failure alert through the project notification center."""

from __future__ import annotations

import os
import sys


def _env(name: str, default: str = "unknown") -> str:
    value = os.getenv(name)
    return value if value else default


def _run_url() -> str:
    server_url = _env("GITHUB_SERVER_URL", "https://github.com")
    repository = _env("GITHUB_REPOSITORY")
    run_id = _env("GITHUB_RUN_ID")
    return f"{server_url}/{repository}/actions/runs/{run_id}"


def _short_sha() -> str:
    sha = _env("GITHUB_SHA")
    return sha[:12] if sha != "unknown" else sha


def build_subject() -> str:
    return f"GitHub Actions failure: {_env('GITHUB_WORKFLOW', 'workflow')}"


def build_body() -> str:
    return "\n".join(
        [
            "The scraping workflow failed.",
            "",
            f"Workflow: {_env('GITHUB_WORKFLOW')}",
            f"Job: {_env('GITHUB_JOB')}",
            f"Failing context: {_env('SCRAPE_FAILED_STEP', 'see linked run logs')}",
            f"Command: {_env('SCRAPE_COMMAND', 'unknown')}",
            f"Exit code: {_env('SCRAPE_EXIT_CODE', 'unknown')}",
            f"Run: {_run_url()}",
            f"Run attempt: {_env('GITHUB_RUN_ATTEMPT')}",
            f"Event: {_env('GITHUB_EVENT_NAME')}",
            f"Branch/ref: {_env('GITHUB_REF')}",
            f"Ref name: {_env('GITHUB_REF_NAME')}",
            f"Commit SHA: {_env('GITHUB_SHA')}",
            f"Commit short SHA: {_short_sha()}",
            "",
            "Short error message: the scrape command exited unsuccessfully. "
            "Open the run URL above for the full, masked GitHub Actions logs.",
        ]
    )


def main() -> int:
    try:
        from src.notification.notification_center import NotificationCenter

        NotificationCenter().send_notification(
            subject=build_subject(),
            body=build_body(),
            channel="email",
        )
        print("Failure notification sent.")
    except Exception as exc:
        print(f"Warning: could not send failure notification: {exc}", file=sys.stderr)

    return 0


if __name__ == "__main__":
    raise SystemExit(main())