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


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
from pprint import pprint
import requests
from autoslug.utils import slugify
from django.conf import settings
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.views.decorators.csrf import csrf_exempt
from ..rainboard.models import Namespace, Project
from . import models
def log(request: HttpRequest, rep: str = 'ok') -> HttpResponse:
"""Just print the body."""
......@@ -19,6 +24,55 @@ def log(request: HttpRequest, rep: str = 'ok') -> HttpResponse:
return HttpResponse(rep)
def check_suite(request: HttpRequest, rep: str) -> HttpResponse:
"""Manage Github's check suites."""
data = loads(request.body.decode())
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_ref = gh_remote.refs[ref_s]
if str(gh_ref.commit) != data['after']:
fail = f'push: wrong commit: {gh_ref.commit} vs {data["after"]}'
return HttpResponseBadRequest(fail)
if gh_ref_s in git_repo.branches:
git_repo.branches[gh_ref_s].commit = data['after']
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']
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']
git_repo.create_head(ref_s, commit=data['after'])
gl_remote = git_repo.remotes[gl_remote_s]
gl_ref = gl_remote.refs[ref_s]
if str(gl_ref.commit) != data['after']:
return HttpResponse(rep)
def webhook(request: HttpRequest) -> HttpResponse:
......@@ -53,7 +107,7 @@ def webhook(request: HttpRequest) -> HttpResponse:
return log(request, 'pong')
if event == 'push':
return log(request, 'push event detected')
return log(request, f'event: {event}')
if event == 'check_suite':
return check_suite(request, 'check_suite event detected')
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