Skip to content

Commit

Permalink
add hyperlink to rustsec advisory
Browse files Browse the repository at this point in the history
  • Loading branch information
nasifimtiazohi committed Jul 22, 2021
1 parent 8dde996 commit 542ea7e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
29 changes: 29 additions & 0 deletions depdive/src/ghcomment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use anyhow::Result;
use markdown_gen::markdown::{AsMarkdown, List, Markdown};
use std::cell::RefCell;
use url::Url;

#[derive(PartialEq)]
#[allow(dead_code)]
Expand Down Expand Up @@ -89,6 +90,16 @@ impl GitHubCommentGenerator {
Ok(())
}

pub fn add_hyperlinked_list<T: AsRef<str>>(
&self,
items: &[(T, Option<Url>)],
list_style: ListStyle,
) -> Result<()> {
self.append_comment(&Self::get_hyperlinked_list(items, list_style)?);
self.add_newline(2);
Ok(())
}

pub fn get_list<T: AsRef<str>>(
items: &[T],
list_style: ListStyle,
Expand Down Expand Up @@ -117,6 +128,24 @@ impl GitHubCommentGenerator {
Self::extract_markdown_generator(md)
}

pub fn get_hyperlinked_list<T: AsRef<str>>(
items: &[(T, Option<Url>)],
list_style: ListStyle,
) -> Result<String> {
let mut md = Self::get_markdown_generator();
let mut list = List::new(list_style == ListStyle::Numbered);
for (body, url) in items {
let body = body.as_ref();
if let Some(url) = url.as_ref() {
list = list.item(body.link_to(url.as_ref()));
} else {
list = list.item(body);
}
}
md.write(list)?;
Self::extract_markdown_generator(md)
}

pub fn add_collapsible_section(&self, title: &str, body: &str) {
self.append_comment(&Self::get_collabsible_section(title, body));
self.add_newline(2);
Expand Down
13 changes: 7 additions & 6 deletions depdive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use guppy::MetadataCommand;
use separator::Separatable;
use std::path::Path;
use tabled::Tabled;
use url::Url;

mod advisory;
mod code;
Expand Down Expand Up @@ -341,28 +342,28 @@ impl UpdateAnalyzer {
),
]);
if !report.updated_version.known_advisories.is_empty() {
let ids: Vec<String> = report
let ids: Vec<(String, Option<Url>)> = report
.updated_version
.known_advisories
.iter()
.map(|a| a.id.clone())
.map(|a| (a.id.clone(), a.url.clone()))
.collect();
gh.add_collapsible_section(
":bomb: The updated version contains known advisories",
&GitHubCommentGenerator::get_list(&ids, Bulleted, Code)?,
&GitHubCommentGenerator::get_hyperlinked_list(&ids, Bulleted)?,
);
}
let fixed_advisories: Vec<String> = report
let fixed_advisories: Vec<(String, Option<Url>)> = report
.prior_version
.known_advisories
.iter()
.filter(|a| !report.updated_version.known_advisories.contains(a))
.map(|a| a.id.clone())
.map(|a| (a.id.clone(), a.url.clone()))
.collect();
if !fixed_advisories.is_empty() {
gh.add_collapsible_section(
":tada: This update fixes known advisories",
&GitHubCommentGenerator::get_list(&fixed_advisories, Bulleted, Code)?,
&GitHubCommentGenerator::get_hyperlinked_list(&fixed_advisories, Bulleted)?,
);
}

Expand Down

0 comments on commit 542ea7e

Please sign in to comment.