From 375179dc63083cd084c857e5a9dc185b73adf76b Mon Sep 17 00:00:00 2001 From: Pierre Camilleri <22995923+pierrecamilleri@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:24:57 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=B5=20factorize=20mismatch=20checks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frictionless/portals/github/plugin.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/frictionless/portals/github/plugin.py b/frictionless/portals/github/plugin.py index 79bb42f40c..e687c79239 100644 --- a/frictionless/portals/github/plugin.py +++ b/frictionless/portals/github/plugin.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional, Tuple +from typing import TYPE_CHECKING, Literal, Optional, Tuple from urllib.parse import urlparse from ...exception import FrictionlessException @@ -33,18 +33,10 @@ def create_adapter( user, repo = self._extract_user_and_repo(parsed.path) - if control.user and control.user != user: - raise FrictionlessException( - 'Mismatch between url and provided "user" information' - ) + self._assert_no_mismatch(user, control.user, "user") + self._assert_no_mismatch(repo, control.repo, "repo") control.user = user - - if control.repo and control.repo != repo: - raise FrictionlessException( - 'Mismatch between url and provided "repo" information' - ) - control.repo = repo return GithubAdapter(control) @@ -64,3 +56,15 @@ def _extract_user_and_repo(url_path: str) -> Tuple[Optional[str], Optional[str]] repo = splitted_url[1] if len(splitted_url) >= 2 else None return (user, repo) + + @staticmethod + def _assert_no_mismatch( + value: Optional[str], + control_value: Optional[str], + user_or_repo: Literal["user", "repo"], + ): + if value and control_value and control_value != value: + raise FrictionlessException( + f'Mismatch between url and provided "{user_or_repo}"' + f"information (in url: {value}), in control: {control_value}" + )