rusty_ci/buildbot/
mail.rs

1use crate::{unmatched_quotes, unwrap};
2use rusty_yaml::Yaml;
3use std::fmt::{Display, Error, Formatter};
4use std::process::exit;
5
6/// This object is responsible for building the `MailNotifier` object
7/// in the buildbot master config. It contains the information for
8/// authenticating an SMTP request to send email. This information is
9/// sensitive and should be kept separate from the master yaml file
10pub struct MailNotifier {
11    /// These are the recipients that will be messages with every test
12    all_recipients: Vec<String>,
13    /// These are the recipients that will be messages with every successful test
14    success_recipients: Vec<String>,
15    /// These are the recipients that will be messages with every failed test
16    failure_recipients: Vec<String>,
17
18    /// The address that the emails will be sent from
19    from_address: String,
20    /// The smtp host that will be used to access the email
21    smtp_relay_host: String,
22    /// The port of the smtp_relay_host to use
23    smtp_port: String,
24
25    /// Identical to from_address
26    smtp_user: String,
27
28    /// Basically this is the suffix to the email address
29    /// to tack on to the end of the usernames of the interested users.
30    /// So, the user `dn-lang` will be emailed at `dn-lang@example.org`
31    /// if lookup is `example.org`
32    lookup: String,
33
34    /// The password to access the from email address
35    smtp_password: String,
36}
37
38impl Display for MailNotifier {
39    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
40        writeln!(
41            f,
42            r#"
43
44# The mail notifier responsible for all info
45all = reporters.MailNotifier(fromaddr="{from_address}",
46                            sendToInterestedUsers=True,
47                            extraRecipients={all_recipients},
48                            lookup="{lookup}",
49                            relayhost="{relay_host}", smtpPort={port},
50                            smtpUser="{user}", buildSetSummary=True,
51                            # addLogs=True,
52                            mode="all",
53                            smtpPassword="{password}")
54c['services'].append(all)
55
56
57# The mail notifier responsible for failures
58failures = reporters.MailNotifier(fromaddr="{from_address}",
59                            sendToInterestedUsers=True,
60                            extraRecipients={failure_recipients},
61                            lookup="{lookup}",
62                            relayhost="{relay_host}", smtpPort={port},
63                            smtpUser="{user}", buildSetSummary=True,
64                            # addLogs=True,
65                            mode="failing",
66                            smtpPassword="{password}")
67c['services'].append(failures)
68
69
70
71# The mail notifier responsible for successes
72successes = reporters.MailNotifier(fromaddr="{from_address}",
73                            sendToInterestedUsers=True,
74                            extraRecipients={success_recipients},
75                            lookup="{lookup}",
76                            relayhost="{relay_host}", smtpPort={port},
77                            smtpUser="{user}", buildSetSummary=True,
78                            # addLogs=True,
79                            mode="passing",
80                            smtpPassword="{password}")
81c['services'].append(successes)
82
83
84"#,
85            all_recipients = format!("{:?}", self.all_recipients),
86            success_recipients = format!("{:?}", self.success_recipients),
87            failure_recipients = format!("{:?}", self.failure_recipients),
88            from_address = self.from_address,
89            relay_host = self.smtp_relay_host,
90            password = self.smtp_password,
91            user = self.smtp_user,
92            port = self.smtp_port,
93            lookup = self.lookup,
94        )
95    }
96}
97
98impl From<Yaml> for MailNotifier {
99    fn from(yaml: Yaml) -> Self {
100        // Verify that the yaml file doesnt have unmatched quotes!
101        if let Some(line) = unmatched_quotes(&yaml) {
102            error!(
103                "There was a problem creating the mail notifier: unmatched quotes in the line '{}'",
104                line.trim()
105            );
106            exit(1);
107        }
108
109        // Confirm that the merge request handler has the required sections
110        for section in [
111            "extra-recipients",
112            "from-address",
113            "smtp-relay-host",
114            "smtp-port",
115            "lookup",
116            "smtp-password",
117        ]
118        .iter()
119        {
120            if !yaml.has_section(section) {
121                error!(
122                    "There was a problem creating the mail notifier: '{}' section not specified.",
123                    section
124                );
125                exit(1);
126            }
127        }
128
129        let extra_recipients = yaml.get_section("extra-recipients").unwrap();
130
131        for section in ["all", "failure", "success"].iter() {
132            if !extra_recipients.has_section(section) {
133                error!("There was a problem creating the mail notifier: '{}' section not specified in the 'extra-recipients' subsection.", section);
134                exit(1);
135            }
136        }
137        let mut all_recipients = vec![];
138        for recipient in extra_recipients.get_section("all").unwrap() {
139            all_recipients.push(recipient.to_string());
140        }
141
142        let mut success_recipients = vec![];
143        for recipient in extra_recipients.get_section("success").unwrap() {
144            success_recipients.push(recipient.to_string());
145        }
146
147        let mut failure_recipients = vec![];
148        for recipient in extra_recipients.get_section("failure").unwrap() {
149            failure_recipients.push(recipient.to_string());
150        }
151
152        let from_address = unwrap(&yaml, "from-address");
153        let smtp_relay_host = unwrap(&yaml, "smtp-relay-host");
154        let smtp_port = unwrap(&yaml, "smtp-port");
155        let smtp_password = unwrap(&yaml, "smtp-password");
156        let lookup = unwrap(&yaml, "lookup");
157
158        Self {
159            all_recipients,
160            success_recipients,
161            failure_recipients,
162            from_address: from_address.clone(),
163            smtp_relay_host,
164            smtp_port,
165            smtp_user: from_address,
166            lookup,
167            smtp_password,
168        }
169    }
170}