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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::fmt::Display;

//
use crate::{git::Commit, BranchName, ForgeAlias, RepoAlias};
use serde_json::json;

use super::graph::Log;

#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum UserNotification {
    CICheckFailed {
        forge_alias: ForgeAlias,
        repo_alias: RepoAlias,
        commit: Commit,
        log: Log,
    },
    RepoConfigLoadFailure {
        forge_alias: ForgeAlias,
        repo_alias: RepoAlias,
        reason: String,
    },
    WebhookRegistration {
        forge_alias: ForgeAlias,
        repo_alias: RepoAlias,
        reason: String,
    },
    DevNotBasedOnMain {
        forge_alias: ForgeAlias,
        repo_alias: RepoAlias,
        dev_branch: BranchName,
        main_branch: BranchName,
        dev_commit: Commit,
        main_commit: Commit,
        log: Log,
    },
}
impl UserNotification {
    #[must_use]
    pub fn as_json(&self, timestamp: time::OffsetDateTime) -> serde_json::Value {
        let timestamp = timestamp.unix_timestamp().to_string();
        match self {
            Self::CICheckFailed {
                forge_alias,
                repo_alias,
                commit,
                log,
            } => json!({
                "type": "cicheck.failed",
                "timestamp": timestamp,
                "data": {
                    "forge_alias": forge_alias,
                    "repo_alias": repo_alias,
                    "commit": {
                        "sha": commit.sha(),
                        "message": commit.message()
                    },
                    "log": **log
                }
            }),
            Self::RepoConfigLoadFailure {
                forge_alias,
                repo_alias,
                reason,
            } => json!({
                "type": "config.load.failed",
                "timestamp": timestamp,
                "data": {
                    "forge_alias": forge_alias,
                    "repo_alias": repo_alias,
                    "reason": reason,
                }
            }),
            Self::WebhookRegistration {
                forge_alias,
                repo_alias,
                reason,
            } => json!({
                "type": "webhook.registration.failed",
                "timestamp": timestamp,
                "data": {
                    "forge_alias": forge_alias,
                    "repo_alias": repo_alias,
                    "reason": reason,
                }
            }),
            Self::DevNotBasedOnMain {
                forge_alias,
                repo_alias,
                dev_branch,
                main_branch,
                dev_commit,
                main_commit,
                log,
            } => json!({
                "type": "branch.dev.not-on-main",
                "timestamp": timestamp,
                "data": {
                    "forge_alias": forge_alias,
                    "repo_alias": repo_alias,
                    "branches": {
                        "dev": dev_branch,
                        "main": main_branch
                    },
                    "commits": {
                        "dev": {
                            "sha": dev_commit.sha(),
                            "message": dev_commit.message()
                        },
                        "main": {
                            "sha": main_commit.sha(),
                            "message": main_commit.message()
                        }
                    },
                    "log": **log
                }
            }),
        }
    }
}
impl Display for UserNotification {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let message = match self {
            Self::CICheckFailed { commit, .. } => format!("CI Check Failed [{commit}]"),
            Self::RepoConfigLoadFailure { reason, .. } => {
                format!("Failed to load repo config: {reason}")
            }
            Self::WebhookRegistration { reason, .. } => {
                format!("Failed to register webhook: {reason}")
            }
            Self::DevNotBasedOnMain { .. } => "Dev not based on main".to_string(),
        };
        write!(f, "{message}")
    }
}