Skip to content

Commit

Permalink
Include the remote repository URL in some of the impact assessment co…
Browse files Browse the repository at this point in the history
…de (#1415)

This is to help with troubleshooting why converted CVEs aren't
successfully having their commits converted back to versions.

Also add type hints
  • Loading branch information
andrewpollock committed Jun 26, 2023
1 parent eda6d26 commit 7b83c62
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions osv/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ def __init__(self, detect_cherrypicks=True):
self.detect_cherrypicks = detect_cherrypicks

def get_affected(self,
repo,
regress_commits,
fix_commits,
limit_commits=None):
repo: pygit2.Repository,
regress_commits: list[str],
fix_commits: list[str],
limit_commits: list[str] = None):
""""Get list of affected tags and commits for a bug given regressed and
fixed commits."""
affected_commits, affected_ranges, tags = self._get_affected_range(
Expand All @@ -105,17 +105,20 @@ def get_affected(self,
return AffectedResult(tags, affected_commits, affected_ranges)

def _get_affected_range(self,
repo,
regress_commits,
fix_commits,
limit_commits=None):
repo: pygit2.Repository,
regress_commits: list[str],
fix_commits: list[str],
limit_commits: list[str] = None):
"""Get affected range."""
range_collector = RangeCollector()
commits = set()
seen_commits = set()
tags = set()
commits_to_tags = _get_commit_to_tag_mappings(repo)
branch_to_limit = {}
repo_url = None
if 'origin' in repo.remotes.names():
repo_url = repo.remotes['origin'].url

branches = []
detect_cherrypicks = self.detect_cherrypicks and not limit_commits
Expand Down Expand Up @@ -150,8 +153,8 @@ def _get_affected_range(self,
# Get the earliest equivalent commit in the regression range.
equivalent_regress_commit = None
for regress_commit in regress_commits:
logging.info('Finding equivalent regress commit to %s in %s',
regress_commit, ref)
logging.info('Finding equivalent regress commit to %s in %s in %s',
regress_commit, ref, repo_url)
equivalent_regress_commit = self._get_equivalent_commit(
repo, ref, regress_commit, detect_cherrypicks=detect_cherrypicks)
if equivalent_regress_commit:
Expand All @@ -164,8 +167,8 @@ def _get_affected_range(self,
# Get the latest equivalent commit in the fix range.
equivalent_fix_commit = None
for fix_commit in fix_commits:
logging.info('Finding equivalent fix commit to %s in %s', fix_commit,
ref)
logging.info('Finding equivalent fix commit to %s in %s in %s',
fix_commit, ref, str(repo_url or 'UNKNOWN_REPO_URL'))
equivalent_fix_commit = self._get_equivalent_commit(
repo, ref, fix_commit, detect_cherrypicks=detect_cherrypicks)
if equivalent_fix_commit:
Expand Down Expand Up @@ -247,7 +250,7 @@ def _get_equivalent_commit(self,
return None


def _get_commit_to_tag_mappings(repo):
def _get_commit_to_tag_mappings(repo: pygit2.Repository):
"""Get all commit to tag mappings"""
mappings = {}
for ref_name in repo.references:
Expand Down Expand Up @@ -276,7 +279,12 @@ def get_commit_and_tag_list(repo,
include_end = False
end_commit = limit_commit

logging.info('Getting commits %s..%s', start_commit, end_commit)
repo_url = None
if 'origin' in repo.remotes.names():
repo_url = repo.remotes['origin'].url

logging.info('Getting commits %s..%s from %s', start_commit, end_commit,
str(repo_url or 'UNKNOWN_REPO_URL'))
try:
walker = repo.walk(end_commit,
pygit2.GIT_SORT_TOPOLOGICAL | pygit2.GIT_SORT_REVERSE)
Expand Down Expand Up @@ -487,8 +495,8 @@ def _analyze_git_ranges(repo_analyzer, checkout_path, affected_range,
result = repo_analyzer.get_affected(package_repo, all_introduced,
all_fixed, all_limit)
except ImpactError:
logging.warning('Got error while analyzing git range: %s',
traceback.format_exc())
logging.warning('Got error while analyzing git range in %s: %s',
affected_range.repo, traceback.format_exc())
return new_versions, commits

for introduced, fixed in result.affected_ranges:
Expand Down

0 comments on commit 7b83c62

Please sign in to comment.