2019-04-16 20:55:25 +02:00
|
|
|
#[macro_use]
|
2019-04-16 16:57:06 +02:00
|
|
|
extern crate actix_web;
|
2019-04-19 16:01:47 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
|
|
|
mod color;
|
|
|
|
mod error;
|
2019-04-16 16:57:06 +02:00
|
|
|
|
2019-04-19 16:01:47 +02:00
|
|
|
use crate::{
|
|
|
|
color::{ColorKind, ToCode},
|
|
|
|
error::Error,
|
|
|
|
};
|
2019-04-18 14:45:57 +02:00
|
|
|
use actix_web::{
|
2019-04-19 16:01:47 +02:00
|
|
|
error::ErrorBadRequest,
|
2019-04-18 15:13:58 +02:00
|
|
|
http::{
|
|
|
|
self,
|
|
|
|
header::{CacheControl, CacheDirective, Expires},
|
|
|
|
},
|
2019-04-19 16:01:47 +02:00
|
|
|
middleware, web, App, HttpResponse, HttpServer,
|
2019-04-18 14:45:57 +02:00
|
|
|
};
|
2019-04-16 16:57:06 +02:00
|
|
|
use badge::{Badge, BadgeOptions};
|
2019-04-16 21:37:39 +02:00
|
|
|
use bytes::Bytes;
|
|
|
|
use futures::{unsync::mpsc, Stream};
|
2019-04-19 12:18:16 +02:00
|
|
|
use git2::Repository;
|
2019-04-16 16:57:06 +02:00
|
|
|
use std::{
|
2019-04-19 16:01:47 +02:00
|
|
|
convert::TryFrom,
|
2019-04-19 12:18:16 +02:00
|
|
|
fs::create_dir_all,
|
2019-04-16 16:57:06 +02:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::Command,
|
|
|
|
sync::Arc,
|
2019-04-18 14:45:57 +02:00
|
|
|
time::{Duration, SystemTime},
|
2019-04-16 16:57:06 +02:00
|
|
|
};
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
type State = Arc<String>;
|
|
|
|
|
2019-04-16 21:37:39 +02:00
|
|
|
const INDEX: &str = include_str!("../static/index.html");
|
2019-04-17 20:48:47 +02:00
|
|
|
const CSS: &str = include_str!("../static/tacit-css.min.css");
|
2019-04-16 21:37:39 +02:00
|
|
|
|
2019-04-16 16:57:06 +02:00
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
struct Opt {
|
|
|
|
#[structopt(
|
|
|
|
short = "o",
|
|
|
|
long = "outdir",
|
|
|
|
parse(from_os_str),
|
|
|
|
default_value = "./repos"
|
|
|
|
)]
|
|
|
|
/// Path to store cloned repositories
|
|
|
|
outdir: PathBuf,
|
|
|
|
#[structopt(short = "p", long = "port", default_value = "8080")]
|
|
|
|
/// Port to listen on
|
|
|
|
port: u16,
|
|
|
|
#[structopt(short = "h", long = "host", default_value = "0.0.0.0")]
|
|
|
|
/// Interface to listen on
|
|
|
|
host: String,
|
|
|
|
}
|
|
|
|
|
2019-04-19 16:01:47 +02:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct BadgeQuery {
|
|
|
|
color: Option<String>,
|
2019-04-16 16:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pull(path: impl AsRef<Path>) -> Result<(), Error> {
|
2019-04-19 12:18:16 +02:00
|
|
|
let repo = Repository::open_bare(path)?;
|
2019-04-16 16:57:06 +02:00
|
|
|
let mut origin = repo.find_remote("origin")?;
|
|
|
|
origin.fetch(&["refs/heads/*:refs/heads/*"], None, None)?;
|
2019-04-19 12:18:16 +02:00
|
|
|
Ok(())
|
2019-04-16 16:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hoc(repo: &str) -> Result<u64, Error> {
|
|
|
|
let output = Command::new("git")
|
|
|
|
.arg("log")
|
|
|
|
.arg("--pretty=tformat:")
|
|
|
|
.arg("--numstat")
|
|
|
|
.arg("--ignore-space-change")
|
|
|
|
.arg("--ignore-all-space")
|
|
|
|
.arg("--ignore-submodules")
|
|
|
|
.arg("--no-color")
|
|
|
|
.arg("--find-copies-harder")
|
|
|
|
.arg("-M")
|
|
|
|
.arg("--diff-filter=ACDM")
|
|
|
|
.arg("--")
|
|
|
|
.arg(".")
|
|
|
|
.current_dir(repo)
|
|
|
|
.output()?
|
|
|
|
.stdout;
|
|
|
|
let output = String::from_utf8_lossy(&output);
|
|
|
|
let res: u64 = output
|
|
|
|
.lines()
|
|
|
|
.map(|s| {
|
|
|
|
s.split_whitespace()
|
|
|
|
.take(2)
|
|
|
|
.map(str::parse::<u64>)
|
|
|
|
.filter_map(Result::ok)
|
|
|
|
.sum::<u64>()
|
|
|
|
})
|
|
|
|
.sum();
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2019-04-16 19:52:12 +02:00
|
|
|
fn calculate_hoc(
|
|
|
|
service: &str,
|
2019-04-16 16:57:06 +02:00
|
|
|
state: web::Data<State>,
|
|
|
|
data: web::Path<(String, String)>,
|
2019-04-19 16:02:04 +02:00
|
|
|
color: web::Query<BadgeQuery>,
|
2019-04-16 16:57:06 +02:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2019-04-16 19:52:12 +02:00
|
|
|
let service_path = format!("{}/{}/{}", service, data.0, data.1);
|
|
|
|
let path = format!("{}/{}", *state, service_path);
|
2019-04-16 16:57:06 +02:00
|
|
|
let file = Path::new(&path);
|
|
|
|
if !file.exists() {
|
2019-04-19 12:18:16 +02:00
|
|
|
create_dir_all(file)?;
|
|
|
|
let repo = Repository::init_bare(file)?;
|
|
|
|
repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
|
|
|
|
repo.remote_set_url("origin", &format!("https://{}", service_path))?;
|
2019-04-16 16:57:06 +02:00
|
|
|
}
|
2019-04-19 12:18:16 +02:00
|
|
|
pull(&path)?;
|
2019-04-16 16:57:06 +02:00
|
|
|
let hoc = hoc(&path)?;
|
2019-04-19 16:02:04 +02:00
|
|
|
let color = color
|
|
|
|
.into_inner()
|
|
|
|
.color
|
|
|
|
.map(|s| ColorKind::try_from(s.as_str()))
|
|
|
|
.and_then(Result::ok)
|
|
|
|
.unwrap_or_default();
|
2019-04-16 16:57:06 +02:00
|
|
|
let badge_opt = BadgeOptions {
|
|
|
|
subject: "Hits-of-Code".to_string(),
|
2019-04-19 16:02:04 +02:00
|
|
|
color: color.to_code(),
|
2019-04-16 16:57:06 +02:00
|
|
|
status: hoc.to_string(),
|
|
|
|
};
|
|
|
|
let badge = Badge::new(badge_opt)?;
|
2019-04-16 21:37:39 +02:00
|
|
|
|
|
|
|
let (tx, rx_body) = mpsc::unbounded();
|
|
|
|
let _ = tx.unbounded_send(Bytes::from(badge.to_svg().as_bytes()));
|
|
|
|
|
2019-04-18 15:13:58 +02:00
|
|
|
let expiration = SystemTime::now() + Duration::from_secs(30);
|
2019-04-16 22:18:40 +02:00
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type("image/svg+xml")
|
2019-04-18 14:45:57 +02:00
|
|
|
.set(Expires(expiration.into()))
|
2019-04-18 15:13:58 +02:00
|
|
|
.set(CacheControl(vec![
|
|
|
|
CacheDirective::MaxAge(0u32),
|
|
|
|
CacheDirective::MustRevalidate,
|
|
|
|
CacheDirective::NoCache,
|
|
|
|
CacheDirective::NoStore,
|
|
|
|
]))
|
2019-04-19 16:02:04 +02:00
|
|
|
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
|
2019-04-16 16:57:06 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 19:52:12 +02:00
|
|
|
fn github(
|
|
|
|
state: web::Data<State>,
|
|
|
|
data: web::Path<(String, String)>,
|
2019-04-19 16:02:04 +02:00
|
|
|
color: web::Query<BadgeQuery>,
|
2019-04-16 19:52:12 +02:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2019-04-19 16:02:04 +02:00
|
|
|
calculate_hoc("github.com", state, data, color)
|
2019-04-16 19:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn gitlab(
|
|
|
|
state: web::Data<State>,
|
|
|
|
data: web::Path<(String, String)>,
|
2019-04-19 16:02:04 +02:00
|
|
|
color: web::Query<BadgeQuery>,
|
2019-04-16 19:52:12 +02:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2019-04-19 16:02:04 +02:00
|
|
|
calculate_hoc("gitlab.com", state, data, color)
|
2019-04-16 19:52:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bitbucket(
|
|
|
|
state: web::Data<State>,
|
|
|
|
data: web::Path<(String, String)>,
|
2019-04-19 16:02:04 +02:00
|
|
|
color: web::Query<BadgeQuery>,
|
2019-04-16 19:52:12 +02:00
|
|
|
) -> Result<HttpResponse, Error> {
|
2019-04-19 16:02:04 +02:00
|
|
|
calculate_hoc("bitbucket.org", state, data, color)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/badge")]
|
|
|
|
fn badge_example(col: web::Query<BadgeQuery>) -> Result<HttpResponse, Error> {
|
|
|
|
let col = col.into_inner();
|
|
|
|
let color = col
|
|
|
|
.color
|
|
|
|
.clone()
|
|
|
|
.map(|s| ColorKind::try_from(s.as_str()))
|
|
|
|
.transpose()?
|
|
|
|
// .and_then(Result::ok)
|
|
|
|
.unwrap_or_default();
|
|
|
|
let badge_opt = BadgeOptions {
|
|
|
|
subject: "Hits-of-Code".to_string(),
|
|
|
|
color: color.to_code(),
|
|
|
|
status: col.color.unwrap_or_else(|| "success".to_string()),
|
|
|
|
};
|
|
|
|
let badge = Badge::new(badge_opt)?;
|
|
|
|
|
|
|
|
let (tx, rx_body) = mpsc::unbounded();
|
|
|
|
let _ = tx.unbounded_send(Bytes::from(badge.to_svg().as_bytes()));
|
|
|
|
|
|
|
|
let expiration = SystemTime::now() + Duration::from_secs(60 * 60 * 24 * 365);
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type("image/svg+xml")
|
|
|
|
.set(Expires(expiration.into()))
|
|
|
|
.set(CacheControl(vec![CacheDirective::Public]))
|
|
|
|
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
|
2019-04-16 19:52:12 +02:00
|
|
|
}
|
|
|
|
|
2019-04-18 14:32:30 +02:00
|
|
|
fn overview(_: web::Path<(String, String)>) -> HttpResponse {
|
|
|
|
HttpResponse::TemporaryRedirect()
|
|
|
|
.header(http::header::LOCATION, "/")
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
|
2019-04-16 20:55:25 +02:00
|
|
|
#[get("/")]
|
|
|
|
fn index() -> HttpResponse {
|
2019-04-16 21:37:39 +02:00
|
|
|
let (tx, rx_body) = mpsc::unbounded();
|
|
|
|
let _ = tx.unbounded_send(Bytes::from(INDEX.as_bytes()));
|
|
|
|
|
2019-04-17 16:16:50 +02:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("text/html")
|
2019-04-19 16:02:04 +02:00
|
|
|
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request")))
|
2019-04-16 20:55:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 20:48:47 +02:00
|
|
|
#[get("/tacit-css.min.css")]
|
|
|
|
fn css() -> HttpResponse {
|
|
|
|
let (tx, rx_body) = mpsc::unbounded();
|
|
|
|
let _ = tx.unbounded_send(Bytes::from(CSS.as_bytes()));
|
|
|
|
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type("text/css")
|
2019-04-19 16:02:04 +02:00
|
|
|
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request")))
|
2019-04-17 20:48:47 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 16:57:06 +02:00
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
std::env::set_var("RUST_LOG", "actix_web=info");
|
|
|
|
pretty_env_logger::init();
|
|
|
|
openssl_probe::init_ssl_cert_env_vars();
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
let interface = format!("{}:{}", opt.host, opt.port);
|
|
|
|
let state = Arc::new(opt.outdir.display().to_string());
|
|
|
|
HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.data(state.clone())
|
|
|
|
.wrap(middleware::Logger::default())
|
2019-04-16 20:55:25 +02:00
|
|
|
.service(index)
|
2019-04-17 20:48:47 +02:00
|
|
|
.service(css)
|
2019-04-19 16:02:04 +02:00
|
|
|
.service(badge_example)
|
2019-04-16 16:57:06 +02:00
|
|
|
.service(web::resource("/github/{user}/{repo}").to(github))
|
2019-04-16 19:52:12 +02:00
|
|
|
.service(web::resource("/gitlab/{user}/{repo}").to(gitlab))
|
|
|
|
.service(web::resource("/bitbucket/{user}/{repo}").to(bitbucket))
|
2019-04-18 14:32:30 +02:00
|
|
|
.service(web::resource("/view/github/{user}/{repo}").to(overview))
|
|
|
|
.service(web::resource("/view/gitlab/{user}/{repo}").to(overview))
|
|
|
|
.service(web::resource("/view/github/{user}/{repo}").to(overview))
|
2019-04-16 16:57:06 +02:00
|
|
|
})
|
|
|
|
.bind(interface)?
|
|
|
|
.run()
|
|
|
|
}
|