summaryrefslogtreecommitdiffstats
path: root/src/permissions.rs
blob: e2f6dc130c0b3d9e4260447308aadbca5ca939f1 (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
use crate::data::Data;
use crate::schema::Person;
use failure::{bail, Error};
use std::collections::HashSet;

macro_rules! permissions {
    (
        booleans {
            $($boolean:ident,)*
        }
        bors_repos {
            $($bors:ident,)*
        }
    ) => {
        #[derive(serde_derive::Deserialize, Debug)]
        #[serde(deny_unknown_fields)]
        pub(crate) struct BorsACL {
            #[serde(default)]
            review: bool,
            #[serde(rename = "try", default)]
            try_: bool,
        }

        impl Default for BorsACL {
            fn default() -> Self {
                BorsACL {
                    review: false,
                    try_: false,
                }
            }
        }

        #[derive(serde_derive::Deserialize, Debug)]
        #[serde(deny_unknown_fields)]
        pub(crate) struct BorsPermissions {
            $(
                #[serde(default)]
                $bors: BorsACL,
            )*
        }

        impl Default for BorsPermissions {
            fn default() -> Self {
                BorsPermissions {
                    $($bors: BorsACL::default(),)*
                }
            }
        }

        #[derive(serde_derive::Deserialize, Debug)]
        #[serde(rename_all = "kebab-case", deny_unknown_fields)]
        pub(crate) struct Permissions {
            $(
                #[serde(default)]
                $boolean: bool,
            )*
            #[serde(default)]
            bors: BorsPermissions,
        }

        impl Default for Permissions {
            fn default() -> Self {
                Permissions {
                    $($boolean: false,)*
                    bors: BorsPermissions::default(),
                }
            }
        }

        impl Permissions {
            pub(crate) const AVAILABLE: &'static [&'static str] = &[
                $(stringify!($boolean),)*
                $(concat!("bors.", stringify!($bors), ".review"),)*
                $(concat!("bors.", stringify!($bors), ".try"),)*
            ];

            pub(crate) fn has(&self, permission: &str) -> bool {
                self.has_directly(permission) || self.has_indirectly(permission)
            }

            pub(crate) fn has_directly(&self, permission: &str) -> bool {
                $(
                    if permission == stringify!($boolean) {
                        return self.$boolean;
                    }
                )*
                $(
                    if permission == concat!("bors.", stringify!($bors), ".review") {
                        return self.bors.$bors.review;
                    }
                    if permission == concat!("bors.", stringify!($bors), ".try") {
                        return self.bors.$bors.try_
                    }
                )*
                false
            }

            pub fn has_indirectly(&self, permission: &str) -> bool {
                $(
                    if permission == concat!("bors.", stringify!($bors), ".try") {
                        return self.bors.$bors.review;
                    }
                )*
                false
            }

            pub(crate) fn has_any(&self) -> bool {
                false
                $(|| self.$boolean)*
                $(|| self.bors.$bors.review)*
                $(|| self.bors.$bors.try_)*
            }

            pub(crate) fn validate(&self, what: String) -> Result<(), Error> {
                $(
                    if self.bors.$bors.try_ == true && self.bors.$bors.review == true {
                        bail!(
                            "{} has both the `bors.{}.review` and `bors.{}.try` permissions",
                            what,
                            stringify!($bors),
                            stringify!($bors),
                        );
                    }
                )*
                Ok(())
            }
        }
    }
}

permissions! {
    booleans {
        perf,
        crater,
    }
    bors_repos {
        cargo,
        clippy,
        compiler_builtins,
        crater,
        crates_io,
        hashbrown,
        miri,
        libc,
        regex,
        rls,
        rust,
        rustlings,
        rustup_rs,
        stdarch,
        team,
    }
}

pub(crate) fn allowed_people<'a>(
    data: &'a Data,
    permission: &str,
) -> Result<Vec<&'a Person>, Error> {
    let mut members_with_perms = HashSet::new();
    for team in data.teams() {
        if team.permissions().has(permission) {
            for member in team.members(&data)? {
                members_with_perms.insert(member);
            }
        }
    }
    Ok(data
        .people()
        .filter(|p| members_with_perms.contains(p.github()) || p.permissions().has(permission))
        .collect())
}