summaryrefslogtreecommitdiffstats
path: root/src/permissions.rs
blob: 3670f593b57d29607fafdfaacd2c1f0b64690964 (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
use crate::data::Data;
use failure::{Error, bail};
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 {
                $(
                    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_ || 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,
        libc,
        regex,
        rls,
        rust,
        rustup_rs,
    }
}

pub(crate) fn allowed_github_users(
    data: &Data,
    permission: &str,
) -> Result<HashSet<String>, Error> {
    let mut github_users = HashSet::new();
    for team in data.teams() {
        if team.permissions().has(permission) {
            for member in team.members(&data)? {
                github_users.insert(member.to_string());
            }
        }
    }
    for person in data.people() {
        if person.permissions().has(permission) {
            github_users.insert(person.github().to_string());
        }
    }
    Ok(github_users)
}