summaryrefslogtreecommitdiff
path: root/ofborg/tickborg/src/maintainers.rs
blob: ff1bec0cee171f9fa8e330fe33e9f8e12d321cab (plain)
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
207
208
209
210
211
use crate::nix::Nix;

use tempfile::NamedTempFile;

use std::collections::{HashMap, HashSet};
use std::io::Write;
use std::path::Path;

#[derive(serde::Deserialize, Debug, Eq, PartialEq)]
pub struct ImpactedMaintainers(HashMap<Maintainer, Vec<Package>>);
pub struct MaintainersByPackage(pub HashMap<Package, HashSet<Maintainer>>);

#[derive(serde::Deserialize, Clone, Debug, Eq, PartialEq, Hash)]
pub struct Maintainer(String);
impl<'a> From<&'a str> for Maintainer {
    fn from(name: &'a str) -> Maintainer {
        Maintainer(name.to_ascii_lowercase())
    }
}
#[derive(serde::Deserialize, Clone, Debug, Eq, PartialEq, Hash)]
pub struct Package(String);
impl<'a> From<&'a str> for Package {
    fn from(name: &'a str) -> Package {
        Package(name.to_owned())
    }
}

#[derive(Debug)]
pub enum CalculationError {
    DeserializeError(serde_json::Error),
    Io(std::io::Error),
    Utf8(std::string::FromUtf8Error),
}
impl From<serde_json::Error> for CalculationError {
    fn from(e: serde_json::Error) -> CalculationError {
        CalculationError::DeserializeError(e)
    }
}
impl From<std::io::Error> for CalculationError {
    fn from(e: std::io::Error) -> CalculationError {
        CalculationError::Io(e)
    }
}
impl From<std::string::FromUtf8Error> for CalculationError {
    fn from(e: std::string::FromUtf8Error) -> CalculationError {
        CalculationError::Utf8(e)
    }
}

impl ImpactedMaintainers {
    pub fn calculate(
        nix: &Nix,
        checkout: &Path,
        paths: &[String],
        attributes: &[Vec<&str>],
    ) -> Result<ImpactedMaintainers, CalculationError> {
        let mut path_file = NamedTempFile::new()?;
        let pathstr = serde_json::to_string(&paths)?;
        write!(path_file, "{pathstr}")?;

        let mut attr_file = NamedTempFile::new()?;
        let attrstr = serde_json::to_string(&attributes)?;
        write!(attr_file, "{attrstr}")?;

        let mut argstrs: HashMap<&str, &str> = HashMap::new();
        argstrs.insert("changedattrsjson", attr_file.path().to_str().unwrap());
        argstrs.insert("changedpathsjson", path_file.path().to_str().unwrap());

        let mut cmd = nix.safely_evaluate_expr_cmd(
            checkout,
            include_str!("./maintainers.nix"),
            argstrs,
            &[path_file.path(), attr_file.path()],
        );

        let ret = cmd.output()?;

        Ok(serde_json::from_str(&String::from_utf8(ret.stdout)?)?)
    }

    pub fn maintainers(&self) -> Vec<&str> {
        self.0
            .keys()
            .map(|Maintainer(name)| name.as_str())
            .collect()
    }

    pub fn maintainers_by_package(&self) -> MaintainersByPackage {
        let mut bypkg = MaintainersByPackage(HashMap::new());

        for (maintainer, packages) in self.0.iter() {
            for package in packages.iter() {
                bypkg
                    .0
                    .entry(package.clone())
                    .or_default()
                    .insert(maintainer.clone());
            }
        }

        bypkg
    }
}

impl std::fmt::Display for ImpactedMaintainers {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let mut is_first = true;
        for (Maintainer(maintainer), packages) in &self.0 {
            if is_first {
                is_first = false;
            } else {
                f.write_str("\n")?;
            }

            f.write_fmt(format_args!("{maintainer}"))?;

            let (first, rest) = {
                let mut packages = packages.iter();
                (packages.next(), packages)
            };
            if let Some(Package(package)) = first {
                f.write_fmt(format_args!(": {package}"))?;

                for Package(package) in rest {
                    f.write_fmt(format_args!(", {package}"))?;
                }
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::checkout::cached_cloner;
    use crate::clone::GitClonable;
    use crate::test_scratch::TestScratch;
    use std::env;
    use std::ffi::OsStr;
    use std::path::{Path, PathBuf};
    use std::process::Command;
    use std::process::Stdio;

    #[cfg(target_os = "linux")]
    const SYSTEM: &str = "x86_64-linux";
    #[cfg(target_os = "macos")]
    const SYSTEM: &str = "x86_64-darwin";

    fn tpath(component: &str) -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR")).join(component)
    }

    fn make_pr_repo(bare: &Path, co: &Path) -> String {
        let output = Command::new("bash")
            .current_dir(tpath("./test-srcs"))
            .arg("./make-maintainer-pr.sh")
            .arg(bare)
            .arg(co)
            .stdout(Stdio::piped())
            .output()
            .expect("building the test PR failed");

        let stderr =
            String::from_utf8(output.stderr).unwrap_or_else(|err| format!("warning: {err}"));
        println!("{stderr}");

        let hash = String::from_utf8(output.stdout).expect("Should just be a hash");
        hash.trim().to_owned()
    }

    #[test]
    fn example() {
        let workingdir = TestScratch::new_dir("test-maintainers-example");

        let bare = TestScratch::new_dir("test-maintainers-example-bare");
        let mk_co = TestScratch::new_dir("test-maintainers-example-co");
        let hash = make_pr_repo(&bare.path(), &mk_co.path());

        let attributes = vec![vec!["foo", "bar", "packageA"]];

        let cloner = cached_cloner(&workingdir.path());
        let project = cloner.project("maintainer-test", bare.string());

        let working_co = project
            .clone_for("testing-maintainer-list".to_owned(), "123".to_owned())
            .expect("clone should work");

        working_co
            .checkout_origin_ref(OsStr::new("master"))
            .unwrap();

        let paths = working_co.files_changed_from_head(&hash).unwrap();

        working_co.checkout_ref(OsStr::new(&hash)).unwrap();

        let remote = env::var("NIX_REMOTE").unwrap_or("".to_owned());
        let nix = Nix::new(SYSTEM.to_owned(), remote, 1800, None);

        let parsed =
            ImpactedMaintainers::calculate(&nix, &working_co.clone_to(), &paths, &attributes);

        let mut expect = ImpactedMaintainers(HashMap::new());
        expect.0.insert(
            Maintainer::from("test"),
            vec![Package::from("foo.bar.packageA")],
        );

        assert_eq!(parsed.unwrap(), expect);
    }
}