Skip to content

Commit

Permalink
Merge pull request #258 from OpenPecha/fix-download-private-pecha
Browse files Browse the repository at this point in the history
Fix: download private pecha
  • Loading branch information
10zinten authored Mar 31, 2023
2 parents cab0f74 + 5ce7113 commit 899518c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
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
24 changes: 19 additions & 5 deletions openpecha/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,43 @@ def _eval_branch(repo, branch):
return "master"


def download_pecha(pecha_id, out_path=None, needs_update=True, branch="main"):
def download_pecha(pecha_id, out_path=None, needs_update=True, branch="main") -> Path:
"""Download pecha from github
Note: If pecha is already downloaded before and needs_update,
it will update the pecha by pulling the remote `branch`.
Args:
pecha_id (str): pecha id
out_path (str, optional): path to download pecha. Defaults to None.
needs_update (bool, optional): if True, update pecha. Defaults to True.
branch (str, optional): branch name. Defaults to "main".
Returns:
Path: pecha path
"""
# resolve defaults kwargs
needs_update = needs_update if needs_update is not None else True
branch = branch if branch is not None else "main"
storage = GithubStorage()

# clone the repo
pecha_url = f"{config.GITHUB_ORG_URL}/{pecha_id}.git"
# create pecha path
if out_path:
out_path = Path(out_path)
out_path.mkdir(exist_ok=True, parents=True)
pecha_path = out_path / pecha_id
else:
pecha_path = config.PECHAS_PATH / pecha_id

if pecha_path.is_dir(): # if repo is already exits at local then try to pull
if pecha_path.is_dir():
repo = Repo(str(pecha_path))
branch = _eval_branch(repo, branch)
repo.git.checkout(branch)
if needs_update:
repo.git.config("pull.rebase", "false")
repo.git.pull("origin", branch)
else:
pecha_url = storage.get_authenticated_repo_remote_url(pecha_id)
try:
Repo.clone_from(pecha_url, str(pecha_path))
except GitCommandError:
Expand All @@ -164,7 +179,6 @@ def download_pecha(pecha_id, out_path=None, needs_update=True, branch="main"):
repo.git.checkout(branch)

# setup auth
storage = GithubStorage()
setup_auth_for_old_repo(repo, org=storage.org_name, token=storage.token)

return pecha_path
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 899518c

Please sign in to comment.