<commit_before>extern crate hyper;
extern crate rustc_serialize;
extern crate time;
extern crate slack_hook;

use std::io::prelude::*;
use std::fs::File;
use std::net::Ipv4Addr;
use std::thread;
use std::convert::AsRef;


use hyper::Server;
use hyper::server::Request;
use hyper::server::Response;
use hyper::uri::RequestUri;
use hyper::net::Fresh;
use hyper::server::Handler;
use rustc_serialize::json::decode;
use std::path::Path;

use std::sync::{Mutex, Arc};
use std::sync::mpsc::{Sender, channel};

use dispatcher::Dispatcher;
use hook_configuration::HookConfiguration;
use deployer::DeployMessage;

mod hook_configuration;
mod dispatcher;
mod deployer;
mod tools;


#[derive(RustcDecodable)]
pub struct GitHook  {
    // before: String,
    // after: String,
    reference: String,
    repository: Repository,
}

trait RefsHeadToBranch {
  fn branch(&self) -> &str;
}
impl RefsHeadToBranch for String {
  fn branch(&self) -> &str {
    self.trim_left_matches("/refs/head/")
  }
}

#[derive(RustcDecodable)]
pub struct Repository {
  name: String,
  // url: String,
}

pub struct Daemon {
  intercom: Arc<Mutex<Sender<DeployMessage>>>,
  config: HookConfiguration,
}

impl Handler for Daemon {
  fn handle(&self, req: Request, res: Response<Fresh>) {

    let mut s = String::new();
    let mut myreq = req;
    if myreq.uri == RequestUri::AbsolutePath("/hook/".to_string()) {
      match myreq.read_to_string(&mut s) {
        Ok(_) => {
          match decode::<GitHook>(s.as_ref()) {
            Ok(decoded ) => {
              let repo_name = decoded.repository.name;
              let branch = decoded.reference.branch();

              match self.config.hooks.iter().filter(|&binding|
                if binding.name == repo_name {
                  let match_branch = binding.branch.clone();
                  match match_branch {
                    Some(b) => b == branch,
                    None => true
                  }
                } else {
                  false
                }
                ).next() {
                Some(hk) => {
                  let _ = self.intercom.lock().unwrap().send(DeployMessage::Deploy(hk.clone()));
                },
                None => println!("No hook for {}", repo_name),
              }
            },
            Err(e) => {
              println!("Error while parsing http: {:?}",  e);
              println!("{}", s);
            }
          }
        },
        _ => {}
      }
    }

    let mut res = res.start().unwrap();
    res.write_all(b"OK.").unwrap();
    res.end().unwrap();
  }
}<commit_msg>Support branches<commit_after>extern crate hyper;
extern crate rustc_serialize;
extern crate time;
extern crate slack_hook;

use std::io::prelude::*;
use std::fs::File;
use std::net::Ipv4Addr;
use std::thread;
use std::convert::AsRef;


use hyper::Server;
use hyper::server::Request;
use hyper::server::Response;
use hyper::uri::RequestUri;
use hyper::net::Fresh;
use hyper::server::Handler;
use rustc_serialize::json::decode;
use std::path::Path;

use std::sync::{Mutex, Arc};
use std::sync::mpsc::{Sender, channel};

use dispatcher::Dispatcher;
use hook_configuration::HookConfiguration;
use deployer::DeployMessage;

mod hook_configuration;
mod dispatcher;
mod deployer;
mod tools;


#[derive(RustcDecodable)]
pub struct GitHook  {
    // before: String,
    // after: String,
    reference: String,
    repository: Repository,
}

trait RefsHeadToBranch {
  fn branch(&self) -> &str;
}
impl RefsHeadToBranch for String {
  fn branch(&self) -> &str {
    self.trim_left_matches("/refs/head/")
  }
}

#[derive(RustcDecodable)]
pub struct Repository {
  name: String,
  // url: String,
}

pub struct Daemon {
  intercom: Arc<Mutex<Sender<DeployMessage>>>,
  config: HookConfiguration,
}

impl Handler for Daemon {
  fn handle(&self, req: Request, res: Response<Fresh>) {

    let mut s = String::new();
    let mut myreq = req;
    if myreq.uri == RequestUri::AbsolutePath("/hook/".to_string()) {
      match myreq.read_to_string(&mut s) {
        Ok(_) => {
          match decode::<GitHook>(s.as_ref()) {
            Ok(decoded ) => {
              let repo_name = decoded.repository.name;
              let branch = decoded.reference.branch();

              match self.config.hooks.iter().filter(|&binding|
                if repo_name == binding.name {
                  match binding.branch.clone() {
                    Some(target_branch) => branch == target_branch,
                    None => true
                  }
                } else {
                  false
                }
                ).next() {
                Some(hk) => {
                  let _ = self.intercom.lock().unwrap().send(DeployMessage::Deploy(hk.clone()));
                },
                None => println!("No hook for {}", repo_name),
              }
            },
            Err(e) => {
              println!("Error while parsing http: {:?}",  e);
              println!("{}", s);
            }
          }
        },
        _ => {}
      }
    }

    let mut res = res.start().unwrap();
    res.write_all(b"OK.").unwrap();
    res.end().unwrap();
  }
}
{'fraction_non_alphanumeric': 0.14925954793452845, 'fraction_numerical': 0.0003897116134060795, 'mean_word_length': 2.2777777777777777, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '45924', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1558, 'n_words': 342}