Commit ba584aaf authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

sync

parent c8c4f73b
Pipeline #8463 failed with stage
in 17 seconds
from django.db import models # noqa from django.db import models
# Create your models here. # from ..rainboard import models as rb_models
class GithubCheckSuite(models.Model):
id = models.PositiveIntegerField(primary_key=True)
...@@ -6,12 +6,17 @@ from json import loads ...@@ -6,12 +6,17 @@ from json import loads
from pprint import pprint from pprint import pprint
import requests import requests
from autoslug.utils import slugify
from django.conf import settings from django.conf import settings
from django.http import HttpRequest from django.http import HttpRequest
from django.http.response import HttpResponse, HttpResponseForbidden, HttpResponseServerError from django.http.response import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, HttpResponseServerError
from django.shortcuts import get_object_or_404
from django.utils.encoding import force_bytes from django.utils.encoding import force_bytes
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from ..rainboard.models import Namespace, Project
from . import models
def log(request: HttpRequest, rep: str = 'ok') -> HttpResponse: def log(request: HttpRequest, rep: str = 'ok') -> HttpResponse:
"""Just print the body.""" """Just print the body."""
...@@ -19,6 +24,55 @@ def log(request: HttpRequest, rep: str = 'ok') -> HttpResponse: ...@@ -19,6 +24,55 @@ def log(request: HttpRequest, rep: str = 'ok') -> HttpResponse:
return HttpResponse(rep) return HttpResponse(rep)
def check_suite(request: HttpRequest, rep: str) -> HttpResponse:
"""Manage Github's check suites."""
data = loads(request.body.decode())
models.GithubCheckSuite.objects.get_or_create(id=data['check_suite']['id'])
return HttpResponse(rep)
def push(request: HttpRequest, rep: str) -> HttpResponse:
"""Someone pushed on github. Synchronise current repo & gitlab."""
data = loads(request.body.decode())
namespace = get_object_or_404(Namespace, slug=slugify(data['repository']['owner']['name']))
project = get_object_or_404(Project, main_namespace=namespace, slug=slugify(data['repository']['name']))
ref_s = data['ref'][11:] # strip 'refs/heads/'
gh_remote_s = f'github/{namespace.slug}'
gl_remote_s = f'gitlab/{namespace.slug}'
gh_ref_s = f'{gh_remote_s}/{ref_s}'
gl_ref_s = f'{gl_remote_s}/{ref_s}'
git_repo = project.git()
gh_remote = git_repo.remotes[gh_remote_s]
gh_remote.fetch()
gh_ref = gh_remote.refs[ref_s]
if str(gh_ref.commit) != data['after']:
fail = f'push: wrong commit: {gh_ref.commit} vs {data["after"]}'
print(fail)
return HttpResponseBadRequest(fail)
if gh_ref_s in git_repo.branches:
git_repo.branches[gh_ref_s].commit = data['after']
else:
git_repo.create_head(gh_ref_s, commit=data['after'])
if gl_ref_s in git_repo.branches:
git_repo.branches[gl_ref_s].commit = data['after']
else:
git_repo.create_head(gl_ref_s, commit=data['after'])
if ref_s in git_repo.branches:
git_repo.branches[ref_s].commit = data['after']
else:
git_repo.create_head(ref_s, commit=data['after'])
gl_remote = git_repo.remotes[gl_remote_s]
gl_remote.fetch()
gl_ref = gl_remote.refs[ref_s]
if str(gl_ref.commit) != data['after']:
gl_remote.push(ref_s)
return HttpResponse(rep)
@csrf_exempt @csrf_exempt
def webhook(request: HttpRequest) -> HttpResponse: def webhook(request: HttpRequest) -> HttpResponse:
""" """
...@@ -53,7 +107,7 @@ def webhook(request: HttpRequest) -> HttpResponse: ...@@ -53,7 +107,7 @@ def webhook(request: HttpRequest) -> HttpResponse:
return log(request, 'pong') return log(request, 'pong')
if event == 'push': if event == 'push':
return log(request, 'push event detected') return log(request, 'push event detected')
else: if event == 'check_suite':
return log(request, f'event: {event}') return check_suite(request, 'check_suite event detected')
return log(request, event) return log(request, event)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment