Skip to content

Commit

Permalink
Implement --overrides flag for package and trust review
Browse files Browse the repository at this point in the history
  • Loading branch information
dpc committed Jan 21, 2022
1 parent 6dd7c8d commit 12d2517
Show file tree
Hide file tree
Showing 16 changed files with 620 additions and 135 deletions.
2 changes: 2 additions & 0 deletions cargo-crev/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Versioning](https://semver.org/spec/v2.0.0.html).
- Deprecate `--no-dev-dependencies`. Make it the default. Introduce `--dev-dependencies` instead.
- Fix binary releases by switching to Github Actions
- Make `crate {goto,dir,open,expand}` assume `-u` outside of an existing Rust project.
- Introduce trust and package review "overrides" which allow overriding (ignoring) specific
trust / package review

## [0.22.2](https://github.com/dpc/crev/compare/cargo-crev-v0.21.4...v0.22.2) - 2022-01-11

Expand Down
6 changes: 4 additions & 2 deletions cargo-crev/src/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ pub fn edit_proof_content_iteractively<C: proof::ContentWithDraft>(
content: &C,
previous_date: Option<&proof::Date>,
base_version: Option<&crev_data::Version>,
extra_comment: Option<&str>,
extra_leading_comment: Option<&str>,
extra_follow_content_fn: impl FnOnce(&mut String) -> Result<()>,
) -> Result<C> {
let mut text = String::new();
if let Some(date) = previous_date {
Expand All @@ -102,13 +103,14 @@ pub fn edit_proof_content_iteractively<C: proof::ContentWithDraft>(
let draft = content.to_draft();

writeln!(&mut text, "# {}", draft.title())?;
if let Some(extra_comment) = extra_comment {
if let Some(extra_comment) = extra_leading_comment {
writeln!(&mut text, "# {}", extra_comment)?;
}
if let Some(base_version) = base_version {
writeln!(&mut text, "# Diff base version: {}", base_version)?;
}
text.write_str(draft.body())?;
(extra_follow_content_fn)(&mut text)?;
text.write_str("\n\n")?;
for line in get_documentation_for(content).lines() {
writeln!(&mut text, "# {}", line)?;
Expand Down
65 changes: 55 additions & 10 deletions cargo-crev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crev_lib::id::LockedId;
use crev_lib::{self, local::Local};
use std::{
collections::{HashMap, HashSet},
io::{self, BufRead, Write},
fmt::Write as _,
io::{self, BufRead, Write as _},
panic,
path::PathBuf,
};
Expand Down Expand Up @@ -138,6 +139,7 @@ fn crate_review(args: opts::CrateReview) -> Result<()> {
&args.common_proof_create,
&args.diff,
args.skip_activity_check || is_advisory || args.issue,
args.overrides,
args.cargo_opts.clone(),
)?;
let has_public_url = local
Expand Down Expand Up @@ -196,18 +198,22 @@ fn print_ids<'a>(
Ok(())
}

fn url_to_status_str<'a>(id_url: &UrlOfId<'a>) -> (&'static str, &'a str) {
match id_url {
UrlOfId::None => ("", ""),
UrlOfId::FromSelfVerified(url) => ("==", url.url.as_str()),
UrlOfId::FromSelf(url) => ("~=", url.url.as_str()),
UrlOfId::FromOthers(url) => ("??", url.url.as_str()),
}
}

fn print_mvp_ids<'a>(
ids: impl Iterator<Item = (&'a Id, u64)>,
trust_set: &TrustSet,
db: &ProofDB,
) -> Result<()> {
for (id, count) in ids {
let (status, url) = match db.lookup_url(id) {
UrlOfId::None => ("", ""),
UrlOfId::FromSelfVerified(url) => ("==", url.url.as_str()),
UrlOfId::FromSelf(url) => ("~=", url.url.as_str()),
UrlOfId::FromOthers(url) => ("??", url.url.as_str()),
};
let (status, url) = url_to_status_str(&db.lookup_url(id));
println!(
"{:>3} {} {:6} {} {}",
count,
Expand Down Expand Up @@ -296,6 +302,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
&args.common_proof_create,
args.level.unwrap_or(TrustLevel::Medium),
args.level.is_none(),
args.overrides,
)?;
}
opts::Id::Untrust(args) => {
Expand All @@ -304,6 +311,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
&args.common_proof_create,
TrustLevel::None,
true,
args.overrides,
)?;
}
opts::Id::Distrust(args) => {
Expand All @@ -312,6 +320,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
&args.common_proof_create,
TrustLevel::Distrust,
true,
args.overrides,
)?;
}
opts::Id::Query(cmd) => match cmd {
Expand Down Expand Up @@ -427,6 +436,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
&args.common_proof_create,
args.level.unwrap_or(TrustLevel::Medium),
args.level.is_none(),
args.overrides,
)?;
// Make sure we have reviews for the new Ids we're trusting
local.fetch_new_trusted(Default::default(), None)?;
Expand Down Expand Up @@ -496,6 +506,7 @@ fn run_command(command: opts::Command) -> Result<CommandExitStatus> {
&args.common_proof_create,
&args.diff,
args.skip_activity_check || is_advisory || args.issue,
args.overrides,
args.cargo_opts.clone(),
)
})?;
Expand Down Expand Up @@ -762,22 +773,56 @@ fn set_trust_level_for_ids(
common_proof_create: &crate::opts::CommonProofCreate,
trust_level: TrustLevel,
edit_interactively: bool,
show_override_suggestions: bool,
) -> Result<()> {
let local = ensure_crev_id_exists_or_make_one()?;
let unlocked_id = local.read_current_unlocked_id(&term::read_passphrase)?;

let mut trust =
local.build_trust_proof(unlocked_id.as_public_id(), ids.to_vec(), trust_level)?;
let overrides = if ids.len() == 1 {
let db = local.load_db()?;

db.get_trust_proof_between(&unlocked_id.id.id, &ids[0])
.map(|trust_proof| trust_proof.override_.clone())
.unwrap_or(vec![])
} else {
vec![]
};

let mut trust = local.build_trust_proof(
unlocked_id.as_public_id(),
ids.to_vec(),
trust_level,
overrides,
)?;

if edit_interactively {
let extra_comment = if trust_level == TrustLevel::Distrust {
Some("WARNING: Distrust has severe consequences. Read documentation below.")
} else {
None
};
trust = edit::edit_proof_content_iteractively(&trust, None, None, extra_comment)?;
trust = edit::edit_proof_content_iteractively(&trust, None, None, extra_comment, |text| {
if show_override_suggestions && trust.override_.is_empty() {
writeln!(text, "# override:")?;
}

if show_override_suggestions {
let db = local.load_db()?;
for (id, trust_level) in ids.into_iter().flat_map(|id| db.get_reverse_trust_for(id))
{
let (status, url) = url_to_status_str(&db.lookup_url(id));
writeln!(text, "# - id-type: {}", "crev")?; // TODO: support other ids?
writeln!(text, "# id: {} # level: {}", id, trust_level)?;
writeln!(text, "# url: {} # {}", url, status)?;
writeln!(text, "# comment: \"\"")?;
}
}

Ok(())
})?;
}

trust.touch_date();
let proof = trust.sign_by(&unlocked_id)?;

if common_proof_create.print_unsigned {
Expand Down
12 changes: 12 additions & 0 deletions cargo-crev/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ pub struct CrateVerify {

#[derive(Debug, StructOpt, Clone)]
pub struct IdTrust {
#[structopt(long = "overrides")]
/// Enable overrides suggestions
pub overrides: bool,

/// Public IDs to create Trust Proof for
pub public_ids: Vec<String>,

Expand All @@ -410,6 +414,10 @@ pub struct IdTrust {

#[derive(Debug, StructOpt, Clone)]
pub struct TrustUrls {
#[structopt(long = "overrides")]
/// Enable overrides suggestions
pub overrides: bool,

/// Public IDs or proof repo URLs to create Trust Proof for
pub public_ids_or_urls: Vec<String>,

Expand Down Expand Up @@ -618,6 +626,10 @@ pub struct CrateReview {
#[structopt(long = "skip-activity-check")]
pub skip_activity_check: bool,

#[structopt(long = "overrides")]
/// Enable overrides suggestions
pub overrides: bool,

/// Review the delta since the given version
#[structopt(long = "diff", name = "base-version")]
#[allow(clippy::option_option)]
Expand Down
Loading

0 comments on commit 12d2517

Please sign in to comment.