Skip to content

Commit

Permalink
[FIX] makes sure that commit classification information is only retri…
Browse files Browse the repository at this point in the history
…eved when using backend is optional or always
  • Loading branch information
lauraschauer authored and copernico committed Sep 2, 2024
1 parent e544450 commit 20aad05
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
27 changes: 23 additions & 4 deletions prospector/core/prospector.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,26 @@ def prospector( # noqa: C901
):
save_or_update_processed_commits(backend_address, payload)
else:
logger.warning("Preprocessed commits are not being sent to backend")
logger.warning(
"Preprocessed commits are not being sent to backend (after phase 1)"
)

ranked_candidates = evaluate_commits(
preprocessed_commits, advisory_record, backend_address, enabled_rules
preprocessed_commits,
advisory_record,
use_backend,
backend_address,
enabled_rules,
)

# Save outcome of security relevance to DB (Phase 2 Rule)
payload = [c.to_dict() for c in ranked_candidates[:NUM_COMMITS_PHASE_2]]
save_or_update_processed_commits(backend_address, payload)
if len(payload) > 0 and use_backend != USE_BACKEND_NEVER:
save_or_update_processed_commits(backend_address, payload)
else:
logger.warning(
"Preprocessed commits are not being sent to backend (after phase 2)"
)

# ConsoleWriter.print("Commit ranking and aggregation...")
ranked_candidates = remove_twins(ranked_candidates)
Expand Down Expand Up @@ -298,6 +309,7 @@ def filter(commits: Dict[str, RawCommit]) -> Dict[str, RawCommit]:
def evaluate_commits(
commits: List[Commit],
advisory: AdvisoryRecord,
use_backend: str,
backend_address: str,
enabled_rules: List[str],
) -> List[Commit]:
Expand All @@ -318,8 +330,15 @@ def evaluate_commits(
"""
with ExecutionTimer(core_statistics.sub_collection("candidates analysis")):
with ConsoleWriter("Candidate analysis") as _:
# Pass True to the rules module if the backend is being used, False
# otherwise (needed to decide whether to update the database)
use_backend = use_backend != USE_BACKEND_NEVER
ranked_commits = apply_rules(
commits, advisory, backend_address, enabled_rules=enabled_rules
commits,
advisory,
use_backend,
backend_address,
enabled_rules=enabled_rules,
)

return ranked_commits
Expand Down
31 changes: 16 additions & 15 deletions prospector/rules/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def get_id(self):
def apply_rules(
candidates: List[Commit],
advisory_record: AdvisoryRecord,
use_backend: bool,
backend_address: str,
enabled_rules: List[str] = [],
) -> List[Commit]:
Expand Down Expand Up @@ -95,7 +96,7 @@ def apply_rules(

for candidate in candidates[:NUM_COMMITS_PHASE_2]:
for rule in phase_2_rules:
if rule.apply(candidate, backend_address):
if rule.apply(candidate, use_backend, backend_address):
counter.increment("matches")
candidate.add_match(rule.as_dict())
candidate.compute_relevance()
Expand Down Expand Up @@ -433,6 +434,7 @@ class CommitIsSecurityRelevant(Rule):
def apply(
self,
candidate: Commit,
use_backend: bool,
backend_address: str,
) -> bool:

Expand All @@ -441,27 +443,26 @@ def apply(
):
# Check if this commit is already in the database
try:
r = requests.get(
f"{backend_address}/commits/{candidate.repository}",
params={"commit_id": candidate.commit_id},
timeout=10,
)
r.raise_for_status()
commit_data = r.json()[0]

is_security_relevant = commit_data.get("security_relevant")
if is_security_relevant is not None:
candidate.security_relevant = is_security_relevant
return is_security_relevant
if use_backend:
r = requests.get(
f"{backend_address}/commits/{candidate.repository}",
params={"commit_id": candidate.commit_id},
timeout=10,
)
r.raise_for_status()
commit_data = r.json()[0]

is_security_relevant = commit_data.get("security_relevant")
if is_security_relevant is not None:
candidate.security_relevant = is_security_relevant
return is_security_relevant

candidate.security_relevant = LLMService().classify_commit(
candidate.diff, candidate.repository, candidate.message
)

return candidate.security_relevant

return candidate.security_relevant

except requests.exceptions.RequestException as e:
error_type = type(e).__name__
print(
Expand Down

0 comments on commit 20aad05

Please sign in to comment.