Skip to main content

mit_commit_message_lints/mit/cmd/
get_commit_coauthor_configuration.rs

1use std::borrow::Cow;
2
3use miette::{IntoDiagnostic, Result};
4use time::OffsetDateTime;
5
6use crate::{
7    external::Vcs,
8    mit::{cmd::CONFIG_KEY_EXPIRES, Author, AuthorState},
9};
10
11/// Get the co-authors that are currently defined for this vcs config source
12///
13/// # Errors
14///
15/// Will fail if reading or writing from the VCS config fails, or it contains
16/// data in an incorrect format
17pub fn get_commit_coauthor_configuration(config: &dyn Vcs) -> Result<AuthorState<Vec<Author<'_>>>> {
18    let config_value = config.get_i64(CONFIG_KEY_EXPIRES)?;
19
20    match config_value {
21        Some(config_value) => {
22            let config_time =
23                OffsetDateTime::from_unix_timestamp(config_value).into_diagnostic()?;
24            if OffsetDateTime::now_utc() < config_time {
25                let author_config = get_vcs_authors(config)?;
26
27                Ok(AuthorState::Some(author_config))
28            } else {
29                Ok(AuthorState::Timeout(config_time))
30            }
31        }
32        None => Ok(AuthorState::None),
33    }
34}
35
36fn get_vcs_authors(config: &'_ dyn Vcs) -> Result<Vec<Author<'_>>> {
37    let co_author_names = get_vcs_coauthor_names(config)?;
38    let co_author_emails = get_vcs_coauthor_emails(config)?;
39
40    Ok(co_author_names
41        .into_iter()
42        .zip(co_author_emails)
43        .filter_map(new_author)
44        .collect())
45}
46
47fn new_author<'a>(parameters: (Option<Cow<'a, str>>, Option<Cow<'a, str>>)) -> Option<Author<'a>> {
48    match parameters {
49        (Some(name), Some(email)) => Some(Author::new(name, email, None)),
50        _ => None,
51    }
52}
53
54fn get_vcs_coauthor_names(config: &'_ dyn Vcs) -> Result<Vec<Option<Cow<'_, str>>>> {
55    super::vcs::get_vcs_coauthors_config(config, "name")
56}
57
58fn get_vcs_coauthor_emails(config: &'_ dyn Vcs) -> Result<Vec<Option<Cow<'_, str>>>> {
59    super::vcs::get_vcs_coauthors_config(config, "email")
60}
61
62#[cfg(test)]
63mod tests {
64    use std::{
65        collections::BTreeMap,
66        convert::TryFrom,
67        ops::Add,
68        time::{Duration, SystemTime, UNIX_EPOCH},
69    };
70
71    use time::OffsetDateTime;
72
73    use crate::{
74        external::InMemory,
75        mit::{get_commit_coauthor_configuration, Author, AuthorState},
76    };
77
78    #[test]
79    fn there_is_no_author_config_if_it_has_expired() {
80        let now_minus_10 = epoch_with_offset(subtract_100_seconds);
81        let mut strings: BTreeMap<String, String> = BTreeMap::new();
82        strings.insert(super::CONFIG_KEY_EXPIRES.into(), format!("{now_minus_10}"));
83        let vcs = InMemory::new(&mut strings);
84
85        let actual = get_commit_coauthor_configuration(&vcs).expect("Failed to read VCS config");
86        let expected =
87            AuthorState::Timeout(OffsetDateTime::from_unix_timestamp(now_minus_10).unwrap());
88        assert_eq!(
89            expected, actual,
90            "Expected the mit config to be {expected:?}, instead got {actual:?}"
91        );
92    }
93
94    #[test]
95    fn there_is_a_config_if_the_config_has_not_expired() {
96        let mut strings = BTreeMap::new();
97        strings.insert(
98            super::CONFIG_KEY_EXPIRES.into(),
99            format!("{}", epoch_with_offset(add_100_seconds)),
100        );
101
102        let vcs = InMemory::new(&mut strings);
103
104        let actual = get_commit_coauthor_configuration(&vcs).expect("Failed to read VCS config");
105        let expected: AuthorState<Vec<Author<'_>>> = AuthorState::Some(vec![]);
106
107        assert_eq!(
108            expected, actual,
109            "Expected the mit config to be {expected:?}, instead got {actual:?}"
110        );
111    }
112
113    #[test]
114    fn we_get_author_config_back_if_there_is_any() {
115        let mut buffer = BTreeMap::new();
116        buffer.insert(
117            super::CONFIG_KEY_EXPIRES.into(),
118            format!("{}", epoch_with_offset(add_100_seconds)),
119        );
120        buffer.insert(
121            "mit.author.coauthors.0.email".into(),
122            "annie@example.com".into(),
123        );
124        buffer.insert("mit.author.coauthors.0.name".into(), "Annie Example".into());
125        let vcs = InMemory::new(&mut buffer);
126
127        let actual = get_commit_coauthor_configuration(&vcs).expect("Failed to read VCS config");
128        let expected = AuthorState::Some(vec![Author::new(
129            "Annie Example".into(),
130            "annie@example.com".into(),
131            None,
132        )]);
133
134        assert_eq!(
135            expected, actual,
136            "Expected the mit config to be {expected:?}, instead got {actual:?}"
137        );
138    }
139
140    fn add_100_seconds(x: Duration) -> Duration {
141        x.add(Duration::from_secs(100))
142    }
143
144    fn subtract_100_seconds(x: Duration) -> Duration {
145        x.checked_sub(Duration::from_secs(100)).unwrap()
146    }
147
148    const fn into_seconds(x: Duration) -> u64 {
149        x.as_secs()
150    }
151
152    #[test]
153    fn we_get_multiple_authors_back_if_there_are_multiple() {
154        let mut buffer = BTreeMap::new();
155        buffer.insert(
156            super::CONFIG_KEY_EXPIRES.into(),
157            format!("{}", epoch_with_offset(add_100_seconds)),
158        );
159        buffer.insert(
160            "mit.author.coauthors.0.email".into(),
161            "annie@example.com".into(),
162        );
163        buffer.insert("mit.author.coauthors.0.name".into(), "Annie Example".into());
164        buffer.insert(
165            "mit.author.coauthors.1.email".into(),
166            "joe@example.com".into(),
167        );
168        buffer.insert("mit.author.coauthors.1.name".into(), "Joe Bloggs".into());
169
170        let vcs = InMemory::new(&mut buffer);
171
172        let actual = get_commit_coauthor_configuration(&vcs).expect("Failed to read VCS config");
173        let expected = AuthorState::Some(vec![
174            Author::new("Annie Example".into(), "annie@example.com".into(), None),
175            Author::new("Joe Bloggs".into(), "joe@example.com".into(), None),
176        ]);
177
178        assert_eq!(
179            expected, actual,
180            "Expected the mit config to be {expected:?}, instead got {actual:?}"
181        );
182    }
183
184    fn epoch_with_offset(x: fn(Duration) -> Duration) -> i64 {
185        SystemTime::now()
186            .duration_since(UNIX_EPOCH)
187            .map(x)
188            .map(into_seconds)
189            .map(i64::try_from)
190            .expect("Failed to get Unix Epoch")
191            .expect("Convert epoch to int")
192    }
193}