add support for private repos
Marco Andronaco andronacomarco@gmail.com
Wed, 16 Aug 2023 16:17:41 +0200
2 files changed,
8 insertions(+),
5 deletions(-)
M
config.example.json
→
config.example.json
@@ -1,4 +1,5 @@
{ + "github_username": "User", "github_auth_token": "abcdef123456", "repo_dir": "repos", "blacklist": [
M
github_backup/__main__.py
→
github_backup/__main__.py
@@ -19,7 +19,7 @@ if default is None:
logger.error("Missing config key: {}.".format(key)) exit(1) return default - +github_user = get_config("github_username") github_token = get_config("github_auth_token") repo_dir = get_config("repo_dir") blacklist = set(get_config("blacklist", []))@@ -28,14 +28,16 @@ g = Github(auth=Auth.Token(github_token))
repos = g.get_user().get_repos() def handle_repo(r): - repo_path = Path(join(repo_dir, r.name + ".git")) + repo_name = r.name + repo_path = Path(join(repo_dir, repo_name + ".git")) if repo_path.exists(): - logger.info("Updating " + r.name) + logger.info("Updating " + repo_name) repo = Repo(repo_path) repo.remote().fetch("+refs/heads/*:refs/heads/*") return repo - logger.info("Cloning " + r.git_url) - return Repo.clone_from(r.clone_url, repo_path, bare=True) + url = f"https://{github_user}:{github_token}@github.com/{github_user}/{repo_name}.git" + logger.info("Cloning " + repo_name) + return Repo.clone_from(url, repo_path, bare=True) results = [ handle_repo(repo) for repo in repos if repo.name not in blacklist ] logger.info(results)