Skip to content

Commit

Permalink
allow inspecting all tags, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika committed Jun 24, 2023
1 parent aab343b commit 0b85249
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 45 deletions.
1 change: 0 additions & 1 deletion src/assets/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ CREATE TABLE IF NOT EXISTS snapshots (
pid INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
created_on DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
-- branch TEXT,
sha TEXT,
timestamp DATETIME
);
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub struct Config {
pub output_dir: PathBuf,
pub verbose: bool,
pub open: bool,
pub tags: bool,
}
2 changes: 1 addition & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ pub fn has_snapshot(conn: &Connection, sha: &str) -> bool {
count > 0
}

pub fn create_ng_version(conn: &Connection, sid: i64, version: &str) -> Result<i64> {
pub fn create_ng_version(conn: &Connection, sid: i64, version: &String) -> Result<i64> {
conn.execute(
"INSERT INTO ng_version (sid, version) VALUES (?1, ?2)",
params![sid, version],
Expand Down
4 changes: 2 additions & 2 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub struct AuthorInfo {
}

pub struct GitProject {
pub working_dir: PathBuf,
repository: Repository,
working_dir: PathBuf,
}

impl GitProject {
Expand All @@ -22,8 +22,8 @@ impl GitProject {
println!("state {:?}", repository.state());

Ok(GitProject {
repository,
working_dir: working_dir.clone(),
repository,
})
}

Expand Down
111 changes: 73 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use ignore::WalkBuilder;
use rusqlite::Connection;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process;

pub async fn run(config: &Config) -> Result<()> {
Expand All @@ -32,8 +33,7 @@ pub async fn run(config: &Config) -> Result<()> {
log::info!("Current branch: {}", project.branch()?);

let conn = db::create_connection(&config.output_dir)?;
let package = PackageJsonFile::from_file(package_json_path)?;
let name = package.name.unwrap();
let name = project_name(&config.working_dir)?;

let pid = match db::get_project_by_name(&conn, &name) {
Ok(project_info) => {
Expand Down Expand Up @@ -67,44 +67,22 @@ pub async fn run(config: &Config) -> Result<()> {
}
};

// todo: load tags
let tags = vec![project.branch()?];

let inspectors: Vec<Box<dyn FileInspector>> = vec![
Box::new(PackageJsonInspector {}),
Box::new(TestInspector {}),
Box::new(AngularInspector {}),
];
// Load all tags or just the current branch
let mut tags = match config.tags {
true => project.tags(),
false => vec![],
};
tags.push(project.branch()?);

log::info!("Processing tags: {:?}", tags);

// TODO: move to a separate func
for tag in tags {
project.checkout(&tag)?;
let sha = project.sha()?;

log::info!("Creating tag {}", tag);
let tag_id = db::create_tag(&conn, pid, &tag)?;

log::info!("Creating new snapshot for tag `{}`({})", tag, sha);
let sid = db::create_snapshot(&conn, pid, tag_id, &project)?;

log::info!("Recording authors");
let authors = &project.authors()?;
db::create_authors(&conn, sid, authors)?;

if let Some(dependencies) = &package.dependencies {
if let Some(version) = dependencies.get("@angular/core") {
db::create_ng_version(&conn, sid, version)?;
}
}

run_inspectors(config, &conn, sid, &inspectors, config.verbose, &project)?;
inspect_tag(&project, pid, &conn, &tag, config.verbose)?;
}

log::info!("Inspection complete");

if config.open {
log::info!("Running web server");
run_server(config.output_dir.to_owned(), true)
.await
.unwrap_or_else(|err| log::error!("{:?}", err));
Expand All @@ -113,15 +91,48 @@ pub async fn run(config: &Config) -> Result<()> {
Ok(())
}

fn inspect_tag(
project: &GitProject,
pid: i64,
conn: &Connection,
tag: &String,
verbose: bool,
) -> Result<()> {
project.checkout(tag)?;
let sha = project.sha()?;

log::info!("Creating tag {}", tag);
let tag_id = db::create_tag(conn, pid, tag)?;

log::info!("Creating new snapshot for tag `{}`({})", tag, sha);
let sid = db::create_snapshot(conn, pid, tag_id, project)?;

log::info!("Recording authors");
let authors = &project.authors()?;
db::create_authors(conn, sid, authors)?;

let package_json_path = &project.working_dir.join("package.json");
let version = angular_version(package_json_path).unwrap_or(String::from("unknown"));
log::info!("Detected Angular version: {}", version);
db::create_ng_version(conn, sid, &version)?;

run_inspectors(&project.working_dir, conn, sid, verbose, project)?;
Ok(())
}

fn run_inspectors(
config: &Config,
connection: &Connection,
working_dir: &PathBuf,
conn: &Connection,
sid: i64,
inspectors: &[Box<dyn FileInspector>],
verbose: bool,
project: &GitProject,
) -> Result<()> {
let working_dir = &config.working_dir;
let inspectors: Vec<Box<dyn FileInspector>> = vec![
Box::new(PackageJsonInspector {}),
Box::new(TestInspector {}),
Box::new(AngularInspector {}),
];

let mut types: HashMap<String, i64> = HashMap::new();

for entry in WalkBuilder::new(working_dir)
Expand Down Expand Up @@ -152,7 +163,7 @@ fn run_inspectors(
url,
};

inspector.inspect_file(connection, &options)?;
inspector.inspect_file(conn, &options)?;
processed = true;
}
}
Expand All @@ -168,8 +179,32 @@ fn run_inspectors(
}

if !types.is_empty() {
db::create_file_types(connection, sid, &types)?;
db::create_file_types(conn, sid, &types)?;
}

Ok(())
}

pub fn project_name(working_dir: &Path) -> Result<String> {
let path = &working_dir.join("package.json");
if !path.exists() {
panic!("Cannot find package.json file");
}

let package = PackageJsonFile::from_file(path)?;
let name = package.name.unwrap();

Ok(name)
}

pub fn angular_version(path: &Path) -> Option<String> {
match PackageJsonFile::from_file(path) {
Ok(package) => match &package.dependencies {
Some(dependencies) => dependencies
.get("angular/core")
.map(|version| version.to_owned()),
None => None,
},
Err(_) => None,
}
}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ enum Commands {
#[arg(short, long)]
output_dir: Option<PathBuf>,

/// Inspect all repository tags
#[arg(long)]
tags: bool,

/// Open report in browser where applicable.
/// Supported for the output formats: html
#[arg(long)]
Expand Down Expand Up @@ -70,6 +74,7 @@ async fn main() -> Result<()> {
verbose,
output_dir,
open,
tags,
}) => {
if working_dir.starts_with("https://") {
let repo_dir = tempdir().expect("Failed creating temporary dir");
Expand Down Expand Up @@ -97,6 +102,7 @@ async fn main() -> Result<()> {
},
verbose: *verbose,
open: *open,
tags: *tags,
};

run(&config).await.unwrap_or_else(|err| {
Expand All @@ -115,6 +121,7 @@ async fn main() -> Result<()> {
},
verbose: *verbose,
open: *open,
tags: *tags,
};

run(&config).await.unwrap_or_else(|err| {
Expand Down
4 changes: 2 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::collections::HashMap;
use std::path::PathBuf;

struct AppState {
app_dir: PathBuf,
// app_dir: PathBuf,
connection: Connection,
}

Expand All @@ -22,7 +22,7 @@ pub async fn run_server(working_dir: PathBuf, open: bool) -> Result<()> {
App::new()
.app_data(web::Data::new(AppState {
connection: db::create_connection(&working_dir).unwrap(),
app_dir: PathBuf::from("static"),
// app_dir: PathBuf::from("static"),
}))
.wrap(middleware::Compress::default())
.wrap(middleware::Logger::default())
Expand Down
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@
<div style="max-height: 500px; overflow-y: scroll">
${rows.map(row => html`
<${LinkRow} caption="${row.name}: ${row.version}"/>
<${LinkRow} caption="${row.name}"/>
`)}
</div>
</nav>
Expand Down

0 comments on commit 0b85249

Please sign in to comment.