def _get_metadata_revision(metadata_repo, mongo_repo, project, revision):
    """Get the metadata revision that corresponds to a given repository, project, revision."""
    for metadata_revision in metadata_repo.list_revisions():
        reference = metadata_repo.get_reference(metadata_revision, project)
        if not reference:
            # No reference for this revision. This should not happen but we keep trying in
            # case we can find an older revision with a reference.
            continue
        if mongo_repo.is_ancestor(reference, revision):
            # We found a reference that is a parent of the current revision.
            return metadata_revision
    return None