|
1 | 1 | use anyhow::{ensure, Context, Result}; |
2 | 2 | use cargo_metadata::MetadataCommand; |
| 3 | +use chrono::Utc; |
3 | 4 | use once_cell::sync::Lazy; |
4 | 5 | use regex::Regex; |
5 | 6 | use rustsec::{advisory::Informational, Advisory, Database}; |
6 | 7 | use rustsec_util::{cargo_unmaintained, command_output, display_advisory_outcomes, Outcome}; |
7 | | -use std::{io::Write, path::Path, process::Command}; |
| 8 | +use std::{env::var, io::Write, path::Path, process::Command}; |
8 | 9 | use strum_macros::{Display, EnumIter}; |
9 | 10 |
|
10 | 11 | // smoelius: See comment in rustsec_issues.rs regarding "../../../". |
11 | 12 | #[path = "../../../src/packaging.rs"] |
12 | 13 | mod packaging; |
13 | 14 | use packaging::temp_package; |
14 | 15 |
|
15 | | -#[derive(Display, EnumIter, Eq, PartialEq)] |
| 16 | +#[derive(Clone, Copy, Display, EnumIter, Eq, PartialEq)] |
16 | 17 | #[strum(serialize_all = "kebab_case")] |
17 | 18 | enum Reason { |
18 | 19 | Error, |
@@ -118,17 +119,62 @@ fn main() -> Result<()> { |
118 | 119 |
|
119 | 120 | display_advisory_outcomes( |
120 | 121 | &advisory_outcomes |
121 | | - .into_iter() |
| 122 | + .iter() |
122 | 123 | .map(|(advisory, outcome)| { |
123 | | - let url = advisory_url(&advisory); |
124 | | - (advisory.metadata.package, url, outcome) |
| 124 | + let url = advisory_url(advisory); |
| 125 | + (&advisory.metadata.package, url, *outcome) |
125 | 126 | }) |
126 | 127 | .collect::<Vec<_>>(), |
127 | 128 | ); |
128 | 129 |
|
| 130 | + if var("GITHUB_TOKEN_PATH").is_ok() { |
| 131 | + println!("---"); |
| 132 | + display_expected_readme_contents( |
| 133 | + &advisory_outcomes |
| 134 | + .iter() |
| 135 | + .map(|&(_, outcome)| outcome) |
| 136 | + .collect::<Vec<_>>(), |
| 137 | + ); |
| 138 | + } |
| 139 | + |
129 | 140 | Ok(()) |
130 | 141 | } |
131 | 142 |
|
| 143 | +macro_rules! count { |
| 144 | + ($outcomes:expr, $pat:pat) => { |
| 145 | + $outcomes |
| 146 | + .iter() |
| 147 | + .filter(|outcome| matches!(outcome, $pat)) |
| 148 | + .count() |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +fn display_expected_readme_contents(outcomes: &[Outcome<Reason>]) { |
| 153 | + let today = Utc::now().date_naive(); |
| 154 | + let count = outcomes.len(); |
| 155 | + let found = count!(outcomes, Outcome::Found); |
| 156 | + let not_found = count!(outcomes, Outcome::NotFound(_)); |
| 157 | + let error = count!(outcomes, Outcome::NotFound(Reason::Error)); |
| 158 | + let leaf = count!(outcomes, Outcome::NotFound(Reason::Leaf)); |
| 159 | + let recently_updated = count!(outcomes, Outcome::NotFound(Reason::RecentlyUpdated)); |
| 160 | + let other = count!(outcomes, Outcome::NotFound(Reason::Other)); |
| 161 | + assert!(found * 3 > count * 2); |
| 162 | + println!( |
| 163 | + "As of {today}, the RustSec Advisory Database contains {count} active advisories for \ |
| 164 | + unmaintained packages. Using the above conditions, `cargo-unmaintained` automatically \ |
| 165 | + identifies {found} of them (more than two thirds). These results can be reproduced by \ |
| 166 | + running the [`rustsec_advisories`] binary within this repository.", |
| 167 | + ); |
| 168 | + println!( |
| 169 | + "- Of the {not_found} packages in the RustSec Advisory Database _not_ identified by \ |
| 170 | + `cargo-unmaintained`:" |
| 171 | + ); |
| 172 | + println!(" - {error} do not build"); |
| 173 | + println!(" - {leaf} are existent, unarchived leaves"); |
| 174 | + println!(" - {recently_updated} were updated within the past 365 days"); |
| 175 | + println!(" - {other} were not identified for other reasons",); |
| 176 | +} |
| 177 | + |
132 | 178 | fn advisory_url(advisory: &Advisory) -> String { |
133 | 179 | format!("https://rustsec.org/advisories/{}.html", advisory.id()) |
134 | 180 | } |
|
0 commit comments