Skip to content

Commit

Permalink
fix(storage): generate auth remote url
Browse files Browse the repository at this point in the history
  • Loading branch information
10zinten committed Mar 31, 2023
1 parent cab0f74 commit 5172865
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion openpecha/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from openpecha.github_utils import create_github_repo

URL: str

class Storages(enum.Enum):
GITHUB = enum.auto()
Expand Down Expand Up @@ -97,7 +98,13 @@ def __init__(
self.token = _get_value(token, "GITHUB_TOKEN")
self._username = _get_value(username, "GITHUB_USERNAME", optional=True)
self._email = _get_value(email, "GITHUB_EMAIL", optional=True)
self.org = Github(self.token).get_organization(self.org_name)
self._org = None

@property
def org(self):
if not self._org:
self._org = Github(self.token).get_organization(self.org_name)
return self._org

@property
def username(self):
Expand Down Expand Up @@ -132,6 +139,9 @@ def is_git_repo(self, path):
except git.exc.InvalidGitRepositoryError:
return False

def get_authenticated_repo_remote_url(self, repo_name: str):
return f"https://{self.username}:{self.token}@github.com/{self.org_name}/{repo_name}.git"

def add_dir(self, path: Path, description: str, is_private: bool = False, branch: str = "master"):
"""dir local dir to github."""
remote_repo_url = self._init_remote_repo(
Expand Down
16 changes: 16 additions & 0 deletions tests/storages/test_github_storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import tempfile
from pathlib import Path

Expand Down Expand Up @@ -59,3 +60,18 @@ def test_add_file():
)
finally:
storage.remove_dir_with_path(tmpdir)


def test_github_storage_authenticated_remote_url():
# arrange
os.environ["GITHUB_USERNAME"] = "test"
os.environ["GITHUB_EMAIL"] = "[email protected]"
os.environ["GITHUB_TOKEN"] = "fake-token"
os.environ["OPENPECHA_DATA_GITHUB_ORG"] = "fake-org"

# act
storage = GithubStorage()

assert storage.get_authenticated_repo_remote_url("test-repo") == (
f"https://test:[email protected]/fake-org/test-repo.git"
)

0 comments on commit 5172865

Please sign in to comment.