Skip to content

Commit 12b37df

Browse files
committed
Generalize pygit2 remote callback handling
Signed-off-by: Tobias Wolf <wolf@b1-systems.de> On-behalf-of: SAP <tobias.wolf@sap.com>
1 parent 446d226 commit 12b37df

4 files changed

Lines changed: 86 additions & 81 deletions

File tree

src/gardenlinux/git/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
Git module
55
"""
66

7-
from .credentials import Credentials
87
from .repository import Repository
98

10-
__all__ = ["Credentials", "Repository"]
9+
__all__ = ["Repository"]

src/gardenlinux/git/credentials.py

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
Git credentials provider
5+
"""
6+
7+
from typing import Any, Optional
8+
9+
from pygit2 import KeypairFromAgent
10+
from pygit2 import RemoteCallbacks as _RemoteCallbacks
11+
from pygit2 import UserPass
12+
from pygit2.enums import CredentialType
13+
14+
15+
class RemoteCallbacks(_RemoteCallbacks): # type: ignore[misc]
16+
"""
17+
pygit2.org: Base class for pygit2 remote callbacks.
18+
19+
:author: Garden Linux Maintainers
20+
:copyright: Copyright 2024 SAP SE
21+
:package: gardenlinux
22+
:subpackage: git
23+
:since: 1.0.0
24+
:license: https://www.apache.org/licenses/LICENSE-2.0
25+
Apache License, Version 2.0
26+
"""
27+
28+
def __init__(
29+
self,
30+
*args: Any,
31+
username: Optional[str] = None,
32+
password: Optional[str] = None,
33+
**kwargs: Any,
34+
):
35+
"""
36+
Constructor __init__(RemoteCallbacks)
37+
38+
:param token: GitHub/Git access token for HTTPS authentication.
39+
Falls back to the GITHUB_TOKEN environment variable if not provided.
40+
41+
:since: 1.0.0
42+
"""
43+
44+
self._username = ""
45+
self._password = ""
46+
47+
if username and password:
48+
self._username = username
49+
self._password = password
50+
51+
def credentials(
52+
self,
53+
url: str,
54+
username_from_url: Optional[str],
55+
allowed_types: CredentialType,
56+
) -> Optional[UserPass | KeypairFromAgent]:
57+
"""
58+
pygit2.org: Credentials callback. If the remote server requires
59+
authentication, this function will be called and its return value
60+
used for authentication.
61+
62+
:param url: The URL being accessed (after any insteadOf rewrites)
63+
:param username_from_url: Username extracted from the URL, if any
64+
:param allowed_types: Bitmask of credential types the server accepts
65+
66+
:return: A pygit2 credential object
67+
:since: 1.0.0
68+
"""
69+
70+
if allowed_types & CredentialType.USERPASS_PLAINTEXT:
71+
if self._password:
72+
return UserPass(self._username, self._password)
73+
74+
if allowed_types & CredentialType.SSH_KEY:
75+
return KeypairFromAgent(username_from_url or "git")
76+
77+
return _RemoteCallbacks.credentials(url, username_from_url, allowed_types)

src/gardenlinux/git/repository.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# -*- coding: utf-8 -*-
22

33
from logging import Logger
4-
from os import PathLike
4+
from os import PathLike, environ
55
from pathlib import Path
66
from typing import Any, List, Optional
77

8-
from pygit2 import Oid, RemoteCallbacks
8+
from pygit2 import Oid
99
from pygit2 import Repository as _Repository
1010
from pygit2 import init_repository
1111

1212
from ..constants import GL_REPOSITORY_URL
1313
from ..logger import LoggerSetup
14-
from .credentials import Credentials
14+
from .remote_callbacks import RemoteCallbacks
1515

1616

1717
class Repository(_Repository): # type: ignore[misc]
@@ -136,8 +136,11 @@ def checkout_repo(
136136
)
137137

138138
repo = init_repository(git_directory, origin_url=repo_url)
139-
callbacks = RemoteCallbacks(credentials=Credentials())
140-
repo.remotes["origin"].fetch(callbacks=callbacks)
139+
repo.remotes["origin"].fetch(
140+
callbacks=RemoteCallbacks(
141+
username=environ.get("GITHUB_TOKEN"), password="x-oauth-basic"
142+
)
143+
)
141144

142145
if commit is None:
143146
refish = f"origin/{branch}"

0 commit comments

Comments
 (0)