Skip to content

Commit

Permalink
feat: add the ability to specify a branch for importing OSV
Browse files Browse the repository at this point in the history
Also, add RustSec as a pre-configured, disabled option.
  • Loading branch information
ctron committed Jul 24, 2024
1 parent 6318b96 commit c31462c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
6 changes: 6 additions & 0 deletions modules/importer/src/model/osv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ pub struct OsvImporter {
#[serde(flatten)]
pub common: CommonImporter,

/// The URL to the git repository of the OSV data
pub source: String,

/// An optional branch. Will use the default branch otherwise.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub branch: Option<String>,

/// An optional path to start searching for documents. Will use the root of the repository otherwise.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
}
Expand Down
21 changes: 18 additions & 3 deletions modules/importer/src/server/common/walker/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ where
/// The git source to clone from
pub source: String,

/// The branch to check out
pub branch: Option<String>,

/// A path inside the cloned repository to start searching for files
pub path: Option<String>,

Expand All @@ -86,6 +89,7 @@ where
pub fn new(source: impl Into<String>, handler: H) -> Self {
Self {
source: source.into(),
branch: None,
path: None,
continuation: Default::default(),
working_dir: (),
Expand All @@ -102,6 +106,7 @@ where
pub fn handler<U: Handler>(self, handler: U) -> GitWalker<U, T> {
GitWalker {
source: self.source,
branch: self.branch,
path: self.path,
continuation: self.continuation,
working_dir: self.working_dir,
Expand All @@ -123,13 +128,19 @@ where
) -> GitWalker<H, U> {
GitWalker {
source: self.source,
branch: self.branch,
path: self.path,
continuation: self.continuation,
working_dir,
handler: self.handler,
}
}

pub fn branch(mut self, branch: Option<impl Into<String>>) -> Self {
self.branch = branch.map(|s| s.into());
self
}

pub fn path(mut self, path: Option<impl Into<String>>) -> Self {
self.path = path.map(|s| s.into());
self
Expand Down Expand Up @@ -186,9 +197,13 @@ where
// clone or open repository

let result = info_span!("clone repository").in_scope(|| {
RepoBuilder::new()
.fetch_options(fo)
.clone(&self.source, path)
let mut builder = RepoBuilder::new();

if let Some(branch) = &self.branch {
builder.branch(branch);
}

builder.fetch_options(fo).clone(&self.source, path)
});

let repo = match result {
Expand Down
1 change: 1 addition & 0 deletions modules/importer/src/server/osv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl super::Server {

let walker = OsvWalker::new(osv.source.clone())
.continuation(continuation)
.branch(osv.branch)
.path(osv.path)
.callbacks(Context {
context,
Expand Down
5 changes: 5 additions & 0 deletions modules/importer/src/server/osv/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ where
}
}

pub fn branch(mut self, branch: Option<impl Into<String>>) -> Self {
self.walker = self.walker.branch(branch);
self
}

pub fn path(mut self, path: Option<impl Into<String>>) -> Self {
self.walker = self.walker.path(path);
self
Expand Down
29 changes: 23 additions & 6 deletions server/src/sample_data.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::collections::HashSet;
use std::time::Duration;
use std::{collections::HashSet, time::Duration};
use trustify_common::config::Database;
use trustify_module_importer::model::{
CommonImporter, CsafImporter, CveImporter, ImporterConfiguration, OsvImporter, SbomImporter,
DEFAULT_SOURCE_CVEPROJECT,
use trustify_module_importer::{
model::{
CommonImporter, CsafImporter, CveImporter, ImporterConfiguration, OsvImporter,
SbomImporter, DEFAULT_SOURCE_CVEPROJECT,
},
service::{Error, ImporterService},
};
use trustify_module_importer::service::{Error, ImporterService};
use url::Url;

async fn add(
Expand All @@ -27,6 +28,7 @@ async fn add_osv(
name: &str,
source: &str,
base: Option<&str>,
branch: Option<&str>,
description: &str,
) -> anyhow::Result<()> {
add(
Expand All @@ -40,6 +42,7 @@ async fn add_osv(
labels: Default::default(),
},
source: source.to_string(),
branch: branch.map(ToString::to_string),
path: base.map(|s| s.into()),
}),
)
Expand Down Expand Up @@ -136,6 +139,7 @@ pub async fn sample_data(db: trustify_common::db::Database) -> anyhow::Result<()
"osv-pypa",
"https://github.com/pypa/advisory-database",
Some("vulns"),
None,
"Python Packaging Advisory Database",
)
.await?;
Expand All @@ -145,6 +149,7 @@ pub async fn sample_data(db: trustify_common::db::Database) -> anyhow::Result<()
"osv-psf",
"https://github.com/psf/advisory-database",
Some("advisories"),
None,
"Python Software Foundation Advisory Database",
)
.await?;
Expand All @@ -154,6 +159,7 @@ pub async fn sample_data(db: trustify_common::db::Database) -> anyhow::Result<()
"osv-r",
"https://github.com/RConsortium/r-advisory-database",
Some("vulns"),
None,
"RConsortium Advisory Database",
)
.await?;
Expand All @@ -163,9 +169,20 @@ pub async fn sample_data(db: trustify_common::db::Database) -> anyhow::Result<()
"osv-oss-fuzz",
"https://github.com/google/oss-fuzz-vulns",
Some("vulns"),
None,
"OSS-Fuzz vulnerabilities",
)
.await?;

add_osv(
&importer,
"osv-rustsec",
"https://github.com/rustsec/advisory-db",
Some("crates"),
Some("osv"),
"RustSec Advisory Database",
)
.await?;

Ok(())
}

0 comments on commit c31462c

Please sign in to comment.