~raboof/nixpkgs-contributor-dashboard

08df9196d56f45cbba0d6d9feeb1231481539f68 — Arnout Engelen 1 year, 3 days ago bd4d138
Add feature for clearing PR's
3 files changed, 60 insertions(+), 2 deletions(-)

M README.md
A src/bin/clean_pulls.rs
M src/models.rs
M README.md => README.md +5 -1
@@ 37,7 37,11 @@ Show items:

    cargo run --bin show

Clean up closed PR's and fixed failures
Clean up closed PR's:

    cargo run --bin clean_pulls

Clean up fixed failures:

    # not implemented yet


A src/bin/clean_pulls.rs => src/bin/clean_pulls.rs +54 -0
@@ 0,0 1,54 @@
extern crate nixpkgs_contributor_dashboard;
extern crate diesel;
extern crate dotenv;

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

use std::env;
use github_rs::client::{Executor, Github};
use serde::Deserialize;
use dotenv::dotenv;

use nixpkgs_contributor_dashboard::schema::items::dsl::*;

#[derive(Deserialize, Debug)]
pub struct Pull {
    pub state: String,
}

fn closed(client: &Github, item: &Item) -> bool {
    let owner = "nixos";
    let repo = "nixpkgs";
    let endpoint = format!(
        "repos/{}/{}/pulls/{}",
        owner, repo, item.number
        );
    println!("Getting pr {}", item.html_url);
    let pr = client.get()
        .custom_endpoint(&endpoint)
        .execute::<Pull>();
    match pr {
        Ok((_headers, _status, entity)) =>
            return entity.unwrap().state == "closed",
        Err(e) => {
            println!("{}", e);
            return false
        }
    }
}

fn main() {
    dotenv().ok();
    let client = Github::new(env::var("GITHUB_TOKEN").unwrap()).unwrap();

    let connection = establish_connection();
    let results = items.filter(kind.eq("PR")).load::<Item>(&connection)
        .expect("Error loading items");
    for item in results {
        if closed(&client, &item) {
            diesel::delete(&item).execute(&connection).expect("Error deleting");
        }
    }
}

M src/models.rs => src/models.rs +1 -1
@@ 1,6 1,6 @@
use super::schema::items;

#[derive(Queryable)]
#[derive(Queryable, Identifiable)]
pub struct Item {
    pub id: i32,
    // 'PR', 'ISSUE' or 'BUILD'