Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't fail clone process if access token doesn't exist #1971

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions git_services/git_services/init/cloner.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ def __init__(
base_path = Path(workspace_mount_path)
logging.basicConfig(level=logging.INFO)
self.repositories: list[Repository] = [
Repository.from_config_repo(r, workspace_mount_path=base_path)
for r in repositories
Repository.from_config_repo(r, workspace_mount_path=base_path) for r in repositories
]
self.git_providers = {p.id: p for p in git_providers}
self.workspace_mount_path = Path(workspace_mount_path)
Expand Down Expand Up @@ -114,19 +113,15 @@ def _exclude_storages_from_git(repository: Repository, storages: list[str]):
if not storages:
return

with open(
repository.absolute_path / ".git" / "info" / "exclude", "a"
) as exclude_file:
with open(repository.absolute_path / ".git" / "info" / "exclude", "a") as exclude_file:
exclude_file.write("\n")

for storage in storages:
storage_path = Path(storage)
if repository.absolute_path not in storage_path.parents:
# The storage path is not inside the repo, no need to gitignore
continue
exclude_path = storage_path.relative_to(
repository.absolute_path
).as_posix()
exclude_path = storage_path.relative_to(repository.absolute_path).as_posix()
exclude_file.write(f"{exclude_path}\n")

def _get_access_token(self, provider_id: str):
Expand All @@ -142,17 +137,16 @@ def _get_access_token(self, provider_id: str):
res = requests.get(request_url, headers=headers)
if res.status_code != 200:
logging.warning(f"Could not get access token for provider {provider_id}")
del self._access_tokens[provider_id]
if provider_id in self._access_tokens:
del self._access_tokens[provider_id]
return None
token = res.json()
logging.info(f"Got token response for {provider_id}")
self._access_tokens[provider_id] = token["access_token"]
return self._access_tokens[provider_id]

@contextmanager
def _temp_plaintext_credentials(
self, repository: Repository, git_user: str, git_access_token: str
):
def _temp_plaintext_credentials(self, repository: Repository, git_user: str, git_access_token: str):
# NOTE: If "lfs." is included in urljoin it does not work properly
lfs_auth_setting = "lfs." + urljoin(f"{repository.url}/", "info/lfs.access")
credential_loc = Path("/tmp/git-credentials")
Expand Down Expand Up @@ -206,9 +200,7 @@ def _get_default_branch(repository: Repository, remote_name: str) -> str:
"""Get the default branch of the repository."""
try:
repository.git_cli.git_remote("set-head", remote_name, "--auto")
res = repository.git_cli.git_symbolic_ref(
f"refs/remotes/{remote_name}/HEAD"
)
res = repository.git_cli.git_symbolic_ref(f"refs/remotes/{remote_name}/HEAD")
except GitCommandError as err:
raise errors.BranchDoesNotExistError from err
r = re.compile(r"^refs/remotes/origin/(?P<branch>.*)$")
Expand All @@ -229,9 +221,7 @@ def _clone(self, repository: Repository):
repository.git_cli.git_fetch(self.remote_name)
except GitCommandError as err:
raise errors.GitFetchError from err
branch = repository.branch or self._get_default_branch(
repository=repository, remote_name=self.remote_name
)
branch = repository.branch or self._get_default_branch(repository=repository, remote_name=self.remote_name)
logging.info(f"Checking out branch {branch}")
try:
repository.git_cli.git_checkout(branch)
Expand Down Expand Up @@ -270,9 +260,7 @@ def run_helper(self, repository: Repository, *, storage_mounts: list[str]):

# TODO: Is this something else for non-GitLab providers?
git_user = "oauth2"
git_access_token = (
self._get_access_token(repository.provider) if repository.provider else None
)
git_access_token = self._get_access_token(repository.provider) if repository.provider else None

self._initialize_repo(repository)
try:
Expand All @@ -283,15 +271,11 @@ def run_helper(self, repository: Repository, *, storage_mounts: list[str]):
elif git_access_token is None:
self._clone(repository)
else:
with self._temp_plaintext_credentials(
repository, git_user, git_access_token
):
with self._temp_plaintext_credentials(repository, git_user, git_access_token):
self._clone(repository)
except errors.GitFetchError as err:
logging.error(msg=f"Cannot clone {repository.url}", exc_info=err)
with open(
repository.absolute_path / "ERROR", mode="w", encoding="utf-8"
) as f:
with open(repository.absolute_path / "ERROR", mode="w", encoding="utf-8") as f:
import traceback

traceback.print_exception(err, file=f)
Expand All @@ -303,9 +287,7 @@ def run_helper(self, repository: Repository, *, storage_mounts: list[str]):
if Path(a_mount).exists():
raise errors.CloudStorageOverwritesExistingFilesError

logging.info(
f"Excluding cloud storage from git: {storage_mounts} for {repository}"
)
logging.info(f"Excluding cloud storage from git: {storage_mounts} for {repository}")
if storage_mounts:
self._exclude_storages_from_git(repository, storage_mounts)

Expand Down
Loading