Fix git cloning, updating, refactor code
This commit is contained in:
parent
f25c79cdc2
commit
46178f0169
2 changed files with 34 additions and 23 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -60,3 +60,4 @@ target/
|
||||||
|
|
||||||
venv
|
venv
|
||||||
settings.py
|
settings.py
|
||||||
|
repos
|
||||||
|
|
56
gogsmaker.py
56
gogsmaker.py
|
@ -4,6 +4,7 @@ A webhook-handler for Gogs running `make` when needed. """
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from functools import wraps
|
||||||
from flask import Flask, request
|
from flask import Flask, request
|
||||||
from . import settings
|
from . import settings
|
||||||
|
|
||||||
|
@ -16,6 +17,7 @@ class UnmonitoredRepository(Exception):
|
||||||
|
|
||||||
class GitError(Exception):
|
class GitError(Exception):
|
||||||
def __init__(self, what):
|
def __init__(self, what):
|
||||||
|
super().__init__()
|
||||||
self.what = what
|
self.what = what
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
@ -39,12 +41,6 @@ def update_repo(hook, clone_url):
|
||||||
''' Update (or clone) the given repository. May raise GitError. '''
|
''' Update (or clone) the given repository. May raise GitError. '''
|
||||||
path = repo_path(hook)
|
path = repo_path(hook)
|
||||||
if os.path.isdir(os.path.join(path, '.git')): # Repo is already cloned
|
if os.path.isdir(os.path.join(path, '.git')): # Repo is already cloned
|
||||||
try:
|
|
||||||
subprocess.run(['git', 'clone', clone_url, path], check=True)
|
|
||||||
except subprocess.CalledProcessError:
|
|
||||||
raise GitError("Cannot clone {}".format(clone_url))
|
|
||||||
|
|
||||||
else: # Simply update
|
|
||||||
try:
|
try:
|
||||||
subprocess.run(['git', '-C', path, 'reset', '--hard'],
|
subprocess.run(['git', '-C', path, 'reset', '--hard'],
|
||||||
check=True) # Just in case.
|
check=True) # Just in case.
|
||||||
|
@ -52,29 +48,43 @@ def update_repo(hook, clone_url):
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
raise GitError("Cannot pull {}".format(hook['name']))
|
raise GitError("Cannot pull {}".format(hook['name']))
|
||||||
|
|
||||||
|
else: # Simply update
|
||||||
|
try:
|
||||||
|
subprocess.run(['mkdir', '-p', path])
|
||||||
|
subprocess.run(['git', 'clone', clone_url, path], check=True)
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
raise GitError("Cannot clone {}".format(clone_url))
|
||||||
|
|
||||||
|
|
||||||
def gogs_payload(required):
|
def gogs_payload(required):
|
||||||
def wrapper(fct):
|
def wrapper(fct):
|
||||||
payload = request.json
|
@wraps(fct)
|
||||||
if payload is None:
|
def wrapped(*args, **kwargs):
|
||||||
return 'Expected json\n', 415
|
# TODO: check signature
|
||||||
|
# payload_raw = request.data
|
||||||
|
|
||||||
for field in required + ['repository/html_url']:
|
payload = request.json
|
||||||
path = field.split('/')
|
if payload is None:
|
||||||
explore = payload
|
return 'Expected json\n', 415
|
||||||
for section in path:
|
|
||||||
if section not in explore:
|
|
||||||
return (
|
|
||||||
'Invalid json: missing {}\n'.format('/'.join(path)),
|
|
||||||
400)
|
|
||||||
explore = explore[section]
|
|
||||||
|
|
||||||
try:
|
for field in required + ['repository/html_url']:
|
||||||
hook = get_hook(payload['repository']['html_url'])
|
path = field.split('/')
|
||||||
except UnmonitoredRepository:
|
explore = payload
|
||||||
return 'Unmonitored repository\n', 403
|
for section in path:
|
||||||
|
if section not in explore:
|
||||||
|
return (
|
||||||
|
'Invalid json: missing {}\n'.format(
|
||||||
|
'/'.join(path)),
|
||||||
|
400)
|
||||||
|
explore = explore[section]
|
||||||
|
|
||||||
return fct(payload, hook)
|
try:
|
||||||
|
hook = get_hook(payload['repository']['html_url'])
|
||||||
|
except UnmonitoredRepository:
|
||||||
|
return 'Unmonitored repository\n', 403
|
||||||
|
|
||||||
|
return fct(payload, hook, *args, **kwargs)
|
||||||
|
return wrapped
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue