@@ -655,15 +655,6 @@ def to_analysis_target(
655655 "Cannot determine the analysis target: PURL and repository path are missing."
656656 )
657657
658- case (None , _):
659- # If only the repository path is provided, we will use the user-provided repository path to create the
660- # ``Repository`` instance. Note that if this case happen, the software component will be initialized
661- # with the PURL generated from the ``Repository`` instance (i.e. as a PURL pointing to a git repository
662- # at a specific commit). For example: ``pkg:github.com/org/name@<commit_digest>``.
663- return Analyzer .AnalysisTarget (
664- parsed_purl = None , repo_path = repo_path_input , branch = input_branch , digest = input_digest
665- )
666-
667658 case (_, "" ):
668659 # If a PURL but no repository path is provided, we try to extract the repository path from the PURL.
669660 # Note that we can't always extract the repository path from any provided PURL.
@@ -699,15 +690,41 @@ def to_analysis_target(
699690 digest = input_digest ,
700691 )
701692
702- case (_, _):
703- # If both the PURL and the repository are provided, we will use the user-provided repository path to
693+ case (_, _) | (None , _):
694+ # 1. If only the repository path is provided, we will use the user-provided repository path to create the
695+ # ``Repository`` instance. Note that if this case happen, the software component will be initialized
696+ # with the PURL generated from the ``Repository`` instance (i.e. as a PURL pointing to a git repository
697+ # at a specific commit). For example: ``pkg:github.com/org/name@<commit_digest>``.
698+ # 2. If both the PURL and the repository are provided, we will use the user-provided repository path to
704699 # create the ``Repository`` instance later on. This ``Repository`` instance is attached to the
705700 # software component initialized from the user-provided PURL.
701+ # For both cases, the digest will be the user input digest if it is provided. If not, it will be taken
702+ # from the provenance if the provenance is available.
703+ if input_digest :
704+ return Analyzer .AnalysisTarget (
705+ parsed_purl = parsed_purl ,
706+ repo_path = repo_path_input ,
707+ branch = input_branch ,
708+ digest = input_digest ,
709+ )
710+
711+ prov_digest = None
712+ if provenance_payload :
713+ try :
714+ _ , prov_digest = extract_repo_and_commit_from_provenance (provenance_payload )
715+ except ProvenanceError as error :
716+ logger .debug ("Failed to extract commit from provenance: %s" , error )
717+
706718 return Analyzer .AnalysisTarget (
707- parsed_purl = parsed_purl , repo_path = repo_path_input , branch = input_branch , digest = input_digest
719+ parsed_purl = parsed_purl ,
720+ repo_path = repo_path_input ,
721+ branch = input_branch ,
722+ digest = prov_digest or "" ,
708723 )
709724
710725 case _:
726+ # Even though this case is unecessary, it is still put here because mypy cannot type-narrow tuples
727+ # correctly (see https://github.com/python/mypy/pull/16905, which was fixed, but not released).
711728 raise InvalidAnalysisTargetError (
712729 "Cannot determine the analysis target: PURL and repository path are missing."
713730 )
0 commit comments