Compare commits

..

2 Commits

Author SHA1 Message Date
bdae157caf Bump serde from 1.0.100 to 1.0.101
Some checks reported errors
continuous-integration/drone/push Build was killed
Bumps [serde](https://github.com/serde-rs/serde) from 1.0.100 to 1.0.101.
- [Release notes](https://github.com/serde-rs/serde/releases)
- [Commits](https://github.com/serde-rs/serde/compare/v1.0.100...v1.0.101)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-10-14 15:26:08 +02:00
d6618a0e61 Bump serde_derive from 1.0.100 to 1.0.101
Bumps [serde_derive](https://github.com/serde-rs/serde) from 1.0.100 to 1.0.101.
- [Release notes](https://github.com/serde-rs/serde/releases)
- [Commits](https://github.com/serde-rs/serde/compare/v1.0.100...v1.0.101)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
2019-10-14 15:26:08 +02:00
11 changed files with 1731 additions and 1581 deletions

View File

@ -1,31 +0,0 @@
name: Security audit
on:
schedule:
- cron: '0 1 * * *'
push:
paths:
- 'Cargo.toml'
- 'Cargo.lock'
pull_request:
jobs:
security_audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: /usr/share/rust/.cargo/registry
key: ${{ runner.os }}-cargo-registry
- name: Cache cargo index
uses: actions/cache@v1
with:
path: /usr/share/rust/.cargo/git
key: ${{ runner.os }}-cargo-index
- uses: actions-rs/audit-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}

View File

@ -1,94 +1,15 @@
name: Rust
on: [push, pull_request]
on: [push]
jobs:
lint:
name: Linting and Formatting Checks
build:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v1
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Install rustfmt
run: rustup component add rustfmt
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
- name: Check Formatting
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
- name: Install clippy
run: rustup component add clippy
- name: Clippy Linting
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
test:
name: Run Tests
runs-on: ${{ matrix.os }}
strategy:
matrix:
# add windows-latest when it is clear why tests are failing
os: [ubuntu-latest, macos-latest]
steps:
- name: Checkout sources
uses: actions/checkout@v1
- name: Install stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
- name: Run Tests
uses: actions-rs/cargo@v1
with:
command: test
- uses: actions/checkout@v1
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

2916
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,28 +1,27 @@
[package]
name = "hoc"
version = "0.11.5"
version = "0.11.3"
authors = ["Valentin Brandl <vbrandl@riseup.net>"]
edition = "2018"
build = "build.rs"
[dependencies]
actix-web = "2.0.0"
actix-web = "1.0.7"
badge = "0.2.0"
bytes = "0.5.4"
futures = "0.3.1"
git2 = "0.11.0"
bytes = "0.4.12"
futures = "0.1.29"
git2 = "0.9.1"
lazy_static = "1.4.0"
log = "0.4.8"
log4rs = "0.10.0"
log4rs = "0.8.3"
number_prefix = "0.3.0"
openssl-probe = "0.1.2"
reqwest = "0.10.1"
serde = "1.0.104"
serde_derive = "1.0.103"
serde_json = "1.0.47"
structopt = "0.3.9"
actix-rt = "1.0.0"
reqwest = "0.9.20"
serde = "1.0.101"
serde_derive = "1.0.101"
serde_json = "1.0.40"
structopt = "0.3.1"
[build-dependencies]
ructe = "0.9.2"
ructe = "0.7.2"
vergen = "3.0.4"

View File

@ -48,7 +48,7 @@ pub(crate) struct Opt {
pub(crate) logfile: PathBuf,
}
pub(crate) async fn init() -> Result<()> {
pub(crate) fn init() -> Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info,hoc=info");
// pretty_env_logger::init();
openssl_probe::init_ssl_cert_env_vars();

View File

@ -18,7 +18,6 @@ pub(crate) enum Error {
LogBuilder(log4rs::config::Errors),
Parse(std::num::ParseIntError),
Serial(serde_json::Error),
GitNoMaster,
}
impl fmt::Display for Error {
@ -33,7 +32,6 @@ impl fmt::Display for Error {
Error::LogBuilder(e) => write!(fmt, "LogBuilder({})", e),
Error::Parse(e) => write!(fmt, "Parse({})", e),
Error::Serial(e) => write!(fmt, "Serial({})", e),
Error::GitNoMaster => write!(fmt, "Repo doesn't have master branch"),
}
}
}
@ -41,20 +39,14 @@ impl fmt::Display for Error {
impl ResponseError for Error {
fn error_response(&self) -> HttpResponse {
let mut buf = Vec::new();
match self {
Error::GitNoMaster => {
templates::p404_no_master(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed)).unwrap();
HttpResponse::NotFound()
.content_type("text/html")
.body(buf)
},
_ => {
templates::p500(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed)).unwrap();
HttpResponse::InternalServerError()
.content_type("text/html")
.body(buf)
}
}
templates::p500(&mut buf, VERSION_INFO, REPO_COUNT.load(Ordering::Relaxed)).unwrap();
HttpResponse::InternalServerError()
.content_type("text/html")
.body(buf)
}
fn render_response(&self) -> HttpResponse {
self.error_response()
}
}

View File

@ -15,21 +15,21 @@ mod count;
mod error;
mod service;
mod statics;
mod template;
use crate::{
cache::CacheState,
error::{Error, Result},
service::{Bitbucket, FormService, GitHub, Gitlab, Service},
statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO},
template::RepoInfo,
};
use actix_web::{
error::ErrorBadRequest,
http::header::{CacheControl, CacheDirective, Expires},
middleware, web, App, HttpResponse, HttpServer,
};
use badge::{Badge, BadgeOptions};
use futures::future::Future;
use bytes::Bytes;
use futures::{unsync::mpsc, Future, Stream};
use git2::Repository;
use number_prefix::{NumberPrefix, Prefixed, Standalone};
use std::{
@ -75,12 +75,7 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String, u64)
let cache_dir = format!("{}/{}.json", cache_dir, repo);
let cache_dir = Path::new(&cache_dir);
let repo = Repository::open_bare(&repo_dir)?;
// TODO: do better...
let head = match repo.head() {
Ok(v) => v,
Err(_) => return Err(Error::GitNoMaster),
};
let head = format!("{}", head.target().ok_or(Error::Internal)?);
let head = format!("{}", repo.head()?.target().ok_or(Error::Internal)?);
let mut arg_commit_count = vec!["rev-list".to_string(), "--count".to_string()];
let mut arg = vec![
"log".to_string(),
@ -142,12 +137,12 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String, u64)
Ok((cache.count, head, commits))
}
async fn remote_exists(url: &str) -> Result<bool> {
let resp = CLIENT.head(url).send().await?;
Ok(resp.status() == reqwest::StatusCode::OK)
// .map(|resp| resp.status() == reqwest::StatusCode::OK)
// .from_err()
fn remote_exists(url: &str) -> impl Future<Item = bool, Error = Error> {
CLIENT
.head(url)
.send()
.map(|resp| resp.status() == reqwest::StatusCode::OK)
.from_err()
}
enum HocResult {
@ -163,11 +158,11 @@ enum HocResult {
NotFound,
}
async fn handle_hoc_request<T, F>(
fn handle_hoc_request<T, F>(
state: web::Data<Arc<State>>,
data: web::Path<(String, String)>,
mapper: F,
) -> Result<HttpResponse>
) -> impl Future<Item = HttpResponse, Error = Error>
where
T: Service,
F: Fn(HocResult) -> Result<HttpResponse>,
@ -176,42 +171,44 @@ where
let service_path = format!("{}/{}", T::domain(), repo);
let path = format!("{}/{}", state.repos, service_path);
let url = format!("https://{}", service_path);
let remote_exists = remote_exists(&url).await?;
let file = Path::new(&path);
if !file.exists() {
if !remote_exists {
warn!("Repository does not exist: {}", url);
return mapper(HocResult::NotFound);
}
info!("Cloning {} for the first time", url);
create_dir_all(file)?;
let repo = Repository::init_bare(file)?;
repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
repo.remote_set_url("origin", &url)?;
REPO_COUNT.fetch_add(1, Ordering::Relaxed);
}
pull(&path)?;
let (hoc, head, commits) = hoc(&service_path, &state.repos, &state.cache)?;
let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
Standalone(hoc) => hoc.to_string(),
Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
};
let res = HocResult::Hoc {
hoc,
commits,
hoc_pretty,
head,
url,
repo,
service_path,
};
mapper(res)
remote_exists(&url)
.and_then(move |remote_exists| {
let file = Path::new(&path);
if !file.exists() {
if !remote_exists {
warn!("Repository does not exist: {}", url);
return Ok(HocResult::NotFound);
}
info!("Cloning {} for the first time", url);
create_dir_all(file)?;
let repo = Repository::init_bare(file)?;
repo.remote_add_fetch("origin", "refs/heads/*:refs/heads/*")?;
repo.remote_set_url("origin", &url)?;
REPO_COUNT.fetch_add(1, Ordering::Relaxed);
}
pull(&path)?;
let (hoc, head, commits) = hoc(&service_path, &state.repos, &state.cache)?;
let hoc_pretty = match NumberPrefix::decimal(hoc as f64) {
Standalone(hoc) => hoc.to_string(),
Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix),
};
Ok(HocResult::Hoc {
hoc,
commits,
hoc_pretty,
head: head.to_string(),
url,
repo,
service_path,
})
})
.and_then(mapper)
}
fn json_hoc<T: Service>(
state: web::Data<Arc<State>>,
data: web::Path<(String, String)>,
) -> impl Future<Output = Result<HttpResponse>> {
) -> impl Future<Item = HttpResponse, Error = Error> {
let mapper = |r| match r {
HocResult::NotFound => p404(),
HocResult::Hoc {
@ -228,8 +225,8 @@ fn json_hoc<T: Service>(
fn calculate_hoc<T: Service>(
state: web::Data<Arc<State>>,
data: web::Path<(String, String)>,
) -> impl Future<Output = Result<HttpResponse>> {
let mapper = move |r| match r {
) -> impl Future<Item = HttpResponse, Error = Error> {
let mapper = |r| match r {
HocResult::NotFound => p404(),
HocResult::Hoc { hoc_pretty, .. } => {
let badge_opt = BadgeOptions {
@ -238,8 +235,9 @@ fn calculate_hoc<T: Service>(
status: hoc_pretty,
};
let badge = Badge::new(badge_opt)?;
// TODO: remove clone
let body = badge.to_svg().as_bytes().to_vec();
let (tx, rx_body) = mpsc::unbounded();
let _ = tx.unbounded_send(Bytes::from(badge.to_svg().as_bytes()));
let expiration = SystemTime::now() + Duration::from_secs(30);
Ok(HttpResponse::Ok()
@ -251,7 +249,7 @@ fn calculate_hoc<T: Service>(
CacheDirective::NoCache,
CacheDirective::NoStore,
]))
.body(body))
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
}
};
handle_hoc_request::<T, _>(state, data, mapper)
@ -260,7 +258,7 @@ fn calculate_hoc<T: Service>(
fn overview<T: Service>(
state: web::Data<Arc<State>>,
data: web::Path<(String, String)>,
) -> impl Future<Output = Result<HttpResponse>> {
) -> impl Future<Item = HttpResponse, Error = Error> {
let mapper = |r| match r {
HocResult::NotFound => p404(),
HocResult::Hoc {
@ -273,31 +271,33 @@ fn overview<T: Service>(
service_path,
} => {
let mut buf = Vec::new();
let repo_info = RepoInfo {
commit_url: &T::commit_url(&repo, &head),
commits,
domain: &OPT.domain,
head: &head,
hoc,
hoc_pretty: &hoc_pretty,
path: &service_path,
url: &url,
};
templates::overview(
&mut buf,
VERSION_INFO,
REPO_COUNT.load(Ordering::Relaxed),
repo_info,
&OPT.domain,
&service_path,
&url,
hoc,
&hoc_pretty,
&head,
&T::commit_url(&repo, &head),
commits,
)?;
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
let (tx, rx_body) = mpsc::unbounded();
let _ = tx.unbounded_send(Bytes::from(buf));
Ok(HttpResponse::Ok()
.content_type("text/html")
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
}
};
handle_hoc_request::<T, _>(state, data, mapper)
}
#[get("/")]
async fn index() -> Result<HttpResponse> {
fn index() -> Result<HttpResponse> {
let mut buf = Vec::new();
templates::index(
&mut buf,
@ -309,7 +309,7 @@ async fn index() -> Result<HttpResponse> {
}
#[post("/generate")]
async fn generate(params: web::Form<GeneratorForm<'_>>) -> Result<HttpResponse> {
fn generate(params: web::Form<GeneratorForm>) -> Result<HttpResponse> {
let repo = format!("{}/{}", params.user, params.repo);
let mut buf = Vec::new();
templates::generate(
@ -321,8 +321,12 @@ async fn generate(params: web::Form<GeneratorForm<'_>>) -> Result<HttpResponse>
params.service.service(),
&repo,
)?;
let (tx, rx_body) = mpsc::unbounded();
let _ = tx.unbounded_send(Bytes::from(buf));
Ok(HttpResponse::Ok().content_type("text/html").body(buf))
Ok(HttpResponse::Ok()
.content_type("text/html")
.streaming(rx_body.map_err(|_| ErrorBadRequest("bad request"))))
}
fn p404() -> Result<HttpResponse> {
@ -331,10 +335,6 @@ fn p404() -> Result<HttpResponse> {
Ok(HttpResponse::NotFound().content_type("text/html").body(buf))
}
async fn async_p404() -> Result<HttpResponse> {
p404()
}
#[get("/tacit-css.min.css")]
fn css() -> HttpResponse {
HttpResponse::Ok().content_type("text/css").body(CSS)
@ -345,13 +345,13 @@ fn favicon32() -> HttpResponse {
HttpResponse::Ok().content_type("image/png").body(FAVICON)
}
async fn start_server() -> std::io::Result<()> {
fn start_server() -> Result<()> {
let interface = format!("{}:{}", OPT.host, OPT.port);
let state = Arc::new(State {
repos: OPT.outdir.display().to_string(),
cache: OPT.cachedir.display().to_string(),
});
HttpServer::new(move || {
Ok(HttpServer::new(move || {
App::new()
.data(state.clone())
.wrap(middleware::Logger::default())
@ -360,25 +360,23 @@ async fn start_server() -> std::io::Result<()> {
.service(css)
.service(favicon32)
.service(generate)
.service(web::resource("/github/{user}/{repo}").to(calculate_hoc::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}").to(calculate_hoc::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}").to(calculate_hoc::<Bitbucket>))
.service(web::resource("/github/{user}/{repo}/json").to(json_hoc::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}/json").to(json_hoc::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}/json").to(json_hoc::<Bitbucket>))
.service(web::resource("/view/github/{user}/{repo}").to(overview::<GitHub>))
.service(web::resource("/view/gitlab/{user}/{repo}").to(overview::<Gitlab>))
.service(web::resource("/view/bitbucket/{user}/{repo}").to(overview::<Bitbucket>))
.default_service(web::resource("").route(web::get().to(async_p404)))
.service(web::resource("/github/{user}/{repo}").to_async(calculate_hoc::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}").to_async(calculate_hoc::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}").to_async(calculate_hoc::<Bitbucket>))
.service(web::resource("/github/{user}/{repo}/json").to_async(json_hoc::<GitHub>))
.service(web::resource("/gitlab/{user}/{repo}/json").to_async(json_hoc::<Gitlab>))
.service(web::resource("/bitbucket/{user}/{repo}/json").to_async(json_hoc::<Bitbucket>))
.service(web::resource("/view/github/{user}/{repo}").to_async(overview::<GitHub>))
.service(web::resource("/view/gitlab/{user}/{repo}").to_async(overview::<Gitlab>))
.service(web::resource("/view/bitbucket/{user}/{repo}").to_async(overview::<Bitbucket>))
.default_service(web::resource("").route(web::get().to_async(p404)))
})
.workers(OPT.workers)
.bind(interface)?
.run()
.await
.run()?)
}
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
config::init().await.unwrap();
start_server().await
fn main() -> Result<()> {
config::init()?;
start_server()
}

View File

@ -15,7 +15,7 @@ pub(crate) const CSS: &str = include_str!("../static/tacit-css.min.css");
pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png");
lazy_static! {
pub(crate) static ref CLIENT: reqwest::Client = reqwest::Client::new();
pub(crate) static ref CLIENT: reqwest::r#async::Client = reqwest::r#async::Client::new();
pub(crate) static ref OPT: Opt = Opt::from_args();
pub(crate) static ref REPO_COUNT: AtomicUsize =
AtomicUsize::new(count_repositories(&OPT.outdir).unwrap());

View File

@ -1,10 +0,0 @@
pub struct RepoInfo<'a> {
pub commit_url: &'a str,
pub commits: u64,
pub domain: &'a str,
pub head: &'a str,
pub hoc: u64,
pub hoc_pretty: &'a str,
pub path: &'a str,
pub url: &'a str,
}

View File

@ -1,16 +1,13 @@
@use super::base;
@use crate::statics::VersionInfo;
@use crate::template::RepoInfo;
@(version_info: VersionInfo, repo_count: usize, repo_info: RepoInfo)
@(version_info: VersionInfo, repo_count: usize, domain: &str, path: &str, url: &str, hoc: u64, hoc_pretty: &str, head: &str, commit_url: &str, commits: u64)
@:base("Hits-of-Code Badges", "Overview", {
<p>
The project <a href="@repo_info.url">@repo_info.url</a> has
<strong>@repo_info.hoc_pretty</strong> (exactly @repo_info.hoc) hits of code at
<a href="@repo_info.commit_url">@repo_info.head</a>. The repository contains
<strong>@repo_info.commits</strong> commits.
The project <a href="@url">@url</a> has <strong>@hoc_pretty</strong> (exactly @hoc) hits of code at
<a href="@commit_url">@head</a>. The repository contains <strong>@commits</strong> commits.
</p>
<p>
@ -18,6 +15,6 @@ To include the badge in your readme, use the following markdown:
</p>
<pre>
[![Hits-of-Code](https://@repo_info.domain/@repo_info.path)](https://@repo_info.domain/view/@repo_info.path)
[![Hits-of-Code](https://@domain/@path)](https://@domain/view/@path)
</pre>
}, version_info, repo_count)

View File

@ -1,16 +0,0 @@
@use super::base;
@use crate::statics::VersionInfo;
@(version_info: VersionInfo, repo_count: usize)
@:base("Master Branch not Found - Hits-of-Code Badges", "404 - Master Branch not Found", {
<p>
<big>Sorry</big>. I couldn't find the master branch of your repositroy.
Currently this service depends on the existence of a master branch. Please go
<a href="/">back to the homepage</a>.
</p>
<p>
If you think, this is a mistake on my side, please <a href="mailto:mail+hoc@@vbrandl.net">drop me a mail</a>.
</p>
}, version_info, repo_count)