Skip to content

Commit

Permalink
solve conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Karina5005 committed Sep 7, 2023
2 parents a617d47 + cdf9662 commit 914ac76
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
13 changes: 11 additions & 2 deletions backup_github/backup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import shutil
import subprocess
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -72,7 +73,8 @@ def __save_repositories(self, repositories, dir):
for repository in repositories:
if self.api.get_repository(repository)["size"] == 0:
continue
self.__save_repo_content(repository, dir)
if not self.__save_repo_content(repository, dir):
continue
repo = self.api.get_repository(repository)
filter_save(
repo,
Expand Down Expand Up @@ -103,10 +105,17 @@ def __save_repo_content(self, repository, dir):
repo_url = (
f"https://{self.token}@github.com/{self.organization}/{repository}.git"
)
subprocess_handle(subprocess.call, ["git", "clone", "--bare", repo_url])
try:
subprocess_handle(subprocess.call, ["git", "clone", "--bare", repo_url])
except subprocess.CalledProcessError:
shutil.rmtree(f"{dir}/{repository}")
logging.error(f"Repository {repository} backup error, will be skipped")
os.chdir(cur_dir)
return False
os.chdir(f"{repository}.git")
subprocess_handle(subprocess.check_output, ["git", "fetch", "-p"])
os.chdir(cur_dir)
return True

def __save_members(self, members, members_dir):
for member in members:
Expand Down
3 changes: 3 additions & 0 deletions backup_github/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import requests

from backup_github.metrics import rate_limit_count


class GithubAPI:
headers = dict
Expand Down Expand Up @@ -30,6 +32,7 @@ def __init__(self, message=None):
def raise_by_status(self, response):
if response.status_code == 403:
logging.warning("Status is 403 - Rate limit exceeded exception")
rate_limit_count.labels(self.organization).inc()
raise self.RateLimitExceededException(response.content)
elif response.status_code == 404:
logging.warning(
Expand Down
6 changes: 3 additions & 3 deletions backup_github/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from backup_github.backup import Backup
from backup_github.metrics import (
backup_interval,
backup_duration,
backup_time,
git_size,
meta_size,
Expand Down Expand Up @@ -60,8 +60,8 @@ def main():
sum(p.stat().st_size for p in Path(parsed_args.output_dir).rglob("*"))
- git_size.labels(parsed_args.organization)._value.get()
)
backup_interval.labels(parsed_args.organization).set(time() - start)
write_to_textfile(f"{parsed_args.metrics_path}/github_backup.prom", registry)
backup_duration.labels(parsed_args.organization).set(time() - start)
write_to_textfile(f"{parsed_args.metrics_path}", registry)


if __name__ == "__main__":
Expand Down
12 changes: 9 additions & 3 deletions backup_github/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
labelnames=["organization"],
registry=registry,
)
backup_interval = Gauge(
"github_backup_interval_timestamp_seconds",
"time of last backup in unixtime",
backup_duration = Gauge(
"github_backup_duration_seconds",
"duration of last backup in seconds",
labelnames=["organization"],
registry=registry,
)
rate_limit_count = Gauge(
"github_backup_rate_limit_count",
"count of rate limit",
labelnames=["organization"],
registry=registry,
)
1 change: 1 addition & 0 deletions backup_github/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def subprocess_handle(func, args):
logging.error("exit code: {}".format(e.returncode))
logging.error("stdout: {}".format(e.output.decode(sys.getfilesystemencoding())))
logging.error("stderr: {}".format(e.stderr.decode(sys.getfilesystemencoding())))
raise e


def filter_save(struct, fields, path):
Expand Down

0 comments on commit 914ac76

Please sign in to comment.