Skip to content

Commit

Permalink
Implement issue #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Szamos96 committed May 4, 2021
1 parent 68afa6c commit d9258c5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions prospector/commit_processor/feature_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def extract_features(commit: Commit, advisory_record: AdvisoryRecord) -> CommitF
references_ghissue = extract_references_ghissue(commit.ghissue_refs)
n_changed_files = extract_n_changed_files(commit.changed_files)
contains_jira_reference = extract_contains_jira_reference(commit.jira_refs)
vulnerability_timestamp = extract_vuln_timestamp(advisory_record)
commit_feature = CommitFeatures(
commit=commit,
references_vuln_id=references_vuln_id,
Expand All @@ -30,6 +31,7 @@ def extract_features(commit: Commit, advisory_record: AdvisoryRecord) -> CommitF
references_ghissue=references_ghissue,
n_changed_files=n_changed_files,
contains_jira_reference=contains_jira_reference,
vulnerability_timestamp=vulnerability_timestamp,
)
return commit_feature

Expand All @@ -54,6 +56,10 @@ def extract_changes_relevant_path(
return any([changed_path in relevant_paths for changed_path in changed_paths])


def extract_vuln_timestamp(advisory_record: AdvisoryRecord) -> int:
return advisory_record.published_timestamp


def extract_avg_hunk_size(hunks: "list[tuple[int]]") -> int:
n_hunks = len(hunks)

Expand Down
13 changes: 13 additions & 0 deletions prospector/commit_processor/test_feature_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
extract_references_ghissue,
extract_references_vuln_id,
extract_time_between_commit_and_advisory_record,
extract_vuln_timestamp,
)
from .preprocessor import preprocess_commit

Expand Down Expand Up @@ -60,6 +61,18 @@ def test_time_between_commit_and_advisory_record():
assert extract_time_between_commit_and_advisory_record(142, 100) == 42


def test_extract_vuln_timestamp():

advisory_record = AdvisoryRecord(
vulnerability_id="TEST",
repository_url="https://github.com/apache/struts",
published_timestamp=100,
paths=["pom.xml"],
)

assert extract_vuln_timestamp(advisory_record) == 100


def test_extract_changes_relevant_path():
path_1 = "a/b.py"
path_2 = "a/c.py"
Expand Down
1 change: 1 addition & 0 deletions prospector/datamodel/commit_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ class CommitFeatures(BaseModel):
n_hunks: int = 0
n_changed_files: int = 0
contains_jira_reference: bool = False
vulnerability_timestamp: int = 0
2 changes: 2 additions & 0 deletions prospector/datamodel/commit_features_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def test_simple():
references_ghissue=True,
n_changed_files=44,
contains_jira_reference=True,
vulnerability_timestamp=100,
)

assert commit_features.commit.repository == "https://github.com/abc/xyz"
Expand All @@ -29,3 +30,4 @@ def test_simple():
assert commit_features.references_ghissue
assert commit_features.n_changed_files == 44
assert commit_features.contains_jira_reference
assert commit_features.vulnerability_timestamp == 100

0 comments on commit d9258c5

Please sign in to comment.