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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use glob::glob;
use retry::{delay::Fixed, retry_with_index, OperationResult};
use slack_hook::{AttachmentBuilder, PayloadBuilder, Slack};
use std::{
    fs::{self, OpenOptions},
    io::{prelude::*, Error, ErrorKind},
    path::Path,
};

use crate::*;


/// Read single Check from text file, return error on parse error
pub fn read_single_check_result(check_path: &str) -> Result<Check, Error> {
    read_text_file(&check_path).and_then(|file_contents| {
        serde_json::from_str(&*file_contents)
            .map_err(|err| Error::new(ErrorKind::InvalidInput, err.to_string()))
    })
}


/// Read single Check from text file
pub fn read_single_check(check_path: &str) -> Option<Check> {
    let result = read_text_file(&check_path).and_then(|file_contents| {
        serde_json::from_str(&*file_contents)
            .map_err(|err| Error::new(ErrorKind::InvalidInput, err.to_string()))
    });
    match result {
        Ok(check) => Some(check),
        Err(err) => {
            debug!(
                "Error reading Check from path: {}. Cause: {}",
                check_path, err
            );
            None
        }
    }
}


/// Warns about notifiers undefined in dynamic configuration:
pub fn warn_for_undefined_notifiers(stories: &[Story]) {
    let notifiers = Config::load().notifiers.unwrap_or_default();
    let notifier_names = notifiers
        .into_iter()
        .map(|notifier| notifier.name)
        .collect::<Vec<_>>();
    let mut undefined = stories
        .iter()
        .cloned()
        .filter(|elem| !notifier_names.contains(&elem.notifier.clone().unwrap_or_default()))
        .filter_map(|elem| elem.notifier)
        .collect::<Vec<String>>();
    undefined.dedup();
    undefined.iter().for_each(|notifier| {
        warn!(
            "Notifier: '{}' is not defined in configuration file. Notifications won't be sent!",
            &notifier
        )
    });
}


/// Sends generic notification over Slack
pub fn notify(webhook: &str, message: &str, icon: &str, fail: bool) {
    retry_with_index(Fixed::from_millis(1000), |current_try| {
        if current_try > 3 {
            return OperationResult::Err("Did not succeed within 3 tries");
        }

        let notification = Slack::new(webhook).and_then(|slack| {
            PayloadBuilder::new()
                .username(DEFAULT_SLACK_NAME)
                .icon_emoji(icon)
                .attachments(vec![
                    if fail {
                        AttachmentBuilder::new(message)
                            .color(DEFAULT_SLACK_FAILURE_COLOR)
                            .build()
                            .unwrap_or_default()
                    } else {
                        AttachmentBuilder::new(message)
                            .color(DEFAULT_SLACK_SUCCESS_COLOR)
                            .build()
                            .unwrap_or_default()
                    },
                ])
                .build()
                .and_then(|payload| {
                    debug!("Sending notification with payload: {:?}", &payload);
                    slack.send(&payload)
                })
        });

        match notification {
            Ok(_) => OperationResult::Ok("Sent!"),
            Err(_) => OperationResult::Retry("Failed to send notification!"),
        }
    })
    .map_err(|err| {
        error!("Error sending notification: {:?}", err);
        err
    })
    .unwrap_or_default();
}


/// Sends success notification to Slack
pub fn notify_success(webhook: &str, message: &str) {
    let success_emoji = Config::load()
        .success_emoji
        .unwrap_or_else(|| String::from(DEFAULT_SLACK_SUCCESS_ICON));
    notify(webhook, message, &success_emoji, false)
}


/// Sends failure notification to Slack
pub fn notify_failure(webhook: &str, message: &str) {
    let failure_emoji = Config::load()
        .failure_emoji
        .unwrap_or_else(|| String::from(DEFAULT_SLACK_FAILURE_ICON));
    notify(webhook, message, &failure_emoji, true)
}


/// Produce list of absolute paths to all files matching given glob pattern:
pub fn produce_list_absolute(glob_pattern: &str) -> Vec<String> {
    let mut list = vec![];
    for entry in glob(&glob_pattern).unwrap() {
        match entry {
            Ok(path) => {
                if let Some(element) = path.to_str() {
                    list.push(element.to_string())
                }
            }
            Err(err) => {
                error!("Error: produce_list(): {}", err);
            }
        }
    }
    trace!("produce_list_absolute(): Elements: {:?}", list);
    list
}


/// List all check files from given dir, also considering krecik_root value
pub fn list_all_checks_from(checks_dir: &str) -> Vec<String> {
    let krecik_root_dir = Config::load().krecik_root.unwrap_or_default();
    let glob_pattern = if !Path::new(&krecik_root_dir).exists() {
        if !krecik_root_dir.is_empty() {
            warn!(
                "Krecik root directory doesn't exists: {}. Falling back to current directory.",
                krecik_root_dir
            );
        } else {
            warn!("Krecik root directory wasn't specified, using current directory.");
        }
        format!("{}/**/*.json", checks_dir)
    } else {
        format!("{}/{}/**/*.json", krecik_root_dir, checks_dir)
    };
    debug!("list_all_checks_from(): {}", glob_pattern);
    produce_list_absolute(&glob_pattern)
}


/// Read text file
pub fn read_text_file(name: &str) -> Result<String, Error> {
    fs::read_to_string(name)
}


/// Write-once-and-atomic to a file
pub fn write_append(file_path: &str, contents: &str) {
    // NOTE: since file is written in "write only, all at once" mode, we have to be sure not to write empty buffer
    if !contents.is_empty() {
        let mut options = OpenOptions::new();
        match options.create(true).append(true).open(&file_path) {
            Ok(mut file) => {
                file.write_all(contents.as_bytes()).unwrap_or_else(|_| {
                    panic!("Access denied? File can't be written: {}", &file_path)
                });
                debug!("Atomically written data to file: {}", &file_path);
            }

            Err(err) => {
                error!(
                    "Atomic write to: {} has failed! Cause: {}",
                    &file_path,
                    err.to_string()
                )
            }
        }
    }
}


/// Extracts file name from full path
pub fn file_name_from_path(path: &str) -> String {
    let path = Path::new(path);
    path.file_name()
        .unwrap_or_default()
        .to_os_string()
        .into_string()
        .unwrap_or_default()
}