~raboof/nixpkgs-contributor-dashboard

ref: 08df9196d56f45cbba0d6d9feeb1231481539f68 nixpkgs-contributor-dashboard/src/bin/show.rs -rw-r--r-- 2.2 KiB
08df9196 — Arnout Engelen Add feature for clearing PR's 1 year, 3 months ago
                                                                                
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
extern crate nixpkgs_contributor_dashboard;
extern crate diesel;

use self::nixpkgs_contributor_dashboard::*;
use self::models::*;
use self::diesel::prelude::*;

use serde::Deserialize;
use std::fs;
use std::collections::HashMap;
use std::path::Path;

use regex::Regex;
use regex::escape;

#[derive(Deserialize)]
pub struct Config {
  pub interests: Vec<String>,
}

pub fn parse_config(path: &str) -> Config {
    if Path::new(path).exists() {
        // TODO not sure it's even necessary to explicitly go via string here:
        let config_string: std::string::String = fs::read_to_string(path).unwrap();
        toml::from_str(config_string.as_str()).unwrap()
    } else {
        Config {
            interests: Vec::new(),
        }
    }
}

// returns 'None' if the item doesn't match any of the keys we're interested
// in, or the key we're interested in.
fn interesting<'a>(item: &Item, interests: &'a Vec<String>, map: &HashMap<&str, Regex>) -> Option<&'a str> {
    if item.draft == 1 {
        return None
    }

    if interests.is_empty() {
        return Some("")
    }

    let pkg: &str = item.package.as_str();

    for interest in interests {
        if map.get(interest.as_str()).unwrap().is_match(pkg) {
            return Some(interest)
        }
    }

    return None
}

fn print(item: Item, interest: &str) {
    println!("{} #{}:\t[{}/{}]\t{}\t{}", item.kind, item.number, interest, item.package, item.title, item.html_url);
}

fn create_regex_map(interests: &Vec<String>) -> HashMap<&str, Regex> {
    let mut result: HashMap<&str, Regex> = HashMap::new();
    for interest in interests {
        result.insert(
            interest.as_str(),
            Regex::new(format!("\\b{}\\b", escape(interest.as_str())).as_str()).unwrap()
        );
    }
    result
}

fn main() {
    use nixpkgs_contributor_dashboard::schema::items::dsl::*;

    let config = parse_config("config.toml");
    let regexes = create_regex_map(&config.interests);

    let connection = establish_connection();
    let results = items.load::<Item>(&connection)
        .expect("Error loading items");
    for item in results {
        if let Some(interest) = interesting(&item, &config.interests, &regexes) {
            print(item, interest)
        }
    }
}