Skip to content

Commit abd7a88

Browse files
sacca97copernico
authored andcommitted
minor changes
1 parent 4ff15de commit abd7a88

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

prospector/client/cli/prospector_client.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@
3333
core_statistics = execution_statistics.sub_collection("core")
3434

3535

36+
def prospector_find_twins(
37+
vulnerability_id: str,
38+
repository_url: str,
39+
commit_id: str,
40+
git_cache: str = "/tmp/git_cache",
41+
):
42+
advisory_record = build_advisory_record(
43+
vulnerability_id,
44+
)
45+
repository = Git(repository_url, git_cache)
46+
repository.clone()
47+
48+
# tags = repository.get_tags()
49+
50+
commits = repository.find_commits_for_twin_lookups(commit_id=commit_id)
51+
preprocessed_commits = list()
52+
pbar = tqdm(
53+
list(commits.values()),
54+
desc="Preprocessing commits",
55+
unit="commit",
56+
)
57+
for raw_commit in pbar:
58+
preprocessed_commits.append(make_from_raw_commit(raw_commit))
59+
60+
ranked_candidates = evaluate_commits(preprocessed_commits, advisory_record, None)
61+
62+
ConsoleWriter.print("Commit ranking and aggregation...")
63+
# I NEED TO GET THE FIRST REACHABLE TAG OR NO-TAG
64+
ranked_candidates = tag_and_aggregate_commits(ranked_candidates, None)
65+
ConsoleWriter.print_(MessageStatus.OK)
66+
67+
return ranked_candidates, advisory_record
68+
69+
3670
# @profile
3771
@measure_execution_time(execution_statistics, name="core")
3872
def prospector( # noqa: C901
@@ -73,6 +107,9 @@ def prospector( # noqa: C901
73107
set(modified_files),
74108
)
75109

110+
fixing_commit = advisory_record.get_fixing_commit()
111+
if fixing_commit is not None:
112+
pass
76113
# obtain a repository object
77114
repository = Git(repository_url, git_cache)
78115

@@ -186,7 +223,7 @@ def evaluate_commits(commits: List[Commit], advisory: AdvisoryRecord, rules: Lis
186223

187224

188225
def tag_and_aggregate_commits(commits: List[Commit], next_tag: str) -> List[Commit]:
189-
if next_tag is None:
226+
if next_tag is None or next_tag == "":
190227
return commits
191228

192229
twin_tags_map = {commit.commit_id: commit.get_tag() for commit in commits}

prospector/datamodel/advisory.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ def parse_advisory(self, data):
139139
]
140140
self.versions["fixed"] = [v for v in self.versions["fixed"] if v is not None]
141141

142+
def get_fixing_commit(self) -> Optional[str]:
143+
for reference in self.references.keys():
144+
if "github.com" in reference and "commit" in reference:
145+
return reference.split("/")[-1]
146+
return None
147+
142148

143149
def get_from_nvd(cve_id: str):
144150
"""Get an advisory from the NVD dtabase"""

prospector/git/git.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
GIT_SEPARATOR = "-@-@-@-@-"
2222

2323
TEN_DAYS_TIME_DELTA = 14 * 24 * 60 * 60
24+
ONE_MONTH_TIME_DELTA = 30 * 24 * 60 * 60
2425

2526

2627
def do_clone(url, output_folder, shallow=False, skip_existing=False):
@@ -207,12 +208,14 @@ def create_commits(
207208

208209
if next_tag:
209210
until = self.extract_tag_timestamp(next_tag) + TEN_DAYS_TIME_DELTA
210-
cmd += f" --until={until}"
211+
if until:
212+
cmd += f" --until={until}"
211213

212214
# TODO: if find twins is true, we dont need the ancestors, only the timestamps
213215
if prev_tag:
214216
since = self.extract_tag_timestamp(prev_tag) - TEN_DAYS_TIME_DELTA
215-
cmd += f" --since={since}"
217+
if since:
218+
cmd += f" --since={since}"
216219

217220
if filter_extension:
218221
cmd += " *." + " *.".join(filter_extension)
@@ -255,6 +258,13 @@ def parse_git_output(self, raw: List[str]) -> Dict[str, RawCommit]:
255258

256259
return commits
257260

261+
def find_commits_for_twin_lookups(self, commit_id):
262+
commit_timestamp = self.extract_tag_timestamp(commit_id)
263+
return self.create_commits(
264+
since=commit_timestamp - ONE_MONTH_TIME_DELTA,
265+
until=commit_timestamp + ONE_MONTH_TIME_DELTA,
266+
)
267+
258268
@measure_execution_time(execution_statistics.sub_collection("core"))
259269
def get_commit(self, id):
260270
return RawCommit(self, id)

prospector/git/version_to_tag.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ def get_possible_tags(tags: list[str], versions: str):
5252
for tag in tags
5353
if next_version == clean_tag(tag) and not is_rc_or_date(tag)
5454
]
55-
55+
# print(tags)
56+
# print(prev_tag, next_tag)
5657
if len(prev_tag) == 1 and len(next_tag) == 1:
5758
return prev_tag[0], next_tag[0]
5859
elif len(prev_tag) == 1 and len(next_tag) > 1:

0 commit comments

Comments
 (0)