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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::{unmatched_quotes, unwrap};
use rusty_yaml::Yaml;
use std::fmt::{Display, Error, Formatter};
use std::process::exit;

/// This object is responsible for building the `MailNotifier` object
/// in the buildbot master config. It contains the information for
/// authenticating an SMTP request to send email. This information is
/// sensitive and should be kept separate from the master yaml file
pub struct MailNotifier {
    /// These are the recipients that will be messages with every test
    all_recipients: Vec<String>,
    /// These are the recipients that will be messages with every successful test
    success_recipients: Vec<String>,
    /// These are the recipients that will be messages with every failed test
    failure_recipients: Vec<String>,

    /// The address that the emails will be sent from
    from_address: String,
    /// The smtp host that will be used to access the email
    smtp_relay_host: String,
    /// The port of the smtp_relay_host to use
    smtp_port: String,

    /// Identical to from_address
    smtp_user: String,

    /// Basically this is the suffix to the email address
    /// to tack on to the end of the usernames of the interested users.
    /// So, the user `dn-lang` will be emailed at `dn-lang@example.org`
    /// if lookup is `example.org`
    lookup: String,

    /// The password to access the from email address
    smtp_password: String,
}

impl Display for MailNotifier {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        writeln!(
            f,
            r#"

# The mail notifier responsible for all info
all = reporters.MailNotifier(fromaddr="{from_address}",
                            sendToInterestedUsers=True,
                            extraRecipients={all_recipients},
                            lookup="{lookup}",
                            relayhost="{relay_host}", smtpPort={port},
                            smtpUser="{user}", buildSetSummary=True,
                            # addLogs=True,
                            mode="all",
                            smtpPassword="{password}")
c['services'].append(all)


# The mail notifier responsible for failures
failures = reporters.MailNotifier(fromaddr="{from_address}",
                            sendToInterestedUsers=True,
                            extraRecipients={failure_recipients},
                            lookup="{lookup}",
                            relayhost="{relay_host}", smtpPort={port},
                            smtpUser="{user}", buildSetSummary=True,
                            # addLogs=True,
                            mode="failing",
                            smtpPassword="{password}")
c['services'].append(failures)



# The mail notifier responsible for successes
successes = reporters.MailNotifier(fromaddr="{from_address}",
                            sendToInterestedUsers=True,
                            extraRecipients={success_recipients},
                            lookup="{lookup}",
                            relayhost="{relay_host}", smtpPort={port},
                            smtpUser="{user}", buildSetSummary=True,
                            # addLogs=True,
                            mode="passing",
                            smtpPassword="{password}")
c['services'].append(successes)


"#,
            all_recipients = format!("{:?}", self.all_recipients),
            success_recipients = format!("{:?}", self.success_recipients),
            failure_recipients = format!("{:?}", self.failure_recipients),
            from_address = self.from_address,
            relay_host = self.smtp_relay_host,
            password = self.smtp_password,
            user = self.smtp_user,
            port = self.smtp_port,
            lookup = self.lookup,
        )
    }
}

impl From<Yaml> for MailNotifier {
    fn from(yaml: Yaml) -> Self {
        // Verify that the yaml file doesnt have unmatched quotes!
        if let Some(line) = unmatched_quotes(&yaml) {
            error!(
                "There was a problem creating the mail notifier: unmatched quotes in the line '{}'",
                line.trim()
            );
            exit(1);
        }

        // Confirm that the merge request handler has the required sections
        for section in [
            "extra-recipients",
            "from-address",
            "smtp-relay-host",
            "smtp-port",
            "lookup",
            "smtp-password",
        ]
        .iter()
        {
            if !yaml.has_section(section) {
                error!(
                    "There was a problem creating the mail notifier: '{}' section not specified.",
                    section
                );
                exit(1);
            }
        }

        let extra_recipients = yaml.get_section("extra-recipients").unwrap();

        for section in ["all", "failure", "success"].iter() {
            if !extra_recipients.has_section(section) {
                error!("There was a problem creating the mail notifier: '{}' section not specified in the 'extra-recipients' subsection.", section);
                exit(1);
            }
        }
        let mut all_recipients = vec![];
        for recipient in extra_recipients.get_section("all").unwrap() {
            all_recipients.push(recipient.to_string());
        }

        let mut success_recipients = vec![];
        for recipient in extra_recipients.get_section("success").unwrap() {
            success_recipients.push(recipient.to_string());
        }

        let mut failure_recipients = vec![];
        for recipient in extra_recipients.get_section("failure").unwrap() {
            failure_recipients.push(recipient.to_string());
        }

        let from_address = unwrap(&yaml, "from-address");
        let smtp_relay_host = unwrap(&yaml, "smtp-relay-host");
        let smtp_port = unwrap(&yaml, "smtp-port");
        let smtp_password = unwrap(&yaml, "smtp-password");
        let lookup = unwrap(&yaml, "lookup");

        Self {
            all_recipients,
            success_recipients,
            failure_recipients,
            from_address: from_address.clone(),
            smtp_relay_host,
            smtp_port,
            smtp_user: from_address,
            lookup,
            smtp_password,
        }
    }
}