about summary refs log tree commit diff
path: root/src/branches.rs
blob: e3ee262abf142af0717f2a33346e9c2be516120d (plain) (blame)
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
// SPDX-License-Identifier: AGPL-3.0-or-later WITH GPL-3.0-linking-exception
// SPDX-FileCopyrightText: 2021 Alyssa Ross <hi@alyssa.is>
// SPDX-FileCopyrightText: 2022 Arnout Engelen <arnout@bzzt.net>

use std::borrow::Cow;
use std::collections::BTreeMap;

use once_cell::sync::Lazy;
use regex::{Regex, RegexSet};

const NEXT_BRANCH_TABLE: [(&str, &str); 10] = [
    (r"\Astaging\z", "staging-next"),
    (r"\Astaging-next\z", "master"),
    (r"\Astaging-next-([\d.]+)\z", "release-$1"),
    (r"\Amaster\z", "nixpkgs-unstable"),
    (r"\Amaster\z", "nixos-unstable-small"),
    (r"\Anixos-(.*)-small\z", "nixos-$1"),
    (r"\Arelease-([\d.]+)\z", "nixpkgs-$1-darwin"),
    (r"\Arelease-([\d.]+)\z", "nixos-$1-small"),
    (r"\Astaging-((1.|20)\.\d{2})\z", "release-$1"),
    (r"\Astaging-((2[1-9]|[3-90].)\.\d{2})\z", "staging-next-$1"),
];

static BRANCH_NEXTS: Lazy<BTreeMap<&str, Vec<&str>>> = Lazy::new(|| {
    NEXT_BRANCH_TABLE
        .iter()
        .fold(BTreeMap::new(), |mut map, (pattern, next)| {
            map.entry(pattern).or_insert_with(Vec::new).push(next);
            map
        })
});

static BRANCH_NEXTS_BY_INDEX: Lazy<Vec<&Vec<&str>>> = Lazy::new(|| BRANCH_NEXTS.values().collect());

static BRANCH_PATTERNS: Lazy<Vec<Regex>> = Lazy::new(|| {
    BRANCH_NEXTS
        .keys()
        .copied()
        .map(Regex::new)
        .map(Result::unwrap)
        .collect()
});

static BRANCH_REGEXES: Lazy<RegexSet> = Lazy::new(|| RegexSet::new(BRANCH_NEXTS.keys()).unwrap());

pub fn next_branches(branch: &str) -> Vec<Cow<str>> {
    BRANCH_REGEXES
        .matches(branch)
        .iter()
        .flat_map(|index| {
            let regex = BRANCH_PATTERNS.get(index).unwrap();
            BRANCH_NEXTS_BY_INDEX
                .get(index)
                .unwrap()
                .iter()
                .map(move |next| regex.replace(branch, *next))
        })
        .collect()
}

const BRANCH_HYDRA_LINK_TABLE: [(&str, &str); 4] = [
    (r"\Anixpkgs-unstable\z", "nixpkgs/trunk/unstable"),
    (r"\Anixos-unstable-small\z", "nixos/unstable-small/tested"),
    (r"\Anixos-unstable\z", "nixos/trunk-combined/tested"),
    (r"\Anixos-(\d.*)\z", "nixos/release-$1/tested"),
];

static BRANCH_HYDRA_LINK_PATTERNS: Lazy<Vec<Regex>> = Lazy::new(|| {
    BRANCH_HYDRA_LINKS
        .keys()
        .copied()
        .map(Regex::new)
        .map(Result::unwrap)
        .collect()
});

static BRANCH_HYDRA_LINKS: Lazy<BTreeMap<&str, &str>> = Lazy::new(|| {
    BRANCH_HYDRA_LINK_TABLE
        .iter()
        .fold(BTreeMap::new(), |mut map, (pattern, next)| {
            // TODO throw an error when this does not return None?
            map.insert(pattern, next);
            map
        })
});

static BRANCH_HYDRA_LINKS_BY_INDEX: Lazy<Vec<&str>> =
    Lazy::new(|| BRANCH_HYDRA_LINKS.values().cloned().collect());

static BRANCH_HYDRA_LINK_REGEXES: Lazy<RegexSet> =
    Lazy::new(|| RegexSet::new(BRANCH_HYDRA_LINKS.keys()).unwrap());

pub fn branch_hydra_link(branch: &str) -> Option<String> {
    BRANCH_HYDRA_LINK_REGEXES
        .matches(branch)
        .iter()
        .next()
        .map(|index| {
            let regex = BRANCH_HYDRA_LINK_PATTERNS.get(index).unwrap();
            BRANCH_HYDRA_LINKS_BY_INDEX
                .get(index)
                .map(move |link| regex.replace(branch, *link))
                .map(move |l| format!("https://hydra.nixos.org/job/{}#tabs-constituents", l))
        })
        .flatten()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn staging_18_03() {
        let branch = "staging-18.03";
        let next = next_branches(branch);
        assert_eq!(next, vec!["release-18.03"]);
        let link = branch_hydra_link(branch);
        assert_eq!(link, None);
    }

    #[test]
    fn staging_20_09() {
        let res = next_branches("staging-20.09");
        assert_eq!(res, vec!["release-20.09"]);
    }

    #[test]
    fn staging_21_05() {
        let res = next_branches("staging-21.05");
        assert_eq!(res, vec!["staging-next-21.05"]);
    }

    #[test]
    fn staging_30_05() {
        let res = next_branches("staging-30.05");
        assert_eq!(res, vec!["staging-next-30.05"]);
    }

    #[test]
    fn staging_00_11() {
        let res = next_branches("staging-00.11");
        assert_eq!(res, vec!["staging-next-00.11"]);
    }

    #[test]
    fn staging_next_21_05() {
        let res = next_branches("staging-next-21.05");
        assert_eq!(res, vec!["release-21.05"]);
    }

    #[test]
    fn release_20_09() {
        let res = next_branches("release-20.09");
        assert_eq!(res, vec!["nixpkgs-20.09-darwin", "nixos-20.09-small"]);
    }

    #[test]
    fn nixpkgs_unstable() {
        let branch = "nixpkgs-unstable";
        let next = next_branches(branch);
        assert!(next.is_empty());
        let link = branch_hydra_link(branch);
        let expected = "https://hydra.nixos.org/job/nixpkgs/trunk/unstable#tabs-constituents";
        assert_eq!(link.unwrap(), expected);
    }

    #[test]
    fn nixos_unstable_small() {
        let branch = "nixos-unstable-small";
        let next = next_branches(branch);
        assert_eq!(next, vec!["nixos-unstable"]);
        let link = branch_hydra_link(branch);
        let expected = "https://hydra.nixos.org/job/nixos/unstable-small/tested#tabs-constituents";
        assert_eq!(link.unwrap(), expected);
    }

    #[test]
    fn nixos_unstable() {
        let branch = "nixos-unstable";
        let next = next_branches(branch);
        assert_eq!(next.len(), 0);
        let link = branch_hydra_link(branch);
        let expected = "https://hydra.nixos.org/job/nixos/trunk-combined/tested#tabs-constituents";
        assert_eq!(link.unwrap(), expected);
    }
}