Implement gitlab and bitbucket support

This commit is contained in:
Valentin Brandl 2019-04-16 19:52:12 +02:00
parent e8fd78c517
commit 5944681086
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D

View File

@ -48,7 +48,7 @@ impl std::fmt::Display for Error {
match self { match self {
Error::Git(e) => write!(fmt, "Git({})", e), Error::Git(e) => write!(fmt, "Git({})", e),
Error::Io(e) => write!(fmt, "Io({})", e), Error::Io(e) => write!(fmt, "Io({})", e),
Error::Badge(s) => write!(fmt, "Basge({})", s), Error::Badge(s) => write!(fmt, "Badge({})", s),
Error::Internal => write!(fmt, "Internal"), Error::Internal => write!(fmt, "Internal"),
} }
} }
@ -121,15 +121,16 @@ fn hoc(repo: &str) -> Result<u64, Error> {
Ok(res) Ok(res)
} }
fn github( fn calculate_hoc(
service: &str,
state: web::Data<State>, state: web::Data<State>,
data: web::Path<(String, String)>, data: web::Path<(String, String)>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let gh_path = format!("github.com/{}/{}", data.0, data.1); let service_path = format!("{}/{}/{}", service, data.0, data.1);
let path = format!("{}/{}", *state, gh_path); let path = format!("{}/{}", *state, service_path);
let file = Path::new(&path); let file = Path::new(&path);
if !file.exists() { if !file.exists() {
Repository::clone(&format!("https://{}", gh_path), file)?; Repository::clone(&format!("https://{}", service_path), file)?;
} else { } else {
pull(&path)?; pull(&path)?;
} }
@ -145,6 +146,27 @@ fn github(
.body(badge.to_svg())) .body(badge.to_svg()))
} }
fn github(
state: web::Data<State>,
data: web::Path<(String, String)>,
) -> Result<HttpResponse, Error> {
calculate_hoc("github.com", state, data)
}
fn gitlab(
state: web::Data<State>,
data: web::Path<(String, String)>,
) -> Result<HttpResponse, Error> {
calculate_hoc("gitlab.com", state, data)
}
fn bitbucket(
state: web::Data<State>,
data: web::Path<(String, String)>,
) -> Result<HttpResponse, Error> {
calculate_hoc("bitbucket.org", state, data)
}
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info"); std::env::set_var("RUST_LOG", "actix_web=info");
pretty_env_logger::init(); pretty_env_logger::init();
@ -157,6 +179,8 @@ fn main() -> std::io::Result<()> {
.data(state.clone()) .data(state.clone())
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(web::resource("/github/{user}/{repo}").to(github)) .service(web::resource("/github/{user}/{repo}").to(github))
.service(web::resource("/gitlab/{user}/{repo}").to(gitlab))
.service(web::resource("/bitbucket/{user}/{repo}").to(bitbucket))
}) })
.bind(interface)? .bind(interface)?
.run() .run()