in nucleus/base/tasks.py [0:0]
def save_to_github(ghl_id):
from .models import GithubLog # avoid circular import
ghl = GithubLog.objects.get(pk=ghl_id)
log.debug(f"save_to_github: {ghl}")
obj = ghl.content_object
file_path = obj.json_file_path
content = obj.to_json()
repo = github.get_repo()
try:
ghf = repo.get_contents(file_path, ref=ghl.branch)
action = "Update"
except UnknownObjectException:
ghf = None
action = "Create"
message = f"{action} {file_path}"
kwargs = {"branch": ghl.branch}
if ghl.author:
kwargs["author"] = github.get_author(ghl.author)
if ghf:
current_data = json.loads(ghf.decoded_content)
if data_matches(current_data, obj.to_dict()):
log.info(f"{file_path} data matches. not updating file.")
return
resp = repo.update_file(file_path, message, content, ghf.sha, **kwargs)
else:
resp = repo.create_file(file_path, message, content, **kwargs)
log.info(f"committed to github: {resp['commit'].sha}")
log.info(message)
ghl.ack = True
ghl.save()